[clang] [Clang] Fix a crash in the diagnostic emission of invalid immediate calls (PR #66699)

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 18 14:12:25 PDT 2023


https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/66699

>From c3267c587368eec3744640f895f9fc88d6c4414d Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 18 Sep 2023 23:06:11 +0200
Subject: [PATCH] [Clang] Fix a crash in the diagnostic emission of invalid
 immediate calls.

`CXXCtorInitializer` may not refer to a FieldDecl because it might
also denote another constructor call.

Fixes #66324
---
 clang/docs/ReleaseNotes.rst                   |  4 ++++
 clang/lib/Sema/SemaDeclCXX.cpp                |  7 ++++--
 .../SemaCXX/cxx2b-consteval-propagate.cpp     | 23 +++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7e964f6eb435bb0..d48bcb24e74ddb8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -297,6 +297,10 @@ Bug Fixes to C++ Support
   definition the specialization was instantiated from.
   (`#26057 <https://github.com/llvm/llvm-project/issues/26057>`_`)
 
+- Fix a crash when a default member initializer of a base aggregate
+  makes an invalid call to an immediate function.
+  (`#66324 <https://github.com/llvm/llvm-project/issues/66324>`_)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 1f2bb1edd3b7be5..83a5674092b2611 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2514,12 +2514,15 @@ void Sema::DiagnoseImmediateEscalatingReason(FunctionDecl *FD) {
         Range = CurrentInit->isWritten() ? CurrentInit->getSourceRange()
                                          : SourceRange();
       }
+
+      FieldDecl* InitializedField = CurrentInit ? CurrentInit->getAnyMember() : nullptr;
+
       SemaRef.Diag(Loc, diag::note_immediate_function_reason)
           << ImmediateFn << Fn << Fn->isConsteval() << IsCall
           << isa<CXXConstructorDecl>(Fn) << ImmediateFnIsConstructor
-          << (CurrentInit != nullptr)
+          << (InitializedField != nullptr)
           << (CurrentInit && !CurrentInit->isWritten())
-          << (CurrentInit ? CurrentInit->getAnyMember() : nullptr) << Range;
+          << InitializedField << Range;
     }
     bool TraverseCallExpr(CallExpr *E) {
       if (const auto *DR =
diff --git a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
index def3bbb8adf0fce..c0adbbdf9be6353 100644
--- a/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ b/clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -330,3 +330,26 @@ struct S {
 S s(0); // expected-note {{in the default initializer of 'j'}}
 
 }
+
+namespace GH66324 {
+
+consteval int allocate();  // expected-note  2{{declared here}}
+
+struct _Vector_base {
+  int b =  allocate(); // expected-note 2{{undefined function 'allocate' cannot be used in a constant expression}} \
+  // expected-error {{call to consteval function 'GH66324::allocate' is not a constant expression}} \
+  // expected-note  {{declared here}}
+};
+
+template <typename>
+struct vector : _Vector_base {
+  constexpr vector()
+  // expected-note at -1 {{'vector' is an immediate constructor because its body contains a call to a consteval function 'allocate' and that call is not a constant expression}}
+  : _Vector_base{} {} // expected-note {{in the default initializer of 'b'}}
+};
+
+vector<void> v{};
+// expected-error at -1 {{call to immediate function 'GH66324::vector<void>::vector' is not a constant expression}}
+// expected-note at -2 {{in call to 'vector()'}}
+
+}



More information about the cfe-commits mailing list