[clang] 6824d15 - [Clang] Fix a crash when an invalid immediate function call appears in a cast

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 24 23:50:47 PDT 2023


Author: Corentin Jabot
Date: 2023-08-25T08:50:41+02:00
New Revision: 6824d156d5dd9f5a3e837d8d4bc1dadb48170e2f

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

LOG: [Clang] Fix a crash when an invalid immediate function call appears in a cast

Fixes #64949

Reviewed By: Fznamznon, erichkeane, shafik

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

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaExpr.cpp
    clang/test/SemaCXX/cxx2a-consteval.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8d9f4a5b86b54c..a7c39bad615406 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -206,6 +206,10 @@ Bug Fixes to C++ Support
   of a binary comparision.
   (`#64923 <https://github.com/llvm/llvm-project/issues/64923>_``)
 
+- Fix a crash when an immediate invocation is not a constant expression
+  and appear in an implicit cast.
+  (`#64949 <https://github.com/llvm/llvm-project/issues/64949>`_).
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index ac6c3ba6b3575d..f9badf4ede847f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -18352,15 +18352,17 @@ static void EvaluateAndDiagnoseImmediateInvocation(
     SemaRef.FailedImmediateInvocations.insert(CE);
     Expr *InnerExpr = CE->getSubExpr()->IgnoreImplicit();
     if (auto *FunctionalCast = dyn_cast<CXXFunctionalCastExpr>(InnerExpr))
-      InnerExpr = FunctionalCast->getSubExpr();
+      InnerExpr = FunctionalCast->getSubExpr()->IgnoreImplicit();
     FunctionDecl *FD = nullptr;
     if (auto *Call = dyn_cast<CallExpr>(InnerExpr))
       FD = cast<FunctionDecl>(Call->getCalleeDecl());
     else if (auto *Call = dyn_cast<CXXConstructExpr>(InnerExpr))
       FD = Call->getConstructor();
-    else
-      llvm_unreachable("unhandled decl kind");
-    assert(FD && FD->isImmediateFunction());
+    else if (auto *Cast = dyn_cast<CastExpr>(InnerExpr))
+      FD = dyn_cast_or_null<FunctionDecl>(Cast->getConversionFunction());
+
+    assert(FD && FD->isImmediateFunction() &&
+           "could not find an immediate function in this expression");
     SemaRef.Diag(CE->getBeginLoc(), diag::err_invalid_consteval_call)
         << FD << FD->isConsteval();
     if (auto Context =

diff  --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp
index 0c24fc74196988..d98ec8048c3246 100644
--- a/clang/test/SemaCXX/cxx2a-consteval.cpp
+++ b/clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -1103,3 +1103,27 @@ void bar() {
                                                 // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
 }
 }
+
+namespace GH64949 {
+struct f {
+  int g; // expected-note 2{{subobject declared here}}
+  constexpr ~f() {}
+};
+class h {
+
+public:
+  consteval h(char *) {}
+  consteval operator int() const { return 1; }
+  f i;
+};
+
+void test() { (int)h{nullptr}; }
+// expected-error at -1 {{call to consteval function 'GH64949::h::h' is not a constant expression}}
+// expected-note at -2 {{subobject 'g' is not initialized}}
+
+int  test2() { return h{nullptr}; }
+// expected-error at -1 {{call to consteval function 'GH64949::h::h' is not a constant expression}}
+// expected-note at -2 {{subobject 'g' is not initialized}}
+
+
+}


        


More information about the cfe-commits mailing list