[clang] de7494a - [AST] fail rather than crash when const evaluating invalid c++ foreach

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 27 13:45:43 PDT 2021


Author: Sam McCall
Date: 2021-10-27T22:45:32+02:00
New Revision: de7494a33a5c3152dd0ed24ea1c8b2760255f2f7

URL: https://github.com/llvm/llvm-project/commit/de7494a33a5c3152dd0ed24ea1c8b2760255f2f7
DIFF: https://github.com/llvm/llvm-project/commit/de7494a33a5c3152dd0ed24ea1c8b2760255f2f7.diff

LOG: [AST] fail rather than crash when const evaluating invalid c++ foreach

Differential Revision: https://reviews.llvm.org/D112633

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/SemaCXX/constexpr-function-recovery-crash.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index c74e4c96f7b0..fa0d22064f0e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -5319,6 +5319,11 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
       return ESR;
     }
 
+    // In error-recovery cases it's possible to get here even if we failed to
+    // synthesize the __begin and __end variables.
+    if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
+      return ESR_Failed;
+
     // Create the __begin and __end iterators.
     ESR = EvaluateStmt(Result, Info, FS->getBeginStmt());
     if (ESR != ESR_Succeeded) {

diff  --git a/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp b/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
index 94be9a12bc66..4b73cff13838 100644
--- a/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
+++ b/clang/test/SemaCXX/constexpr-function-recovery-crash.cpp
@@ -69,3 +69,8 @@ constexpr int test9(int x) {
 
 constexpr int test10() { return undef(); } // expected-error {{use of undeclared identifier 'undef'}}
 static_assert(test10() <= 1, "should not crash"); // expected-error {{static_assert expression is not an integral constant expression}}
+
+struct X {} array[] = {undef()}; // expected-error {{use of undeclared identifier 'undef'}}
+constexpr void test11() {
+  for (X& e : array) {}
+}


        


More information about the cfe-commits mailing list