Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add _for_break variable when we break inside of nested do/while(false…
…) loop (#1436)
  • Loading branch information
Kevin Egan committed Nov 29, 2025
commit e5f3f978a6fbccfb0dae438a0a8f108ce7db1e5d
19 changes: 12 additions & 7 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -2409,18 +2409,17 @@ class cppfront
printer.print_extra("{");
}

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

assert(n.body);
emit(*n.body);

if (n.next_expression) {
printer.print_cpp2(" while (false); ", n.position());
printer.print_cpp2(" while (false); if (_for_break) { break; } ", n.position());
emit(*n.next_expression);
printer.print_cpp2("; }", n.position());
}
Expand Down Expand Up @@ -2684,8 +2683,14 @@ class cppfront
);
}
else {
emit(*n.keyword);
printer.print_cpp2(";", n.position());
if (*n.keyword == "break" && iteration_statements.back().stmt->next_expression
&& *(iteration_statements.back().stmt->identifier) == "for") {
// if we have a next expression there is an nested "do/while(false) loop
printer.print_cpp2("_for_break = true; continue;", n.position());
} else {
emit(*n.keyword);
printer.print_cpp2(";", n.position());
}
}
}

Expand Down