Skip to content

Commit de7494a

Browse files
committed
[AST] fail rather than crash when const evaluating invalid c++ foreach
Differential Revision: https://reviews.llvm.org/D112633
1 parent 3d13ee2 commit de7494a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5319,6 +5319,11 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
53195319
return ESR;
53205320
}
53215321

5322+
// In error-recovery cases it's possible to get here even if we failed to
5323+
// synthesize the __begin and __end variables.
5324+
if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
5325+
return ESR_Failed;
5326+
53225327
// Create the __begin and __end iterators.
53235328
ESR = EvaluateStmt(Result, Info, FS->getBeginStmt());
53245329
if (ESR != ESR_Succeeded) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ constexpr int test9(int x) {
6969

7070
constexpr int test10() { return undef(); } // expected-error {{use of undeclared identifier 'undef'}}
7171
static_assert(test10() <= 1, "should not crash"); // expected-error {{static_assert expression is not an integral constant expression}}
72+
73+
struct X {} array[] = {undef()}; // expected-error {{use of undeclared identifier 'undef'}}
74+
constexpr void test11() {
75+
for (X& e : array) {}
76+
}

0 commit comments

Comments
 (0)