Commit 83d2d7ab8a

Tadeo Kondrak <me@tadeo.ca>
2020-04-28 15:30:24
Mangle field names with a local counter in records
See https://github.com/ifreund/river/issues/17 for an issue that occurs because the field names are mangled globally. When using the generated bindings, you have no choice but to use the unstable names or redeclare the entire struct. This commit changes the behaviour to use a local counter per record declaration, so the names are predictable each time.
1 parent 01605a7
Changed files (1)
src-self-hosted
src-self-hosted/translate_c.zig
@@ -788,6 +788,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
             .rbrace_token = undefined,
         };
 
+        var unnamed_field_count: u32 = 0;
         var it = ZigClangRecordDecl_field_begin(record_def);
         const end_it = ZigClangRecordDecl_field_end(record_def);
         while (ZigClangRecordDecl_field_iterator_neq(it, end_it)) : (it = ZigClangRecordDecl_field_iterator_next(it)) {
@@ -812,7 +813,9 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
             var is_anon = false;
             var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
             if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl) or raw_name.len == 0) {
-                raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
+                // Context.getMangle() is not used here because doing so causes unpredictable field names for anonymous fields.
+                raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{unnamed_field_count});
+                unnamed_field_count += 1;
                 is_anon = true;
             }
             const field_name = try appendIdentifier(c, raw_name);