Author: cor3ntin
Date: 2025-05-28T07:00:22+02:00
New Revision: 5c063bebe410b7ff803e377ab40d405db2956339

URL: 
https://github.com/llvm/llvm-project/commit/5c063bebe410b7ff803e377ab40d405db2956339
DIFF: 
https://github.com/llvm/llvm-project/commit/5c063bebe410b7ff803e377ab40d405db2956339.diff

LOG: [Clang] Fix a regression introduced by #138518 (#141342)

We did not handle the case where a variable could be initialized by a
CXXParenListInitExpr.

---------

Co-authored-by: Shafik Yaghmour <shafik.yaghm...@intel.com>

Added: 
    

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/SemaCXX/paren-list-agg-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 555faf6aa8e5a..86b871396ec90 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13774,7 +13774,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr 
*Init, bool DirectInit) {
   }
 
   // Perform the initialization.
-  ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init);
+  bool InitializedFromParenListExpr = false;
   bool IsParenListInit = false;
   if (!VDecl->isInvalidDecl()) {
     InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl);
@@ -13782,9 +13782,14 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr 
*Init, bool DirectInit) {
         VDecl->getLocation(), DirectInit, Init);
 
     MultiExprArg Args = Init;
-    if (CXXDirectInit)
-      Args = MultiExprArg(CXXDirectInit->getExprs(),
-                          CXXDirectInit->getNumExprs());
+    if (auto *CXXDirectInit = dyn_cast<ParenListExpr>(Init)) {
+      Args =
+          MultiExprArg(CXXDirectInit->getExprs(), 
CXXDirectInit->getNumExprs());
+      InitializedFromParenListExpr = true;
+    } else if (auto *CXXDirectInit = dyn_cast<CXXParenListInitExpr>(Init)) {
+      Args = CXXDirectInit->getInitExprs();
+      InitializedFromParenListExpr = true;
+    }
 
     // Try to correct any TypoExprs in the initialization arguments.
     for (size_t Idx = 0; Idx < Args.size(); ++Idx) {
@@ -14082,10 +14087,9 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr 
*Init, bool DirectInit) {
   // special case code.
 
   // C++ 8.5p11:
-  // The form of initialization (using parentheses or '=') is generally
-  // insignificant, but does matter when the entity being initialized has a
-  // class type.
-  if (CXXDirectInit) {
+  // The form of initialization (using parentheses or '=') matters
+  // when the entity being initialized has class type.
+  if (InitializedFromParenListExpr) {
     assert(DirectInit && "Call-style initializer must be direct init.");
     VDecl->setInitStyle(IsParenListInit ? VarDecl::ParenListInit
                                         : VarDecl::CallInit);

diff  --git a/clang/test/SemaCXX/paren-list-agg-init.cpp 
b/clang/test/SemaCXX/paren-list-agg-init.cpp
index ba7dffdc1af9f..680fdcdbe7b1c 100644
--- a/clang/test/SemaCXX/paren-list-agg-init.cpp
+++ b/clang/test/SemaCXX/paren-list-agg-init.cpp
@@ -403,3 +403,25 @@ void test() {
     S<int>{}.f(); // beforecxx20-note {{requested here}}
 }
 }
+
+namespace GH72880_regression {
+struct E {
+    int i = 42;
+};
+struct G {
+  E e;
+};
+template <typename>
+struct Test {
+  void f() {
+    constexpr E e;
+    //FIXME: We should only warn one
+    constexpr G g(e); // beforecxx20-warning 2{{C++20 extension}}
+    static_assert(g.e.i == 42);
+  }
+};
+void test() {
+    Test<int>{}.f(); // beforecxx20-note {{requested here}}
+}
+
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to