blob: 665cfe7e91182e7b6b25097ec94765ed9e5cd3c2 [file] [log] [blame]
Richard Trieude5e75c2012-06-14 23:11:34 +00001// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -std=c++11 -verify %s
Ted Kremenekc75f6182010-10-30 00:43:15 +00002
Chandler Carruth349894e2011-03-27 20:35:59 +00003int foo(int x);
4int bar(int* x);
5int boo(int& x);
6int far(const int& x);
7
8// Test self-references within initializers which are guaranteed to be
9// uninitialized.
Chandler Carruthb414c4f2011-04-05 17:41:31 +000010int a = a; // no-warning: used to signal intended lack of initialization.
Chandler Carruth349894e2011-03-27 20:35:59 +000011int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
12int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}}
Chandler Carruth349894e2011-03-27 20:35:59 +000013int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
14int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
15
16// Thes don't warn as they don't require the value.
17int g = sizeof(g);
18void* ptr = &ptr;
19int h = bar(&h);
20int i = boo(i);
21int j = far(j);
22int k = __alignof__(k);
23
Richard Trieu7e9f8af2012-05-09 00:21:34 +000024int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
25int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
26int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
Richard Trieu898267f2011-09-01 21:44:13 +000027
Ted Kremenek3f635c02012-07-14 05:04:10 +000028void test_stuff () {
Richard Trieuf6278e52012-05-09 21:08:22 +000029 int a = a; // no-warning: used to signal intended lack of initialization.
30 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
31 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
32 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
33 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
34 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
35
36 // Thes don't warn as they don't require the value.
37 int g = sizeof(g);
38 void* ptr = &ptr;
39 int h = bar(&h);
40 int i = boo(i);
41 int j = far(j);
42 int k = __alignof__(k);
43
Ted Kremenek77fd3c02013-01-19 00:25:06 +000044 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}}
45 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}}
Richard Trieuf6278e52012-05-09 21:08:22 +000046 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
47
48 for (;;) {
49 int a = a; // no-warning: used to signal intended lack of initialization.
50 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
51 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
52 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
53 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
54 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
55
56 // Thes don't warn as they don't require the value.
57 int g = sizeof(g);
58 void* ptr = &ptr;
59 int h = bar(&h);
60 int i = boo(i);
61 int j = far(j);
62 int k = __alignof__(k);
63
Ted Kremenek77fd3c02013-01-19 00:25:06 +000064 int l = k ? l : l; // expected-warning {{variable 'l' is uninitialized when used within its own initialization}}
65 int m = 1 + (k ? m : m); // expected-warning {{'m' is uninitialized when used within its own initialization}}
Richard Trieuf6278e52012-05-09 21:08:22 +000066 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
67 }
68}
69
Richard Trieu898267f2011-09-01 21:44:13 +000070// Test self-references with record types.
71class A {
72 // Non-POD class.
73 public:
74 enum count { ONE, TWO, THREE };
75 int num;
76 static int count;
77 int get() const { return num; }
Richard Trieuffea6b42012-03-08 01:15:31 +000078 int get2() { return num; }
Richard Trieu898267f2011-09-01 21:44:13 +000079 void set(int x) { num = x; }
80 static int zero() { return 0; }
81
82 A() {}
83 A(A const &a) {}
84 A(int x) {}
85 A(int *x) {}
86 A(A *a) {}
Rafael Espindola1a5d3552012-01-06 04:54:01 +000087 ~A();
Richard Trieu898267f2011-09-01 21:44:13 +000088};
89
90A getA() { return A(); }
91A getA(int x) { return A(); }
92A getA(A* a) { return A(); }
Richard Trieu7e9f8af2012-05-09 00:21:34 +000093A getA(A a) { return A(); }
Richard Trieu898267f2011-09-01 21:44:13 +000094
Richard Trieu7e9f8af2012-05-09 00:21:34 +000095void setupA(bool x) {
Richard Trieu898267f2011-09-01 21:44:13 +000096 A a1;
97 a1.set(a1.get());
98 A a2(a1.get());
99 A a3(a1);
100 A a4(&a4);
101 A a5(a5.zero());
102 A a6(a6.ONE);
103 A a7 = getA();
104 A a8 = getA(a8.TWO);
105 A a9 = getA(&a9);
106 A a10(a10.count);
107
108 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
109 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
110 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
111 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
112 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
113 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
Richard Trieuffea6b42012-03-08 01:15:31 +0000114 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
Richard Trieu7e9f8af2012-05-09 00:21:34 +0000115 A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
116 A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
Richard Trieu568f7852012-10-01 17:39:51 +0000117 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
118 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
Richard Trieu6b2cc422012-10-03 00:41:36 +0000119
120 // FIXME: Make the local uninitialized warning consistant with the global
121 // uninitialized checking.
122 A *a22 = new A(a22->count); // expected-warning {{variable 'a22' is uninitialized when used within its own initialization}}
123 A *a23 = new A(a23->ONE); // expected-warning {{variable 'a23' is uninitialized when used within its own initialization}}
124 A *a24 = new A(a24->TWO); // expected-warning {{variable 'a24' is uninitialized when used within its own initialization}}
125 A *a25 = new A(a25->zero()); // expected-warning {{variable 'a25' is uninitialized when used within its own initialization}}
126
127 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
128 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
129 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
Richard Trieu898267f2011-09-01 21:44:13 +0000130}
131
Richard Trieue27a08a2012-08-06 21:09:23 +0000132bool x;
133
134A a1;
135A a2(a1.get());
136A a3(a1);
137A a4(&a4);
138A a5(a5.zero());
139A a6(a6.ONE);
140A a7 = getA();
141A a8 = getA(a8.TWO);
142A a9 = getA(&a9);
143A a10(a10.count);
144
145A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
146A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
147A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
148A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
149A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
150A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
151A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
152A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
153A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
Richard Trieu568f7852012-10-01 17:39:51 +0000154A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
155A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
Richard Trieue27a08a2012-08-06 21:09:23 +0000156
Richard Trieu6b2cc422012-10-03 00:41:36 +0000157A *a22 = new A(a22->count);
158A *a23 = new A(a23->ONE);
159A *a24 = new A(a24->TWO);
160A *a25 = new A(a25->zero());
161
162A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
163A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
164A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
165
Richard Trieu898267f2011-09-01 21:44:13 +0000166struct B {
167 // POD struct.
168 int x;
169 int *y;
170};
171
172B getB() { return B(); };
173B getB(int x) { return B(); };
174B getB(int *x) { return B(); };
175B getB(B *b) { return B(); };
176
Richard Trieu6b2cc422012-10-03 00:41:36 +0000177B* getPtrB() { return 0; };
178B* getPtrB(int x) { return 0; };
179B* getPtrB(int *x) { return 0; };
180B* getPtrB(B **b) { return 0; };
181
Richard Trieu898267f2011-09-01 21:44:13 +0000182void setupB() {
183 B b1;
184 B b2(b1);
185 B b3 = { 5, &b3.x };
186 B b4 = getB();
187 B b5 = getB(&b5);
188 B b6 = getB(&b6.x);
189
190 // Silence unused warning
191 (void) b2;
192 (void) b4;
193
194 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
195 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
196 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
Richard Trieu7e9f8af2012-05-09 00:21:34 +0000197 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
Richard Trieu6b2cc422012-10-03 00:41:36 +0000198
199 B* b11 = 0;
200 B* b12(b11);
201 B* b13 = getPtrB();
202 B* b14 = getPtrB(&b14);
203
204 (void) b12;
205 (void) b13;
206
207 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
208 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
209
210 B b17 = { b17.x = 5, b17.y = 0 };
211 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
Richard Trieu898267f2011-09-01 21:44:13 +0000212}
213
Richard Trieu6b2cc422012-10-03 00:41:36 +0000214B b1;
215B b2(b1);
216B b3 = { 5, &b3.x };
217B b4 = getB();
218B b5 = getB(&b5);
219B b6 = getB(&b6.x);
220
221B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
222B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
223B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
224B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
225
226B* b11 = 0;
227B* b12(b11);
228B* b13 = getPtrB();
229B* b14 = getPtrB(&b14);
230
231B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
232B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
233
234B b17 = { b17.x = 5, b17.y = 0 };
235B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
236
237
Chandler Carruth349894e2011-03-27 20:35:59 +0000238// Also test similar constructs in a field's initializer.
239struct S {
240 int x;
241 void *ptr;
242
Hans Wennborg7821e072012-09-21 08:58:33 +0000243 S(bool (*)[1]) : x(x) {} // expected-warning {{field 'x' is uninitialized when used here}}
244 S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field 'x' is uninitialized when used here}}
245 S(bool (*)[3]) : x(x + x) {} // expected-warning 2{{field 'x' is uninitialized when used here}}
246 S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field 'x' is uninitialized when used here}}
247 S(bool (*)[5]) : x(foo(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
Chandler Carruth349894e2011-03-27 20:35:59 +0000248
249 // These don't actually require the value of x and so shouldn't warn.
250 S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363
251 S(char (*)[2]) : ptr(&ptr) {}
252 S(char (*)[3]) : x(__alignof__(x)) {}
253 S(char (*)[4]) : x(bar(&x)) {}
254 S(char (*)[5]) : x(boo(x)) {}
255 S(char (*)[6]) : x(far(x)) {}
Ted Kremenekc75f6182010-10-30 00:43:15 +0000256};
Richard Trieu47eb8982011-09-07 00:58:53 +0000257
258struct C { char a[100], *e; } car = { .e = car.a };
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000259
260// <rdar://problem/10398199>
261namespace rdar10398199 {
262 class FooBase { protected: ~FooBase() {} };
263 class Foo : public FooBase {
264 public:
265 operator int&() const;
266 };
267 void stuff();
268 template <typename T> class FooImpl : public Foo {
269 T val;
270 public:
271 FooImpl(const T &x) : val(x) {}
272 ~FooImpl() { stuff(); }
273 };
274
275 template <typename T> FooImpl<T> makeFoo(const T& x) {
276 return FooImpl<T>(x);
277 }
278
279 void test() {
280 const Foo &x = makeFoo(42);
281 const int&y = makeFoo(42u);
282 (void)x;
283 (void)y;
284 };
285}
Ted Kremenek550f2232012-03-22 05:57:43 +0000286
287// PR 12325 - this was a false uninitialized value warning due to
288// a broken CFG.
289int pr12325(int params) {
290 int x = ({
291 while (false)
292 ;
293 int _v = params;
294 if (false)
295 ;
296 _v; // no-warning
297 });
298 return x;
299}
300
Ted Kremenekc02af352012-04-12 20:03:47 +0000301// Test lambda expressions with -Wuninitialized
302int test_lambda() {
Aaron Ballman44f5b1d2012-06-04 18:57:41 +0000303 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}}
Ted Kremenekc02af352012-04-12 20:03:47 +0000304 return f1(1, 2);
305}
Richard Trieude5e75c2012-06-14 23:11:34 +0000306
307namespace {
308 struct A {
309 enum { A1 };
310 static int A2() {return 5;}
311 int A3;
312 int A4() { return 5;}
313 };
314
315 struct B {
316 A a;
317 };
318
319 struct C {
320 C() {}
321 C(int x) {}
322 static A a;
323 B b;
324 };
325 A C::a = A();
326
327 // Accessing non-static members will give a warning.
328 struct D {
329 C c;
330 D(char (*)[1]) : c(c.b.a.A1) {}
331 D(char (*)[2]) : c(c.b.a.A2()) {}
Hans Wennborg7821e072012-09-21 08:58:33 +0000332 D(char (*)[3]) : c(c.b.a.A3) {} // expected-warning {{field 'c' is uninitialized when used here}}
333 D(char (*)[4]) : c(c.b.a.A4()) {} // expected-warning {{field 'c' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000334
335 // c::a is static, so it is already initialized
336 D(char (*)[5]) : c(c.a.A1) {}
337 D(char (*)[6]) : c(c.a.A2()) {}
338 D(char (*)[7]) : c(c.a.A3) {}
339 D(char (*)[8]) : c(c.a.A4()) {}
340 };
341
342 struct E {
343 int a, b, c;
Hans Wennborg7821e072012-09-21 08:58:33 +0000344 E(char (*)[1]) : a(a ? b : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
345 E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
346 E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
347 E(char (*)[4]) : a(b ? c : (a+c)) {} // expected-warning {{field 'a' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000348 E(char (*)[5]) : a(b ? c : b) {}
349
Hans Wennborg7821e072012-09-21 08:58:33 +0000350 E(char (*)[6]) : a(a ?: a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
351 E(char (*)[7]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}}
352 E(char (*)[8]) : a(a ?: c) {} // expected-warning {{field 'a' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000353 E(char (*)[9]) : a(b ?: c) {}
354
355 E(char (*)[10]) : a((a, a, b)) {}
Hans Wennborg7821e072012-09-21 08:58:33 +0000356 E(char (*)[11]) : a((c + a, a + 1, b)) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
357 E(char (*)[12]) : a((b + c, c, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
358 E(char (*)[13]) : a((a, a, a, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000359 E(char (*)[14]) : a((b, c, c)) {}
360 };
361
362 struct F {
363 int a;
364 F* f;
365 F(int) {}
366 F() {}
367 };
368
369 int F::*ptr = &F::a;
370 F* F::*f_ptr = &F::f;
371 struct G {
372 F f1, f2;
373 F *f3, *f4;
Hans Wennborg7821e072012-09-21 08:58:33 +0000374 G(char (*)[1]) : f1(f1) {} // expected-warning {{field 'f1' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000375 G(char (*)[2]) : f2(f1) {}
376 G(char (*)[3]) : f2(F()) {}
377
Hans Wennborg7821e072012-09-21 08:58:33 +0000378 G(char (*)[4]) : f1(f1.*ptr) {} // expected-warning {{field 'f1' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000379 G(char (*)[5]) : f2(f1.*ptr) {}
380
Hans Wennborg7821e072012-09-21 08:58:33 +0000381 G(char (*)[6]) : f3(f3) {} // expected-warning {{field 'f3' is uninitialized when used here}}
382 G(char (*)[7]) : f3(f3->*f_ptr) {} // expected-warning {{field 'f3' is uninitialized when used here}}
383 G(char (*)[8]) : f3(new F(f3->*ptr)) {} // expected-warning {{field 'f3' is uninitialized when used here}}
Richard Trieude5e75c2012-06-14 23:11:34 +0000384 };
385}
Richard Trieu1b4b1462012-08-14 23:50:52 +0000386
387namespace statics {
388 static int a = a; // no-warning: used to signal intended lack of initialization.
389 static int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
390 static int c = (c + c); // expected-warning 2{{variable 'c' is uninitialized when used within its own initialization}}
391 static int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
392 static int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
393
394 // Thes don't warn as they don't require the value.
395 static int g = sizeof(g);
396 int gg = g; // Silence unneeded warning
397 static void* ptr = &ptr;
398 static int h = bar(&h);
399 static int i = boo(i);
400 static int j = far(j);
401 static int k = __alignof__(k);
402
403 static int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
404 static int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
405 static int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
406
407 void test() {
408 static int a = a; // no-warning: used to signal intended lack of initialization.
Ted Kremenek39371b82013-01-19 04:33:14 +0000409 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
410 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
411 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
412 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
413 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
Richard Trieu1b4b1462012-08-14 23:50:52 +0000414
415 // Thes don't warn as they don't require the value.
416 static int g = sizeof(g);
417 static void* ptr = &ptr;
418 static int h = bar(&h);
419 static int i = boo(i);
420 static int j = far(j);
421 static int k = __alignof__(k);
422
Ted Kremenek39371b82013-01-19 04:33:14 +0000423 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
424 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
425 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
Richard Trieu1b4b1462012-08-14 23:50:52 +0000426 for (;;) {
427 static int a = a; // no-warning: used to signal intended lack of initialization.
Ted Kremenek39371b82013-01-19 04:33:14 +0000428 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
429 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
430 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
431 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
432 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
Richard Trieu1b4b1462012-08-14 23:50:52 +0000433
434 // Thes don't warn as they don't require the value.
435 static int g = sizeof(g);
436 static void* ptr = &ptr;
437 static int h = bar(&h);
438 static int i = boo(i);
439 static int j = far(j);
440 static int k = __alignof__(k);
441
Ted Kremenek39371b82013-01-19 04:33:14 +0000442 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
443 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
444 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
Richard Trieu1b4b1462012-08-14 23:50:52 +0000445 }
446 }
447}
Hans Wennborg8be9e772012-08-17 10:12:33 +0000448
Hans Wennborg471f9852012-09-18 15:58:06 +0000449namespace in_class_initializers {
450 struct S {
Hans Wennborg7821e072012-09-21 08:58:33 +0000451 S() : a(a + 1) {} // expected-warning{{field 'a' is uninitialized when used here}}
Hans Wennborg471f9852012-09-18 15:58:06 +0000452 int a = 42; // Note: because a is in a member initializer list, this initialization is ignored.
453 };
454
455 struct T {
456 T() : b(a + 1) {} // No-warning.
457 int a = 42;
458 int b;
459 };
460
461 struct U {
462 U() : a(b + 1), b(a + 1) {} // FIXME: Warn here.
463 int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored.
464 int b = 1;
465 };
466}
467
Hans Wennborg8be9e772012-08-17 10:12:33 +0000468namespace references {
Hans Wennborg5965b7c2012-08-20 08:52:22 +0000469 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
Richard Trieu568f7852012-10-01 17:39:51 +0000470 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
471 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
472 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
Hans Wennborg8be9e772012-08-17 10:12:33 +0000473
474 struct S {
Hans Wennborg7821e072012-09-21 08:58:33 +0000475 S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}}
Hans Wennborg8be9e772012-08-17 10:12:33 +0000476 int &a;
477 };
478
479 void f() {
Hans Wennborg5965b7c2012-08-20 08:52:22 +0000480 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
Richard Trieu568f7852012-10-01 17:39:51 +0000481 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
482 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
483 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
Hans Wennborg8be9e772012-08-17 10:12:33 +0000484 }
485
486 struct T {
487 T() : a(b), b(a) {} // FIXME: Warn here.
488 int &a, &b;
Hans Wennborg7821e072012-09-21 08:58:33 +0000489 int &c = c; // expected-warning{{reference 'c' is not yet bound to a value when used here}}
Hans Wennborg471f9852012-09-18 15:58:06 +0000490 };
491
492 int x;
493 struct U {
494 U() : b(a) {} // No-warning.
495 int &a = x;
496 int &b;
Hans Wennborg8be9e772012-08-17 10:12:33 +0000497 };
498}
Richard Trieu8af742a2013-03-26 03:41:40 +0000499
500namespace operators {
501 struct A {
502 A(bool);
503 bool operator==(A);
504 };
505
506 A makeA();
507
508 A a1 = a1 = makeA(); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
509 A a2 = a2 == a1; // expected-warning{{variable 'a2' is uninitialized when used within its own initialization}}
510 A a3 = a2 == a3; // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}}
511
512 int x = x = 5;
513}
Richard Smith0f2fc5f2013-05-03 19:16:22 +0000514
515namespace lambdas {
516 struct A {
517 template<typename T> A(T) {}
518 int x;
519 };
520 A a0([] { return a0.x; }); // ok
521 void f() {
522 A a1([=] { return a1.x; }); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
523 A a2([&] { return a2.x; }); // ok
524 }
525}