authorgravatar for garrettlennoxbeck@gmail.comGarrett Beck <garrettlennoxbeck@gmail.com> 2023-11-24 16:44:33-06:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-24 22:44:33+00:00
loga277181c6615e40f7b2e7392958f127fd934027c
tree8592deca9862274ce41d9b6b2ca356ad25b757b5
parent608b5d06eace0ccdce816725b8f0d7a6e9c12b81
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

translate-c: use struct_init_one for empty struct initializer


3 files changed, 42 insertions(+), 16 deletions(-)

src/translate_c/ast.zig+24-15
...@@ -2048,29 +2048,38 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -2048,29 +2048,38 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
2048 }2048 }
2049 _ = try c.addToken(.r_brace, "}");2049 _ = try c.addToken(.r_brace, "}");
20502050
2051 if (payload.inits.len < 2) {2051 return switch (payload.inits.len) {
2052 return c.addNode(.{2052 0 => c.addNode(.{
2053 .tag = .struct_init_one_comma,2053 .tag = .struct_init_one,
2054 .main_token = l_brace,2054 .main_token = l_brace,
2055 .data = .{2055 .data = .{
2056 .lhs = lhs,2056 .lhs = lhs,
2057 .rhs = inits[0],2057 .rhs = 0,
2058 },2058 },
2059 });2059 }),
2060 } else {2060 1 => c.addNode(.{
2061 const span = try c.listToSpan(inits);2061 .tag = .struct_init_one_comma,
2062 return c.addNode(.{
2063 .tag = .struct_init_comma,
2064 .main_token = l_brace,2062 .main_token = l_brace,
2065 .data = .{2063 .data = .{
2066 .lhs = lhs,2064 .lhs = lhs,
2067 .rhs = try c.addExtra(NodeSubRange{2065 .rhs = inits[0],
2068 .start = span.start,
2069 .end = span.end,
2070 }),
2071 },2066 },
2072 });2067 }),
2073 }2068 else => blk: {
2069 const span = try c.listToSpan(inits);
2070 break :blk c.addNode(.{
2071 .tag = .struct_init_comma,
2072 .main_token = l_brace,
2073 .data = .{
2074 .lhs = lhs,
2075 .rhs = try c.addExtra(NodeSubRange{
2076 .start = span.start,
2077 .end = span.end,
2078 }),
2079 },
2080 });
2081 },
2082 };
2074 },2083 },
2075 .@"anytype" => unreachable, // Handled in renderParams2084 .@"anytype" => unreachable, // Handled in renderParams
2076 }2085 }
test/cases/translate_c/static empty struct.c created+17
...@@ -0,0 +1,17 @@
1struct empty_struct {};
2
3static inline void foo() {
4 static struct empty_struct bar = {};
5}
6
7// translate-c
8// target=x86_64-linux
9// c_frontend=clang
10//
11// pub const struct_empty_struct = extern struct {};
12// pub fn foo() callconv(.C) void {
13// const bar = struct {
14// var static: struct_empty_struct = struct_empty_struct{};
15// };
16// _ = &bar;
17// }
test/src/Cases.zig+1-1
...@@ -976,7 +976,7 @@ const TestManifest = struct {...@@ -976,7 +976,7 @@ const TestManifest = struct {
976976
977 fn next(self: *TrailingIterator) ?[]const u8 {977 fn next(self: *TrailingIterator) ?[]const u8 {
978 const next_inner = self.inner.next() orelse return null;978 const next_inner = self.inner.next() orelse return null;
979 return std.mem.trim(u8, next_inner[2..], " \t");979 return if (next_inner.len == 2) "" else std.mem.trimRight(u8, next_inner[3..], " \t");
980 }980 }
981 };981 };
982982