diff options
author | 2023-04-26 14:50:01 -0700 | |
---|---|---|
committer | 2023-05-01 21:16:19 -0700 | |
commit | 33b41a6564f94e51d918cdabf44e92a60e596325 (patch) | |
tree | eed7fbce36d2369a8008ed36fbb9eb874582a7fc | |
parent | [LLD][COFF] Add /inferasanlibs to lld-link as ignored flag (diff) | |
download | llvm-project-33b41a6564f94e51d918cdabf44e92a60e596325.tar.gz llvm-project-33b41a6564f94e51d918cdabf44e92a60e596325.tar.bz2 llvm-project-33b41a6564f94e51d918cdabf44e92a60e596325.zip |
[clang] Fix a crash with parenthesized aggregate initialization and base classes
When calling InitializeBase(...), TryOrBuidlParenListInit(...) needs to
pass in the parent entity; otherwise, we erroneously try to cast
CurContext to a CXXConstructorDecl[0], which can't be done since we're
performing aggregate initialization, not constructor initialization.
Field initialization is not affected, but this patch still adds some
tests for it.
Fixes 62296
[0]: https://github.com/llvm/llvm-project/blob/33d6bd1c667456f7f4a9d338a7996a30a3af50a3/clang/lib/Sema/SemaAccess.cpp#L1696
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D149301
-rw-r--r-- | clang/docs/ReleaseNotes.rst | 4 | ||||
-rw-r--r-- | clang/lib/Sema/SemaInit.cpp | 5 | ||||
-rw-r--r-- | clang/test/SemaCXX/paren-list-agg-init.cpp | 23 |
3 files changed, 30 insertions, 2 deletions
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 215c6857dd2e..c255f037c9ec 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -715,6 +715,10 @@ Improvements to Clang's diagnostics Bug Fixes in This Version ------------------------- +- Fix crash when attempting to perform parenthesized initialization of an + aggregate with a base class with only non-public constructors. + (`#62296 <https://github.com/llvm/llvm-project/issues/62296>`_) + Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index ddb2b5cf5cd1..d27641f4c0c0 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -5420,8 +5420,9 @@ static void TryOrBuildParenListInitialization( } else if (auto *RT = Entity.getType()->getAs<RecordType>()) { const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); - auto BaseRange = map_range(RD->bases(), [&S](auto &base) { - return InitializedEntity::InitializeBase(S.getASTContext(), &base, false); + auto BaseRange = map_range(RD->bases(), [&](auto &base) { + return InitializedEntity::InitializeBase(S.getASTContext(), &base, false, + &Entity); }); auto FieldRange = map_range(RD->fields(), [](auto *field) { return InitializedEntity::InitializeMember(field); diff --git a/clang/test/SemaCXX/paren-list-agg-init.cpp b/clang/test/SemaCXX/paren-list-agg-init.cpp index ec577e74509b..0a150d64a749 100644 --- a/clang/test/SemaCXX/paren-list-agg-init.cpp +++ b/clang/test/SemaCXX/paren-list-agg-init.cpp @@ -199,3 +199,26 @@ void bar() { // expected-error@-1 {{call to implicitly-deleted copy constructor of 'V'}} } } + +namespace gh62296 { +struct L { +protected: + L(int); + // expected-note@-1 2{{declared protected here}} +}; + +struct M : L {}; + +struct N { + L l; +}; + +M m(42); +// expected-error@-1 {{base class 'L' has protected constructor}} +// beforecxx20-warning@-2 {{aggregate initialization of type 'M' from a parenthesized list of values is a C++20 extension}} + +N n(43); +// expected-error@-1 {{field of type 'L' has protected constructor}} +// beforecxx20-warning@-2 {{aggregate initialization of type 'N' from a parenthesized list of values is a C++20 extension}} + +} |