@@ -1728,12 +1728,11 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
17281728 // while, for, and switch statements are local to the if, while, for, or
17291729 // switch statement (including the controlled statement).
17301730 //
1731- unsigned ScopeFlags;
1731+ unsigned ScopeFlags = 0 ;
17321732 if (C99orCXX)
1733- ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1734- Scope::DeclScope | Scope::ControlScope;
1735- else
1736- ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1733+ ScopeFlags = Scope::DeclScope | Scope::ControlScope;
1734+
1735+ ParseScope WhileScope (this , ScopeFlags);
17371736
17381737 // Parse the condition.
17391738 Sema::ConditionResult Cond;
@@ -1743,7 +1742,7 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
17431742 Sema::ConditionKind::Boolean, LParen, RParen))
17441743 return StmtError ();
17451744
1746- ParseScope WhileScope ( this , ScopeFlags );
1745+ getCurScope ()-> AddFlags (Scope::BreakScope | Scope::ContinueScope );
17471746
17481747 // OpenACC Restricts a while-loop inside of certain construct/clause
17491748 // combinations, so diagnose that here in OpenACC mode.
@@ -1839,6 +1838,8 @@ StmtResult Parser::ParseDoStatement() {
18391838 // A do-while expression is not a condition, so can't have attributes.
18401839 DiagnoseAndSkipCXX11Attributes ();
18411840
1841+ DoScope.Exit ();
1842+
18421843 SourceLocation Start = Tok.getLocation ();
18431844 ExprResult Cond = ParseExpression ();
18441845 if (!Cond.isUsable ()) {
@@ -1849,7 +1850,6 @@ StmtResult Parser::ParseDoStatement() {
18491850 Actions.getASTContext ().BoolTy );
18501851 }
18511852 T.consumeClose ();
1852- DoScope.Exit ();
18531853
18541854 if (Cond.isInvalid () || Body.isInvalid ())
18551855 return StmtError ();
@@ -2124,9 +2124,6 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
21242124 }
21252125
21262126 } else {
2127- // We permit 'continue' and 'break' in the condition of a for loop.
2128- getCurScope ()->AddFlags (Scope::BreakScope | Scope::ContinueScope);
2129-
21302127 ExprResult SecondExpr = ParseExpression ();
21312128 if (SecondExpr.isInvalid ())
21322129 SecondPart = Sema::ConditionError ();
@@ -2138,11 +2135,6 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
21382135 }
21392136 }
21402137
2141- // Enter a break / continue scope, if we didn't already enter one while
2142- // parsing the second part.
2143- if (!getCurScope ()->isContinueScope ())
2144- getCurScope ()->AddFlags (Scope::BreakScope | Scope::ContinueScope);
2145-
21462138 // Parse the third part of the for statement.
21472139 if (!ForEach && !ForRangeInfo.ParsedForRangeDecl ()) {
21482140 if (Tok.isNot (tok::semi)) {
@@ -2165,6 +2157,8 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
21652157 // Match the ')'.
21662158 T.consumeClose ();
21672159
2160+ getCurScope ()->AddFlags (Scope::BreakScope | Scope::ContinueScope);
2161+
21682162 // C++ Coroutines [stmt.iter]:
21692163 // 'co_await' can only be used for a range-based for statement.
21702164 if (CoawaitLoc.isValid () && !ForRangeInfo.ParsedForRangeDecl ()) {
0 commit comments