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.
master
Tadeo Kondrak 2020-04-28 07:30:24 -06:00
parent 01605a7742
commit 83d2d7ab8a
No known key found for this signature in database
GPG Key ID: D41E092CA43F1D8B
1 changed files with 4 additions and 1 deletions

View File

@ -788,6 +788,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
.rbrace_token = undefined, .rbrace_token = undefined,
}; };
var unnamed_field_count: u32 = 0;
var it = ZigClangRecordDecl_field_begin(record_def); var it = ZigClangRecordDecl_field_begin(record_def);
const end_it = ZigClangRecordDecl_field_end(record_def); const end_it = ZigClangRecordDecl_field_end(record_def);
while (ZigClangRecordDecl_field_iterator_neq(it, end_it)) : (it = ZigClangRecordDecl_field_iterator_next(it)) { 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 is_anon = false;
var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl))); 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) { 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; is_anon = true;
} }
const field_name = try appendIdentifier(c, raw_name); const field_name = try appendIdentifier(c, raw_name);