authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:40:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:40:14-04:00
log63a23e848a62d5f167f8d5478de9766cb24aa6eb
treed92a84eb207777a6876ac964d64c476fc9d12a64
parent387fab60a61611c6cb48a5d9dd9d356911733210

translate-c: fix for loops with var init and empty body


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

src/translate_c.cpp+10-6
......@@ -2783,7 +2783,12 @@ static AstNode *trans_for_loop(Context *c, TransScope *parent_scope, const ForSt
27832783 TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(), &body_statement);
27842784 if (body_scope == nullptr)
27852785 return nullptr;
2786 while_scope->node->data.while_expr.body = body_statement;
2786
2787 if (body_statement == nullptr) {
2788 while_scope->node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
2789 } else {
2790 while_scope->node->data.while_expr.body = body_statement;
2791 }
27872792
27882793 return loop_block_node;
27892794}
......@@ -3020,6 +3025,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
30203025 return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope);
30213026 case Stmt::WhileStmtClass: {
30223027 AstNode *while_node = trans_while_loop(c, scope, (const WhileStmt *)stmt);
3028 assert(while_node->type == NodeTypeWhileExpr);
30233029 if (while_node->data.while_expr.body == nullptr) {
30243030 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
30253031 }
......@@ -3049,17 +3055,15 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
30493055 trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt));
30503056 case Stmt::DoStmtClass: {
30513057 AstNode *while_node = trans_do_loop(c, scope, (const DoStmt *)stmt);
3058 assert(while_node->type == NodeTypeWhileExpr);
30523059 if (while_node->data.while_expr.body == nullptr) {
30533060 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
30543061 }
30553062 return wrap_stmt(out_node, out_child_scope, scope, while_node);
30563063 }
30573064 case Stmt::ForStmtClass: {
3058 AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)stmt);
3059 if (while_node->data.while_expr.body == nullptr) {
3060 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
3061 }
3062 return wrap_stmt(out_node, out_child_scope, scope, while_node);
3065 AstNode *node = trans_for_loop(c, scope, (const ForStmt *)stmt);
3066 return wrap_stmt(out_node, out_child_scope, scope, node);
30633067 }
30643068 case Stmt::StringLiteralClass:
30653069 return wrap_stmt(out_node, out_child_scope, scope,
test/translate_c.zig+13
......@@ -1,6 +1,19 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.TranslateCContext) void {
4 cases.add("for loop with var init but empty body",
5 \\void foo(void) {
6 \\ for (int x = 0; x < 10; x++);
7 \\}
8 ,
9 \\pub fn foo() void {
10 \\ {
11 \\ var x: c_int = 0;
12 \\ while (x < 10) : (x += 1) {}
13 \\ }
14 \\}
15 );
16
417 cases.add("do while with empty body",
518 \\void foo(void) {
619 \\ do ; while (1);