Skip to content

Commit

Permalink
Use mem.zeroes for empty union initializer list
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsan901998 authored and Vexu committed Jan 31, 2024
1 parent fc48bbd commit d7a27bf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,11 @@ fn transInitListExprRecord(
var field_inits = std.ArrayList(ast.Payload.ContainerInit.Initializer).init(c.gpa);
defer field_inits.deinit();

if (init_count == 0) {
const source_loc = @as(*const clang.Expr, @ptrCast(expr)).getBeginLoc();
return transZeroInitExpr(c, scope, source_loc, ty);
}

var init_i: c_uint = 0;
var it = record_def.field_begin();
const end_it = record_def.field_end();
Expand Down
20 changes: 20 additions & 0 deletions test/cases/translate_c/empty union initializer list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
union U {
int x;
long y;
};

void foo(void) {
union U u = {};
}
// translate-c
// target=x86_64-linux
// c_frontend=clang
//
// pub const union_U = extern union {
// x: c_int,
// y: c_long,
// };
// pub export fn foo() void {
// var u: union_U = @import("std").mem.zeroes(union_U);
// _ = &u;
// }
2 changes: 1 addition & 1 deletion test/cases/translate_c/static empty struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static inline void foo() {
// pub const struct_empty_struct = extern struct {};
// pub fn foo() callconv(.C) void {
// const bar = struct {
// var static: struct_empty_struct = struct_empty_struct{};
// var static: struct_empty_struct = @import("std").mem.zeroes(struct_empty_struct);
// };
// _ = &bar;
// }

0 comments on commit d7a27bf

Please sign in to comment.