commit aa232089f2beaa273458a9fa75b2ba5c70f71805 (tree)
parent 9bd8b01650f9cf21e601117951711b21aa5fd216
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Sun, 5 Aug 2018 18:06:39 -0400
translate-c: fix while loop with no body
Diffstat:
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/src/translate_c.cpp b/src/translate_c.cpp
@@ -3015,9 +3015,14 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt,
trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt));
case Stmt::DeclStmtClass:
return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope);
- case Stmt::WhileStmtClass:
+ case Stmt::WhileStmtClass: {
+ AstNode *while_node = trans_while_loop(c, scope, (const WhileStmt *)stmt);
+ if (while_node->data.while_expr.body == nullptr) {
+ while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock);
+ }
return wrap_stmt(out_node, out_child_scope, scope,
- trans_while_loop(c, scope, (const WhileStmt *)stmt));
+ while_node);
+ }
case Stmt::IfStmtClass:
return wrap_stmt(out_node, out_child_scope, scope,
trans_if_statement(c, scope, (const IfStmt *)stmt));
diff --git a/test/translate_c.zig b/test/translate_c.zig
@@ -1,6 +1,16 @@
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.TranslateCContext) void {
+ cases.add("while with empty body",
+ \\void foo(void) {
+ \\ while (1);
+ \\}
+ ,
+ \\pub fn foo() void {
+ \\ while (1 != 0) {}
+ \\}
+ );
+
cases.add("double define struct",
\\typedef struct Bar Bar;
\\typedef struct Foo Foo;