Skip to content

Commit 9765fff

Browse files
committed
Add run-translated-c test & fix one more edge case
1 parent 47b1555 commit 9765fff

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src-self-hosted/translate_c.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,11 @@ fn transInitListExprRecord(
18411841
// .field_name = expr
18421842
const period_tok = try appendToken(rp.c, .Period, ".");
18431843

1844-
const raw_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, field_decl)));
1845-
if (raw_name.len < 1) continue;
1844+
var raw_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, field_decl)));
1845+
if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {
1846+
const name = rp.c.decl_table.get(@ptrToInt(ZigClangFieldDecl_getCanonicalDecl(field_decl))).?;
1847+
raw_name = try mem.dupe(rp.c.a(), u8, name.value);
1848+
}
18461849
const field_name_tok = try appendIdentifier(rp.c, raw_name);
18471850

18481851
_ = try appendToken(rp.c, .Equal, "=");

test/run_translated_c.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
2323
\\ return 0;
2424
\\}
2525
, "");
26+
27+
cases.add("anonymous struct & unions",
28+
\\#include <stdlib.h>
29+
\\#include <stdint.h>
30+
\\static struct { struct { uint16_t x, y; }; } x = { 1 };
31+
\\static struct { union { uint32_t x; uint8_t y; }; } y = { 0x55AA55AA };
32+
\\int main(int argc, char **argv) {
33+
\\ if (x.x != 1) abort();
34+
\\ if (x.y != 0) abort();
35+
\\ if (y.x != 0x55AA55AA) abort();
36+
\\ if (y.y != 0xAA) abort();
37+
\\ return 0;
38+
\\}
39+
, "");
2640
}

0 commit comments

Comments
 (0)