authorgravatar for xnhp0320@126.comPeng He <xnhp0320@126.com> 2023-10-15 01:09:54+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-14 17:09:54+00:00
loga126afa1c3fe7e39678c201f6470b7899aedd47e
tree64e2b09a82a6788b22d01c16107bb76472dc6c48
parent8a15c9249c6b6d47c22651d65a7b68db487cd479
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

translate-c: fix crash when last stmt of stmt expr is a decl


2 files changed, 26 insertions(+), 5 deletions(-)

src/translate_c.zig+12-5
...@@ -3269,11 +3269,18 @@ fn transStmtExpr(c: *Context, scope: *Scope, stmt: *const clang.StmtExpr, used:...@@ -3269,11 +3269,18 @@ fn transStmtExpr(c: *Context, scope: *Scope, stmt: *const clang.StmtExpr, used:
3269 else => try block_scope.statements.append(result),3269 else => try block_scope.statements.append(result),
3270 }3270 }
3271 }3271 }
3272 const break_node = try Tag.break_val.create(c.arena, .{3272
3273 .label = block_scope.label,3273 const last_result = try transStmt(c, &block_scope.base, it[0], .used);
3274 .val = try transStmt(c, &block_scope.base, it[0], .used),3274 switch (last_result.tag()) {
3275 });3275 .declaration, .empty_block => {},
3276 try block_scope.statements.append(break_node);3276 else => {
3277 const break_node = try Tag.break_val.create(c.arena, .{
3278 .label = block_scope.label,
3279 .val = last_result,
3280 });
3281 try block_scope.statements.append(break_node);
3282 },
3283 }
3277 const res = try block_scope.complete(c);3284 const res = try block_scope.complete(c);
3278 return maybeSuppressResult(c, used, res);3285 return maybeSuppressResult(c, used, res);
3279}3286}
test/translate_c.zig+14
...@@ -4150,4 +4150,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -4150,4 +4150,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
4150 ,4150 ,
4151 \\pub export var struct_foo: [*c]const u8 = "hello world";4151 \\pub export var struct_foo: [*c]const u8 = "hello world";
4152 });4152 });
4153
4154 cases.add("unsupport declare statement at the last of a compound statement which belongs to a statement expr",
4155 \\void somefunc(void) {
4156 \\ int y;
4157 \\ (void)({y=1; _Static_assert(1);});
4158 \\}
4159 , &[_][]const u8{
4160 \\pub export fn somefunc() void {
4161 \\ var y: c_int = undefined;
4162 \\ _ = blk: {
4163 \\ y = 1;
4164 \\ };
4165 \\}
4166 });
4153}4167}