diff --git a/src/translate_c/ast.zig b/src/translate_c/ast.zig index 8330e6785f..9d274d7733 100644 --- a/src/translate_c/ast.zig +++ b/src/translate_c/ast.zig @@ -2048,29 +2048,38 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { } _ = try c.addToken(.r_brace, "}"); - if (payload.inits.len < 2) { - return c.addNode(.{ + return switch (payload.inits.len) { + 0 => c.addNode(.{ + .tag = .struct_init_one, + .main_token = l_brace, + .data = .{ + .lhs = lhs, + .rhs = 0, + }, + }), + 1 => c.addNode(.{ .tag = .struct_init_one_comma, .main_token = l_brace, .data = .{ .lhs = lhs, .rhs = inits[0], }, - }); - } else { - const span = try c.listToSpan(inits); - return c.addNode(.{ - .tag = .struct_init_comma, - .main_token = l_brace, - .data = .{ - .lhs = lhs, - .rhs = try c.addExtra(NodeSubRange{ - .start = span.start, - .end = span.end, - }), - }, - }); - } + }), + else => blk: { + const span = try c.listToSpan(inits); + break :blk c.addNode(.{ + .tag = .struct_init_comma, + .main_token = l_brace, + .data = .{ + .lhs = lhs, + .rhs = try c.addExtra(NodeSubRange{ + .start = span.start, + .end = span.end, + }), + }, + }); + }, + }; }, .@"anytype" => unreachable, // Handled in renderParams } diff --git a/test/cases/translate_c/static empty struct.c b/test/cases/translate_c/static empty struct.c new file mode 100644 index 0000000000..080d7977fd --- /dev/null +++ b/test/cases/translate_c/static empty struct.c @@ -0,0 +1,17 @@ +struct empty_struct {}; + +static inline void foo() { + static struct empty_struct bar = {}; +} + +// translate-c +// target=x86_64-linux +// c_frontend=clang +// +// pub const struct_empty_struct = extern struct {}; +// pub fn foo() callconv(.C) void { +// const bar = struct { +// var static: struct_empty_struct = struct_empty_struct{}; +// }; +// _ = &bar; +// } diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 12852d388b..d69c389bc2 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -976,7 +976,7 @@ const TestManifest = struct { fn next(self: *TrailingIterator) ?[]const u8 { const next_inner = self.inner.next() orelse return null; - return std.mem.trim(u8, next_inner[2..], " \t"); + return if (next_inner.len == 2) "" else std.mem.trimRight(u8, next_inner[3..], " \t"); } };