Skip to content

Commit 467f72d

Browse files
committed
[clang][Sema] Fix the continue and break scope for while loops
Make sure we don't push the break and continue scope for a while loop until after we have evaluated the condition.
1 parent 82046c7 commit 467f72d

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Bug Fixes in This Version
161161
targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously
162162
the warning was silently lost because the operands differed only by an implicit
163163
cast chain. (#GH149967).
164+
- Correct the continue and break scope for while statements to be after the
165+
condition is evaluated.
164166

165167
Bug Fixes to Compiler Builtins
166168
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Parse/ParseStmt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,6 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
17341734
Scope::DeclScope | Scope::ControlScope;
17351735
else
17361736
ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1737-
ParseScope WhileScope(this, ScopeFlags);
17381737

17391738
// Parse the condition.
17401739
Sema::ConditionResult Cond;
@@ -1744,6 +1743,8 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
17441743
Sema::ConditionKind::Boolean, LParen, RParen))
17451744
return StmtError();
17461745

1746+
ParseScope WhileScope(this, ScopeFlags);
1747+
17471748
// OpenACC Restricts a while-loop inside of certain construct/clause
17481749
// combinations, so diagnose that here in OpenACC mode.
17491750
SemaOpenACC::LoopInConstructRAII LCR{getActions().OpenACC()};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
void f() {
4+
while (({ continue; 1; })) {
5+
// expected-error@-1 {{'continue' statement not in loop statement}}
6+
7+
}
8+
while (({ break; 1; })) {
9+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
10+
}
11+
}

0 commit comments

Comments
 (0)