Skip to content

Commit 33b41a6

Browse files
alanzhao1tstellar
authored andcommitted
[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
1 parent 7a26555 commit 33b41a6

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,10 @@ Improvements to Clang's diagnostics
715715
Bug Fixes in This Version
716716
-------------------------
717717

718+
- Fix crash when attempting to perform parenthesized initialization of an
719+
aggregate with a base class with only non-public constructors.
720+
(`#62296 <https://github.com/llvm/llvm-project/issues/62296>`_)
721+
718722
Bug Fixes to Compiler Builtins
719723
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
720724

clang/lib/Sema/SemaInit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5420,8 +5420,9 @@ static void TryOrBuildParenListInitialization(
54205420
} else if (auto *RT = Entity.getType()->getAs<RecordType>()) {
54215421
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
54225422

5423-
auto BaseRange = map_range(RD->bases(), [&S](auto &base) {
5424-
return InitializedEntity::InitializeBase(S.getASTContext(), &base, false);
5423+
auto BaseRange = map_range(RD->bases(), [&](auto &base) {
5424+
return InitializedEntity::InitializeBase(S.getASTContext(), &base, false,
5425+
&Entity);
54255426
});
54265427
auto FieldRange = map_range(RD->fields(), [](auto *field) {
54275428
return InitializedEntity::InitializeMember(field);

clang/test/SemaCXX/paren-list-agg-init.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,26 @@ void bar() {
199199
// expected-error@-1 {{call to implicitly-deleted copy constructor of 'V'}}
200200
}
201201
}
202+
203+
namespace gh62296 {
204+
struct L {
205+
protected:
206+
L(int);
207+
// expected-note@-1 2{{declared protected here}}
208+
};
209+
210+
struct M : L {};
211+
212+
struct N {
213+
L l;
214+
};
215+
216+
M m(42);
217+
// expected-error@-1 {{base class 'L' has protected constructor}}
218+
// beforecxx20-warning@-2 {{aggregate initialization of type 'M' from a parenthesized list of values is a C++20 extension}}
219+
220+
N n(43);
221+
// expected-error@-1 {{field of type 'L' has protected constructor}}
222+
// beforecxx20-warning@-2 {{aggregate initialization of type 'N' from a parenthesized list of values is a C++20 extension}}
223+
224+
}

0 commit comments

Comments
 (0)