[vala/wip/issue/1329: 4/4] codegen: Check cname of fields and methods against reserved identfiers




commit 27d55956ff1e5064ff7b44b24bb9f528f3862faa
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Jun 11 13:04:00 2022 +0200

    codegen: Check cname of fields and methods against reserved identfiers
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1329

 codegen/valaccodeattribute.vala                | 12 +++++++++---
 tests/Makefile.am                              |  2 ++
 tests/semantic/field-reserved-identifier.test  |  8 ++++++++
 tests/semantic/method-reserved-identifier.test |  7 +++++++
 4 files changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index a996480d7..9b49fe9e1 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -734,7 +734,8 @@ public class Vala.CCodeAttribute : AttributeCache {
                                }
                                if (cname[0].isdigit ()) {
                                        Report.error (node.source_reference, "Field name starts with a digit. 
Use the `cname' attribute to provide a valid C name if intended");
-                                       return "";
+                               } else if (CCodeBaseModule.reserved_identifiers.contains (cname)) {
+                                       Report.error (node.source_reference, "Field name `%s' collides with 
reserved identifier. Use the `cname' attribute to provide a valid C name if intended", cname);
                                }
                                return cname;
                        } else if (sym is CreationMethod) {
@@ -760,6 +761,7 @@ public class Vala.CCodeAttribute : AttributeCache {
                                if (m.signal_reference != null) {
                                        return "%s%s".printf (get_ccode_lower_case_prefix (m.parent_symbol), 
get_ccode_lower_case_name (m.signal_reference));
                                }
+                               string cname;
                                if (sym.name == "main" && sym.parent_symbol.name == null) {
                                        // avoid conflict with generated main function
                                        if (m.coroutine) {
@@ -768,10 +770,14 @@ public class Vala.CCodeAttribute : AttributeCache {
                                                return "_vala_main";
                                        }
                                } else if (sym.name.has_prefix ("_")) {
-                                       return "_%s%s".printf (get_ccode_lower_case_prefix 
(sym.parent_symbol), sym.name.substring (1));
+                                       cname = "_%s%s".printf (get_ccode_lower_case_prefix 
(sym.parent_symbol), sym.name.substring (1));
                                } else {
-                                       return "%s%s".printf (get_ccode_lower_case_prefix 
(sym.parent_symbol), sym.name);
+                                       cname = "%s%s".printf (get_ccode_lower_case_prefix 
(sym.parent_symbol), sym.name);
+                               }
+                               if (CCodeBaseModule.reserved_identifiers.contains (cname)) {
+                                       Report.error (node.source_reference, "Method name `%s' collides with 
reserved identifier. Use the `cname' attribute to provide a valid C name if intended", cname);
                                }
+                               return cname;
                        } else if (sym is Property) {
                                return sym.name.replace ("_", "-");
                        } else if (sym is PropertyAccessor) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3dd2cad45..c219e72f3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1112,6 +1112,7 @@ TESTS = \
        semantic/field-namespace-owned.test \
        semantic/field-non-constant.test \
        semantic/field-owned-to-unowned.test \
+       semantic/field-reserved-identifier.test \
        semantic/field-static-instance-access.test \
        semantic/field-too-few-type-arguments.test \
        semantic/field-too-many-type-arguments.test \
@@ -1183,6 +1184,7 @@ TESTS = \
        semantic/method-private-override.test \
        semantic/method-private-virtual.test \
        semantic/method-protected.test \
+       semantic/method-reserved-identifier.test \
        semantic/method-return-accessibility.test \
        semantic/method-return-too-few-type-arguments.test \
        semantic/method-return-too-many-type-arguments.test \
diff --git a/tests/semantic/field-reserved-identifier.test b/tests/semantic/field-reserved-identifier.test
new file mode 100644
index 000000000..304cef6dd
--- /dev/null
+++ b/tests/semantic/field-reserved-identifier.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+struct Foo {
+       int auto;
+}
+
+void main () {
+}
diff --git a/tests/semantic/method-reserved-identifier.test b/tests/semantic/method-reserved-identifier.test
new file mode 100644
index 000000000..feeb6a68d
--- /dev/null
+++ b/tests/semantic/method-reserved-identifier.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+void auto () {
+}
+
+void main () {
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]