Skip to content

Commit e2b0c23

Browse files
committed
other loop kinds in progress
1 parent 467f72d commit e2b0c23

File tree

5 files changed

+38
-40
lines changed

5 files changed

+38
-40
lines changed

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,10 +1877,8 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
18771877
struct ForConditionScopeRAII {
18781878
Scope *S;
18791879
void enter(bool IsConditionVariable) {
1880-
if (S) {
1881-
S->AddFlags(Scope::BreakScope | Scope::ContinueScope);
1880+
if (S)
18821881
S->setIsConditionVarScope(IsConditionVariable);
1883-
}
18841882
}
18851883
~ForConditionScopeRAII() {
18861884
if (S)

clang/lib/Parse/ParseStmt.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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()) {

clang/test/CoverageMapping/break.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,3 @@ int main(void) { // CHECK: File 0, [[@LINE]]:16 -> {{[0-9]+}}:2 = #0
3131
++cnt;
3232
}
3333
}
34-
35-
// CHECK-LABEL: break_continue_in_increment:
36-
// CHECK: [[@LINE+6]]:11 -> [[@LINE+6]]:45 = #1
37-
// CHECK: [[@LINE+5]]:18 -> [[@LINE+5]]:19 = #1
38-
// CHECK: [[@LINE+4]]:21 -> [[@LINE+4]]:26 = #2
39-
// CHECK: [[@LINE+3]]:33 -> [[@LINE+3]]:41 = (#1 - #2)
40-
// CHECK: [[@LINE+3]]:5 -> [[@LINE+3]]:6 = #1
41-
void break_continue_in_increment(int x) {
42-
for (;; ({ if (x) break; else continue; }))
43-
;
44-
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
while (({ break; 1; })) {}
7+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
8+
do {} while (({ break; 1; }));
9+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
10+
do {} while (({ continue; 1;}));
11+
// expected-error@-1 {{'continue' statement not in loop statement}}
12+
for (({ continue; });;) {}
13+
// expected-error@-1 {{'continue' statement not in loop statement}}
14+
for (;({ continue; 1;});) {}
15+
// expected-error@-1 {{'continue' statement not in loop statement}}
16+
for (;;({ continue;})) {}
17+
// expected-error@-1 {{'continue' statement not in loop statement}}
18+
for (({ break;});;) {}
19+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
20+
for (;({ break; 1;});) {}
21+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
22+
for (;;({ break;})) {}
23+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
24+
switch(({break;1;})){
25+
// expected-error@-1 {{'break' statement not in loop or switch statement}}
26+
case 1: break;
27+
}
28+
}

clang/test/Sema/while-loop-condition-scope.c

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)