authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:32:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:32:38-04:00
log387fab60a61611c6cb48a5d9dd9d356911733210
tree0e3e9334333587c609a187f58afc4423068d0210
parentc420b234cc5698d44b11cf313f14a9608328f111

translate-c: fix do while with empty body


2 files changed, 23 insertions(+), 4 deletions(-)

src/translate_c.cpp+11-4
......@@ -2707,7 +2707,9 @@ static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const DoStmt
27072707 AstNode *child_statement;
27082708 child_scope = trans_stmt(c, &child_block_scope->base, stmt->getBody(), &child_statement);
27092709 if (child_scope == nullptr) return nullptr;
2710 body_node->data.block.statements.append(child_statement);
2710 if (child_statement != nullptr) {
2711 body_node->data.block.statements.append(child_statement);
2712 }
27112713 }
27122714
27132715 // if (!cond) break;
......@@ -2717,6 +2719,7 @@ static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const DoStmt
27172719 terminator_node->data.if_bool_expr.condition = trans_create_node_prefix_op(c, PrefixOpBoolNot, condition_node);
27182720 terminator_node->data.if_bool_expr.then_block = trans_create_node(c, NodeTypeBreak);
27192721
2722 assert(terminator_node != nullptr);
27202723 body_node->data.block.statements.append(terminator_node);
27212724
27222725 while_scope->node->data.while_expr.body = body_node;
......@@ -3044,9 +3047,13 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
30443047 case Stmt::UnaryExprOrTypeTraitExprClass:
30453048 return wrap_stmt(out_node, out_child_scope, scope,
30463049 trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt));
3047 case Stmt::DoStmtClass:
3048 return wrap_stmt(out_node, out_child_scope, scope,
3049 trans_do_loop(c, scope, (const DoStmt *)stmt));
3050 case Stmt::DoStmtClass: {
3051 AstNode *while_node = trans_do_loop(c, scope, (const DoStmt *)stmt);
3052 if (while_node->data.while_expr.body == nullptr) {
3053 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
3054 }
3055 return wrap_stmt(out_node, out_child_scope, scope, while_node);
3056 }
30503057 case Stmt::ForStmtClass: {
30513058 AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)stmt);
30523059 if (while_node->data.while_expr.body == nullptr) {
test/translate_c.zig+12
......@@ -1,6 +1,18 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.TranslateCContext) void {
4 cases.add("do while with empty body",
5 \\void foo(void) {
6 \\ do ; while (1);
7 \\}
8 , // TODO this should be if (1 != 0) break
9 \\pub fn foo() void {
10 \\ while (true) {
11 \\ if (!1) break;
12 \\ }
13 \\}
14 );
15
416 cases.add("for with empty body",
517 \\void foo(void) {
618 \\ for (;;);