Debian Codegraph @

clang/test/SemaCXX/uninitialized.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++1z -verify %s -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -fsyntax-only -Wall -Wc++20-compat -Wuninitialized -Wno-unused-value -Wno-unused-lambda-capture -Wno-uninitialized-const-reference -std=c++20 -verify %s
4
5 #if defined(BE_THE_HEADER)
6
7 // Wuninitialized-explicit-init should warn in system headers (std::construct_at...) too.
8
9 #pragma clang system_header
10 namespace std {
11 template <class T, class... U>
12 constexpr T* construct_at(T* ptr, U&&... args) {
13 return ::new (static_cast<void*>(ptr)) T(static_cast<U&&>(args)...); // #FIELD_EMBED2_CONSTRUCT_AT
14 }
15 }
16
17 #else
18
19 #define BE_THE_HEADER
20 #include __FILE__
21
22 void* operator new(__SIZE_TYPE__, void*);
23
24 // definitions for std::move
25 namespace std {
26 inline namespace foo {
27 template <class T> struct remove_reference { typedef T type; };
28 template <class T> struct remove_reference<T&> { typedef T type; };
29 template <class T> struct remove_reference<T&&> { typedef T type; };
30
31 template <class T> typename remove_reference<T>::type&& move(T&& t);
32 }
33 }
34
35 int foo(int x);
36 int bar(int* x);
37 int boo(int& x);
38 int far(const int& x);
39 int moved(int&& x);
40 int &ref(int x);
41
42 // Test self-references within initializers which are guaranteed to be
43 // uninitialized.
44 int a = a; // no-warning: used to signal intended lack of initialization.
45 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
46 int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}}
47 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
48 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
49
50 // These don't warn as they don't require the value.
51 int g = sizeof(g);
52 void* ptr = &ptr;
53 int h = bar(&h);
54 int i = boo(i);
55 int j = far(j);
56 int k = __alignof__(k);
57
58 int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
59 int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
60 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
61 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
62 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
63 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
64 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
65 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
66 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
67 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
68 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
69 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
70 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
71 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
72 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
73 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
74 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
75
76 void test_stuff () {
77 int a = a; // no-warning: used to signal intended lack of initialization.
78 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
79 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
80 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
81 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
82 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
83
84 // These don't warn as they don't require the value.
85 int g = sizeof(g);
86 void* ptr = &ptr;
87 int h = bar(&h);
88 int i = boo(i);
89 int j = far(j);
90 int k = __alignof__(k);
91
92 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}}
93 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}}
94 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
95 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
96 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
97 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
98 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
99 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
100 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
101 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
102 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
103 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
104 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
105 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
106 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
107 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
108 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
109
110 for (;;) {
111 int a = a; // no-warning: used to signal intended lack of initialization.
112 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
113 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
114 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
115 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
116 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
117
118 // These don't warn as they don't require the value.
119 int g = sizeof(g);
120 void* ptr = &ptr;
121 int h = bar(&h);
122 int i = boo(i);
123 int j = far(j);
124 int k = __alignof__(k);
125
126 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}}
127 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}}
128 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
129 int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
130 const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
131 int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
132 int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
133 int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
134 int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
135 int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
136 int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
137 int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
138 int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
139 int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
140 int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
141 int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
142 int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
143
144 }
145 }
146
147 void test_comma() {
148 int a; // expected-note {{initialize the variable 'a' to silence this warning}}
149 int b = (a, a ?: 2); // expected-warning {{variable 'a' is uninitialized when used here}}
150 int c = (a, a, b, c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
151 int d; // expected-note {{initialize the variable 'd' to silence this warning}}
152 int e = (foo(d), e, b); // expected-warning {{variable 'd' is uninitialized when used here}}
153 int f; // expected-note {{initialize the variable 'f' to silence this warning}}
154 f = f + 1, 2; // expected-warning {{variable 'f' is uninitialized when used here}}
155 int h;
156 int g = (h, g, 2); // no-warning: h, g are evaluated but not used.
157 }
158
159 namespace member_ptr {
160 struct A {
161 int x;
162 int y;
163 A(int x) : x{x} {}
164 };
165
166 void test_member_ptr() {
167 int A::* px = &A::x;
168 A a{a.*px}; // expected-warning {{variable 'a' is uninitialized when used within its own initialization}}
169 A b = b; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
170 }
171 }
172
173 namespace const_ptr {
174 void foo(int *a);
175 void bar(const int *a);
176 void foobar(const int **a);
177
178 void test_const_ptr() {
179 int a;
180 int b; // expected-note {{initialize the variable 'b' to silence this warning}}
181 foo(&a);
182 bar(&b);
183 b = a + b; // expected-warning {{variable 'b' is uninitialized when used here}}
184 int *ptr; //expected-note {{initialize the variable 'ptr' to silence this warning}}
185 const int *ptr2;
186 foo(ptr); // expected-warning {{variable 'ptr' is uninitialized when used here}}
187 foobar(&ptr2);
188 int *ptr3; // expected-note {{initialize the variable 'ptr3' to silence this warning}}
189 const int *ptr4; // expected-note {{initialize the variable 'ptr4' to silence this warning}}
190 bar(ptr3); // expected-warning {{variable 'ptr3' is uninitialized when used here}}
191 bar(ptr4); // expected-warning {{variable 'ptr4' is uninitialized when used here}}
192 }
193 }
194
195 // Also test similar constructs in a field's initializer.
196 struct S {
197 int x;
198 int y;
199 const int z = 5;
200 void *ptr;
201
202 S(bool (*)[1]) : x(x) {} // expected-warning {{field 'x' is uninitialized when used here}}
203 S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field 'x' is uninitialized when used here}}
204 S(bool (*)[3]) : x(x + x) {} // expected-warning 2{{field 'x' is uninitialized when used here}}
205 S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field 'x' is uninitialized when used here}}
206 S(bool (*)[5]) : x(foo(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
207
208 // These don't actually require the value of x and so shouldn't warn.
209 S(char (*)[1]) : x(sizeof(x)) {}
210 S(char (*)[2]) : ptr(&ptr) {}
211 S(char (*)[3]) : x(bar(&x)) {}
212 S(char (*)[4]) : x(boo(x)) {}
213 S(char (*)[5]) : x(far(x)) {}
214 S(char (*)[6]) : x(__alignof__(x)) {}
215
216 S(int (*)[1]) : x(0), y(x ? y : y) {} // expected-warning 2{{field 'y' is uninitialized when used here}}
217 S(int (*)[2]) : x(0), y(1 + (x ? y : y)) {} // expected-warning 2{{field 'y' is uninitialized when used here}}
218 S(int (*)[3]) : x(-x) {} // expected-warning {{field 'x' is uninitialized when used here}}
219 S(int (*)[4]) : x(std::move(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
220 S(int (*)[5]) : z(std::move(z)) {} // expected-warning {{field 'z' is uninitialized when used here}}
221 S(int (*)[6]) : x(moved(std::move(x))) {} // expected-warning {{field 'x' is uninitialized when used here}}
222 S(int (*)[7]) : x(0), y(std::move((x ? x : (18, y)))) {} // expected-warning {{field 'y' is uninitialized when used here}}
223 S(int (*)[8]) : x(0), y(x ?: y) {} // expected-warning {{field 'y' is uninitialized when used here}}
224 S(int (*)[9]) : x(0), y(y ?: x) {} // expected-warning {{field 'y' is uninitialized when used here}}
225 S(int (*)[10]) : x(0), y((foo(y), x)) {} // expected-warning {{field 'y' is uninitialized when used here}}
226 S(int (*)[11]) : x(0), y(x += y) {} // expected-warning {{field 'y' is uninitialized when used here}}
227 S(int (*)[12]) : x(x += 10) {} // expected-warning {{field 'x' is uninitialized when used here}}
228 S(int (*)[13]) : x(x++) {} // expected-warning {{field 'x' is uninitialized when used here}}
229 S(int (*)[14]) : x(0), y(((x ? (y, x) : (77, y))++, sizeof(y))) {} // expected-warning {{field 'y' is uninitialized when used here}}
230 S(int (*)[15]) : x(++ref(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
231 S(int (*)[16]) : x((ref(x) += 10)) {} // expected-warning {{field 'x' is uninitialized when used here}}
232 S(int (*)[17]) : x(0), y(y ? x : x) {} // expected-warning {{field 'y' is uninitialized when used here}}
233 };
234
235 // Test self-references with record types.
236 class A {
237 // Non-POD class.
238 public:
239 enum count { ONE, TWO, THREE };
240 int num;
241 static int count;
242 int get() const { return num; }
243 int get2() { return num; }
244 int set(int x) { num = x; return num; }
245 static int zero() { return 0; }
246
247 A() {}
248 A(A const &a) {}
249 A(int x) {}
250 A(int *x) {}
251 A(A *a) {}
252 A(A &&a) {}
253 ~A();
254 bool operator!();
255 bool operator!=(const A&);
256 };
257
258 bool operator!=(int, const A&);
259
260 A getA() { return A(); }
261 A getA(int x) { return A(); }
262 A getA(A* a) { return A(); }
263 A getA(A a) { return A(); }
264 A moveA(A&& a) { return A(); }
265 A const_refA(const A& a) { return A(); }
266
267 void setupA(bool x) {
268 A a1;
269 a1.set(a1.get());
270 A a2(a1.get());
271 A a3(a1);
272 A a4(&a4);
273 A a5(a5.zero());
274 A a6(a6.ONE);
275 A a7 = getA();
276 A a8 = getA(a8.TWO);
277 A a9 = getA(&a9);
278 A a10(a10.count);
279
280 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
281 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
282 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
283 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
284 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
285 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
286 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
287 A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
288 A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
289 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
290 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
291
292 // FIXME: Make the local uninitialized warning consistent with the global
293 // uninitialized checking.
294 A *a22 = new A(a22->count); // expected-warning {{variable 'a22' is uninitialized when used within its own initialization}}
295 A *a23 = new A(a23->ONE); // expected-warning {{variable 'a23' is uninitialized when used within its own initialization}}
296 A *a24 = new A(a24->TWO); // expected-warning {{variable 'a24' is uninitialized when used within its own initialization}}
297 A *a25 = new A(a25->zero()); // expected-warning {{variable 'a25' is uninitialized when used within its own initialization}}
298
299 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
300 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
301 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
302
303 const A a29(a29); // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}}
304 const A a30 = a30; // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}}
305
306 A a31 = std::move(a31); // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}}
307 A a32 = moveA(std::move(a32)); // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}}
308 A a33 = A(std::move(a33)); // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}}
309 A a34(std::move(a34)); // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}}
310 A a35 = std::move(x ? a34 : (37, a35)); // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}}
311
312 A a36 = const_refA(a36);
313 A a37(const_refA(a37));
314
315 A a38({a38}); // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}}
316 A a39 = {a39}; // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}}
317 A a40 = A({a40}); // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}}
318
319 A a41 = !a41; // expected-warning {{variable 'a41' is uninitialized when used within its own initialization}}
320 A a42 = !(a42); // expected-warning {{variable 'a42' is uninitialized when used within its own initialization}}
321 A a43 = a43 != a42; // expected-warning {{variable 'a43' is uninitialized when used within its own initialization}}
322 A a44 = a43 != a44; // expected-warning {{variable 'a44' is uninitialized when used within its own initialization}}
323 A a45 = a45 != a45; // expected-warning 2{{variable 'a45' is uninitialized when used within its own initialization}}
324 A a46 = 0 != a46; // expected-warning {{variable 'a46' is uninitialized when used within its own initialization}}
325
326 A a47(a47.set(a47.num)); // expected-warning 2{{variable 'a47' is uninitialized when used within its own initialization}}
327 A a48(a47.set(a48.num)); // expected-warning {{variable 'a48' is uninitialized when used within its own initialization}}
328 A a49(a47.set(a48.num));
329 }
330
331 bool cond;
332
333 A a1;
334 A a2(a1.get());
335 A a3(a1);
336 A a4(&a4);
337 A a5(a5.zero());
338 A a6(a6.ONE);
339 A a7 = getA();
340 A a8 = getA(a8.TWO);
341 A a9 = getA(&a9);
342 A a10(a10.count);
343
344 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
345 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
346 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
347 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
348 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
349 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
350 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
351 A a18 = cond ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
352 A a19 = getA(cond ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
353 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
354 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
355
356 A *a22 = new A(a22->count);
357 A *a23 = new A(a23->ONE);
358 A *a24 = new A(a24->TWO);
359 A *a25 = new A(a25->zero());
360
361 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
362 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
363 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
364
365 const A a29(a29); // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}}
366 const A a30 = a30; // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}}
367
368 A a31 = std::move(a31); // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}}
369 A a32 = moveA(std::move(a32)); // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}}
370 A a33 = A(std::move(a33)); // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}}
371 A a34(std::move(a34)); // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}}
372 A a35 = std::move(x ? a34 : (37, a35)); // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}}
373
374 A a36 = const_refA(a36);
375 A a37(const_refA(a37));
376
377 A a38({a38}); // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}}
378 A a39 = {a39}; // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}}
379 A a40 = A({a40}); // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}}
380
381 A a41 = !a41; // expected-warning {{variable 'a41' is uninitialized when used within its own initialization}}
382 A a42 = !(a42); // expected-warning {{variable 'a42' is uninitialized when used within its own initialization}}
383 A a43 = a43 != a42; // expected-warning {{variable 'a43' is uninitialized when used within its own initialization}}
384 A a44 = a43 != a44; // expected-warning {{variable 'a44' is uninitialized when used within its own initialization}}
385 A a45 = a45 != a45; // expected-warning 2{{variable 'a45' is uninitialized when used within its own initialization}}
386
387 A a46 = 0 != a46; // expected-warning {{variable 'a46' is uninitialized when used within its own initialization}}
388
389 A a47(a47.set(a47.num)); // expected-warning 2{{variable 'a47' is uninitialized when used within its own initialization}}
390 A a48(a47.set(a48.num)); // expected-warning {{variable 'a48' is uninitialized when used within its own initialization}}
391 A a49(a47.set(a48.num));
392
393 class T {
394 A a, a2;
395 const A c_a;
396 A* ptr_a;
397
398 T() {}
399 T(bool (*)[1]) : a() {}
400 T(bool (*)[2]) : a2(a.get()) {}
401 T(bool (*)[3]) : a2(a) {}
402 T(bool (*)[4]) : a(&a) {}
403 T(bool (*)[5]) : a(a.zero()) {}
404 T(bool (*)[6]) : a(a.ONE) {}
405 T(bool (*)[7]) : a(getA()) {}
406 T(bool (*)[8]) : a2(getA(a.TWO)) {}
407 T(bool (*)[9]) : a(getA(&a)) {}
408 T(bool (*)[10]) : a(a.count) {}
409
410 T(bool (*)[11]) : a(a) {} // expected-warning {{field 'a' is uninitialized when used here}}
411 T(bool (*)[12]) : a(a.get()) {} // expected-warning {{field 'a' is uninitialized when used here}}
412 T(bool (*)[13]) : a(a.num) {} // expected-warning {{field 'a' is uninitialized when used here}}
413 T(bool (*)[14]) : a(A(a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
414 T(bool (*)[15]) : a(getA(a.num)) {} // expected-warning {{field 'a' is uninitialized when used here}}
415 T(bool (*)[16]) : a(&a.num) {} // expected-warning {{field 'a' is uninitialized when used here}}
416 T(bool (*)[17]) : a(a.get2()) {} // expected-warning {{field 'a' is uninitialized when used here}}
417 T(bool (*)[18]) : a2(cond ? a2 : a) {} // expected-warning {{field 'a2' is uninitialized when used here}}
418 T(bool (*)[19]) : a2(cond ? a2 : a) {} // expected-warning {{field 'a2' is uninitialized when used here}}
419 T(bool (*)[20]) : a{a} {} // expected-warning {{field 'a' is uninitialized when used here}}
420 T(bool (*)[21]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}}
421
422 T(bool (*)[22]) : ptr_a(new A(ptr_a->count)) {}
423 T(bool (*)[23]) : ptr_a(new A(ptr_a->ONE)) {}
424 T(bool (*)[24]) : ptr_a(new A(ptr_a->TWO)) {}
425 T(bool (*)[25]) : ptr_a(new A(ptr_a->zero())) {}
426
427 T(bool (*)[26]) : ptr_a(new A(ptr_a->get())) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}}
428 T(bool (*)[27]) : ptr_a(new A(ptr_a->get2())) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}}
429 T(bool (*)[28]) : ptr_a(new A(ptr_a->num)) {} // expected-warning {{field 'ptr_a' is uninitialized when used here}}
430
431 T(bool (*)[29]) : c_a(c_a) {} // expected-warning {{field 'c_a' is uninitialized when used here}}
432 T(bool (*)[30]) : c_a(A(c_a)) {} // expected-warning {{field 'c_a' is uninitialized when used here}}
433
434 T(bool (*)[31]) : a(std::move(a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
435 T(bool (*)[32]) : a(moveA(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}}
436 T(bool (*)[33]) : a(A(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}}
437 T(bool (*)[34]) : a(A(std::move(a))) {} // expected-warning {{field 'a' is uninitialized when used here}}
438 T(bool (*)[35]) : a2(std::move(x ? a : (37, a2))) {} // expected-warning {{field 'a2' is uninitialized when used here}}
439
440 T(bool (*)[36]) : a(const_refA(a)) {}
441 T(bool (*)[37]) : a(A(const_refA(a))) {}
442
443 T(bool (*)[38]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}}
444 T(bool (*)[39]) : a{a} {} // expected-warning {{field 'a' is uninitialized when used here}}
445 T(bool (*)[40]) : a({a}) {} // expected-warning {{field 'a' is uninitialized when used here}}
446
447 T(bool (*)[41]) : a(!a) {} // expected-warning {{field 'a' is uninitialized when used here}}
448 T(bool (*)[42]) : a(!(a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
449 T(bool (*)[43]) : a(), a2(a2 != a) {} // expected-warning {{field 'a2' is uninitialized when used here}}
450 T(bool (*)[44]) : a(), a2(a != a2) {} // expected-warning {{field 'a2' is uninitialized when used here}}
451 T(bool (*)[45]) : a(a != a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
452 T(bool (*)[46]) : a(0 != a) {} // expected-warning {{field 'a' is uninitialized when used here}}
453
454 T(bool (*)[47]) : a2(a2.set(a2.num)) {} // expected-warning 2{{field 'a2' is uninitialized when used here}}
455 T(bool (*)[48]) : a2(a.set(a2.num)) {} // expected-warning {{field 'a2' is uninitialized when used here}}
456 T(bool (*)[49]) : a2(a.set(a.num)) {}
457
458 };
459
460 struct B {
461 // POD struct.
462 int x;
463 int *y;
464 };
465
466 B getB() { return B(); };
467 B getB(int x) { return B(); };
468 B getB(int *x) { return B(); };
469 B getB(B *b) { return B(); };
470 B moveB(B &&b) { return B(); };
471
472 B* getPtrB() { return 0; };
473 B* getPtrB(int x) { return 0; };
474 B* getPtrB(int *x) { return 0; };
475 B* getPtrB(B **b) { return 0; };
476
477 void setupB(bool x) {
478 B b1;
479 B b2(b1);
480 B b3 = { 5, &b3.x };
481 B b4 = getB();
482 B b5 = getB(&b5);
483 B b6 = getB(&b6.x);
484
485 // Silence unused warning
486 (void) b2;
487 (void) b4;
488
489 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
490 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
491 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
492 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
493
494 B* b11 = 0;
495 B* b12(b11);
496 B* b13 = getPtrB();
497 B* b14 = getPtrB(&b14);
498
499 (void) b12;
500 (void) b13;
501
502 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
503 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
504
505 B b17 = { b17.x = 5, b17.y = 0 };
506 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
507
508 const B b19 = b19; // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}}
509 const B b20(b20); // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}}
510
511 B b21 = std::move(b21); // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}}
512 B b22 = moveB(std::move(b22)); // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}}
513 B b23 = B(std::move(b23)); // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}}
514 B b24 = std::move(x ? b23 : (18, b24)); // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}}
515 }
516
517 B b1;
518 B b2(b1);
519 B b3 = { 5, &b3.x };
520 B b4 = getB();
521 B b5 = getB(&b5);
522 B b6 = getB(&b6.x);
523
524 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
525 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
526 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
527 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
528
529 B* b11 = 0;
530 B* b12(b11);
531 B* b13 = getPtrB();
532 B* b14 = getPtrB(&b14);
533
534 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
535 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
536
537 B b17 = { b17.x = 5, b17.y = 0 };
538 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
539
540 const B b19 = b19; // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}}
541 const B b20(b20); // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}}
542
543 B b21 = std::move(b21); // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}}
544 B b22 = moveB(std::move(b22)); // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}}
545 B b23 = B(std::move(b23)); // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}}
546 B b24 = std::move(x ? b23 : (18, b24)); // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}}
547
548 class U {
549 B b1, b2;
550 B *ptr1, *ptr2;
551 const B constb = {};
552
553 U() {}
554 U(bool (*)[1]) : b1() {}
555 U(bool (*)[2]) : b2(b1) {}
556 U(bool (*)[3]) : b1{ 5, &b1.x } {}
557 U(bool (*)[4]) : b1(getB()) {}
558 U(bool (*)[5]) : b1(getB(&b1)) {}
559 U(bool (*)[6]) : b1(getB(&b1.x)) {}
560
561 U(bool (*)[7]) : b1(b1) {} // expected-warning {{field 'b1' is uninitialized when used here}}
562 U(bool (*)[8]) : b1(getB(b1.x)) {} // expected-warning {{field 'b1' is uninitialized when used here}}
563 U(bool (*)[9]) : b1(getB(b1.y)) {} // expected-warning {{field 'b1' is uninitialized when used here}}
564 U(bool (*)[10]) : b1(getB(-b1.x)) {} // expected-warning {{field 'b1' is uninitialized when used here}}
565
566 U(bool (*)[11]) : ptr1(0) {}
567 U(bool (*)[12]) : ptr1(0), ptr2(ptr1) {}
568 U(bool (*)[13]) : ptr1(getPtrB()) {}
569 U(bool (*)[14]) : ptr1(getPtrB(&ptr1)) {}
570
571 U(bool (*)[15]) : ptr1(getPtrB(ptr1->x)) {} // expected-warning {{field 'ptr1' is uninitialized when used here}}
572 U(bool (*)[16]) : ptr2(getPtrB(ptr2->y)) {} // expected-warning {{field 'ptr2' is uninitialized when used here}}
573
574 U(bool (*)[17]) : b1 { b1.x = 5, b1.y = 0 } {}
575 U(bool (*)[18]) : b1 { b1.x + 1, b1.y } {} // expected-warning 2{{field 'b1' is uninitialized when used here}}
576
577 U(bool (*)[19]) : constb(constb) {} // expected-warning {{field 'constb' is uninitialized when used here}}
578 U(bool (*)[20]) : constb(B(constb)) {} // expected-warning {{field 'constb' is uninitialized when used here}}
579
580 U(bool (*)[21]) : b1(std::move(b1)) {} // expected-warning {{field 'b1' is uninitialized when used here}}
581 U(bool (*)[22]) : b1(moveB(std::move(b1))) {} // expected-warning {{field 'b1' is uninitialized when used here}}
582 U(bool (*)[23]) : b1(B(std::move(b1))) {} // expected-warning {{field 'b1' is uninitialized when used here}}
583 U(bool (*)[24]) : b2(std::move(x ? b1 : (18, b2))) {} // expected-warning {{field 'b2' is uninitialized when used here}}
584 };
585
586 struct C { char a[100], *e; } car = { .e = car.a };
587
588 namespace rdar10398199 {
589 class FooBase { protected: ~FooBase() {} };
590 class Foo : public FooBase {
591 public:
592 operator int&() const;
593 };
594 void stuff();
595 template <typename T> class FooImpl : public Foo {
596 T val;
597 public:
598 FooImpl(const T &x) : val(x) {}
599 ~FooImpl() { stuff(); }
600 };
601
602 template <typename T> FooImpl<T> makeFoo(const T& x) {
603 return FooImpl<T>(x);
604 }
605
606 void test() {
607 const Foo &x = makeFoo(42);
608 const int&y = makeFoo(42u);
609 (void)x;
610 (void)y;
611 };
612 }
613
614 // PR 12325 - this was a false uninitialized value warning due to
615 // a broken CFG.
616 int pr12325(int params) {
617 int x = ({
618 while (false)
619 ;
620 int _v = params;
621 if (false)
622 ;
623 _v; // no-warning
624 });
625 return x;
626 }
627
628 // Test lambda expressions with -Wuninitialized
629 int test_lambda() {
630 auto f1 = [] (int x, int y) { int z; return x + y + z; }; // expected-warning{{variable 'z' is uninitialized when used here}} expected-note {{initialize the variable 'z' to silence this warning}}
631 return f1(1, 2);
632 }
633
634 namespace {
635 struct A {
636 enum { A1 };
637 static int A2() {return 5;}
638 int A3;
639 int A4() { return 5;}
640 };
641
642 struct B {
643 A a;
644 };
645
646 struct C {
647 C() {}
648 C(int x) {}
649 static A a;
650 B b;
651 };
652 A C::a = A();
653
654 // Accessing non-static members will give a warning.
655 struct D {
656 C c;
657 D(char (*)[1]) : c(c.b.a.A1) {}
658 D(char (*)[2]) : c(c.b.a.A2()) {}
659 D(char (*)[3]) : c(c.b.a.A3) {} // expected-warning {{field 'c' is uninitialized when used here}}
660 D(char (*)[4]) : c(c.b.a.A4()) {} // expected-warning {{field 'c' is uninitialized when used here}}
661
662 // c::a is static, so it is already initialized
663 D(char (*)[5]) : c(c.a.A1) {}
664 D(char (*)[6]) : c(c.a.A2()) {}
665 D(char (*)[7]) : c(c.a.A3) {}
666 D(char (*)[8]) : c(c.a.A4()) {}
667 };
668
669 struct E {
670 int b = 1;
671 int c = 1;
672 int a; // This field needs to be last to prevent the cross field
673 // uninitialized warning.
674 E(char (*)[1]) : a(a ? b : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
675 E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
676 E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
677 E(char (*)[4]) : a(b ? c : (a+c)) {} // expected-warning {{field 'a' is uninitialized when used here}}
678 E(char (*)[5]) : a(b ? c : b) {}
679
680 E(char (*)[6]) : a(a ?: a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
681 E(char (*)[7]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}}
682 E(char (*)[8]) : a(a ?: c) {} // expected-warning {{field 'a' is uninitialized when used here}}
683 E(char (*)[9]) : a(b ?: c) {}
684
685 E(char (*)[10]) : a((a, a, b)) {}
686 E(char (*)[11]) : a((c + a, a + 1, b)) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
687 E(char (*)[12]) : a((b + c, c, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
688 E(char (*)[13]) : a((a, a, a, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
689 E(char (*)[14]) : a((b, c, c)) {}
690 E(char (*)[15]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}}
691 E(char (*)[16]) : a(a ?: b) {} // expected-warning {{field 'a' is uninitialized when used here}}
692 };
693
694 struct F {
695 int a;
696 F* f;
697 F(int) {}
698 F() {}
699 };
700
701 int F::*ptr = &F::a;
702 F* F::*f_ptr = &F::f;
703 struct G {
704 F f1, f2;
705 F *f3, *f4;
706 G(char (*)[1]) : f1(f1) {} // expected-warning {{field 'f1' is uninitialized when used here}}
707 G(char (*)[2]) : f2(f1) {}
708 G(char (*)[3]) : f2(F()) {}
709
710 G(char (*)[4]) : f1(f1.*ptr) {} // expected-warning {{field 'f1' is uninitialized when used here}}
711 G(char (*)[5]) : f2(f1.*ptr) {}
712
713 G(char (*)[6]) : f3(f3) {} // expected-warning {{field 'f3' is uninitialized when used here}}
714 G(char (*)[7]) : f3(f3->*f_ptr) {} // expected-warning {{field 'f3' is uninitialized when used here}}
715 G(char (*)[8]) : f3(new F(f3->*ptr)) {} // expected-warning {{field 'f3' is uninitialized when used here}}
716 };
717
718 struct H {
719 H() : a(a) {} // expected-warning {{field 'a' is uninitialized when used here}}
720 const A a;
721 };
722 }
723
724 namespace statics {
725 static int a = a; // no-warning: used to signal intended lack of initialization.
726 static int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
727 static int c = (c + c); // expected-warning 2{{variable 'c' is uninitialized when used within its own initialization}}
728 static int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
729 static int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
730
731 // These don't warn as they don't require the value.
732 static int g = sizeof(g);
733 int gg = g; // Silence unneeded warning
734 static void* ptr = &ptr;
735 static int h = bar(&h);
736 static int i = boo(i);
737 static int j = far(j);
738 static int k = __alignof__(k);
739
740 static int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
741 static int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
742 static int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
743 static int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
744 static const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
745 static int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
746 static int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
747 static int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
748 static int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
749 static int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
750 static int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
751 static int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
752 static int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
753 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
754 static int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
755 static int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
756 static int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
757
758
759 void test() {
760 static int a = a; // no-warning: used to signal intended lack of initialization.
761 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
762 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
763 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
764 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
765 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
766
767 // These don't warn as they don't require the value.
768 static int g = sizeof(g);
769 static void* ptr = &ptr;
770 static int h = bar(&h);
771 static int i = boo(i);
772 static int j = far(j);
773 static int k = __alignof__(k);
774
775 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
776 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
777 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
778 static int o = std::move(o); // expected-warning {{static variable 'o' is suspiciously used within its own initialization}}
779 static const int p = std::move(p); // expected-warning {{static variable 'p' is suspiciously used within its own initialization}}
780 static int q = moved(std::move(q)); // expected-warning {{static variable 'q' is suspiciously used within its own initialization}}
781 static int r = std::move((p ? q : (18, r))); // expected-warning {{static variable 'r' is suspiciously used within its own initialization}}
782 static int s = r ?: s; // expected-warning {{static variable 's' is suspiciously used within its own initialization}}
783 static int t = t ?: s; // expected-warning {{static variable 't' is suspiciously used within its own initialization}}
784 static int u = (foo(u), s); // expected-warning {{static variable 'u' is suspiciously used within its own initialization}}
785 static int v = (u += v); // expected-warning {{static variable 'v' is suspiciously used within its own initialization}}
786 static int w = (w += 10); // expected-warning {{static variable 'w' is suspiciously used within its own initialization}}
787 static int x = x++; // expected-warning {{static variable 'x' is suspiciously used within its own initialization}}
788 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{static variable 'y' is suspiciously used within its own initialization}}
789 static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}}
790 static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}}
791 static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}}
792
793 for (;;) {
794 static int a = a; // no-warning: used to signal intended lack of initialization.
795 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
796 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
797 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
798 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
799 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
800
801 // These don't warn as they don't require the value.
802 static int g = sizeof(g);
803 static void* ptr = &ptr;
804 static int h = bar(&h);
805 static int i = boo(i);
806 static int j = far(j);
807 static int k = __alignof__(k);
808
809 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
810 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
811 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
812 static int o = std::move(o); // expected-warning {{static variable 'o' is suspiciously used within its own initialization}}
813 static const int p = std::move(p); // expected-warning {{static variable 'p' is suspiciously used within its own initialization}}
814 static int q = moved(std::move(q)); // expected-warning {{static variable 'q' is suspiciously used within its own initialization}}
815 static int r = std::move((p ? q : (18, r))); // expected-warning {{static variable 'r' is suspiciously used within its own initialization}}
816 static int s = r ?: s; // expected-warning {{static variable 's' is suspiciously used within its own initialization}}
817 static int t = t ?: s; // expected-warning {{static variable 't' is suspiciously used within its own initialization}}
818 static int u = (foo(u), s); // expected-warning {{static variable 'u' is suspiciously used within its own initialization}}
819 static int v = (u += v); // expected-warning {{static variable 'v' is suspiciously used within its own initialization}}
820 static int w = (w += 10); // expected-warning {{static variable 'w' is suspiciously used within its own initialization}}
821 static int x = x++; // expected-warning {{static variable 'x' is suspiciously used within its own initialization}}
822 static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{static variable 'y' is suspiciously used within its own initialization}}
823 static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}}
824 static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}}
825 static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}}
826 }
827 }
828 }
829
830 namespace in_class_initializers {
831 struct S {
832 S() : a(a + 1) {} // expected-warning{{field 'a' is uninitialized when used here}}
833 int a = 42; // Note: because a is in a member initializer list, this initialization is ignored.
834 };
835
836 struct T {
837 T() : b(a + 1) {} // No-warning.
838 int a = 42;
839 int b;
840 };
841
842 struct U {
843 U() : a(b + 1), b(a + 1) {} // expected-warning{{field 'b' is uninitialized when used here}}
844 int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored.
845 int b = 1;
846 };
847 }
848
849 namespace references {
850 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
851 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
852 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
853 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
854 int &e = d ?: e; // expected-warning{{reference 'e' is not yet bound to a value when used within its own initialization}}
855 int &f = f ?: d; // expected-warning{{reference 'f' is not yet bound to a value when used within its own initialization}}
856
857 int &return_ref1(int);
858 int &return_ref2(int&);
859
860 int &g = return_ref1(g); // expected-warning{{reference 'g' is not yet bound to a value when used within its own initialization}}
861 int &h = return_ref2(h); // expected-warning{{reference 'h' is not yet bound to a value when used within its own initialization}}
862
863 struct S {
864 S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}}
865 int &a;
866 };
867
868 void test() {
869 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
870 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
871 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
872 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
873 }
874
875 struct T {
876 T() // expected-note{{during field initialization in this constructor}}
877 : a(b), b(a) {} // expected-warning{{reference 'b' is not yet bound to a value when used here}}
878 int &a, &b;
879 int &c = c; // expected-warning{{reference 'c' is not yet bound to a value when used here}}
880 };
881
882 int x;
883 struct U {
884 U() : b(a) {} // No-warning.
885 int &a = x;
886 int &b;
887 };
888 }
889
890 namespace operators {
891 struct A {
892 A(bool);
893 bool operator==(A);
894 };
895
896 A makeA();
897
898 A a1 = a1 = makeA(); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
899 A a2 = a2 == a1; // expected-warning{{variable 'a2' is uninitialized when used within its own initialization}}
900 A a3 = a2 == a3; // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}}
901
902 int x = x = 5;
903 }
904
905 namespace lambdas {
906 struct A {
907 template<typename T> A(T) {}
908 int x;
909 };
910 A a0([] { return a0.x; }); // ok
911 void f() {
912 A a1([=] { // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
913 return a1.x;
914 });
915 A a2([&] { return a2.x; }); // ok
916 A a3([=] { return a3.x; }()); // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}}
917 A a4([&] { return a4.x; }()); // expected-warning{{variable 'a4' is uninitialized when used within its own initialization}}
918 A a5([&] { return a5; }()); // expected-warning{{variable 'a5' is uninitialized when used within its own initialization}}
919 A a6([&] { return a5.x; }()); // ok
920 A a7 = [&a7] { return a7; }(); // expected-warning{{variable 'a7' is uninitialized when used within its own initialization}}
921 }
922 }
923
924 namespace record_fields {
925 bool x;
926 struct A {
927 A() {}
928 A get();
929 static A num();
930 static A copy(A);
931 static A something(A&);
932 };
933
934 A ref(A&);
935 A const_ref(const A&);
936 A pointer(A*);
937 A normal(A);
938 A rref(A&&);
939
940 struct B {
941 A a;
942 B(char (*)[1]) : a(a) {} // expected-warning {{uninitialized}}
943 B(char (*)[2]) : a(a.get()) {} // expected-warning {{uninitialized}}
944 B(char (*)[3]) : a(a.num()) {}
945 B(char (*)[4]) : a(a.copy(a)) {} // expected-warning {{uninitialized}}
946 B(char (*)[5]) : a(a.something(a)) {}
947 B(char (*)[6]) : a(ref(a)) {}
948 B(char (*)[7]) : a(const_ref(a)) {}
949 B(char (*)[8]) : a(pointer(&a)) {}
950 B(char (*)[9]) : a(normal(a)) {} // expected-warning {{uninitialized}}
951 B(char (*)[10]) : a(std::move(a)) {} // expected-warning {{uninitialized}}
952 B(char (*)[11]) : a(A(std::move(a))) {} // expected-warning {{uninitialized}}
953 B(char (*)[12]) : a(rref(std::move(a))) {} // expected-warning {{uninitialized}}
954 B(char (*)[13]) : a(std::move(x ? a : (25, a))) {} // expected-warning 2{{uninitialized}}
955 };
956 struct C {
957 C() {} // expected-note9{{in this constructor}}
958 A a1 = a1; // expected-warning {{uninitialized}}
959 A a2 = a2.get(); // expected-warning {{uninitialized}}
960 A a3 = a3.num();
961 A a4 = a4.copy(a4); // expected-warning {{uninitialized}}
962 A a5 = a5.something(a5);
963 A a6 = ref(a6);
964 A a7 = const_ref(a7);
965 A a8 = pointer(&a8);
966 A a9 = normal(a9); // expected-warning {{uninitialized}}
967 const A a10 = a10; // expected-warning {{uninitialized}}
968 A a11 = std::move(a11); // expected-warning {{uninitialized}}
969 A a12 = A(std::move(a12)); // expected-warning {{uninitialized}}
970 A a13 = rref(std::move(a13)); // expected-warning {{uninitialized}}
971 A a14 = std::move(x ? a13 : (22, a14)); // expected-warning {{uninitialized}}
972 };
973 struct D { // expected-note9{{in the implicit default constructor}}
974 A a1 = a1; // expected-warning {{uninitialized}}
975 A a2 = a2.get(); // expected-warning {{uninitialized}}
976 A a3 = a3.num();
977 A a4 = a4.copy(a4); // expected-warning {{uninitialized}}
978 A a5 = a5.something(a5);
979 A a6 = ref(a6);
980 A a7 = const_ref(a7);
981 A a8 = pointer(&a8);
982 A a9 = normal(a9); // expected-warning {{uninitialized}}
983 const A a10 = a10; // expected-warning {{uninitialized}}
984 A a11 = std::move(a11); // expected-warning {{uninitialized}}
985 A a12 = A(std::move(a12)); // expected-warning {{uninitialized}}
986 A a13 = rref(std::move(a13)); // expected-warning {{uninitialized}}
987 A a14 = std::move(x ? a13 : (22, a14)); // expected-warning {{uninitialized}}
988 };
989 D d; // expected-note {{in implicit default constructor for 'record_fields::D' first required here}}
990 struct E {
991 A a1 = a1;
992 A a2 = a2.get();
993 A a3 = a3.num();
994 A a4 = a4.copy(a4);
995 A a5 = a5.something(a5);
996 A a6 = ref(a6);
997 A a7 = const_ref(a7);
998 A a8 = pointer(&a8);
999 A a9 = normal(a9);
1000 const A a10 = a10;
1001 A a11 = std::move(a11);
1002 A a12 = A(std::move(a12));
1003 A a13 = rref(std::move(a13));
1004 A a14 = std::move(x ? a13 : (22, a14));
1005 };
1006 }
1007
1008 namespace cross_field_warnings {
1009 struct A {
1010 int a, b;
1011 A() {}
1012 A(char (*)[1]) : b(a) {} // expected-warning{{field 'a' is uninitialized when used here}}
1013 A(char (*)[2]) : a(b) {} // expected-warning{{field 'b' is uninitialized when used here}}
1014 };
1015
1016 struct B {
1017 int a = b; // expected-warning{{field 'b' is uninitialized when used here}}
1018 int b;
1019 B() {} // expected-note{{during field initialization in this constructor}}
1020 };
1021
1022 struct C {
1023 int a;
1024 int b = a; // expected-warning{{field 'a' is uninitialized when used here}}
1025 C(char (*)[1]) : a(5) {}
1026 C(char (*)[2]) {} // expected-note{{during field initialization in this constructor}}
1027 };
1028
1029 struct D {
1030 int a;
1031 int &b;
1032 int &c = a;
1033 int d = b;
1034 D() : b(a) {}
1035 };
1036
1037 struct E {
1038 int a;
1039 int get();
1040 static int num();
1041 E() {}
1042 E(int) {}
1043 };
1044
1045 struct F {
1046 int a;
1047 E e;
1048 int b;
1049 F(char (*)[1]) : a(e.get()) {} // expected-warning{{field 'e' is uninitialized when used here}}
1050 F(char (*)[2]) : a(e.num()) {}
1051 F(char (*)[3]) : e(a) {} // expected-warning{{field 'a' is uninitialized when used here}}
1052 F(char (*)[4]) : a(4), e(a) {}
1053 F(char (*)[5]) : e(b) {} // expected-warning{{field 'b' is uninitialized when used here}}
1054 F(char (*)[6]) : e(b), b(4) {} // expected-warning{{field 'b' is uninitialized when used here}}
1055 };
1056
1057 struct G {
1058 G(const A&) {};
1059 };
1060
1061 struct H {
1062 A a1;
1063 G g;
1064 A a2;
1065 H() : g(a1) {}
1066 H(int) : g(a2) {}
1067 };
1068
1069 struct I {
1070 I(int*) {}
1071 };
1072
1073 struct J : public I {
1074 int *a;
1075 int *b;
1076 int c;
1077 J() : I((a = new int(5))), b(a), c(*a) {}
1078 };
1079
1080 struct K {
1081 int a = (b = 5);
1082 int b = b + 5;
1083 };
1084
1085 struct L {
1086 int a = (b = 5);
1087 int b = b + 5; // expected-warning{{field 'b' is uninitialized when used here}}
1088 L() : a(5) {} // expected-note{{during field initialization in this constructor}}
1089 };
1090
1091 struct M { };
1092
1093 struct N : public M {
1094 int a;
1095 int b;
1096 N() : b(a) { } // expected-warning{{field 'a' is uninitialized when used here}}
1097 };
1098
1099 struct O {
1100 int x = 42;
1101 int get() { return x; }
1102 };
1103
1104 struct P {
1105 O o;
1106 int x = o.get();
1107 P() : x(o.get()) { }
1108 };
1109
1110 struct Q {
1111 int a;
1112 int b;
1113 int &c;
1114 Q() :
1115 a(c = 5), // expected-warning{{reference 'c' is not yet bound to a value when used here}}
1116 b(c), // expected-warning{{reference 'c' is not yet bound to a value when used here}}
1117 c(a) {}
1118 };
1119
1120 struct R {
1121 int a;
1122 int b;
1123 int c;
1124 int d = a + b + c;
1125 R() : a(c = 5), b(c), c(a) {}
1126 };
1127
1128 // FIXME: Use the CFG-based analysis to give a sometimes uninitialized
1129 // warning on y.
1130 struct T {
1131 int x;
1132 int y;
1133 T(bool b)
1134 : x(b ? (y = 5) : (1 + y)), // expected-warning{{field 'y' is uninitialized when used here}}
1135 y(y + 1) {}
1136 T(int b)
1137 : x(!b ? (1 + y) : (y = 5)), // expected-warning{{field 'y' is uninitialized when used here}}
1138 y(y + 1) {}
1139 };
1140
1141 }
1142
1143 namespace base_class {
1144 struct A {
1145 A (int) {}
1146 };
1147
1148 struct B : public A {
1149 int x;
1150 B() : A(x) {} // expected-warning{{field 'x' is uninitialized when used here}}
1151 };
1152
1153 struct C : public A {
1154 int x;
1155 int y;
1156 C() : A(y = 4), x(y) {}
1157 };
1158 }
1159
1160 namespace delegating_constructor {
1161 struct A {
1162 A(int);
1163 A(int&, int);
1164
1165 A(char (*)[1]) : A(x) {}
1166 // expected-warning@-1 {{field 'x' is uninitialized when used here}}
1167 A(char (*)[2]) : A(x, x) {}
1168 // expected-warning@-1 {{field 'x' is uninitialized when used here}}
1169
1170 A(char (*)[3]) : A(x, 0) {}
1171
1172 int x;
1173 };
1174 }
1175
1176 namespace init_list {
1177 int num = 5;
1178 struct A { int i1, i2; };
1179 struct B { A a1, a2; };
1180
1181 A a1{1,2};
1182 A a2{a2.i1 + 2}; // expected-warning{{uninitialized}}
1183 A a3 = {a3.i1 + 2}; // expected-warning{{uninitialized}}
1184 A a4 = A{a4.i2 + 2}; // expected-warning{{uninitialized}}
1185
1186 B b1 = { {}, {} };
1187 B b2 = { {}, b2.a1 };
1188 B b3 = { b3.a1 }; // expected-warning{{uninitialized}}
1189 B b4 = { {}, b4.a2} ; // expected-warning{{uninitialized}}
1190 B b5 = { b5.a2 }; // expected-warning{{uninitialized}}
1191
1192 B b6 = { {b6.a1.i1} }; // expected-warning{{uninitialized}}
1193 B b7 = { {0, b7.a1.i1} };
1194 B b8 = { {}, {b8.a1.i1} };
1195 B b9 = { {}, {0, b9.a1.i1} };
1196
1197 B b10 = { {b10.a1.i2} }; // expected-warning{{uninitialized}}
1198 B b11 = { {0, b11.a1.i2} }; // expected-warning{{uninitialized}}
1199 B b12 = { {}, {b12.a1.i2} };
1200 B b13 = { {}, {0, b13.a1.i2} };
1201
1202 B b14 = { {b14.a2.i1} }; // expected-warning{{uninitialized}}
1203 B b15 = { {0, b15.a2.i1} }; // expected-warning{{uninitialized}}
1204 B b16 = { {}, {b16.a2.i1} }; // expected-warning{{uninitialized}}
1205 B b17 = { {}, {0, b17.a2.i1} };
1206
1207 B b18 = { {b18.a2.i2} }; // expected-warning{{uninitialized}}
1208 B b19 = { {0, b19.a2.i2} }; // expected-warning{{uninitialized}}
1209 B b20 = { {}, {b20.a2.i2} }; // expected-warning{{uninitialized}}
1210 B b21 = { {}, {0, b21.a2.i2} }; // expected-warning{{uninitialized}}
1211
1212 B b22 = { {b18.a2.i2 + 5} };
1213
1214 struct C {int a; int& b; int c; };
1215 C c1 = { 0, num, 0 };
1216 C c2 = { 1, num, c2.b };
1217 C c3 = { c3.b, num }; // expected-warning{{uninitialized}}
1218 C c4 = { 0, c4.b, 0 }; // expected-warning{{uninitialized}}
1219 C c5 = { 0, c5.c, 0 };
1220 C c6 = { c6.b, num, 0 }; // expected-warning{{uninitialized}}
1221 C c7 = { 0, c7.a, 0 };
1222
1223 struct D {int &a; int &b; };
1224 D d1 = { num, num };
1225 D d2 = { num, d2.a };
1226 D d3 = { d3.b, num }; // expected-warning{{uninitialized}}
1227
1228 // Same as above in member initializer form.
1229 struct Awrapper {
1230 A a1{1,2};
1231 A a2{a2.i1 + 2}; // expected-warning{{uninitialized}}
1232 A a3 = {a3.i1 + 2}; // expected-warning{{uninitialized}}
1233 A a4 = A{a4.i2 + 2}; // expected-warning{{uninitialized}}
1234 Awrapper() {} // expected-note 3{{in this constructor}}
1235 Awrapper(int) :
1236 a1{1,2},
1237 a2{a2.i1 + 2}, // expected-warning{{uninitialized}}
1238 a3{a3.i1 + 2}, // expected-warning{{uninitialized}}
1239 a4{a4.i2 + 2} // expected-warning{{uninitialized}}
1240 {}
1241 };
1242
1243 struct Bwrapper {
1244 B b1 = { {}, {} };
1245 B b2 = { {}, b2.a1 };
1246 B b3 = { b3.a1 }; // expected-warning{{uninitialized}}
1247 B b4 = { {}, b4.a2} ; // expected-warning{{uninitialized}}
1248 B b5 = { b5.a2 }; // expected-warning{{uninitialized}}
1249
1250 B b6 = { {b6.a1.i1} }; // expected-warning{{uninitialized}}
1251 B b7 = { {0, b7.a1.i1} };
1252 B b8 = { {}, {b8.a1.i1} };
1253 B b9 = { {}, {0, b9.a1.i1} };
1254
1255 B b10 = { {b10.a1.i2} }; // expected-warning{{uninitialized}}
1256 B b11 = { {0, b11.a1.i2} }; // expected-warning{{uninitialized}}
1257 B b12 = { {}, {b12.a1.i2} };
1258 B b13 = { {}, {0, b13.a1.i2} };
1259
1260 B b14 = { {b14.a2.i1} }; // expected-warning{{uninitialized}}
1261 B b15 = { {0, b15.a2.i1} }; // expected-warning{{uninitialized}}
1262 B b16 = { {}, {b16.a2.i1} }; // expected-warning{{uninitialized}}
1263 B b17 = { {}, {0, b17.a2.i1} };
1264
1265 B b18 = { {b18.a2.i2} }; // expected-warning{{uninitialized}}
1266 B b19 = { {0, b19.a2.i2} }; // expected-warning{{uninitialized}}
1267 B b20 = { {}, {b20.a2.i2} }; // expected-warning{{uninitialized}}
1268 B b21 = { {}, {0, b21.a2.i2} }; // expected-warning{{uninitialized}}
1269
1270 B b22 = { {b18.a2.i2 + 5} };
1271 Bwrapper() {} // expected-note 13{{in this constructor}}
1272 Bwrapper(int) :
1273 b1{ {}, {} },
1274 b2{ {}, b2.a1 },
1275 b3{ b3.a1 }, // expected-warning{{uninitialized}}
1276 b4{ {}, b4.a2}, // expected-warning{{uninitialized}}
1277 b5{ b5.a2 }, // expected-warning{{uninitialized}}
1278
1279 b6{ {b6.a1.i1} }, // expected-warning{{uninitialized}}
1280 b7{ {0, b7.a1.i1} },
1281 b8{ {}, {b8.a1.i1} },
1282 b9{ {}, {0, b9.a1.i1} },
1283
1284 b10{ {b10.a1.i2} }, // expected-warning{{uninitialized}}
1285 b11{ {0, b11.a1.i2} }, // expected-warning{{uninitialized}}
1286 b12{ {}, {b12.a1.i2} },
1287 b13{ {}, {0, b13.a1.i2} },
1288
1289 b14{ {b14.a2.i1} }, // expected-warning{{uninitialized}}
1290 b15{ {0, b15.a2.i1} }, // expected-warning{{uninitialized}}
1291 b16{ {}, {b16.a2.i1} }, // expected-warning{{uninitialized}}
1292 b17{ {}, {0, b17.a2.i1} },
1293
1294 b18{ {b18.a2.i2} }, // expected-warning{{uninitialized}}
1295 b19{ {0, b19.a2.i2} }, // expected-warning{{uninitialized}}
1296 b20{ {}, {b20.a2.i2} }, // expected-warning{{uninitialized}}
1297 b21{ {}, {0, b21.a2.i2} }, // expected-warning{{uninitialized}}
1298
1299 b22{ {b18.a2.i2 + 5} }
1300 {}
1301 };
1302
1303 struct Cwrapper {
1304 C c1 = { 0, num, 0 };
1305 C c2 = { 1, num, c2.b };
1306 C c3 = { c3.b, num }; // expected-warning{{uninitialized}}
1307 C c4 = { 0, c4.b, 0 }; // expected-warning{{uninitialized}}
1308 C c5 = { 0, c5.c, 0 };
1309 C c6 = { c6.b, num, 0 }; // expected-warning{{uninitialized}}
1310 C c7 = { 0, c7.a, 0 };
1311
1312 Cwrapper() {} // expected-note 3{{in this constructor}}
1313 Cwrapper(int) :
1314 c1{ 0, num, 0 },
1315 c2{ 1, num, c2.b },
1316 c3{ c3.b, num }, // expected-warning{{uninitialized}}
1317 c4{ 0, c4.b, 0 }, // expected-warning{{uninitialized}}
1318 c5{ 0, c5.c, 0 },
1319 c6{ c6.b, num, 0 }, // expected-warning{{uninitialized}}
1320 c7{ 0, c7.a, 0 }
1321 {}
1322 };
1323
1324 struct Dwrapper {
1325 D d1 = { num, num };
1326 D d2 = { num, d2.a };
1327 D d3 = { d3.b, num }; // expected-warning{{uninitialized}}
1328 Dwrapper() {} // expected-note{{in this constructor}}
1329 Dwrapper(int) :
1330 d1{ num, num },
1331 d2{ num, d2.a },
1332 d3{ d3.b, num } // expected-warning{{uninitialized}}
1333 {}
1334 };
1335
1336 struct E {
1337 E();
1338 E foo();
1339 E* operator->();
1340 };
1341
1342 struct F { F(E); };
1343
1344 struct EFComposed {
1345 F f;
1346 E e;
1347 EFComposed() : f{ e->foo() }, e() {} // expected-warning{{uninitialized}}
1348 };
1349 }
1350
1351 namespace template_class {
1352 class Foo {
1353 public:
1354 int *Create() { return nullptr; }
1355 };
1356
1357 template <typename T>
1358 class A {
1359 public:
1360 // Don't warn on foo here.
1361 A() : ptr(foo->Create()) {}
1362
1363 private:
1364 Foo *foo = new Foo;
1365 int *ptr;
1366 };
1367
1368 template <typename T>
1369 class B {
1370 public:
1371 // foo is uninitialized here, but class B is never instantiated.
1372 B() : ptr(foo->Create()) {}
1373
1374 private:
1375 Foo *foo;
1376 int *ptr;
1377 };
1378
1379 template <typename T>
1380 class C {
1381 public:
1382 C() : ptr(foo->Create()) {}
1383 // expected-warning@-1 {{field 'foo' is uninitialized when used here}}
1384 private:
1385 Foo *foo;
1386 int *ptr;
1387 };
1388
1389 C<int> c;
1390 // expected-note@-1 {{in instantiation of member function 'template_class::C<int>::C' requested here}}
1391
1392 }
1393
1394 namespace base_class_access {
1395 struct A {
1396 A();
1397 A(int);
1398
1399 int i;
1400 int foo();
1401
1402 static int bar();
1403 };
1404
1405 struct B : public A {
1406 B(int (*)[1]) : A() {}
1407 B(int (*)[2]) : A(bar()) {}
1408
1409 B(int (*)[3]) : A(i) {}
1410 // expected-warning@-1 {{base class 'base_class_access::A' is uninitialized when used here to access 'base_class_access::A::i'}}
1411
1412 B(int (*)[4]) : A(foo()) {}
1413 // expected-warning@-1 {{base_class_access::A' is uninitialized when used here to access 'base_class_access::A::foo'}}
1414 };
1415
1416 struct C {
1417 C(int) {}
1418 };
1419
1420 struct D : public C, public A {
1421 D(int (*)[1]) : C(0) {}
1422 D(int (*)[2]) : C(bar()) {}
1423
1424 D(int (*)[3]) : C(i) {}
1425 // expected-warning@-1 {{base class 'base_class_access::A' is uninitialized when used here to access 'base_class_access::A::i'}}
1426
1427 D(int (*)[4]) : C(foo()) {}
1428 // expected-warning@-1 {{base_class_access::A' is uninitialized when used here to access 'base_class_access::A::foo'}}
1429 };
1430
1431 }
1432
1433 namespace value {
1434 template <class T> T move(T t);
1435 template <class T> T notmove(T t);
1436 }
1437 namespace lvalueref {
1438 template <class T> T move(T& t);
1439 template <class T> T notmove(T& t);
1440 }
1441 namespace rvalueref {
1442 template <class T> T move(T&& t);
1443 template <class T> T notmove(T&& t);
1444 }
1445
1446 namespace move_test {
1447 int a1 = std::move(a1); // expected-warning {{uninitialized}}
1448 int a2 = value::move(a2); // expected-warning {{uninitialized}}
1449 int a3 = value::notmove(a3); // expected-warning {{uninitialized}}
1450 int a4 = lvalueref::move(a4);
1451 int a5 = lvalueref::notmove(a5);
1452 int a6 = rvalueref::move(a6);
1453 int a7 = rvalueref::notmove(a7);
1454
1455 void test() {
1456 int a1 = std::move(a1); // expected-warning {{uninitialized}}
1457 int a2 = value::move(a2); // expected-warning {{uninitialized}}
1458 int a3 = value::notmove(a3); // expected-warning {{uninitialized}}
1459 int a4 = lvalueref::move(a4);
1460 int a5 = lvalueref::notmove(a5);
1461 int a6 = rvalueref::move(a6);
1462 int a7 = rvalueref::notmove(a7);
1463 }
1464
1465 class A {
1466 int a;
1467 A(int (*) [1]) : a(std::move(a)) {} // expected-warning {{uninitialized}}
1468 A(int (*) [2]) : a(value::move(a)) {} // expected-warning {{uninitialized}}
1469 A(int (*) [3]) : a(value::notmove(a)) {} // expected-warning {{uninitialized}}
1470 A(int (*) [4]) : a(lvalueref::move(a)) {}
1471 A(int (*) [5]) : a(lvalueref::notmove(a)) {}
1472 A(int (*) [6]) : a(rvalueref::move(a)) {}
1473 A(int (*) [7]) : a(rvalueref::notmove(a)) {}
1474 };
1475 }
1476
1477 void array_capture(bool b) {
1478 const char fname[] = "array_capture";
1479 if (b) {
1480 int unused; // expected-warning {{unused variable}}
1481 } else {
1482 [fname]{};
1483 }
1484 }
1485
1486 void if_switch_init_stmt(int k) {
1487 if (int n = 0; (n == k || k > 5)) {}
1488
1489 if (int n; (n == k || k > 5)) {} // expected-warning {{uninitialized}} expected-note {{initialize}}
1490
1491 switch (int n = 0; (n == k || k > 5)) {} // expected-warning {{boolean}}
1492
1493 switch (int n; (n == k || k > 5)) {} // expected-warning {{uninitialized}} expected-note {{initialize}} expected-warning {{boolean}}
1494 }
1495
1496 template<typename T> struct Outer {
1497 struct Inner {
1498 int a = 1;
1499 int b;
1500 Inner() : b(a) {}
1501 };
1502 };
1503 Outer<int>::Inner outerinner;
1504
1505 struct Polymorphic { virtual ~Polymorphic() { } };
1506
1507 template<class... Bases>
1508 struct Inherit : Bases... { // #TYPE_INHERIT
1509 int g1; // #FIELD_G1
1510 };
1511
1512 template<class... Bases>
1513 struct InheritWithExplicit : Bases... { // #TYPE_INHERIT_WITH_EXPLICIT
1514 int g2 [[clang::require_explicit_initialization]]; // #FIELD_G2
1515 };
1516
1517 struct Special {};
1518
1519 template<>
1520 struct Inherit<Special> {
1521 int g3 [[clang::require_explicit_initialization]]; // #FIELD_G3
1522 };
1523
1524 template<>
1525 struct InheritWithExplicit<Special> {
1526 int g4; // #FIELD_G4
1527 };
1528
1529 void aggregate() {
1530 struct NonAgg {
1531 NonAgg() { }
1532 [[clang::require_explicit_initialization]] int na; // expected-warning {{'clang::require_explicit_initialization' attribute is ignored in non-aggregate type 'NonAgg'}}
1533 };
1534 NonAgg nonagg; // no-warning
1535 (void)nonagg;
1536
1537 struct S {
1538 [[clang::require_explicit_initialization]] int s1; // #FIELD_S1
1539 int s2;
1540 int s3 = 12;
1541 [[clang::require_explicit_initialization]] int s4 = 100; // #FIELD_S4
1542 static void foo(S) { }
1543 };
1544
1545 struct C {
1546 #if __cplusplus < 202002L
1547 // expected-warning@+1 {{explicit initialization of field 'c1' will not be enforced in C++20 and later because 'C' has a user-declared constructor, making the type no longer an aggregate}}
1548 [[clang::require_explicit_initialization]]
1549 #endif
1550 int c1; // #FIELD_C1
1551 C() = default; // Test pre-C++20 aggregates
1552 };
1553
1554 struct D : S { // #TYPE_D
1555 int d1;
1556 int d2 [[clang::require_explicit_initialization]]; // #FIELD_D2
1557 };
1558
1559 struct D2 : D { // #TYPE_D2
1560 };
1561
1562 struct E { // #TYPE_E
1563 int e1;
1564 D e2 [[clang::require_explicit_initialization]]; // #FIELD_E2
1565 struct {
1566 [[clang::require_explicit_initialization]] D e3;
1567 D2 e4 [[clang::require_explicit_initialization]];
1568 };
1569 };
1570
1571 struct CopyAndMove {
1572 CopyAndMove() = default;
1573 CopyAndMove(const CopyAndMove &) {}
1574 CopyAndMove(CopyAndMove &&) {}
1575 };
1576 struct Embed {
1577 int embed1; // #FIELD_EMBED1
1578 int embed2 [[clang::require_explicit_initialization]]; // #FIELD_EMBED2
1579 CopyAndMove force_separate_move_ctor;
1580 };
1581 struct EmbedDerived : Embed {};
1582 struct F {
1583 Embed f1;
1584 // expected-warning@+1 {{field in 'Embed' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1585 explicit F(const char(&)[1]) : f1() {
1586 // expected-warning@+1 {{field in 'Embed' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1587 ::new(static_cast<void*>(&f1)) decltype(f1);
1588 // expected-warning@#FIELD_EMBED2_CONSTRUCT_AT {{field in 'Embed' requires explicit initialization but is not explicitly initialized}} expected-note@+1 {{in instantiation of function template specialization 'std::construct_at<Embed>' requested here}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1589 std::construct_at(&f1);
1590 #if __cplusplus >= 202002L
1591 // expected-warning@#FIELD_EMBED2_CONSTRUCT_AT {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@+1 {{in instantiation of function template specialization 'std::construct_at<Embed, int>' requested here}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1592 std::construct_at(&f1, 1);
1593 #endif
1594 // expected-warning@+1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1595 ::new(static_cast<void*>(&f1)) decltype(f1){1};
1596 }
1597 #if __cplusplus >= 202002L
1598 // expected-warning@+1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1599 explicit F(const char(&)[2]) : f1(1) {}
1600 #else
1601 explicit F(const char(&)[2]) : f1{1, 2} { }
1602 #endif
1603 // expected-warning@+1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1604 explicit F(const char(&)[3]) : f1{} {}
1605 // expected-warning@+1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1606 explicit F(const char(&)[4]) : f1{1} {}
1607 // expected-warning@+1 {{field 'embed2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_EMBED2 {{'embed2' declared here}}
1608 explicit F(const char(&)[5]) : f1{.embed1 = 1} {}
1609 };
1610 F ctors[] = {
1611 F(""),
1612 F("_"),
1613 F("__"),
1614 F("___"),
1615 F("____")
1616 };
1617
1618 struct MoveOrCopy {
1619 Embed e;
1620 EmbedDerived ed;
1621 F f;
1622 // no-error
1623 MoveOrCopy(const MoveOrCopy &c) : e(c.e), ed(c.ed), f(c.f) {}
1624 // no-error
1625 MoveOrCopy(MoveOrCopy &&c)
1626 : e(std::move(c.e)), ed(std::move(c.ed)), f(std::move(c.f)) {}
1627 };
1628 F copy1(ctors[0]); // no-error
1629 (void)copy1;
1630 F move1(std::move(ctors[0])); // no-error
1631 (void)move1;
1632 F copy2{ctors[0]}; // no-error
1633 (void)copy2;
1634 F move2{std::move(ctors[0])}; // no-error
1635 (void)move2;
1636 F copy3 = ctors[0]; // no-error
1637 (void)copy3;
1638 F move3 = std::move(ctors[0]); // no-error
1639 (void)move3;
1640 F copy4 = {ctors[0]}; // no-error
1641 (void)copy4;
1642 F move4 = {std::move(ctors[0])}; // no-error
1643 (void)move4;
1644
1645 S::foo(S{1, 2, 3, 4});
1646 S::foo(S{.s1 = 100, .s4 = 100});
1647 S::foo(S{.s1 = 100}); // expected-warning {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1648
1649 (void)sizeof(S{}); // no warning -- unevaluated operand
1650
1651 S s{.s1 = 100, .s4 = 100};
1652 (void)s;
1653
1654 S t{.s4 = 100}; // expected-warning {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1655 (void)t;
1656
1657 S *ptr1 = new S; // expected-warning {{field in 'S' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}} expected-note@#FIELD_S1 {{'s1' declared here}}
1658 delete ptr1;
1659
1660 S *ptr2 = new S{.s1 = 100, .s4 = 100};
1661 delete ptr2;
1662
1663 #if __cplusplus >= 202002L
1664 // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1665 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1666 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1667 D a1({}, 0);
1668 (void)a1;
1669
1670 // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1671 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1672 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1673 D a2(S{}, 0);
1674 (void)a2;
1675
1676 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1677 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1678 D a3(S{.s1 = 0}, 0);
1679 (void)a3;
1680
1681 // expected-warning@+3 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1682 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1683 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1684 D a4(S(), 0);
1685 (void)a4;
1686
1687 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1688 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1689 D a5(S(0), 0);
1690 (void)a5;
1691
1692 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1693 // expected-warning@+1 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1694 D a6 = {S(0), 0};
1695 (void)a6;
1696 #endif
1697
1698 #if 201103L <= __cplusplus && __cplusplus < 202002L
1699 C a; // expected-warning {{field in 'C' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_C1 {{'c1' declared here}}
1700 (void)a;
1701 #endif
1702
1703 // expected-warning@+2 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1704 // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1705 D b{.d2 = 1};
1706 (void)b;
1707
1708 // expected-warning@+3 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1709 // expected-warning@+2 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1710 // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1711 D c{.d1 = 5};
1712
1713 // expected-warning@+3 {{field 's4' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S4 {{'s4' declared here}}
1714 // expected-warning@+2 {{field 'd2' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1715 // expected-warning@+1 {{field 's1' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}}
1716 c = {{}, 0};
1717 (void)c;
1718
1719 // expected-note@+3 {{in implicit default constructor for 'D' first required here}}
1720 // expected-warning@#TYPE_D {{field in 'S' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_S1 {{'s1' declared here}} expected-note@#FIELD_S4 {{'s4' declared here}}
1721 // expected-warning@+1 {{field in 'D' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_D2 {{'d2' declared here}}
1722 D d;
1723 (void)d;
1724
1725 // expected-warning@+12 {{field in 'E' requires explicit initialization but is not explicitly initialized}}
1726 // expected-note@#FIELD_E2 {{'e2' declared here}}
1727 // expected-warning@#TYPE_E {{field in 'D' requires explicit initialization but is not explicitly initialized}}
1728 // expected-note@+9 {{in implicit default constructor for 'E' first required here}}
1729 // expected-note@#FIELD_D2 {{'d2' declared here}}
1730 // expected-warning@#TYPE_E {{field in 'D' requires explicit initialization but is not explicitly initialized}}
1731 // expected-note@#FIELD_D2 {{'d2' declared here}}
1732 // expected-warning@#TYPE_E {{field in 'D2' requires explicit initialization but is not explicitly initialized}}
1733 // expected-note@#TYPE_E {{in implicit default constructor for 'D2' first required here}}
1734 // expected-warning@#TYPE_D2 {{field in 'D' requires explicit initialization but is not explicitly initialized}}
1735 // expected-note@+2 {{in implicit default constructor for 'E' first required here}}
1736 // expected-note@#FIELD_D2 {{'d2' declared here}}
1737 E e;
1738 (void)e;
1739
1740 InheritWithExplicit<> agg; // expected-warning {{field in 'InheritWithExplicit<>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G2 {{'g2' declared here}}
1741 (void)agg;
1742
1743 InheritWithExplicit<Polymorphic> polymorphic; // expected-warning@#FIELD_G2 {{'clang::require_explicit_initialization' attribute is ignored in non-aggregate type 'InheritWithExplicit<Polymorphic>'}}
1744 (void)polymorphic;
1745
1746 Inherit<Special> specialized_explicit; // expected-warning {{field in 'Inherit<Special>' requires explicit initialization but is not explicitly initialized}} expected-note@#FIELD_G3 {{'g3' declared here}}
1747 (void)specialized_explicit;
1748
1749 InheritWithExplicit<Special> specialized_implicit; // no-warning
1750 (void)specialized_implicit;
1751 }
1752
1753 #endif