@@ -990,6 +990,7 @@ bool Converter::VisitIfStmt(clang::IfStmt *stmt) {
990990}
991991
992992bool Converter::VisitWhileStmt (clang::WhileStmt *stmt) {
993+ PushBreakTarget push (break_target_stack_, BreakTarget::Loop);
993994 StrCat (" 'loop_:" );
994995 StrCat (keyword::kWhile );
995996 ConvertCondition (stmt->getCond ());
@@ -1002,6 +1003,7 @@ bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
10021003}
10031004
10041005bool Converter::VisitDoStmt (clang::DoStmt *stmt) {
1006+ PushBreakTarget push (break_target_stack_, BreakTarget::Loop);
10051007 StrCat (" 'loop_:" );
10061008 StrCat (keyword::kLoop , token::kOpenCurlyBracket );
10071009 curr_for_inc_.emplace (nullptr );
@@ -1016,6 +1018,7 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
10161018}
10171019
10181020bool Converter::VisitForStmt (clang::ForStmt *stmt) {
1021+ PushBreakTarget push (break_target_stack_, BreakTarget::Loop);
10191022 Convert (stmt->getInit ());
10201023 StrCat (" 'loop_:" );
10211024 StrCat (keyword::kWhile );
@@ -1055,6 +1058,7 @@ void Converter::ConvertLoopVariable(clang::VarDecl *decl,
10551058
10561059void Converter::ConvertForRangeBody (clang::CXXForRangeStmt *stmt,
10571060 const clang::VarDecl *map_iter_decl) {
1061+ PushBreakTarget push (break_target_stack_, BreakTarget::Loop);
10581062 std::optional<ScopedMapIterDecl> skip;
10591063 if (map_iter_decl)
10601064 skip.emplace (*this , map_iter_decl);
@@ -1136,10 +1140,12 @@ bool Converter::VisitCXXForRangeStmtIndexBased(clang::CXXForRangeStmt *stmt,
11361140}
11371141
11381142bool Converter::VisitBreakStmt ([[maybe_unused]] clang::BreakStmt *stmt) {
1139- StrCat (keyword:: kBreak );
1140- if (switch_depth_ > 0 ) {
1143+ if (break_target_stack_. isRegularSwitch ()) {
1144+ StrCat (keyword:: kBreak );
11411145 StrCat (" 'switch" );
1146+ return false ;
11421147 }
1148+ StrCat (keyword::kBreak );
11431149 return false ;
11441150}
11451151
@@ -2657,9 +2663,9 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26572663 StrCat (" {" );
26582664 }
26592665
2660- if (!has_fallthrough) {
2661- ++switch_depth_;
2662- }
2666+ PushBreakTarget push (break_target_stack_, has_fallthrough
2667+ ? BreakTarget::FallthroughSwitch
2668+ : BreakTarget::RegularSwitch);
26632669
26642670 clang::SwitchCase *default_case = nullptr ;
26652671 for (auto *sc : GetTopLevelSwitchCases (stmt)) {
@@ -2685,10 +2691,6 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
26852691 StrCat (R"( _ => {})" );
26862692 }
26872693
2688- if (!has_fallthrough) {
2689- --switch_depth_;
2690- }
2691-
26922694 if (has_fallthrough) {
26932695 StrCat (" })" );
26942696 } else {
0 commit comments