From 83d2d7ab8afed6d24238dbb2696fb5bc35cdb05d Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Tue, 28 Apr 2020 07:30:24 -0600 Subject: [PATCH] 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. --- src-self-hosted/translate_c.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 8b137c4d97..853c2bef5b 100644 --- a/src-self-hosted/translate_c.zig +++ b/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);