[clang] 7417e9d - [clang] Fix a crash with parenthesized aggregate initialization and base classes

Alan Zhao via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 27 10:43:23 PDT 2023


Author: Alan Zhao
Date: 2023-04-27T10:43:16-07:00
New Revision: 7417e9d75c9af7dd0d3dad12eebdee84b10b56d7

URL: https://github.com/llvm/llvm-project/commit/7417e9d75c9af7dd0d3dad12eebdee84b10b56d7
DIFF: https://github.com/llvm/llvm-project/commit/7417e9d75c9af7dd0d3dad12eebdee84b10b56d7.diff

LOG: [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

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaInit.cpp
    clang/test/SemaCXX/paren-list-agg-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index aff9ce378253..85eb445a8739 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -331,6 +331,9 @@ Bug Fixes in This Version
   constructor declaration.
   (`#62361 <https://github.com/llvm/llvm-project/issues/62361>`_)
   (`#62362 <https://github.com/llvm/llvm-project/issues/62362>`_)
+- 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 243c3c1c9a4d..c218470d37ae 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -5449,8 +5449,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 a5f39ff6c477..c9d73327025c 100644
--- a/clang/test/SemaCXX/paren-list-agg-init.cpp
+++ b/clang/test/SemaCXX/paren-list-agg-init.cpp
@@ -200,3 +200,26 @@ void bar() {
   // expected-error at -1 {{call to implicitly-deleted copy constructor of 'V'}}
 }
 }
+
+namespace gh62296 {
+struct L {
+protected:
+  L(int);
+  // expected-note at -1 2{{declared protected here}}
+};
+
+struct M : L {};
+
+struct N {
+  L l;
+};
+
+M m(42);
+// expected-error at -1 {{base class 'L' has protected constructor}}
+// beforecxx20-warning at -2 {{aggregate initialization of type 'M' from a parenthesized list of values is a C++20 extension}}
+
+N n(43);
+// expected-error at -1 {{field of type 'L' has protected constructor}}
+// beforecxx20-warning at -2 {{aggregate initialization of type 'N' from a parenthesized list of values is a C++20 extension}}
+
+}


        


More information about the cfe-commits mailing list