Skip to content

Commit e5f3f97

Browse files
author
Kevin Egan
committed
Add _for_break variable when we break inside of nested do/while(false) loop (#1436)
1 parent 1a6062a commit e5f3f97

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

source/to_cpp1.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,18 +2409,17 @@ class cppfront
24092409
printer.print_extra("{");
24102410
}
24112411

2412-
// If there's a next-expression, smuggle it in via a nested do/while(false) loop
2413-
// (nested "continue" will work, but "break" won't until we do extra work to implement
2414-
// that using a flag and implementing "break" as "_for_break = true; continue;")
2412+
// If there's a next-expression, smuggle it in via a nested do/while(false) loop.
2413+
// We implement break as "_for_break = true; continue;")
24152414
if (n.next_expression) {
2416-
printer.print_cpp2(" { do ", n.position());
2415+
printer.print_cpp2(" { bool _for_break = false; do ", n.position());
24172416
}
24182417

24192418
assert(n.body);
24202419
emit(*n.body);
24212420

24222421
if (n.next_expression) {
2423-
printer.print_cpp2(" while (false); ", n.position());
2422+
printer.print_cpp2(" while (false); if (_for_break) { break; } ", n.position());
24242423
emit(*n.next_expression);
24252424
printer.print_cpp2("; }", n.position());
24262425
}
@@ -2684,8 +2683,14 @@ class cppfront
26842683
);
26852684
}
26862685
else {
2687-
emit(*n.keyword);
2688-
printer.print_cpp2(";", n.position());
2686+
if (*n.keyword == "break" && iteration_statements.back().stmt->next_expression
2687+
&& *(iteration_statements.back().stmt->identifier) == "for") {
2688+
// if we have a next expression there is an nested "do/while(false) loop
2689+
printer.print_cpp2("_for_break = true; continue;", n.position());
2690+
} else {
2691+
emit(*n.keyword);
2692+
printer.print_cpp2(";", n.position());
2693+
}
26892694
}
26902695
}
26912696

0 commit comments

Comments
 (0)