translate-c-2 fix assertion failure rendering do while

master
Vexu 2019-12-29 17:09:31 +02:00
parent f5e7d2d00c
commit 621629e20d
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
2 changed files with 17 additions and 1 deletions

View File

@ -1792,7 +1792,10 @@ fn transDoWhileLoop(
// zig: b;
// zig: if (!cond) break;
// zig: }
break :blk (try transStmt(rp, &loop_scope, ZigClangDoStmt_getBody(stmt), .unused, .r_value)).cast(ast.Node.Block).?;
const body = (try transStmt(rp, &loop_scope, ZigClangDoStmt_getBody(stmt), .unused, .r_value)).cast(ast.Node.Block).?;
// if this is used as an expression in Zig it needs to be immediately followed by a semicolon
_ = try appendToken(rp.c, .Semicolon, ";");
break :blk body;
} else blk: {
// the C statement is without a block, so we need to create a block to contain it.
// c: do

View File

@ -2185,4 +2185,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub const FOO = if (a) b else c;
});
cases.add("do while as expr",
\\static void foo(void) {
\\ if (1)
\\ do {} while (0);
\\}
, &[_][]const u8{
\\pub fn foo() void {
\\ if (1 != 0) while (true) {
\\ if (!(0 != 0)) break;
\\ };
\\}
});
}