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...@@ -2783,7 +2783,12 @@ static AstNode *trans_for_loop(Context *c, TransScope *parent_scope, const ForSt
2783 TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(), &body_statement);2783 TransScope *body_scope = trans_stmt(c, &while_scope->base, stmt->getBody(), &body_statement);
2784 if (body_scope == nullptr)2784 if (body_scope == nullptr)
2785 return nullptr;2785 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
2788 return loop_block_node;2793 return loop_block_node;
2789}2794}
...@@ -3020,6 +3025,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,...@@ -3020,6 +3025,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
3020 return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope);3025 return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope);
3021 case Stmt::WhileStmtClass: {3026 case Stmt::WhileStmtClass: {
3022 AstNode *while_node = trans_while_loop(c, scope, (const WhileStmt *)stmt);3027 AstNode *while_node = trans_while_loop(c, scope, (const WhileStmt *)stmt);
3028 assert(while_node->type == NodeTypeWhileExpr);
3023 if (while_node->data.while_expr.body == nullptr) {3029 if (while_node->data.while_expr.body == nullptr) {
3024 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);3030 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
3025 }3031 }
...@@ -3049,17 +3055,15 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,...@@ -3049,17 +3055,15 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
3049 trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt));3055 trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt));
3050 case Stmt::DoStmtClass: {3056 case Stmt::DoStmtClass: {
3051 AstNode *while_node = trans_do_loop(c, scope, (const DoStmt *)stmt);3057 AstNode *while_node = trans_do_loop(c, scope, (const DoStmt *)stmt);
3058 assert(while_node->type == NodeTypeWhileExpr);
3052 if (while_node->data.while_expr.body == nullptr) {3059 if (while_node->data.while_expr.body == nullptr) {
3053 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);3060 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
3054 }3061 }
3055 return wrap_stmt(out_node, out_child_scope, scope, while_node);3062 return wrap_stmt(out_node, out_child_scope, scope, while_node);
3056 }3063 }
3057 case Stmt::ForStmtClass: {3064 case Stmt::ForStmtClass: {
3058 AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)stmt);3065 AstNode *node = trans_for_loop(c, scope, (const ForStmt *)stmt);
3059 if (while_node->data.while_expr.body == nullptr) {3066 return wrap_stmt(out_node, out_child_scope, scope, node);
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);
3063 }3067 }
3064 case Stmt::StringLiteralClass:3068 case Stmt::StringLiteralClass:
3065 return wrap_stmt(out_node, out_child_scope, scope,3069 return wrap_stmt(out_node, out_child_scope, scope,
test/translate_c.zig+13
...@@ -1,6 +1,19 @@...@@ -1,6 +1,19 @@
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("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
4 cases.add("do while with empty body",17 cases.add("do while with empty body",
5 \\void foo(void) {18 \\void foo(void) {
6 \\ do ; while (1);19 \\ do ; while (1);