https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/66699
`CXXCtorInitializer` may not refer to a FieldDecl because it might also denote another constructor call. Fixes #66324 >From b7438d2ea42fbdaabaea186fb0d185712fd48826 Mon Sep 17 00:00:00 2001 From: Corentin Jabot <corentinja...@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 500f9c9a0cda741..b06d777e1490602 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -291,6 +291,10 @@ Bug Fixes to C++ Support pointers on win32. (`#62594 <https://github.com/llvm/llvm-project/issues/62594>`_) +- 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 c5eb7f139327505..2726590873cbb74 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@-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@-1 {{call to immediate function 'GH66324::vector<void>::vector' is not a constant expression}} +// expected-note@-2 {{in call to 'vector()'}} + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits