authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-04-28 07:30:24-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-04-28 08:45:51-06:00
log83d2d7ab8afed6d24238dbb2696fb5bc35cdb05d
tree2897cb95bbd65f5fa091fbbd84515ff7ad988754
parent01605a77428aefedbdf76830b6e4cab4853f2e4e
signature Commit is signed but in an unrecognized format.

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 files changed, 4 insertions(+), 1 deletions(-)

src-self-hosted/translate_c.zig+4-1
......@@ -788,6 +788,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
788788 .rbrace_token = undefined,
789789 };
790790
791 var unnamed_field_count: u32 = 0;
791792 var it = ZigClangRecordDecl_field_begin(record_def);
792793 const end_it = ZigClangRecordDecl_field_end(record_def);
793794 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!?*
812813 var is_anon = false;
813814 var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
814815 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl) or raw_name.len == 0) {
815 raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
816 // Context.getMangle() is not used here because doing so causes unpredictable field names for anonymous fields.
817 raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{unnamed_field_count});
818 unnamed_field_count += 1;
816819 is_anon = true;
817820 }
818821 const field_name = try appendIdentifier(c, raw_name);