authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-04 11:49:43+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-04 11:49:43+01:00
loga712ea333b9ddb140331c159d23d16ef6ed4ef14
treeb77ee53815e969a40198b1edc912ce773cf1e9e5
parent51e430fac01a4b36a61d4c626d7c25158898e216

Fix translation of for loop init

Closes #4067

2 files changed, 21 insertions(+), 4 deletions(-)

src-self-hosted/translate_c.zig+6-4
......@@ -2119,14 +2119,16 @@ fn transForLoop(
21192119 .parent = scope,
21202120 .id = .Loop,
21212121 };
2122 var block = false;
2122
21232123 var block_scope: ?*Scope.Block = null;
21242124 if (ZigClangForStmt_getInit(stmt)) |init| {
21252125 block_scope = try Scope.Block.init(rp.c, scope, null);
2126 block_scope.?.block_node = try transCreateNodeBlock(rp.c, null);
2126 const block = try transCreateNodeBlock(rp.c, null);
2127 block_scope.?.block_node = block;
21272128 loop_scope.parent = &block_scope.?.base;
2128 const init_stmt = try transStmt(rp, &loop_scope, init, .unused, .r_value);
2129 try block_scope.?.block_node.statements.push(init_stmt);
2129 const result = try transStmt(rp, &block_scope.?.base, init, .unused, .r_value);
2130 if (result != &block.base)
2131 try block.statements.push(result);
21302132 }
21312133 var cond_scope = Scope{
21322134 .parent = scope,
test/translate_c.zig+15
......@@ -696,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
696696 \\}
697697 });
698698
699 cases.add("for loop with simple init expression",
700 \\void foo(void) {
701 \\ int i;
702 \\ for (i = 3; i; i--) { }
703 \\}
704 , &[_][]const u8{
705 \\pub export fn foo() void {
706 \\ var i: c_int = undefined;
707 \\ {
708 \\ i = 3;
709 \\ while (i != 0) : (i -= 1) {}
710 \\ }
711 \\}
712 });
713
699714 cases.add("break statement",
700715 \\void foo(void) {
701716 \\ for (;;) {