Skip to content

Commit 62413da

Browse files
LemonBoyandrewrk
authored andcommitted
Add run-translated-c test & fix one more edge case
1 parent 2da9e00 commit 62413da

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
@@ -39,4 +39,18 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
3939
\\ return 0;
4040
\\}
4141
, "");
42+
43+
cases.add("anonymous struct & unions",
44+
\\#include <stdlib.h>
45+
\\#include <stdint.h>
46+
\\static struct { struct { uint16_t x, y; }; } x = { 1 };
47+
\\static struct { union { uint32_t x; uint8_t y; }; } y = { 0x55AA55AA };
48+
\\int main(int argc, char **argv) {
49+
\\ if (x.x != 1) abort();
50+
\\ if (x.y != 0) abort();
51+
\\ if (y.x != 0x55AA55AA) abort();
52+
\\ if (y.y != 0xAA) abort();
53+
\\ return 0;
54+
\\}
55+
, "");
4256
}

0 commit comments

Comments
 (0)