authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:18:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:18:24-04:00
logc420b234cc5698d44b11cf313f14a9608328f111
tree10a0452bcd450020ab0fa5429b6ffd1b1e190b0c
parentaa232089f2beaa273458a9fa75b2ba5c70f71805

translate-c: handle for loop with empty body


2 files changed, 18 insertions(+), 5 deletions(-)

src/translate_c.cpp+8-5
...@@ -3020,8 +3020,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,...@@ -3020,8 +3020,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
3020 if (while_node->data.while_expr.body == nullptr) {3020 if (while_node->data.while_expr.body == nullptr) {
3021 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);3021 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
3022 }3022 }
3023 return wrap_stmt(out_node, out_child_scope, scope,3023 return wrap_stmt(out_node, out_child_scope, scope, while_node);
3024 while_node);
3025 }3024 }
3026 case Stmt::IfStmtClass:3025 case Stmt::IfStmtClass:
3027 return wrap_stmt(out_node, out_child_scope, scope,3026 return wrap_stmt(out_node, out_child_scope, scope,
...@@ -3048,9 +3047,13 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,...@@ -3048,9 +3047,13 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
3048 case Stmt::DoStmtClass:3047 case Stmt::DoStmtClass:
3049 return wrap_stmt(out_node, out_child_scope, scope,3048 return wrap_stmt(out_node, out_child_scope, scope,
3050 trans_do_loop(c, scope, (const DoStmt *)stmt));3049 trans_do_loop(c, scope, (const DoStmt *)stmt));
3051 case Stmt::ForStmtClass:3050 case Stmt::ForStmtClass: {
3052 return wrap_stmt(out_node, out_child_scope, scope,3051 AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)stmt);
3053 trans_for_loop(c, scope, (const ForStmt *)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 }
3054 case Stmt::StringLiteralClass:3057 case Stmt::StringLiteralClass:
3055 return wrap_stmt(out_node, out_child_scope, scope,3058 return wrap_stmt(out_node, out_child_scope, scope,
3056 trans_string_literal(c, scope, (const StringLiteral *)stmt));3059 trans_string_literal(c, scope, (const StringLiteral *)stmt));
test/translate_c.zig+10
...@@ -1,6 +1,16 @@...@@ -1,6 +1,16 @@
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 with empty body",
5 \\void foo(void) {
6 \\ for (;;);
7 \\}
8 ,
9 \\pub fn foo() void {
10 \\ while (true) {}
11 \\}
12 );
13
4 cases.add("while with empty body",14 cases.add("while with empty body",
5 \\void foo(void) {15 \\void foo(void) {
6 \\ while (1);16 \\ while (1);