[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / cxx11-default-member-initializers.cpp
blob5e26c3a3b82cd55d85ac4c33470f408480b7ad16
1 // RUN: %clang_cc1 -std=c++11 -verify %s -pedantic
2 // RUN: %clang_cc1 -std=c++11 -verify %s -pedantic -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -std=c++20 -verify %s -pedantic
4 // RUN: %clang_cc1 -std=c++20 -verify %s -pedantic -fexperimental-new-constant-interpreter
7 namespace PR31692 {
8 struct A {
9 struct X { int n = 0; } x;
10 // Trigger construction of X() from a SFINAE context. This must not mark
11 // any part of X as invalid.
12 static_assert(!__is_constructible(X), "");
13 // Check that X::n is not marked invalid.
14 double &r = x.n; // expected-error {{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
16 // A::X can now be default-constructed.
17 static_assert(__is_constructible(A::X), "");
21 struct S {
22 } constexpr s;
23 struct C {
24 C(S);
26 class MemInit {
27 C m = s;
30 namespace std {
31 typedef decltype(sizeof(int)) size_t;
33 // libc++'s implementation
34 template <class _E> class initializer_list {
35 const _E *__begin_;
36 size_t __size_;
38 initializer_list(const _E *__b, size_t __s) : __begin_(__b), __size_(__s) {}
40 public:
41 typedef _E value_type;
42 typedef const _E &reference;
43 typedef const _E &const_reference;
44 typedef size_t size_type;
46 typedef const _E *iterator;
47 typedef const _E *const_iterator;
49 initializer_list() : __begin_(nullptr), __size_(0) {}
51 size_t size() const { return __size_; }
52 const _E *begin() const { return __begin_; }
53 const _E *end() const { return __begin_ + __size_; }
55 } // namespace std
57 #if __cplusplus >= 201703L
59 // Test CXXDefaultInitExpr rebuild issue in
60 // https://github.com/llvm/llvm-project/pull/87933
61 namespace test_rebuild {
62 template <typename T, int> class C {
63 public:
64 C(std::initializer_list<T>);
67 template <typename T> using Ptr = __remove_pointer(T) *;
68 template <typename T> C(T) -> C<Ptr<T>, sizeof(T)>;
70 class A {
71 public:
72 template <typename T1, typename T2> T1 *some_func(T2 &&);
75 struct B : A {
76 int *ar = some_func<int>(C{some_func<int>(0)});
77 B() {}
80 int TestBody_got;
81 template <int> class Vector {
82 public:
83 Vector(std::initializer_list<int>);
85 template <typename... Ts> Vector(Ts...) -> Vector<sizeof...(Ts)>;
86 class ProgramBuilder {
87 public:
88 template <typename T, typename ARGS> int *create(ARGS);
91 struct TypeTest : ProgramBuilder {
92 int *str_f16 = create<int>(Vector{0});
93 TypeTest() {}
95 class TypeTest_Element_Test : TypeTest {
96 void TestBody();
98 void TypeTest_Element_Test::TestBody() {
99 int *expect = str_f16;
100 &TestBody_got != expect; // expected-warning {{inequality comparison result unused}}
102 } // namespace test_rebuild
104 // Test CXXDefaultInitExpr rebuild issue in
105 // https://github.com/llvm/llvm-project/pull/92527
106 namespace test_rebuild2 {
107 struct F {
108 int g;
110 struct H {};
111 struct I {
112 I(const F &);
113 I(H);
115 struct L {
116 I i = I({.g = 0});
118 struct N : L {};
120 void f() {
121 delete new L; // Ok
122 delete new N; // Ok
124 } // namespace test_rebuild2
125 #endif // __cplusplus >= 201703L
127 #if __cplusplus >= 202002L
128 // This test ensures cleanup expressions are correctly produced
129 // in the presence of default member initializers.
130 namespace PR136554 {
131 struct string {
132 constexpr string(const char*) {};
133 constexpr ~string();
135 struct S;
136 struct optional {
137 template <typename U = S>
138 constexpr optional(U &&) {}
140 struct S {
141 string a;
142 optional b;
143 int defaulted = 0;
144 } test {
145 "", {
146 { "", 0 }
150 // Ensure that the this pointer is
151 // transformed without crashing
152 consteval int immediate() { return 0;}
153 struct StructWithThisInInitializer {
154 int member() const {
155 return 0;
157 int m = member() + immediate();
158 int m2 = this->member() + immediate();
161 template <typename T>
162 struct StructWithThisInInitializerTPL {
163 template <typename U>
164 int member() const {
165 return 0;
167 int m = member<int>() + immediate();
168 int m2 = this->member<int>() + immediate();
171 void test_this() {
172 (void)StructWithThisInInitializer{};
173 (void)StructWithThisInInitializerTPL<int>{};
176 struct ReferenceToNestedMembers {
177 int m;
178 int a = ((void)immediate(), m); // ensure g is found in the correct scope
179 int b = ((void)immediate(), this->m); // ensure g is found in the correct scope
181 struct ReferenceToNestedMembersTest {
182 void* m = nullptr;
183 ReferenceToNestedMembers j{0};
184 } test_reference_to_nested_members;
189 namespace odr_in_unevaluated_context {
190 template <typename e, bool = __is_constructible(e)> struct f {
191 using type = bool;
194 template <class k, f<k>::type = false> int l;
195 int m;
196 struct p {
197 // This used to crash because m is first marked odr used
198 // during parsing, but subsequently used in an unevaluated context
199 // without being transformed.
200 int o = m;
201 p() {}
204 int i = l<p>;
207 #endif