Commit c420b234cc

Andrew Kelley <superjoe30@gmail.com>
2018-08-06 00:18:24
translate-c: handle for loop with empty body
1 parent aa23208
Changed files (2)
src/translate_c.cpp
@@ -3020,8 +3020,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *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,
-                    while_node);
+            return wrap_stmt(out_node, out_child_scope, scope, while_node);
         }
         case Stmt::IfStmtClass:
             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,
         case Stmt::DoStmtClass:
             return wrap_stmt(out_node, out_child_scope, scope,
                     trans_do_loop(c, scope, (const DoStmt *)stmt));
-        case Stmt::ForStmtClass:
-            return wrap_stmt(out_node, out_child_scope, scope,
-                    trans_for_loop(c, scope, (const ForStmt *)stmt));
+        case Stmt::ForStmtClass: {
+            AstNode *while_node = trans_for_loop(c, scope, (const ForStmt *)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, while_node);
+        }
         case Stmt::StringLiteralClass:
             return wrap_stmt(out_node, out_child_scope, scope,
                     trans_string_literal(c, scope, (const StringLiteral *)stmt));
test/translate_c.zig
@@ -1,6 +1,16 @@
 const tests = @import("tests.zig");
 
 pub fn addCases(cases: *tests.TranslateCContext) void {
+    cases.add("for with empty body",
+        \\void foo(void) {
+        \\    for (;;);
+        \\}
+    ,
+        \\pub fn foo() void {
+        \\    while (true) {}
+        \\}
+    );
+
     cases.add("while with empty body",
         \\void foo(void) {
         \\    while (1);