llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akash Manna (akash-manna-sky)
<details>
<summary>Changes</summary>
Fixes #<!-- -->176161
Since #<!-- -->138518, template instantiation keeps a parenthesized aggregate
initialization as a `CXXParenListInitExpr`, but its elements have already been
reverted to their written form (a braced list stays a braced list), so whoever
consumes the node has to redo the initialization from them. Variable
initializers, new-expressions and functional casts do that.
`BuildMemberInitializer` didn't: it took the whole node as one argument,
guaranteed elision accepted it as-is, and the constructor ended up with a
member initializer holding a typeless braced list. For the reported case that's
the `EmitAggExpr` assertion. The same gap also rejects valid code for an array
member like `arr(1, 2)` ("no viable conversion from 'X[3]' to 'X'") and
silently leaves a union member uninitialized when its argument needs a
conversion. Clang 20 handled all three. The lambda and local class in the
report are incidental; a plain class template reproduces it.
`BuildMemberInitializer` now treats a `CXXParenListInitExpr` the way it treats
a `ParenListExpr` and performs the initialization from the user-specified
elements. Using only those elements (not the trailing
default-member-initializer and value-initialization ones Sema appends) means
they get rebuilt for the instantiated class rather than fed back in as
arguments.
---
Full diff: https://github.com/llvm/llvm-project/pull/224918.diff
4 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+4)
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+4)
- (added) clang/test/CodeGenCXX/GH176161.cpp (+41)
- (modified) clang/test/SemaCXX/paren-list-agg-init.cpp (+41)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index f4a34a37aff52e..5f6e65fdb10a70 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -707,6 +707,10 @@ features cannot lower the translation-unit ABI level;
inside a union caused the union to be treated as a polymorphic class.
(#GH213854)
+- Fixed a crash, a rejected-valid case and a miscompile when instantiating a
+ constructor whose mem-initializer used parenthesized aggregate
initialization,
+ e.g. ``: agg({1, 2})`` or ``: arr(1, 2)``. (#GH176161)
+
- Fixed an assertion when a type-trait keyword that had already been made
available as an identifier (e.g. `struct __make_unsigned`) was seen again
in a token that was lexed and cached before the first occurrence was parsed.
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 02b4c347dee094..ffd6d6cbf8596c 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -4685,6 +4685,10 @@ Sema::BuildMemberInitializer(ValueDecl *Member, Expr
*Init,
Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs());
} else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
Args = MultiExprArg(InitList->getInits(), InitList->getNumInits());
+ } else if (auto *ParenListInit = dyn_cast<CXXParenListInitExpr>(Init)) {
+ // Template instantiation reverts the elements to their syntactic form;
+ // redo the initialization from the written arguments.
+ Args = ParenListInit->getUserSpecifiedInitExprs();
} else {
// Template instantiation doesn't reconstruct ParenListExprs for us.
Args = Init;
diff --git a/clang/test/CodeGenCXX/GH176161.cpp
b/clang/test/CodeGenCXX/GH176161.cpp
new file mode 100644
index 00000000000000..98b1fe4d97340b
--- /dev/null
+++ b/clang/test/CodeGenCXX/GH176161.cpp
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -emit-llvm -o - %s |
FileCheck %s
+
+template <typename T> struct ConfigFlag { T value; };
+
+struct Tuple {
+ Tuple(ConfigFlag<bool> &&a, ConfigFlag<bool> &&b) : first(a), second(b) {}
+ ConfigFlag<bool> first, second;
+};
+
+struct ConfigSystem {
+ Tuple flags;
+};
+
+template <unsigned R> class M {
+public:
+ M(const int *arr) {
+ auto flagProcessing = [] {
+ struct Configurator {
+ ConfigSystem config;
+ Configurator()
+ : config({ConfigFlag<bool>(true), ConfigFlag<bool>(false)}) {}
+ };
+ Configurator configurator;
+ };
+ flagProcessing();
+ for (unsigned long r = 0; r < R; ++r)
+ m[r] = arr[r];
+ }
+ int m[R];
+};
+
+int main() {
+ int arr[2] = {1, 2};
+ M<2> m(arr);
+}
+
+// CHECK-LABEL: define {{.*}}12ConfiguratorC2Ev(
+// CHECK: store i8 1, ptr
+// CHECK: store i8 0, ptr
+// CHECK: call void @_ZN5TupleC{{[12]}}E
+// CHECK: ret void
diff --git a/clang/test/SemaCXX/paren-list-agg-init.cpp
b/clang/test/SemaCXX/paren-list-agg-init.cpp
index 680fdcdbe7b1ce..90414dbd5a78fd 100644
--- a/clang/test/SemaCXX/paren-list-agg-init.cpp
+++ b/clang/test/SemaCXX/paren-list-agg-init.cpp
@@ -425,3 +425,44 @@ void test() {
}
}
+
+namespace GH176161 {
+struct Inner {
+ constexpr Inner(int a, int b) : x(a), y(b) {}
+ int x, y;
+};
+struct Agg {
+ Inner i;
+ int k = 7;
+};
+
+template <class T> struct S {
+ Agg a;
+ constexpr S() : a({1, 2}) {} // beforecxx20-warning 2{{C++20 extension}}
+};
+
+constexpr S<int> s; // beforecxx20-note {{requested here}}
+static_assert(s.a.i.x == 1);
+static_assert(s.a.i.y == 2);
+static_assert(s.a.k == 7);
+
+struct X {
+ constexpr X() : v(5) {}
+ constexpr X(int v) : v(v) {}
+ int v;
+};
+template <class T> struct Arr {
+ X arr[3];
+ constexpr Arr() : arr(1, 2) {} // beforecxx20-warning 2{{C++20 extension}}
+};
+constexpr Arr<int> arr; // beforecxx20-note {{requested here}}
+static_assert(arr.arr[0].v == 1 && arr.arr[1].v == 2 && arr.arr[2].v == 5);
+
+union U { int a; float b; };
+template <class T> struct Un {
+ U u;
+ constexpr Un() : u('a') {} // beforecxx20-warning 2{{C++20 extension}}
+};
+constexpr Un<int> un; // beforecxx20-note {{requested here}}
+static_assert(un.u.a == 'a');
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/224918
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits