translate-c: fix while loop with no body

This commit is contained in:
Andrew Kelley 2018-08-05 18:06:39 -04:00
parent 9bd8b01650
commit aa232089f2
2 changed files with 17 additions and 2 deletions

View File

@ -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));

View File

@ -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;