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...@@ -2707,7 +2707,9 @@ static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const DoStmt
2707 AstNode *child_statement;2707 AstNode *child_statement;
2708 child_scope = trans_stmt(c, &child_block_scope->base, stmt->getBody(), &child_statement);2708 child_scope = trans_stmt(c, &child_block_scope->base, stmt->getBody(), &child_statement);
2709 if (child_scope == nullptr) return nullptr;2709 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 }
2711 }2713 }
27122714
2713 // if (!cond) break;2715 // if (!cond) break;
...@@ -2717,6 +2719,7 @@ static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const DoStmt...@@ -2717,6 +2719,7 @@ static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const DoStmt
2717 terminator_node->data.if_bool_expr.condition = trans_create_node_prefix_op(c, PrefixOpBoolNot, condition_node);2719 terminator_node->data.if_bool_expr.condition = trans_create_node_prefix_op(c, PrefixOpBoolNot, condition_node);
2718 terminator_node->data.if_bool_expr.then_block = trans_create_node(c, NodeTypeBreak);2720 terminator_node->data.if_bool_expr.then_block = trans_create_node(c, NodeTypeBreak);
27192721
2722 assert(terminator_node != nullptr);
2720 body_node->data.block.statements.append(terminator_node);2723 body_node->data.block.statements.append(terminator_node);
27212724
2722 while_scope->node->data.while_expr.body = body_node;2725 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,...@@ -3044,9 +3047,13 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
3044 case Stmt::UnaryExprOrTypeTraitExprClass:3047 case Stmt::UnaryExprOrTypeTraitExprClass:
3045 return wrap_stmt(out_node, out_child_scope, scope,3048 return wrap_stmt(out_node, out_child_scope, scope,
3046 trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt));3049 trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt));
3047 case Stmt::DoStmtClass:3050 case Stmt::DoStmtClass: {
3048 return wrap_stmt(out_node, out_child_scope, scope,3051 AstNode *while_node = trans_do_loop(c, scope, (const DoStmt *)stmt);
3049 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 }
3050 case Stmt::ForStmtClass: {3057 case Stmt::ForStmtClass: {
3051 AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)stmt);3058 AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)stmt);
3052 if (while_node->data.while_expr.body == nullptr) {3059 if (while_node->data.while_expr.body == nullptr) {
test/translate_c.zig+12
...@@ -1,6 +1,18 @@...@@ -1,6 +1,18 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.TranslateCContext) void {3pub 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
4 cases.add("for with empty body",16 cases.add("for with empty body",
5 \\void foo(void) {17 \\void foo(void) {
6 \\ for (;;);18 \\ for (;;);