authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:06:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-05 18:06:39-04:00
logaa232089f2beaa273458a9fa75b2ba5c70f71805
tree4d7c282e55605199dc4249e22ce2fcd9e8e51828
parent9bd8b01650f9cf21e601117951711b21aa5fd216

translate-c: fix while loop with no body


2 files changed, 17 insertions(+), 2 deletions(-)

src/translate_c.cpp+7-2
......@@ -3015,9 +3015,14 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
30153015 trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt));
30163016 case Stmt::DeclStmtClass:
30173017 return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope);
3018 case Stmt::WhileStmtClass:
3018 case Stmt::WhileStmtClass: {
3019 AstNode *while_node = trans_while_loop(c, scope, (const WhileStmt *)stmt);
3020 if (while_node->data.while_expr.body == nullptr) {
3021 while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
3022 }
30193023 return wrap_stmt(out_node, out_child_scope, scope,
3020 trans_while_loop(c, scope, (const WhileStmt *)stmt));
3024 while_node);
3025 }
30213026 case Stmt::IfStmtClass:
30223027 return wrap_stmt(out_node, out_child_scope, scope,
30233028 trans_if_statement(c, scope, (const IfStmt *)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("while with empty body",
5 \\void foo(void) {
6 \\ while (1);
7 \\}
8 ,
9 \\pub fn foo() void {
10 \\ while (1 != 0) {}
11 \\}
12 );
13
414 cases.add("double define struct",
515 \\typedef struct Bar Bar;
616 \\typedef struct Foo Foo;