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,...@@ -3015,9 +3015,14 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
3015 trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt));3015 trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt));
3016 case Stmt::DeclStmtClass:3016 case Stmt::DeclStmtClass:
3017 return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope);3017 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 }
3019 return wrap_stmt(out_node, out_child_scope, scope,3023 return wrap_stmt(out_node, out_child_scope, scope,
3020 trans_while_loop(c, scope, (const WhileStmt *)stmt));3024 while_node);
3025 }
3021 case Stmt::IfStmtClass:3026 case Stmt::IfStmtClass:
3022 return wrap_stmt(out_node, out_child_scope, scope,3027 return wrap_stmt(out_node, out_child_scope, scope,
3023 trans_if_statement(c, scope, (const IfStmt *)stmt));3028 trans_if_statement(c, scope, (const IfStmt *)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("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
4 cases.add("double define struct",14 cases.add("double define struct",
5 \\typedef struct Bar Bar;15 \\typedef struct Bar Bar;
6 \\typedef struct Foo Foo;16 \\typedef struct Foo Foo;