authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2021-05-09 22:59:22+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-10 21:59:42+03:00
log8f8efcdd6e333e5ee3cf406fd095d0c8d47ab89c
tree7a07814a436f444fb6e60fd6a7cf09d3dce7d581
parent9c03b39d1ebb52a94f1e6ee6f92a19f54a0ee528

translate-c: fix typedefs with multiple names


2 files changed, 21 insertions(+), 1 deletions(-)

src/translate_c.zig+7-1
......@@ -447,7 +447,13 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void {
447447 // TODO https://github.com/ziglang/zig/issues/3756
448448 // TODO https://github.com/ziglang/zig/issues/1802
449449 const name = if (isZigPrimitiveType(decl_name)) try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ decl_name, c.getMangle() }) else decl_name;
450 try c.unnamed_typedefs.putNoClobber(c.gpa, addr, name);
450 const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr);
451 if (result.found_existing) {
452 // One typedef can declare multiple names.
453 // Don't put this one in `decl_table` so it's processed later.
454 return;
455 }
456 result.entry.value = name;
451457 // Put this typedef in the decl_table to avoid redefinitions.
452458 try c.decl_table.putNoClobber(c.gpa, @ptrToInt(typedef_decl.getCanonicalDecl()), name);
453459 }
test/run_translated_c.zig+14
......@@ -1476,4 +1476,18 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
14761476 \\ return 0;
14771477 \\}
14781478 , "");
1479
1480 cases.add("typedef with multiple names",
1481 \\#include <stdlib.h>
1482 \\typedef struct {
1483 \\ char field;
1484 \\} a_t, b_t;
1485 \\
1486 \\int main(void) {
1487 \\ a_t a = { .field = 42 };
1488 \\ b_t b = a;
1489 \\ if (b.field != 42) abort();
1490 \\ return 0;
1491 \\}
1492 , "");
14791493}