authorgravatar for danielsan901998@gmail.comdanielsan901998 <danielsan901998@gmail.com> 2024-01-30 16:24:41+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-31 06:38:44+02:00
logd7a27bf803fd29c499090c7bd0460fae9f0cf0f7
tree155a2d73dc37e03aba25059ff82c0164456dad49
parentfc48bbdf90bbfa0ed1f5a95e9e5412351d1df6d7

Use mem.zeroes for empty union initializer list


3 files changed, 26 insertions(+), 1 deletions(-)

src/translate_c.zig+5
......@@ -2505,6 +2505,11 @@ fn transInitListExprRecord(
25052505 var field_inits = std.ArrayList(ast.Payload.ContainerInit.Initializer).init(c.gpa);
25062506 defer field_inits.deinit();
25072507
2508 if (init_count == 0) {
2509 const source_loc = @as(*const clang.Expr, @ptrCast(expr)).getBeginLoc();
2510 return transZeroInitExpr(c, scope, source_loc, ty);
2511 }
2512
25082513 var init_i: c_uint = 0;
25092514 var it = record_def.field_begin();
25102515 const end_it = record_def.field_end();
test/cases/translate_c/empty union initializer list.c created+20
......@@ -0,0 +1,20 @@
1union U {
2 int x;
3 long y;
4};
5
6void foo(void) {
7 union U u = {};
8}
9// translate-c
10// target=x86_64-linux
11// c_frontend=clang
12//
13// pub const union_U = extern union {
14// x: c_int,
15// y: c_long,
16// };
17// pub export fn foo() void {
18// var u: union_U = @import("std").mem.zeroes(union_U);
19// _ = &u;
20// }
test/cases/translate_c/static empty struct.c +1-1
......@@ -11,7 +11,7 @@ static inline void foo() {
1111// pub const struct_empty_struct = extern struct {};
1212// pub fn foo() callconv(.C) void {
1313// const bar = struct {
14// var static: struct_empty_struct = struct_empty_struct{};
14// var static: struct_empty_struct = @import("std").mem.zeroes(struct_empty_struct);
1515// };
1616// _ = &bar;
1717// }