]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Fix local and field names starting with a digit
authorSimon Werbeck <simon.werbeck@gmail.com>
Sun, 6 Apr 2014 11:04:04 +0000 (13:04 +0200)
committerLuca Bruno <lucabru@src.gnome.org>
Sun, 6 Apr 2014 13:54:33 +0000 (15:54 +0200)
Fixes bug 647018

codegen/valaccodeattribute.vala
codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/objects/bug647018.vala [new file with mode: 0644]
vala/valaparser.vala

index ce1dcc78366d12b2c1e87796ec903db5b51d2168..7e1c38a8a33a0d94274d50501a298934fd032571 100644 (file)
@@ -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;
index 8b097f5a2282f7054c17de89860688184acf73a2..8e7c4c476aa6cce8a98e2dbf6cb8ee5fbe7387ee 100644 (file)
@@ -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) {
index d8043323dcefc6772c96e1995b6205224a876567..21da161c7c4b1ca7c7d61e13237d144b9d15c17d 100644 (file)
@@ -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 (file)
index 0000000..27fbdab
--- /dev/null
@@ -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 () {
+}
index 3070cc03f07949e781d92d04ccf48b1d28e824a9..75ba5a4291a95719a0c58060088430e8b407847a 100644 (file)
@@ -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);