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(...@@ -2119,14 +2119,16 @@ fn transForLoop(
2119 .parent = scope,2119 .parent = scope,
2120 .id = .Loop,2120 .id = .Loop,
2121 };2121 };
2122 var block = false;2122
2123 var block_scope: ?*Scope.Block = null;2123 var block_scope: ?*Scope.Block = null;
2124 if (ZigClangForStmt_getInit(stmt)) |init| {2124 if (ZigClangForStmt_getInit(stmt)) |init| {
2125 block_scope = try Scope.Block.init(rp.c, scope, null);2125 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;
2127 loop_scope.parent = &block_scope.?.base;2128 loop_scope.parent = &block_scope.?.base;
2128 const init_stmt = try transStmt(rp, &loop_scope, init, .unused, .r_value);2129 const result = try transStmt(rp, &block_scope.?.base, init, .unused, .r_value);
2129 try block_scope.?.block_node.statements.push(init_stmt);2130 if (result != &block.base)
2131 try block.statements.push(result);
2130 }2132 }
2131 var cond_scope = Scope{2133 var cond_scope = Scope{
2132 .parent = scope,2134 .parent = scope,
test/translate_c.zig+15
...@@ -696,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -696,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
696 \\}696 \\}
697 });697 });
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
699 cases.add("break statement",714 cases.add("break statement",
700 \\void foo(void) {715 \\void foo(void) {
701 \\ for (;;) {716 \\ for (;;) {