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,
30203020 if (while_node->data.while_expr.body == nullptr) {
30213021 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
30223022 }
3023 return wrap_stmt(out_node, out_child_scope, scope,
3024 while_node);
3023 return wrap_stmt(out_node, out_child_scope, scope, while_node);
30253024 }
30263025 case Stmt::IfStmtClass:
30273026 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,
30483047 case Stmt::DoStmtClass:
30493048 return wrap_stmt(out_node, out_child_scope, scope,
30503049 trans_do_loop(c, scope, (const DoStmt *)stmt));
3051 case Stmt::ForStmtClass:
3052 return wrap_stmt(out_node, out_child_scope, scope,
3053 trans_for_loop(c, scope, (const ForStmt *)stmt));
3050 case Stmt::ForStmtClass: {
3051 AstNode *while_node = 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 }
30543057 case Stmt::StringLiteralClass:
30553058 return wrap_stmt(out_node, out_child_scope, scope,
30563059 trans_string_literal(c, scope, (const StringLiteral *)stmt));
test/translate_c.zig+10
......@@ -1,6 +1,16 @@
11const tests = @import("tests.zig");
22
33pub 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
414 cases.add("while with empty body",
515 \\void foo(void) {
616 \\ while (1);