commit a126afa1c3fe7e39678c201f6470b7899aedd47e (tree)
parent 8a15c9249c6b6d47c22651d65a7b68db487cd479
Author: Peng He <xnhp0320@126.com>
Date: Sun, 15 Oct 2023 01:09:54 +0800
translate-c: fix crash when last stmt of stmt expr is a decl
Diffstat:
2 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/src/translate_c.zig b/src/translate_c.zig
@@ -3269,11 +3269,18 @@ fn transStmtExpr(c: *Context, scope: *Scope, stmt: *const clang.StmtExpr, used:
else => try block_scope.statements.append(result),
}
}
- const break_node = try Tag.break_val.create(c.arena, .{
- .label = block_scope.label,
- .val = try transStmt(c, &block_scope.base, it[0], .used),
- });
- try block_scope.statements.append(break_node);
+
+ const last_result = try transStmt(c, &block_scope.base, it[0], .used);
+ switch (last_result.tag()) {
+ .declaration, .empty_block => {},
+ else => {
+ const break_node = try Tag.break_val.create(c.arena, .{
+ .label = block_scope.label,
+ .val = last_result,
+ });
+ try block_scope.statements.append(break_node);
+ },
+ }
const res = try block_scope.complete(c);
return maybeSuppressResult(c, used, res);
}
diff --git a/test/translate_c.zig b/test/translate_c.zig
@@ -4150,4 +4150,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
,
\\pub export var struct_foo: [*c]const u8 = "hello world";
});
+
+ cases.add("unsupport declare statement at the last of a compound statement which belongs to a statement expr",
+ \\void somefunc(void) {
+ \\ int y;
+ \\ (void)({y=1; _Static_assert(1);});
+ \\}
+ , &[_][]const u8{
+ \\pub export fn somefunc() void {
+ \\ var y: c_int = undefined;
+ \\ _ = blk: {
+ \\ y = 1;
+ \\ };
+ \\}
+ });
}