[vala] codegen: Fix local and field names starting with a digit



commit 0d449a938dc2153a9b70ba4bcef35a8f1a421aa6
Author: Simon Werbeck <simon werbeck gmail com>
Date:   Sun Apr 6 13:04:04 2014 +0200

    codegen: Fix local and field names starting with a digit
    
    Fixes bug 647018

 codegen/valaccodeattribute.vala  |   10 +++++++---
 codegen/valaccodebasemodule.vala |    3 +++
 tests/Makefile.am                |    1 +
 tests/objects/bug647018.vala     |    9 +++++++++
 vala/valaparser.vala             |    1 -
 5 files changed, 20 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala
index ce1dcc7..7e1c38a 100644
--- a/codegen/valaccodeattribute.vala
+++ b/codegen/valaccodeattribute.vala
@@ -575,11 +575,15 @@ public class Vala.CCodeAttribute : AttributeCache {
                                }
                                return "%s%s".printf (CCodeBaseModule.get_ccode_lower_case_prefix 
(sym.parent_symbol).up (), sym.name);
                        } else if (sym is Field) {
+                               var cname = sym.name;
                                if (((Field) sym).binding == MemberBinding.STATIC) {
-                                       return "%s%s".printf (CCodeBaseModule.get_ccode_lower_case_prefix 
(sym.parent_symbol), sym.name);
-                               } else {
-                                       return sym.name;
+                                       cname = "%s%s".printf (CCodeBaseModule.get_ccode_lower_case_prefix 
(sym.parent_symbol), sym.name);
                                }
+                               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 "";
+                               }
+                               return cname;
                        } else if (sym is CreationMethod) {
                                var m = (CreationMethod) sym;
                                string infix;
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 8b097f5..8e7c4c4 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2237,6 +2237,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
        public string get_local_cname (LocalVariable local) {
                var cname = get_variable_cname (local.name);
+               if (cname[0].isdigit ()) {
+                       cname = "_%s_".printf (cname);
+               }
                if (is_in_coroutine ()) {
                        var clash_index = emit_context.closure_variable_clash_map.get (local);
                        if (clash_index > 0) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d804332..21da161 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -141,6 +141,7 @@ TESTS = \
        objects/bug643711.vala \
        objects/bug646362.vala \
        objects/bug646792.vala \
+       objects/bug647018.vala \
        objects/bug653138.vala \
        objects/bug654702.vala \
        objects/bug663134.vala \
diff --git a/tests/objects/bug647018.vala b/tests/objects/bug647018.vala
new file mode 100644
index 0000000..27fbdab
--- /dev/null
+++ b/tests/objects/bug647018.vala
@@ -0,0 +1,9 @@
+// allow fields starting with a digit if the cname is valid
+[CCode (cname = "good")]
+int 2good;
+
+// gets default cname - must fail
+//int 2bad;
+
+void main () {
+}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 3070cc0..75ba5a4 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2507,7 +2507,6 @@ public class Vala.Parser : CodeVisitor {
                var flags = parse_member_declaration_modifiers ();
                var type = parse_type (true, true);
                string id = parse_identifier ();
-
                type = parse_inline_array_type (type);
 
                var f = new Field (id, type, null, get_src (begin), comment);


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