Fix translation of for loop init

Closes #4067
This commit is contained in:
LemonBoy 2020-01-04 11:49:43 +01:00
parent 51e430fac0
commit a712ea333b
2 changed files with 21 additions and 4 deletions

View File

@ -2119,14 +2119,16 @@ fn transForLoop(
.parent = scope,
.id = .Loop,
};
var block = false;
var block_scope: ?*Scope.Block = null;
if (ZigClangForStmt_getInit(stmt)) |init| {
block_scope = try Scope.Block.init(rp.c, scope, null);
block_scope.?.block_node = try transCreateNodeBlock(rp.c, null);
const block = try transCreateNodeBlock(rp.c, null);
block_scope.?.block_node = block;
loop_scope.parent = &block_scope.?.base;
const init_stmt = try transStmt(rp, &loop_scope, init, .unused, .r_value);
try block_scope.?.block_node.statements.push(init_stmt);
const result = try transStmt(rp, &block_scope.?.base, init, .unused, .r_value);
if (result != &block.base)
try block.statements.push(result);
}
var cond_scope = Scope{
.parent = scope,

View File

@ -696,6 +696,21 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
});
cases.add("for loop with simple init expression",
\\void foo(void) {
\\ int i;
\\ for (i = 3; i; i--) { }
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ var i: c_int = undefined;
\\ {
\\ i = 3;
\\ while (i != 0) : (i -= 1) {}
\\ }
\\}
});
cases.add("break statement",
\\void foo(void) {
\\ for (;;) {