Skip to content

Commit 3cfdef3

Browse files
yronglinshafik
andauthored
[Clang] Fix the for statement disappearing in AST when an error occurs in the conditional expression of the for statement (#65381)
Consider: ``` constexpr int f() { int sum = 0; for (int i = 0; undefined_var; ++i) { sum += i; } return sum; } static_assert(f()); ``` The AST before this patch: ``` |-FunctionDecl <line:1:1, line:7:1> line:1:15 used constexpr f 'int ()' implicit-inline | `-CompoundStmt <col:19, line:7:1> | |-DeclStmt <line:2:5, col:16> | | `-VarDecl <col:5, col:15> col:9 used sum 'int' cinit | | `-IntegerLiteral <col:15> 'int' 0 | `-ReturnStmt <line:6:5, col:12> | `-ImplicitCastExpr <col:12> 'int' <LValueToRValue> | `-DeclRefExpr <col:12> 'int' lvalue Var 0xb870518 'sum' 'int' ``` The AST after this patch: ``` |-FunctionDecl 0x11d0f63f8 <./main.cpp:1:1, line:7:1> line:1:15 used constexpr f 'int ()' implicit-inline | `-CompoundStmt 0x11d110880 <col:19, line:7:1> | |-DeclStmt 0x11d0f65c8 <line:2:5, col:16> | | `-VarDecl 0x11d0f6528 <col:5, col:15> col:9 used sum 'int' cinit | | `-IntegerLiteral 0x11d0f6590 <col:15> 'int' 0 | |-ForStmt 0x11d110800 <line:3:5, line:5:5> | | |-DeclStmt 0x11d0f66a0 <line:3:10, col:19> | | | `-VarDecl 0x11d0f6600 <col:10, col:18> col:14 used i 'int' cinit | | | `-IntegerLiteral 0x11d0f6668 <col:18> 'int' 0 | | |-<<<NULL>>> | | |-RecoveryExpr 0x11d0f66e8 <col:21> 'bool' contains-errors | | |-UnaryOperator 0x11d0f6728 <col:36, col:38> 'int' lvalue prefix '++' | | | `-DeclRefExpr 0x11d0f6708 <col:38> 'int' lvalue Var 0x11d0f6600 'i' 'int' | | `-CompoundStmt 0x11d0f67c8 <col:41, line:5:5> | | `-CompoundAssignOperator 0x11d0f6798 <line:4:9, col:16> 'int' lvalue '+=' ComputeLHSTy='int' ComputeResultTy='int' | | |-DeclRefExpr 0x11d0f6740 <col:9> 'int' lvalue Var 0x11d0f6528 'sum' 'int' | | `-ImplicitCastExpr 0x11d0f6780 <col:16> 'int' <LValueToRValue> | | `-DeclRefExpr 0x11d0f6760 <col:16> 'int' lvalue Var 0x11d0f6600 'i' 'int' | `-ReturnStmt 0x11d110870 <line:6:5, col:12> | `-ImplicitCastExpr 0x11d110858 <col:12> 'int' <LValueToRValue> | `-DeclRefExpr 0x11d110838 <col:12> 'int' lvalue Var 0x11d0f6528 'sum' 'int' ``` --------- Co-authored-by: Shafik Yaghmour <[email protected]>
1 parent 123bf08 commit 3cfdef3

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

clang/lib/Parse/ParseStmt.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,11 +2158,13 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
21582158
// for-range-declaration next.
21592159
bool MightBeForRangeStmt = !ForRangeInfo.ParsedForRangeDecl();
21602160
ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
2161+
SourceLocation SecondPartStart = Tok.getLocation();
2162+
Sema::ConditionKind CK = Sema::ConditionKind::Boolean;
21612163
SecondPart = ParseCXXCondition(
2162-
nullptr, ForLoc, Sema::ConditionKind::Boolean,
2164+
/*InitStmt=*/nullptr, ForLoc, CK,
21632165
// FIXME: recovery if we don't see another semi!
21642166
/*MissingOK=*/true, MightBeForRangeStmt ? &ForRangeInfo : nullptr,
2165-
/*EnterForConditionScope*/ true);
2167+
/*EnterForConditionScope=*/true);
21662168

21672169
if (ForRangeInfo.ParsedForRangeDecl()) {
21682170
Diag(FirstPart.get() ? FirstPart.get()->getBeginLoc()
@@ -2178,6 +2180,19 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
21782180
<< FixItHint::CreateRemoval(EmptyInitStmtSemiLoc);
21792181
}
21802182
}
2183+
2184+
if (SecondPart.isInvalid()) {
2185+
ExprResult CondExpr = Actions.CreateRecoveryExpr(
2186+
SecondPartStart,
2187+
Tok.getLocation() == SecondPartStart ? SecondPartStart
2188+
: PrevTokLocation,
2189+
{}, Actions.PreferredConditionType(CK));
2190+
if (!CondExpr.isInvalid())
2191+
SecondPart = Actions.ActOnCondition(getCurScope(), ForLoc,
2192+
CondExpr.get(), CK,
2193+
/*MissingOK=*/false);
2194+
}
2195+
21812196
} else {
21822197
// We permit 'continue' and 'break' in the condition of a for loop.
21832198
getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);

clang/test/AST/ast-dump-recovery.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,18 @@ void RecoveryToDoWhileStmtCond() {
432432
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 10
433433
do {} while (some_invalid_val + 1 < 10);
434434
}
435+
436+
void RecoveryForStmtCond() {
437+
// CHECK:FunctionDecl {{.*}} RecoveryForStmtCond
438+
// CHECK-NEXT:`-CompoundStmt {{.*}}
439+
// CHECK-NEXT: `-ForStmt {{.*}}
440+
// CHECK-NEXT: |-DeclStmt {{.*}}
441+
// CHECK-NEXT: | `-VarDecl {{.*}}
442+
// CHECK-NEXT: | `-IntegerLiteral {{.*}} <col:16> 'int' 0
443+
// CHECK-NEXT: |-<<<NULL>>>
444+
// CHECK-NEXT: |-RecoveryExpr {{.*}} 'bool' contains-errors
445+
// CHECK-NEXT: |-UnaryOperator {{.*}} 'int' lvalue prefix '++'
446+
// CHECK-NEXT: | `-DeclRefExpr {{.*}} 'int' lvalue Var {{.*}} 'i' 'int'
447+
// CHECK-NEXT: `-CompoundStmt {{.*}}
448+
for (int i = 0; i < invalid; ++i) {}
449+
}

clang/test/SemaCXX/constexpr-function-recovery-crash.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ TEST_EVALUATE(ForCond, for (; !!;){};);// expected-error + {{}}
106106
TEST_EVALUATE(ForInc, for (;; !!){};);// expected-error + {{}}
107107
// expected-note@-1 + {{infinite loop}}
108108
// expected-note@-2 {{in call}}
109+
TEST_EVALUATE(ForCondUnDef, for (;some_cond;){};); // expected-error + {{}}

0 commit comments

Comments
 (0)