uninitialized.cpp revision 0e2c34f92f00628d48968dfea096d36381f494cb
1// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -std=c++11 -verify %s
2
3// definitions for std::move
4namespace std {
5inline namespace foo {
6template <class T> struct remove_reference { typedef T type; };
7template <class T> struct remove_reference<T&> { typedef T type; };
8template <class T> struct remove_reference<T&&> { typedef T type; };
9
10template <class T> typename remove_reference<T>::type&& move(T&& t);
11}
12}
13
14int foo(int x);
15int bar(int* x);
16int boo(int& x);
17int far(const int& x);
18int moved(int&& x);
19int &ref(int x);
20
21// Test self-references within initializers which are guaranteed to be
22// uninitialized.
23int a = a; // no-warning: used to signal intended lack of initialization.
24int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
25int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}}
26int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
27int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
28
29// Thes don't warn as they don't require the value.
30int g = sizeof(g);
31void* ptr = &ptr;
32int h = bar(&h);
33int i = boo(i);
34int j = far(j);
35int k = __alignof__(k);
36
37int l = k ? l : l;  // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
38int m = 1 + (k ? m : m);  // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
39int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
40int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
41const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
42int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
43int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
44int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
45int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
46int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
47int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
48int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
49int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
50int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
51int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
52int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
53int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
54
55void test_stuff () {
56  int a = a; // no-warning: used to signal intended lack of initialization.
57  int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
58  int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
59  int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
60  int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
61  int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
62
63  // Thes don't warn as they don't require the value.
64  int g = sizeof(g);
65  void* ptr = &ptr;
66  int h = bar(&h);
67  int i = boo(i);
68  int j = far(j);
69  int k = __alignof__(k);
70
71  int l = k ? l : l;  // expected-warning {{variable 'l' is uninitialized when used within its own initialization}}
72  int m = 1 + (k ? m : m);  // expected-warning {{'m' is uninitialized when used within its own initialization}}
73  int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
74  int o = std::move(o);  // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
75  const int p = std::move(p);  // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
76  int q = moved(std::move(q));  // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
77  int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
78  int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
79  int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
80  int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
81  int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
82  int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
83  int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
84  int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
85  int z = ++ref(z);                              // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
86  int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
87  int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
88
89
90  for (;;) {
91    int a = a; // no-warning: used to signal intended lack of initialization.
92    int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
93    int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
94    int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
95    int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
96    int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
97
98    // Thes don't warn as they don't require the value.
99    int g = sizeof(g);
100    void* ptr = &ptr;
101    int h = bar(&h);
102    int i = boo(i);
103    int j = far(j);
104    int k = __alignof__(k);
105
106    int l = k ? l : l;  // expected-warning {{variable 'l' is uninitialized when used within its own initialization}}
107    int m = 1 + (k ? m : m);  // expected-warning {{'m' is uninitialized when used within its own initialization}}
108    int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
109    int o = std::move(o);  // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
110    const int p = std::move(p);  // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
111    int q = moved(std::move(q));  // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
112    int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
113    int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
114    int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
115    int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
116    int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
117    int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
118    int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
119    int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
120    int z = ++ref(z);                              // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
121    int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
122    int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
123
124  }
125}
126
127// Also test similar constructs in a field's initializer.
128struct S {
129  int x;
130  int y;
131  const int z = 5;
132  void *ptr;
133
134  S(bool (*)[1]) : x(x) {} // expected-warning {{field 'x' is uninitialized when used here}}
135  S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field 'x' is uninitialized when used here}}
136  S(bool (*)[3]) : x(x + x) {} // expected-warning 2{{field 'x' is uninitialized when used here}}
137  S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field 'x' is uninitialized when used here}}
138  S(bool (*)[5]) : x(foo(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
139
140  // These don't actually require the value of x and so shouldn't warn.
141  S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363
142  S(char (*)[2]) : ptr(&ptr) {}
143  S(char (*)[3]) : x(bar(&x)) {}
144  S(char (*)[4]) : x(boo(x)) {}
145  S(char (*)[5]) : x(far(x)) {}
146  S(char (*)[6]) : x(__alignof__(x)) {}
147
148  S(int (*)[1]) : x(0), y(x ? y : y) {} // expected-warning 2{{field 'y' is uninitialized when used here}}
149  S(int (*)[2]) : x(0), y(1 + (x ? y : y)) {} // expected-warning 2{{field 'y' is uninitialized when used here}}
150  S(int (*)[3]) : x(-x) {} // expected-warning {{field 'x' is uninitialized when used here}}
151  S(int (*)[4]) : x(std::move(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
152  S(int (*)[5]) : z(std::move(z)) {} // expected-warning {{field 'z' is uninitialized when used here}}
153  S(int (*)[6]) : x(moved(std::move(x))) {} // expected-warning {{field 'x' is uninitialized when used here}}
154  S(int (*)[7]) : x(0), y(std::move((x ? x : (18, y)))) {} // expected-warning {{field 'y' is uninitialized when used here}}
155  S(int (*)[8]) : x(0), y(x ?: y) {} // expected-warning {{field 'y' is uninitialized when used here}}
156  S(int (*)[9]) : x(0), y(y ?: x) {} // expected-warning {{field 'y' is uninitialized when used here}}
157  S(int (*)[10]) : x(0), y((foo(y), x)) {} // expected-warning {{field 'y' is uninitialized when used here}}
158  S(int (*)[11]) : x(0), y(x += y) {} // expected-warning {{field 'y' is uninitialized when used here}}
159  S(int (*)[12]) : x(x += 10) {} // expected-warning {{field 'x' is uninitialized when used here}}
160  S(int (*)[13]) : x(x++) {} // expected-warning {{field 'x' is uninitialized when used here}}
161  S(int (*)[14]) : x(0), y(((x ? (y, x) : (77, y))++, sizeof(y))) {} // expected-warning {{field 'y' is uninitialized when used here}}
162  S(int (*)[15]) : x(++ref(x)) {} // expected-warning {{field 'x' is uninitialized when used here}}
163  S(int (*)[16]) : x((ref(x) += 10)) {} // expected-warning {{field 'x' is uninitialized when used here}}
164  S(int (*)[17]) : x(0), y(y ? x : x) {} // expected-warning {{field 'y' is uninitialized when used here}}
165};
166
167// Test self-references with record types.
168class A {
169  // Non-POD class.
170  public:
171    enum count { ONE, TWO, THREE };
172    int num;
173    static int count;
174    int get() const { return num; }
175    int get2() { return num; }
176    int set(int x) { num = x; return num; }
177    static int zero() { return 0; }
178
179    A() {}
180    A(A const &a) {}
181    A(int x) {}
182    A(int *x) {}
183    A(A *a) {}
184    A(A &&a) {}
185    ~A();
186    bool operator!();
187    bool operator!=(const A&);
188};
189
190bool operator!=(int, const A&);
191
192A getA() { return A(); }
193A getA(int x) { return A(); }
194A getA(A* a) { return A(); }
195A getA(A a) { return A(); }
196A moveA(A&& a) { return A(); }
197A const_refA(const A& a) { return A(); }
198
199void setupA(bool x) {
200  A a1;
201  a1.set(a1.get());
202  A a2(a1.get());
203  A a3(a1);
204  A a4(&a4);
205  A a5(a5.zero());
206  A a6(a6.ONE);
207  A a7 = getA();
208  A a8 = getA(a8.TWO);
209  A a9 = getA(&a9);
210  A a10(a10.count);
211
212  A a11(a11);  // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
213  A a12(a12.get());  // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
214  A a13(a13.num);  // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
215  A a14 = A(a14);  // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
216  A a15 = getA(a15.num);  // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
217  A a16(&a16.num);  // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
218  A a17(a17.get2());  // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
219  A a18 = x ? a18 : a17;  // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
220  A a19 = getA(x ? a19 : a17);  // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
221  A a20{a20};  // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
222  A a21 = {a21};  // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
223
224  // FIXME: Make the local uninitialized warning consistent with the global
225  // uninitialized checking.
226  A *a22 = new A(a22->count);  // expected-warning {{variable 'a22' is uninitialized when used within its own initialization}}
227  A *a23 = new A(a23->ONE);  // expected-warning {{variable 'a23' is uninitialized when used within its own initialization}}
228  A *a24 = new A(a24->TWO);  // expected-warning {{variable 'a24' is uninitialized when used within its own initialization}}
229  A *a25 = new A(a25->zero());  // expected-warning {{variable 'a25' is uninitialized when used within its own initialization}}
230
231  A *a26 = new A(a26->get());    // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
232  A *a27 = new A(a27->get2());  // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
233  A *a28 = new A(a28->num);  // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
234
235  const A a29(a29);  // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}}
236  const A a30 = a30;  // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}}
237
238  A a31 = std::move(a31);  // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}}
239  A a32 = moveA(std::move(a32));  // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}}
240  A a33 = A(std::move(a33));   // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}}
241  A a34(std::move(a34));   // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}}
242  A a35 = std::move(x ? a34 : (37, a35));  // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}}
243
244  A a36 = const_refA(a36);
245  A a37(const_refA(a37));
246
247  A a38({a38});  // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}}
248  A a39 = {a39};  // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}}
249  A a40 = A({a40});  // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}}
250
251  A a41 = !a41;  // expected-warning {{variable 'a41' is uninitialized when used within its own initialization}}
252  A a42 = !(a42);  // expected-warning {{variable 'a42' is uninitialized when used within its own initialization}}
253  A a43 = a43 != a42;  // expected-warning {{variable 'a43' is uninitialized when used within its own initialization}}
254  A a44 = a43 != a44;  // expected-warning {{variable 'a44' is uninitialized when used within its own initialization}}
255  A a45 = a45 != a45;  // expected-warning 2{{variable 'a45' is uninitialized when used within its own initialization}}
256  A a46 = 0 != a46;  // expected-warning {{variable 'a46' is uninitialized when used within its own initialization}}
257
258  A a47(a47.set(a47.num));  // expected-warning 2{{variable 'a47' is uninitialized when used within its own initialization}}
259  A a48(a47.set(a48.num));  // expected-warning {{variable 'a48' is uninitialized when used within its own initialization}}
260  A a49(a47.set(a48.num));
261}
262
263bool cond;
264
265A a1;
266A a2(a1.get());
267A a3(a1);
268A a4(&a4);
269A a5(a5.zero());
270A a6(a6.ONE);
271A a7 = getA();
272A a8 = getA(a8.TWO);
273A a9 = getA(&a9);
274A a10(a10.count);
275
276A a11(a11);  // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
277A a12(a12.get());  // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
278A a13(a13.num);  // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
279A a14 = A(a14);  // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
280A a15 = getA(a15.num);  // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
281A a16(&a16.num);  // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
282A a17(a17.get2());  // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
283A a18 = cond ? a18 : a17;  // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
284A a19 = getA(cond ? a19 : a17);  // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
285A a20{a20};  // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
286A a21 = {a21};  // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
287
288A *a22 = new A(a22->count);
289A *a23 = new A(a23->ONE);
290A *a24 = new A(a24->TWO);
291A *a25 = new A(a25->zero());
292
293A *a26 = new A(a26->get());    // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
294A *a27 = new A(a27->get2());  // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
295A *a28 = new A(a28->num);  // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
296
297const A a29(a29);  // expected-warning {{variable 'a29' is uninitialized when used within its own initialization}}
298const A a30 = a30;  // expected-warning {{variable 'a30' is uninitialized when used within its own initialization}}
299
300A a31 = std::move(a31);  // expected-warning {{variable 'a31' is uninitialized when used within its own initialization}}
301A a32 = moveA(std::move(a32));  // expected-warning {{variable 'a32' is uninitialized when used within its own initialization}}
302A a33 = A(std::move(a33));   // expected-warning {{variable 'a33' is uninitialized when used within its own initialization}}
303A a34(std::move(a34));   // expected-warning {{variable 'a34' is uninitialized when used within its own initialization}}
304A a35 = std::move(x ? a34 : (37, a35));  // expected-warning {{variable 'a35' is uninitialized when used within its own initialization}}
305
306A a36 = const_refA(a36);
307A a37(const_refA(a37));
308
309A a38({a38});  // expected-warning {{variable 'a38' is uninitialized when used within its own initialization}}
310A a39 = {a39};  // expected-warning {{variable 'a39' is uninitialized when used within its own initialization}}
311A a40 = A({a40});  // expected-warning {{variable 'a40' is uninitialized when used within its own initialization}}
312
313A a41 = !a41;  // expected-warning {{variable 'a41' is uninitialized when used within its own initialization}}
314A a42 = !(a42);  // expected-warning {{variable 'a42' is uninitialized when used within its own initialization}}
315A a43 = a43 != a42;  // expected-warning {{variable 'a43' is uninitialized when used within its own initialization}}
316A a44 = a43 != a44;  // expected-warning {{variable 'a44' is uninitialized when used within its own initialization}}
317A a45 = a45 != a45;  // expected-warning 2{{variable 'a45' is uninitialized when used within its own initialization}}
318
319A a46 = 0 != a46;  // expected-warning {{variable 'a46' is uninitialized when used within its own initialization}}
320
321A a47(a47.set(a47.num));  // expected-warning 2{{variable 'a47' is uninitialized when used within its own initialization}}
322A a48(a47.set(a48.num));  // expected-warning {{variable 'a48' is uninitialized when used within its own initialization}}
323A a49(a47.set(a48.num));
324
325class T {
326  A a, a2;
327  const A c_a;
328  A* ptr_a;
329
330  T() {}
331  T(bool (*)[1]) : a() {}
332  T(bool (*)[2]) : a2(a.get()) {}
333  T(bool (*)[3]) : a2(a) {}
334  T(bool (*)[4]) : a(&a) {}
335  T(bool (*)[5]) : a(a.zero()) {}
336  T(bool (*)[6]) : a(a.ONE) {}
337  T(bool (*)[7]) : a(getA()) {}
338  T(bool (*)[8]) : a2(getA(a.TWO)) {}
339  T(bool (*)[9]) : a(getA(&a)) {}
340  T(bool (*)[10]) : a(a.count) {}
341
342  T(bool (*)[11]) : a(a) {}  // expected-warning {{field 'a' is uninitialized when used here}}
343  T(bool (*)[12]) : a(a.get()) {}  // expected-warning {{field 'a' is uninitialized when used here}}
344  T(bool (*)[13]) : a(a.num) {}  // expected-warning {{field 'a' is uninitialized when used here}}
345  T(bool (*)[14]) : a(A(a)) {}  // expected-warning {{field 'a' is uninitialized when used here}}
346  T(bool (*)[15]) : a(getA(a.num)) {}  // expected-warning {{field 'a' is uninitialized when used here}}
347  T(bool (*)[16]) : a(&a.num) {}  // expected-warning {{field 'a' is uninitialized when used here}}
348  T(bool (*)[17]) : a(a.get2()) {}  // expected-warning {{field 'a' is uninitialized when used here}}
349  T(bool (*)[18]) : a2(cond ? a2 : a) {}  // expected-warning {{field 'a2' is uninitialized when used here}}
350  T(bool (*)[19]) : a2(cond ? a2 : a) {}  // expected-warning {{field 'a2' is uninitialized when used here}}
351  T(bool (*)[20]) : a{a} {}  // expected-warning {{field 'a' is uninitialized when used here}}
352  T(bool (*)[21]) : a({a}) {}  // expected-warning {{field 'a' is uninitialized when used here}}
353
354  T(bool (*)[22]) : ptr_a(new A(ptr_a->count)) {}
355  T(bool (*)[23]) : ptr_a(new A(ptr_a->ONE)) {}
356  T(bool (*)[24]) : ptr_a(new A(ptr_a->TWO)) {}
357  T(bool (*)[25]) : ptr_a(new A(ptr_a->zero())) {}
358
359  T(bool (*)[26]) : ptr_a(new A(ptr_a->get())) {}  // expected-warning {{field 'ptr_a' is uninitialized when used here}}
360  T(bool (*)[27]) : ptr_a(new A(ptr_a->get2())) {}  // expected-warning {{field 'ptr_a' is uninitialized when used here}}
361  T(bool (*)[28]) : ptr_a(new A(ptr_a->num)) {}  // expected-warning {{field 'ptr_a' is uninitialized when used here}}
362
363  T(bool (*)[29]) : c_a(c_a) {}  // expected-warning {{field 'c_a' is uninitialized when used here}}
364  T(bool (*)[30]) : c_a(A(c_a)) {}  // expected-warning {{field 'c_a' is uninitialized when used here}}
365
366  T(bool (*)[31]) : a(std::move(a)) {}  // expected-warning {{field 'a' is uninitialized when used here}}
367  T(bool (*)[32]) : a(moveA(std::move(a))) {}  // expected-warning {{field 'a' is uninitialized when used here}}
368  T(bool (*)[33]) : a(A(std::move(a))) {}  // expected-warning {{field 'a' is uninitialized when used here}}
369  T(bool (*)[34]) : a(A(std::move(a))) {}  // expected-warning {{field 'a' is uninitialized when used here}}
370  T(bool (*)[35]) : a2(std::move(x ? a : (37, a2))) {}  // expected-warning {{field 'a2' is uninitialized when used here}}
371
372  T(bool (*)[36]) : a(const_refA(a)) {}
373  T(bool (*)[37]) : a(A(const_refA(a))) {}
374
375  T(bool (*)[38]) : a({a}) {}  // expected-warning {{field 'a' is uninitialized when used here}}
376  T(bool (*)[39]) : a{a} {}  // expected-warning {{field 'a' is uninitialized when used here}}
377  T(bool (*)[40]) : a({a}) {}  // expected-warning {{field 'a' is uninitialized when used here}}
378
379  T(bool (*)[41]) : a(!a) {}  // expected-warning {{field 'a' is uninitialized when used here}}
380  T(bool (*)[42]) : a(!(a)) {}  // expected-warning {{field 'a' is uninitialized when used here}}
381  T(bool (*)[43]) : a(), a2(a2 != a) {}  // expected-warning {{field 'a2' is uninitialized when used here}}
382  T(bool (*)[44]) : a(), a2(a != a2) {}  // expected-warning {{field 'a2' is uninitialized when used here}}
383  T(bool (*)[45]) : a(a != a) {}  // expected-warning 2{{field 'a' is uninitialized when used here}}
384  T(bool (*)[46]) : a(0 != a) {}  // expected-warning {{field 'a' is uninitialized when used here}}
385
386  T(bool (*)[47]) : a2(a2.set(a2.num)) {}  // expected-warning 2{{field 'a2' is uninitialized when used here}}
387  T(bool (*)[48]) : a2(a.set(a2.num)) {}  // expected-warning {{field 'a2' is uninitialized when used here}}
388  T(bool (*)[49]) : a2(a.set(a.num)) {}
389
390};
391
392struct B {
393  // POD struct.
394  int x;
395  int *y;
396};
397
398B getB() { return B(); };
399B getB(int x) { return B(); };
400B getB(int *x) { return B(); };
401B getB(B *b) { return B(); };
402B moveB(B &&b) { return B(); };
403
404B* getPtrB() { return 0; };
405B* getPtrB(int x) { return 0; };
406B* getPtrB(int *x) { return 0; };
407B* getPtrB(B **b) { return 0; };
408
409void setupB(bool x) {
410  B b1;
411  B b2(b1);
412  B b3 = { 5, &b3.x };
413  B b4 = getB();
414  B b5 = getB(&b5);
415  B b6 = getB(&b6.x);
416
417  // Silence unused warning
418  (void) b2;
419  (void) b4;
420
421  B b7(b7);  // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
422  B b8 = getB(b8.x);  // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
423  B b9 = getB(b9.y);  // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
424  B b10 = getB(-b10.x);  // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
425
426  B* b11 = 0;
427  B* b12(b11);
428  B* b13 = getPtrB();
429  B* b14 = getPtrB(&b14);
430
431  (void) b12;
432  (void) b13;
433
434  B* b15 = getPtrB(b15->x);  // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
435  B* b16 = getPtrB(b16->y);  // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
436
437  B b17 = { b17.x = 5, b17.y = 0 };
438  B b18 = { b18.x + 1, b18.y };  // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
439
440  const B b19 = b19;  // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}}
441  const B b20(b20);  // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}}
442
443  B b21 = std::move(b21);  // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}}
444  B b22 = moveB(std::move(b22));  // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}}
445  B b23 = B(std::move(b23));   // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}}
446  B b24 = std::move(x ? b23 : (18, b24));  // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}}
447}
448
449B b1;
450B b2(b1);
451B b3 = { 5, &b3.x };
452B b4 = getB();
453B b5 = getB(&b5);
454B b6 = getB(&b6.x);
455
456B b7(b7);  // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
457B b8 = getB(b8.x);  // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
458B b9 = getB(b9.y);  // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
459B b10 = getB(-b10.x);  // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
460
461B* b11 = 0;
462B* b12(b11);
463B* b13 = getPtrB();
464B* b14 = getPtrB(&b14);
465
466B* b15 = getPtrB(b15->x);  // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
467B* b16 = getPtrB(b16->y);  // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
468
469B b17 = { b17.x = 5, b17.y = 0 };
470B b18 = { b18.x + 1, b18.y };  // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
471
472const B b19 = b19;  // expected-warning {{variable 'b19' is uninitialized when used within its own initialization}}
473const B b20(b20);  // expected-warning {{variable 'b20' is uninitialized when used within its own initialization}}
474
475B b21 = std::move(b21);  // expected-warning {{variable 'b21' is uninitialized when used within its own initialization}}
476B b22 = moveB(std::move(b22));  // expected-warning {{variable 'b22' is uninitialized when used within its own initialization}}
477B b23 = B(std::move(b23));   // expected-warning {{variable 'b23' is uninitialized when used within its own initialization}}
478B b24 = std::move(x ? b23 : (18, b24));  // expected-warning {{variable 'b24' is uninitialized when used within its own initialization}}
479
480class U {
481  B b1, b2;
482  B *ptr1, *ptr2;
483  const B constb = {};
484
485  U() {}
486  U(bool (*)[1]) : b1() {}
487  U(bool (*)[2]) : b2(b1) {}
488  U(bool (*)[3]) : b1{ 5, &b1.x } {}
489  U(bool (*)[4]) : b1(getB()) {}
490  U(bool (*)[5]) : b1(getB(&b1)) {}
491  U(bool (*)[6]) : b1(getB(&b1.x)) {}
492
493  U(bool (*)[7]) : b1(b1) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
494  U(bool (*)[8]) : b1(getB(b1.x)) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
495  U(bool (*)[9]) : b1(getB(b1.y)) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
496  U(bool (*)[10]) : b1(getB(-b1.x)) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
497
498  U(bool (*)[11]) : ptr1(0) {}
499  U(bool (*)[12]) : ptr1(0), ptr2(ptr1) {}
500  U(bool (*)[13]) : ptr1(getPtrB()) {}
501  U(bool (*)[14]) : ptr1(getPtrB(&ptr1)) {}
502
503  U(bool (*)[15]) : ptr1(getPtrB(ptr1->x)) {}  // expected-warning {{field 'ptr1' is uninitialized when used here}}
504  U(bool (*)[16]) : ptr2(getPtrB(ptr2->y)) {}  // expected-warning {{field 'ptr2' is uninitialized when used here}}
505
506  U(bool (*)[17]) : b1 { b1.x = 5, b1.y = 0 } {}
507  U(bool (*)[18]) : b1 { b1.x + 1, b1.y } {}  // expected-warning 2{{field 'b1' is uninitialized when used here}}
508
509  U(bool (*)[19]) : constb(constb) {}  // expected-warning {{field 'constb' is uninitialized when used here}}
510  U(bool (*)[20]) : constb(B(constb)) {}  // expected-warning {{field 'constb' is uninitialized when used here}}
511
512  U(bool (*)[21]) : b1(std::move(b1)) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
513  U(bool (*)[22]) : b1(moveB(std::move(b1))) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
514  U(bool (*)[23]) : b1(B(std::move(b1))) {}  // expected-warning {{field 'b1' is uninitialized when used here}}
515  U(bool (*)[24]) : b2(std::move(x ? b1 : (18, b2))) {}  // expected-warning {{field 'b2' is uninitialized when used here}}
516};
517
518struct C { char a[100], *e; } car = { .e = car.a };
519
520// <rdar://problem/10398199>
521namespace rdar10398199 {
522  class FooBase { protected: ~FooBase() {} };
523  class Foo : public FooBase {
524  public:
525    operator int&() const;
526  };
527  void stuff();
528  template <typename T> class FooImpl : public Foo {
529    T val;
530  public:
531    FooImpl(const T &x) : val(x) {}
532    ~FooImpl() { stuff(); }
533  };
534
535  template <typename T> FooImpl<T> makeFoo(const T& x) {
536    return FooImpl<T>(x);
537  }
538
539  void test() {
540    const Foo &x = makeFoo(42);
541    const int&y = makeFoo(42u);
542    (void)x;
543    (void)y;
544  };
545}
546
547// PR 12325 - this was a false uninitialized value warning due to
548// a broken CFG.
549int pr12325(int params) {
550  int x = ({
551    while (false)
552      ;
553    int _v = params;
554    if (false)
555      ;
556    _v; // no-warning
557  });
558  return x;
559}
560
561// Test lambda expressions with -Wuninitialized
562int test_lambda() {
563  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}}
564  return f1(1, 2);
565}
566
567namespace {
568  struct A {
569    enum { A1 };
570    static int A2() {return 5;}
571    int A3;
572    int A4() { return 5;}
573  };
574
575  struct B {
576    A a;
577  };
578
579  struct C {
580    C() {}
581    C(int x) {}
582    static A a;
583    B b;
584  };
585  A C::a = A();
586
587  // Accessing non-static members will give a warning.
588  struct D {
589    C c;
590    D(char (*)[1]) : c(c.b.a.A1) {}
591    D(char (*)[2]) : c(c.b.a.A2()) {}
592    D(char (*)[3]) : c(c.b.a.A3) {}    // expected-warning {{field 'c' is uninitialized when used here}}
593    D(char (*)[4]) : c(c.b.a.A4()) {}  // expected-warning {{field 'c' is uninitialized when used here}}
594
595    // c::a is static, so it is already initialized
596    D(char (*)[5]) : c(c.a.A1) {}
597    D(char (*)[6]) : c(c.a.A2()) {}
598    D(char (*)[7]) : c(c.a.A3) {}
599    D(char (*)[8]) : c(c.a.A4()) {}
600  };
601
602  struct E {
603    int b = 1;
604    int c = 1;
605    int a;  // This field needs to be last to prevent the cross field
606            // uninitialized warning.
607    E(char (*)[1]) : a(a ? b : c) {}  // expected-warning {{field 'a' is uninitialized when used here}}
608    E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
609    E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
610    E(char (*)[4]) : a(b ? c : (a+c)) {} // expected-warning {{field 'a' is uninitialized when used here}}
611    E(char (*)[5]) : a(b ? c : b) {}
612
613    E(char (*)[6]) : a(a ?: a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
614    E(char (*)[7]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}}
615    E(char (*)[8]) : a(a ?: c) {} // expected-warning {{field 'a' is uninitialized when used here}}
616    E(char (*)[9]) : a(b ?: c) {}
617
618    E(char (*)[10]) : a((a, a, b)) {}
619    E(char (*)[11]) : a((c + a, a + 1, b)) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
620    E(char (*)[12]) : a((b + c, c, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
621    E(char (*)[13]) : a((a, a, a, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
622    E(char (*)[14]) : a((b, c, c)) {}
623    E(char (*)[15]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}}
624    E(char (*)[16]) : a(a ?: b) {} // expected-warning {{field 'a' is uninitialized when used here}}
625  };
626
627  struct F {
628    int a;
629    F* f;
630    F(int) {}
631    F() {}
632  };
633
634  int F::*ptr = &F::a;
635  F* F::*f_ptr = &F::f;
636  struct G {
637    F f1, f2;
638    F *f3, *f4;
639    G(char (*)[1]) : f1(f1) {} // expected-warning {{field 'f1' is uninitialized when used here}}
640    G(char (*)[2]) : f2(f1) {}
641    G(char (*)[3]) : f2(F()) {}
642
643    G(char (*)[4]) : f1(f1.*ptr) {} // expected-warning {{field 'f1' is uninitialized when used here}}
644    G(char (*)[5]) : f2(f1.*ptr) {}
645
646    G(char (*)[6]) : f3(f3) {}  // expected-warning {{field 'f3' is uninitialized when used here}}
647    G(char (*)[7]) : f3(f3->*f_ptr) {} // expected-warning {{field 'f3' is uninitialized when used here}}
648    G(char (*)[8]) : f3(new F(f3->*ptr)) {} // expected-warning {{field 'f3' is uninitialized when used here}}
649  };
650
651  struct H {
652    H() : a(a) {}  // expected-warning {{field 'a' is uninitialized when used here}}
653    const A a;
654  };
655}
656
657namespace statics {
658  static int a = a; // no-warning: used to signal intended lack of initialization.
659  static int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
660  static int c = (c + c); // expected-warning 2{{variable 'c' is uninitialized when used within its own initialization}}
661  static int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
662  static int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
663
664  // Thes don't warn as they don't require the value.
665  static int g = sizeof(g);
666  int gg = g;  // Silence unneeded warning
667  static void* ptr = &ptr;
668  static int h = bar(&h);
669  static int i = boo(i);
670  static int j = far(j);
671  static int k = __alignof__(k);
672
673  static int l = k ? l : l;  // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
674  static int m = 1 + (k ? m : m);  // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
675  static int n = -n;  // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
676  static int o = std::move(o); // expected-warning {{variable 'o' is uninitialized when used within its own initialization}}
677  static const int p = std::move(p); // expected-warning {{variable 'p' is uninitialized when used within its own initialization}}
678  static int q = moved(std::move(q)); // expected-warning {{variable 'q' is uninitialized when used within its own initialization}}
679  static int r = std::move((p ? q : (18, r))); // expected-warning {{variable 'r' is uninitialized when used within its own initialization}}
680  static int s = r ?: s; // expected-warning {{variable 's' is uninitialized when used within its own initialization}}
681  static int t = t ?: s; // expected-warning {{variable 't' is uninitialized when used within its own initialization}}
682  static int u = (foo(u), s); // expected-warning {{variable 'u' is uninitialized when used within its own initialization}}
683  static int v = (u += v); // expected-warning {{variable 'v' is uninitialized when used within its own initialization}}
684  static int w = (w += 10); // expected-warning {{variable 'w' is uninitialized when used within its own initialization}}
685  static int x = x++; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
686  static int y = ((s ? (y, v) : (77, y))++, sizeof(y)); // expected-warning {{variable 'y' is uninitialized when used within its own initialization}}
687  static int z = ++ref(z); // expected-warning {{variable 'z' is uninitialized when used within its own initialization}}
688  static int aa = (ref(aa) += 10); // expected-warning {{variable 'aa' is uninitialized when used within its own initialization}}
689  static int bb = bb ? x : y; // expected-warning {{variable 'bb' is uninitialized when used within its own initialization}}
690
691
692  void test() {
693    static int a = a; // no-warning: used to signal intended lack of initialization.
694    static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
695    static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
696    static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
697    static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
698    static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
699
700    // Thes don't warn as they don't require the value.
701    static int g = sizeof(g);
702    static void* ptr = &ptr;
703    static int h = bar(&h);
704    static int i = boo(i);
705    static int j = far(j);
706    static int k = __alignof__(k);
707
708    static int l = k ? l : l;  // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
709    static int m = 1 + (k ? m : m);  // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
710    static int n = -n;  // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
711    static int o = std::move(o);  // expected-warning {{static variable 'o' is suspiciously used within its own initialization}}
712    static const int p = std::move(p);  // expected-warning {{static variable 'p' is suspiciously used within its own initialization}}
713    static int q = moved(std::move(q));  // expected-warning {{static variable 'q' is suspiciously used within its own initialization}}
714    static int r = std::move((p ? q : (18, r)));  // expected-warning {{static variable 'r' is suspiciously used within its own initialization}}
715    static int s = r ?: s;  // expected-warning {{static variable 's' is suspiciously used within its own initialization}}
716    static int t = t ?: s;  // expected-warning {{static variable 't' is suspiciously used within its own initialization}}
717    static int u = (foo(u), s);  // expected-warning {{static variable 'u' is suspiciously used within its own initialization}}
718    static int v = (u += v);  // expected-warning {{static variable 'v' is suspiciously used within its own initialization}}
719    static int w = (w += 10);  // expected-warning {{static variable 'w' is suspiciously used within its own initialization}}
720    static int x = x++;  // expected-warning {{static variable 'x' is suspiciously used within its own initialization}}
721    static int y = ((s ? (y, v) : (77, y))++, sizeof(y));  // expected-warning {{static variable 'y' is suspiciously used within its own initialization}}
722    static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}}
723    static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}}
724    static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}}
725
726    for (;;) {
727      static int a = a; // no-warning: used to signal intended lack of initialization.
728      static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
729      static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
730      static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
731      static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
732      static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
733
734      // Thes don't warn as they don't require the value.
735      static int g = sizeof(g);
736      static void* ptr = &ptr;
737      static int h = bar(&h);
738      static int i = boo(i);
739      static int j = far(j);
740      static int k = __alignof__(k);
741
742      static int l = k ? l : l;  // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
743      static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
744      static int n = -n;  // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
745      static int o = std::move(o);  // expected-warning {{static variable 'o' is suspiciously used within its own initialization}}
746      static const int p = std::move(p);  // expected-warning {{static variable 'p' is suspiciously used within its own initialization}}
747      static int q = moved(std::move(q));  // expected-warning {{static variable 'q' is suspiciously used within its own initialization}}
748      static int r = std::move((p ? q : (18, r)));  // expected-warning {{static variable 'r' is suspiciously used within its own initialization}}
749      static int s = r ?: s;  // expected-warning {{static variable 's' is suspiciously used within its own initialization}}
750      static int t = t ?: s;  // expected-warning {{static variable 't' is suspiciously used within its own initialization}}
751      static int u = (foo(u), s);  // expected-warning {{static variable 'u' is suspiciously used within its own initialization}}
752      static int v = (u += v);  // expected-warning {{static variable 'v' is suspiciously used within its own initialization}}
753      static int w = (w += 10);  // expected-warning {{static variable 'w' is suspiciously used within its own initialization}}
754      static int x = x++;  // expected-warning {{static variable 'x' is suspiciously used within its own initialization}}
755      static int y = ((s ? (y, v) : (77, y))++, sizeof(y));  // expected-warning {{static variable 'y' is suspiciously used within its own initialization}}
756      static int z = ++ref(z); // expected-warning {{static variable 'z' is suspiciously used within its own initialization}}
757      static int aa = (ref(aa) += 10); // expected-warning {{static variable 'aa' is suspiciously used within its own initialization}}
758      static int bb = bb ? x : y; // expected-warning {{static variable 'bb' is suspiciously used within its own initialization}}
759    }
760  }
761}
762
763namespace in_class_initializers {
764  struct S {
765    S() : a(a + 1) {} // expected-warning{{field 'a' is uninitialized when used here}}
766    int a = 42; // Note: because a is in a member initializer list, this initialization is ignored.
767  };
768
769  struct T {
770    T() : b(a + 1) {} // No-warning.
771    int a = 42;
772    int b;
773  };
774
775  struct U {
776    U() : a(b + 1), b(a + 1) {} // expected-warning{{field 'b' is uninitialized when used here}}
777    int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored.
778    int b = 1;
779  };
780}
781
782namespace references {
783  int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
784  int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
785  int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
786  int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
787  int &e = d ?: e; // expected-warning{{reference 'e' is not yet bound to a value when used within its own initialization}}
788  int &f = f ?: d; // expected-warning{{reference 'f' is not yet bound to a value when used within its own initialization}}
789
790  int &return_ref1(int);
791  int &return_ref2(int&);
792
793  int &g = return_ref1(g); // expected-warning{{reference 'g' is not yet bound to a value when used within its own initialization}}
794  int &h = return_ref2(h); // expected-warning{{reference 'h' is not yet bound to a value when used within its own initialization}}
795
796  struct S {
797    S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}}
798    int &a;
799  };
800
801  void test() {
802    int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
803    int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
804    int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
805    int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
806  }
807
808  struct T {
809    T() // expected-note{{during field initialization in this constructor}}
810     : a(b), b(a) {} // expected-warning{{reference 'b' is not yet bound to a value when used here}}
811    int &a, &b;
812    int &c = c; // expected-warning{{reference 'c' is not yet bound to a value when used here}}
813  };
814
815  int x;
816  struct U {
817    U() : b(a) {} // No-warning.
818    int &a = x;
819    int &b;
820  };
821}
822
823namespace operators {
824  struct A {
825    A(bool);
826    bool operator==(A);
827  };
828
829  A makeA();
830
831  A a1 = a1 = makeA();  // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
832  A a2 = a2 == a1;  // expected-warning{{variable 'a2' is uninitialized when used within its own initialization}}
833  A a3 = a2 == a3;  // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}}
834
835  int x = x = 5;
836}
837
838namespace lambdas {
839  struct A {
840    template<typename T> A(T) {}
841    int x;
842  };
843  A a0([] { return a0.x; }); // ok
844  void f() {
845    A a1([=] { return a1.x; }); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
846    A a2([&] { return a2.x; }); // ok
847  }
848}
849
850namespace record_fields {
851  bool x;
852  struct A {
853    A() {}
854    A get();
855    static A num();
856    static A copy(A);
857    static A something(A&);
858  };
859
860  A ref(A&);
861  A const_ref(const A&);
862  A pointer(A*);
863  A normal(A);
864  A rref(A&&);
865
866  struct B {
867    A a;
868    B(char (*)[1]) : a(a) {}  // expected-warning {{uninitialized}}
869    B(char (*)[2]) : a(a.get()) {}  // expected-warning {{uninitialized}}
870    B(char (*)[3]) : a(a.num()) {}
871    B(char (*)[4]) : a(a.copy(a)) {}  // expected-warning {{uninitialized}}
872    B(char (*)[5]) : a(a.something(a)) {}
873    B(char (*)[6]) : a(ref(a)) {}
874    B(char (*)[7]) : a(const_ref(a)) {}
875    B(char (*)[8]) : a(pointer(&a)) {}
876    B(char (*)[9]) : a(normal(a)) {}  // expected-warning {{uninitialized}}
877    B(char (*)[10]) : a(std::move(a)) {}  // expected-warning {{uninitialized}}
878    B(char (*)[11]) : a(A(std::move(a))) {}  // expected-warning {{uninitialized}}
879    B(char (*)[12]) : a(rref(std::move(a))) {}  // expected-warning {{uninitialized}}
880    B(char (*)[13]) : a(std::move(x ? a : (25, a))) {}  // expected-warning 2{{uninitialized}}
881  };
882  struct C {
883    C() {} // expected-note9{{in this constructor}}
884    A a1 = a1;  // expected-warning {{uninitialized}}
885    A a2 = a2.get();  // expected-warning {{uninitialized}}
886    A a3 = a3.num();
887    A a4 = a4.copy(a4);  // expected-warning {{uninitialized}}
888    A a5 = a5.something(a5);
889    A a6 = ref(a6);
890    A a7 = const_ref(a7);
891    A a8 = pointer(&a8);
892    A a9 = normal(a9);  // expected-warning {{uninitialized}}
893    const A a10 = a10;  // expected-warning {{uninitialized}}
894    A a11 = std::move(a11);  // expected-warning {{uninitialized}}
895    A a12 = A(std::move(a12));  // expected-warning {{uninitialized}}
896    A a13 = rref(std::move(a13));  // expected-warning {{uninitialized}}
897    A a14 = std::move(x ? a13 : (22, a14));  // expected-warning {{uninitialized}}
898  };
899  struct D {  // expected-note9{{in the implicit default constructor}}
900    A a1 = a1;  // expected-warning {{uninitialized}}
901    A a2 = a2.get();  // expected-warning {{uninitialized}}
902    A a3 = a3.num();
903    A a4 = a4.copy(a4);  // expected-warning {{uninitialized}}
904    A a5 = a5.something(a5);
905    A a6 = ref(a6);
906    A a7 = const_ref(a7);
907    A a8 = pointer(&a8);
908    A a9 = normal(a9);  // expected-warning {{uninitialized}}
909    const A a10 = a10;  // expected-warning {{uninitialized}}
910    A a11 = std::move(a11);  // expected-warning {{uninitialized}}
911    A a12 = A(std::move(a12));  // expected-warning {{uninitialized}}
912    A a13 = rref(std::move(a13));  // expected-warning {{uninitialized}}
913    A a14 = std::move(x ? a13 : (22, a14));  // expected-warning {{uninitialized}}
914  };
915  D d;
916  struct E {
917    A a1 = a1;
918    A a2 = a2.get();
919    A a3 = a3.num();
920    A a4 = a4.copy(a4);
921    A a5 = a5.something(a5);
922    A a6 = ref(a6);
923    A a7 = const_ref(a7);
924    A a8 = pointer(&a8);
925    A a9 = normal(a9);
926    const A a10 = a10;
927    A a11 = std::move(a11);
928    A a12 = A(std::move(a12));
929    A a13 = rref(std::move(a13));
930    A a14 = std::move(x ? a13 : (22, a14));
931  };
932}
933
934namespace cross_field_warnings {
935  struct A {
936    int a, b;
937    A() {}
938    A(char (*)[1]) : b(a) {}  // expected-warning{{field 'a' is uninitialized when used here}}
939    A(char (*)[2]) : a(b) {}  // expected-warning{{field 'b' is uninitialized when used here}}
940  };
941
942  struct B {
943    int a = b;  // expected-warning{{field 'b' is uninitialized when used here}}
944    int b;
945    B() {} // expected-note{{during field initialization in this constructor}}
946  };
947
948  struct C {
949    int a;
950    int b = a;  // expected-warning{{field 'a' is uninitialized when used here}}
951    C(char (*)[1]) : a(5) {}
952    C(char (*)[2]) {} // expected-note{{during field initialization in this constructor}}
953  };
954
955  struct D {
956    int a;
957    int &b;
958    int &c = a;
959    int d = b;
960    D() : b(a) {}
961  };
962
963  struct E {
964    int a;
965    int get();
966    static int num();
967    E() {}
968    E(int) {}
969  };
970
971  struct F {
972    int a;
973    E e;
974    int b;
975    F(char (*)[1]) : a(e.get()) {}  // expected-warning{{field 'e' is uninitialized when used here}}
976    F(char (*)[2]) : a(e.num()) {}
977    F(char (*)[3]) : e(a) {}  // expected-warning{{field 'a' is uninitialized when used here}}
978    F(char (*)[4]) : a(4), e(a) {}
979    F(char (*)[5]) : e(b) {}  // expected-warning{{field 'b' is uninitialized when used here}}
980    F(char (*)[6]) : e(b), b(4) {}  // expected-warning{{field 'b' is uninitialized when used here}}
981  };
982
983  struct G {
984    G(const A&) {};
985  };
986
987  struct H {
988    A a1;
989    G g;
990    A a2;
991    H() : g(a1) {}
992    H(int) : g(a2) {}
993  };
994
995  struct I {
996    I(int*) {}
997  };
998
999  struct J : public I {
1000    int *a;
1001    int *b;
1002    int c;
1003    J() : I((a = new int(5))), b(a), c(*a) {}
1004  };
1005
1006  struct K {
1007    int a = (b = 5);
1008    int b = b + 5;
1009  };
1010
1011  struct L {
1012    int a = (b = 5);
1013    int b = b + 5;  // expected-warning{{field 'b' is uninitialized when used here}}
1014    L() : a(5) {}  // expected-note{{during field initialization in this constructor}}
1015  };
1016
1017  struct M { };
1018
1019  struct N : public M {
1020    int a;
1021    int b;
1022    N() : b(a) { }  // expected-warning{{field 'a' is uninitialized when used here}}
1023  };
1024
1025  struct O {
1026    int x = 42;
1027    int get() { return x; }
1028  };
1029
1030  struct P {
1031    O o;
1032    int x = o.get();
1033    P() : x(o.get()) { }
1034  };
1035
1036  struct Q {
1037    int a;
1038    int b;
1039    int &c;
1040    Q() :
1041      a(c = 5),  // expected-warning{{reference 'c' is not yet bound to a value when used here}}
1042      b(c),  // expected-warning{{reference 'c' is not yet bound to a value when used here}}
1043      c(a) {}
1044  };
1045
1046  struct R {
1047    int a;
1048    int b;
1049    int c;
1050    int d = a + b + c;
1051    R() : a(c = 5), b(c), c(a) {}
1052  };
1053
1054  // FIXME: Use the CFG-based analysis to give a sometimes uninitialized
1055  // warning on y.
1056  struct T {
1057    int x;
1058    int y;
1059    T(bool b)
1060        : x(b ? (y = 5) : (1 + y)),  // expected-warning{{field 'y' is uninitialized when used here}}
1061          y(y + 1) {}
1062    T(int b)
1063        : x(!b ? (1 + y) : (y = 5)),  // expected-warning{{field 'y' is uninitialized when used here}}
1064          y(y + 1) {}
1065  };
1066
1067}
1068
1069namespace base_class {
1070  struct A {
1071    A (int) {}
1072  };
1073
1074  struct B : public A {
1075    int x;
1076    B() : A(x) {}   // expected-warning{{field 'x' is uninitialized when used here}}
1077  };
1078
1079  struct C : public A {
1080    int x;
1081    int y;
1082    C() : A(y = 4), x(y) {}
1083  };
1084}
1085
1086namespace delegating_constructor {
1087  struct A {
1088    A(int);
1089    A(int&, int);
1090
1091    A(char (*)[1]) : A(x) {}
1092    // expected-warning@-1 {{field 'x' is uninitialized when used here}}
1093    A(char (*)[2]) : A(x, x) {}
1094    // expected-warning@-1 {{field 'x' is uninitialized when used here}}
1095
1096    A(char (*)[3]) : A(x, 0) {}
1097
1098    int x;
1099  };
1100}
1101
1102namespace init_list {
1103  int num = 5;
1104  struct A { int i1, i2; };
1105  struct B { A a1, a2; };
1106
1107  A a1{1,2};
1108  A a2{a2.i1 + 2};  // expected-warning{{uninitialized}}
1109  A a3 = {a3.i1 + 2};  // expected-warning{{uninitialized}}
1110  A a4 = A{a4.i2 + 2};  // expected-warning{{uninitialized}}
1111
1112  B b1 = { {}, {} };
1113  B b2 = { {}, b2.a1 };
1114  B b3 = { b3.a1 };  // expected-warning{{uninitialized}}
1115  B b4 = { {}, b4.a2} ;  // expected-warning{{uninitialized}}
1116  B b5 = { b5.a2 };  // expected-warning{{uninitialized}}
1117
1118  B b6 = { {b6.a1.i1} };  // expected-warning{{uninitialized}}
1119  B b7 = { {0, b7.a1.i1} };
1120  B b8 = { {}, {b8.a1.i1} };
1121  B b9 = { {}, {0, b9.a1.i1} };
1122
1123  B b10 = { {b10.a1.i2} };  // expected-warning{{uninitialized}}
1124  B b11 = { {0, b11.a1.i2} };  // expected-warning{{uninitialized}}
1125  B b12 = { {}, {b12.a1.i2} };
1126  B b13 = { {}, {0, b13.a1.i2} };
1127
1128  B b14 = { {b14.a2.i1} };  // expected-warning{{uninitialized}}
1129  B b15 = { {0, b15.a2.i1} };  // expected-warning{{uninitialized}}
1130  B b16 = { {}, {b16.a2.i1} };  // expected-warning{{uninitialized}}
1131  B b17 = { {}, {0, b17.a2.i1} };
1132
1133  B b18 = { {b18.a2.i2} };  // expected-warning{{uninitialized}}
1134  B b19 = { {0, b19.a2.i2} };  // expected-warning{{uninitialized}}
1135  B b20 = { {}, {b20.a2.i2} };  // expected-warning{{uninitialized}}
1136  B b21 = { {}, {0, b21.a2.i2} };  // expected-warning{{uninitialized}}
1137
1138  B b22 = { {b18.a2.i2 + 5} };
1139
1140  struct C {int a; int& b; int c; };
1141  C c1 = { 0, num, 0 };
1142  C c2 = { 1, num, c2.b };
1143  C c3 = { c3.b, num };  // expected-warning{{uninitialized}}
1144  C c4 = { 0, c4.b, 0 };  // expected-warning{{uninitialized}}
1145  C c5 = { 0, c5.c, 0 };
1146  C c6 = { c6.b, num, 0 };  // expected-warning{{uninitialized}}
1147  C c7 = { 0, c7.a, 0 };
1148
1149  struct D {int &a; int &b; };
1150  D d1 = { num, num };
1151  D d2 = { num, d2.a };
1152  D d3 = { d3.b, num };  // expected-warning{{uninitialized}}
1153
1154  // Same as above in member initializer form.
1155  struct Awrapper {
1156    A a1{1,2};
1157    A a2{a2.i1 + 2};  // expected-warning{{uninitialized}}
1158    A a3 = {a3.i1 + 2};  // expected-warning{{uninitialized}}
1159    A a4 = A{a4.i2 + 2};  // expected-warning{{uninitialized}}
1160    Awrapper() {}  // expected-note 3{{in this constructor}}
1161    Awrapper(int) :
1162      a1{1,2},
1163      a2{a2.i1 + 2},  // expected-warning{{uninitialized}}
1164      a3{a3.i1 + 2},  // expected-warning{{uninitialized}}
1165      a4{a4.i2 + 2}  // expected-warning{{uninitialized}}
1166    {}
1167  };
1168
1169  struct Bwrapper {
1170    B b1 = { {}, {} };
1171    B b2 = { {}, b2.a1 };
1172    B b3 = { b3.a1 };  // expected-warning{{uninitialized}}
1173    B b4 = { {}, b4.a2} ;  // expected-warning{{uninitialized}}
1174    B b5 = { b5.a2 };  // expected-warning{{uninitialized}}
1175
1176    B b6 = { {b6.a1.i1} };  // expected-warning{{uninitialized}}
1177    B b7 = { {0, b7.a1.i1} };
1178    B b8 = { {}, {b8.a1.i1} };
1179    B b9 = { {}, {0, b9.a1.i1} };
1180
1181    B b10 = { {b10.a1.i2} };  // expected-warning{{uninitialized}}
1182    B b11 = { {0, b11.a1.i2} };  // expected-warning{{uninitialized}}
1183    B b12 = { {}, {b12.a1.i2} };
1184    B b13 = { {}, {0, b13.a1.i2} };
1185
1186    B b14 = { {b14.a2.i1} };  // expected-warning{{uninitialized}}
1187    B b15 = { {0, b15.a2.i1} };  // expected-warning{{uninitialized}}
1188    B b16 = { {}, {b16.a2.i1} };  // expected-warning{{uninitialized}}
1189    B b17 = { {}, {0, b17.a2.i1} };
1190
1191    B b18 = { {b18.a2.i2} };  // expected-warning{{uninitialized}}
1192    B b19 = { {0, b19.a2.i2} };  // expected-warning{{uninitialized}}
1193    B b20 = { {}, {b20.a2.i2} };  // expected-warning{{uninitialized}}
1194    B b21 = { {}, {0, b21.a2.i2} };  // expected-warning{{uninitialized}}
1195
1196    B b22 = { {b18.a2.i2 + 5} };
1197    Bwrapper() {}  // expected-note 13{{in this constructor}}
1198    Bwrapper(int) :
1199      b1{ {}, {} },
1200      b2{ {}, b2.a1 },
1201      b3{ b3.a1 },  // expected-warning{{uninitialized}}
1202      b4{ {}, b4.a2}, // expected-warning{{uninitialized}}
1203      b5{ b5.a2 },  // expected-warning{{uninitialized}}
1204
1205      b6{ {b6.a1.i1} },  // expected-warning{{uninitialized}}
1206      b7{ {0, b7.a1.i1} },
1207      b8{ {}, {b8.a1.i1} },
1208      b9{ {}, {0, b9.a1.i1} },
1209
1210      b10{ {b10.a1.i2} },  // expected-warning{{uninitialized}}
1211      b11{ {0, b11.a1.i2} },  // expected-warning{{uninitialized}}
1212      b12{ {}, {b12.a1.i2} },
1213      b13{ {}, {0, b13.a1.i2} },
1214
1215      b14{ {b14.a2.i1} },  // expected-warning{{uninitialized}}
1216      b15{ {0, b15.a2.i1} },  // expected-warning{{uninitialized}}
1217      b16{ {}, {b16.a2.i1} },  // expected-warning{{uninitialized}}
1218      b17{ {}, {0, b17.a2.i1} },
1219
1220      b18{ {b18.a2.i2} },  // expected-warning{{uninitialized}}
1221      b19{ {0, b19.a2.i2} },  // expected-warning{{uninitialized}}
1222      b20{ {}, {b20.a2.i2} },  // expected-warning{{uninitialized}}
1223      b21{ {}, {0, b21.a2.i2} },  // expected-warning{{uninitialized}}
1224
1225      b22{ {b18.a2.i2 + 5} }
1226    {}
1227  };
1228
1229  struct Cwrapper {
1230    C c1 = { 0, num, 0 };
1231    C c2 = { 1, num, c2.b };
1232    C c3 = { c3.b, num };  // expected-warning{{uninitialized}}
1233    C c4 = { 0, c4.b, 0 };  // expected-warning{{uninitialized}}
1234    C c5 = { 0, c5.c, 0 };
1235    C c6 = { c6.b, num, 0 };  // expected-warning{{uninitialized}}
1236    C c7 = { 0, c7.a, 0 };
1237
1238    Cwrapper() {} // expected-note 3{{in this constructor}}
1239    Cwrapper(int) :
1240      c1{ 0, num, 0 },
1241      c2{ 1, num, c2.b },
1242      c3{ c3.b, num },  // expected-warning{{uninitialized}}
1243      c4{ 0, c4.b, 0 },  // expected-warning{{uninitialized}}
1244      c5{ 0, c5.c, 0 },
1245      c6{ c6.b, num, 0 },  // expected-warning{{uninitialized}}
1246      c7{ 0, c7.a, 0 }
1247    {}
1248  };
1249
1250  struct Dwrapper {
1251    D d1 = { num, num };
1252    D d2 = { num, d2.a };
1253    D d3 = { d3.b, num }; // expected-warning{{uninitialized}}
1254    Dwrapper() {}  // expected-note{{in this constructor}}
1255    Dwrapper(int) :
1256      d1{ num, num },
1257      d2{ num, d2.a },
1258      d3{ d3.b, num } // expected-warning{{uninitialized}}
1259    {}
1260  };
1261}
1262
1263namespace template_class {
1264class Foo {
1265 public:
1266    int *Create() { return nullptr; }
1267};
1268
1269template <typename T>
1270class A {
1271public:
1272  // Don't warn on foo here.
1273  A() : ptr(foo->Create()) {}
1274
1275private:
1276  Foo *foo = new Foo;
1277  int *ptr;
1278};
1279
1280template <typename T>
1281class B {
1282public:
1283  // foo is uninitialized here, but class B is never instantiated.
1284  B() : ptr(foo->Create()) {}
1285
1286private:
1287  Foo *foo;
1288  int *ptr;
1289};
1290
1291template <typename T>
1292class C {
1293public:
1294  C() : ptr(foo->Create()) {}
1295  // expected-warning@-1 {{field 'foo' is uninitialized when used here}}
1296private:
1297  Foo *foo;
1298  int *ptr;
1299};
1300
1301C<int> c;
1302// expected-note@-1 {{in instantiation of member function 'template_class::C<int>::C' requested here}}
1303
1304}
1305
1306namespace base_class_access {
1307struct A {
1308  A();
1309  A(int);
1310
1311  int i;
1312  int foo();
1313
1314  static int bar();
1315};
1316
1317struct B : public A {
1318  B(int (*)[1]) : A() {}
1319  B(int (*)[2]) : A(bar()) {}
1320
1321  B(int (*)[3]) : A(i) {}
1322  // expected-warning@-1 {{base class 'base_class_access::A' is uninitialized when used here to access 'base_class_access::A::i'}}
1323
1324  B(int (*)[4]) : A(foo()) {}
1325  // expected-warning@-1 {{base_class_access::A' is uninitialized when used here to access 'base_class_access::A::foo'}}
1326};
1327
1328struct C {
1329  C(int) {}
1330};
1331
1332struct D : public C, public A {
1333  D(int (*)[1]) : C(0) {}
1334  D(int (*)[2]) : C(bar()) {}
1335
1336  D(int (*)[3]) : C(i) {}
1337  // expected-warning@-1 {{base class 'base_class_access::A' is uninitialized when used here to access 'base_class_access::A::i'}}
1338
1339  D(int (*)[4]) : C(foo()) {}
1340  // expected-warning@-1 {{base_class_access::A' is uninitialized when used here to access 'base_class_access::A::foo'}}
1341};
1342
1343}
1344
1345namespace value {
1346template <class T> T move(T t);
1347template <class T> T notmove(T t);
1348}
1349namespace lvalueref {
1350template <class T> T move(T& t);
1351template <class T> T notmove(T& t);
1352}
1353namespace rvalueref {
1354template <class T> T move(T&& t);
1355template <class T> T notmove(T&& t);
1356}
1357
1358namespace move_test {
1359int a1 = std::move(a1); // expected-warning {{uninitialized}}
1360int a2 = value::move(a2); // expected-warning {{uninitialized}}
1361int a3 = value::notmove(a3); // expected-warning {{uninitialized}}
1362int a4 = lvalueref::move(a4);
1363int a5 = lvalueref::notmove(a5);
1364int a6 = rvalueref::move(a6);
1365int a7 = rvalueref::notmove(a7);
1366
1367void test() {
1368  int a1 = std::move(a1); // expected-warning {{uninitialized}}
1369  int a2 = value::move(a2); // expected-warning {{uninitialized}}
1370  int a3 = value::notmove(a3); // expected-warning {{uninitialized}}
1371  int a4 = lvalueref::move(a4);
1372  int a5 = lvalueref::notmove(a5);
1373  int a6 = rvalueref::move(a6);
1374  int a7 = rvalueref::notmove(a7);
1375}
1376
1377class A {
1378  int a;
1379  A(int (*) [1]) : a(std::move(a)) {} // expected-warning {{uninitialized}}
1380  A(int (*) [2]) : a(value::move(a)) {} // expected-warning {{uninitialized}}
1381  A(int (*) [3]) : a(value::notmove(a)) {} // expected-warning {{uninitialized}}
1382  A(int (*) [4]) : a(lvalueref::move(a)) {}
1383  A(int (*) [5]) : a(lvalueref::notmove(a)) {}
1384  A(int (*) [6]) : a(rvalueref::move(a)) {}
1385  A(int (*) [7]) : a(rvalueref::notmove(a)) {}
1386};
1387}
1388