1 // RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -Wno-unused-value -std=c++11 -verify %s
8 // Test self-references within initializers which are guaranteed to be
10 int a = a; // no-warning: used to signal intended lack of initialization.
11 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
12 int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}}
13 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
14 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
16 // Thes don't warn as they don't require the value.
22 int k = __alignof__(k);
24 int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
25 int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
26 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
29 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}}
36 // Thes don't warn as they don't require the value.
42 int k = __alignof__(k);
44 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}}
46 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
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}}
56 // Thes don't warn as they don't require the value.
62 int k = __alignof__(k);
64 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}}
66 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
70 // Test self-references with record types.
74 enum count { ONE, TWO, THREE };
77 int get() const { return num; }
78 int get2() { return num; }
79 void set(int x) { num = x; }
80 static int zero() { return 0; }
90 A getA() { return A(); }
91 A getA(int x) { return A(); }
92 A getA(A* a) { return A(); }
93 A getA(A a) { return A(); }
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}}
114 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
115 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}}
117 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}}
120 // FIXME: Make the local uninitialized warning consistent 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}}
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}}
145 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
146 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
147 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
148 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
149 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
150 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
151 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
152 A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
153 A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
154 A a20{a20}; // expected-warning {{variable 'a20' is uninitialized when used within its own initialization}}
155 A a21 = {a21}; // expected-warning {{variable 'a21' is uninitialized when used within its own initialization}}
157 A *a22 = new A(a22->count);
158 A *a23 = new A(a23->ONE);
159 A *a24 = new A(a24->TWO);
160 A *a25 = new A(a25->zero());
162 A *a26 = new A(a26->get()); // expected-warning {{variable 'a26' is uninitialized when used within its own initialization}}
163 A *a27 = new A(a27->get2()); // expected-warning {{variable 'a27' is uninitialized when used within its own initialization}}
164 A *a28 = new A(a28->num); // expected-warning {{variable 'a28' is uninitialized when used within its own initialization}}
172 B getB() { return B(); };
173 B getB(int x) { return B(); };
174 B getB(int *x) { return B(); };
175 B getB(B *b) { return B(); };
177 B* getPtrB() { return 0; };
178 B* getPtrB(int x) { return 0; };
179 B* getPtrB(int *x) { return 0; };
180 B* getPtrB(B **b) { return 0; };
190 // Silence unused warning
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}}
197 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
202 B* b14 = getPtrB(&b14);
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}}
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}}
221 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
222 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
223 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
224 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
229 B* b14 = getPtrB(&b14);
231 B* b15 = getPtrB(b15->x); // expected-warning {{variable 'b15' is uninitialized when used within its own initialization}}
232 B* b16 = getPtrB(b16->y); // expected-warning {{variable 'b16' is uninitialized when used within its own initialization}}
234 B b17 = { b17.x = 5, b17.y = 0 };
235 B b18 = { b18.x + 1, b18.y }; // expected-warning 2{{variable 'b18' is uninitialized when used within its own initialization}}
238 // Also test similar constructs in a field's initializer.
243 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}}
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)) {}
258 struct C { char a[100], *e; } car = { .e = car.a };
260 // <rdar://problem/10398199>
261 namespace rdar10398199 {
262 class FooBase { protected: ~FooBase() {} };
263 class Foo : public FooBase {
265 operator int&() const;
268 template <typename T> class FooImpl : public Foo {
271 FooImpl(const T &x) : val(x) {}
272 ~FooImpl() { stuff(); }
275 template <typename T> FooImpl<T> makeFoo(const T& x) {
276 return FooImpl<T>(x);
280 const Foo &x = makeFoo(42);
281 const int&y = makeFoo(42u);
287 // PR 12325 - this was a false uninitialized value warning due to
289 int pr12325(int params) {
301 // Test lambda expressions with -Wuninitialized
303 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}}
310 static int A2() {return 5;}
312 int A4() { return 5;}
327 // Accessing non-static members will give a warning.
330 D(char (*)[1]) : c(c.b.a.A1) {}
331 D(char (*)[2]) : c(c.b.a.A2()) {}
332 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}}
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()) {}
345 int a; // This field needs to be last to prevent the cross field
346 // uninitialized warning.
347 E(char (*)[1]) : a(a ? b : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
348 E(char (*)[2]) : a(b ? a : a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
349 E(char (*)[3]) : a(b ? (a) : c) {} // expected-warning {{field 'a' is uninitialized when used here}}
350 E(char (*)[4]) : a(b ? c : (a+c)) {} // expected-warning {{field 'a' is uninitialized when used here}}
351 E(char (*)[5]) : a(b ? c : b) {}
353 E(char (*)[6]) : a(a ?: a) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
354 E(char (*)[7]) : a(b ?: a) {} // expected-warning {{field 'a' is uninitialized when used here}}
355 E(char (*)[8]) : a(a ?: c) {} // expected-warning {{field 'a' is uninitialized when used here}}
356 E(char (*)[9]) : a(b ?: c) {}
358 E(char (*)[10]) : a((a, a, b)) {}
359 E(char (*)[11]) : a((c + a, a + 1, b)) {} // expected-warning 2{{field 'a' is uninitialized when used here}}
360 E(char (*)[12]) : a((b + c, c, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
361 E(char (*)[13]) : a((a, a, a, a)) {} // expected-warning {{field 'a' is uninitialized when used here}}
362 E(char (*)[14]) : a((b, c, c)) {}
373 F* F::*f_ptr = &F::f;
377 G(char (*)[1]) : f1(f1) {} // expected-warning {{field 'f1' is uninitialized when used here}}
378 G(char (*)[2]) : f2(f1) {}
379 G(char (*)[3]) : f2(F()) {}
381 G(char (*)[4]) : f1(f1.*ptr) {} // expected-warning {{field 'f1' is uninitialized when used here}}
382 G(char (*)[5]) : f2(f1.*ptr) {}
384 G(char (*)[6]) : f3(f3) {} // expected-warning {{field 'f3' is uninitialized when used here}}
385 G(char (*)[7]) : f3(f3->*f_ptr) {} // expected-warning {{field 'f3' is uninitialized when used here}}
386 G(char (*)[8]) : f3(new F(f3->*ptr)) {} // expected-warning {{field 'f3' is uninitialized when used here}}
391 static int a = a; // no-warning: used to signal intended lack of initialization.
392 static int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
393 static int c = (c + c); // expected-warning 2{{variable 'c' is uninitialized when used within its own initialization}}
394 static int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
395 static int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
397 // Thes don't warn as they don't require the value.
398 static int g = sizeof(g);
399 int gg = g; // Silence unneeded warning
400 static void* ptr = &ptr;
401 static int h = bar(&h);
402 static int i = boo(i);
403 static int j = far(j);
404 static int k = __alignof__(k);
406 static int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
407 static int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
408 static int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
411 static int a = a; // no-warning: used to signal intended lack of initialization.
412 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
413 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
414 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
415 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
416 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
418 // Thes don't warn as they don't require the value.
419 static int g = sizeof(g);
420 static void* ptr = &ptr;
421 static int h = bar(&h);
422 static int i = boo(i);
423 static int j = far(j);
424 static int k = __alignof__(k);
426 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
427 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
428 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
430 static int a = a; // no-warning: used to signal intended lack of initialization.
431 static int b = b + 1; // expected-warning {{static variable 'b' is suspiciously used within its own initialization}}
432 static int c = (c + c); // expected-warning 2{{static variable 'c' is suspiciously used within its own initialization}}
433 static int d = ({ d + d ;}); // expected-warning 2{{static variable 'd' is suspiciously used within its own initialization}}
434 static int e = static_cast<long>(e) + 1; // expected-warning {{static variable 'e' is suspiciously used within its own initialization}}
435 static int f = foo(f); // expected-warning {{static variable 'f' is suspiciously used within its own initialization}}
437 // Thes don't warn as they don't require the value.
438 static int g = sizeof(g);
439 static void* ptr = &ptr;
440 static int h = bar(&h);
441 static int i = boo(i);
442 static int j = far(j);
443 static int k = __alignof__(k);
445 static int l = k ? l : l; // expected-warning 2{{static variable 'l' is suspiciously used within its own initialization}}
446 static int m = 1 + (k ? m : m); // expected-warning 2{{static variable 'm' is suspiciously used within its own initialization}}
447 static int n = -n; // expected-warning {{static variable 'n' is suspiciously used within its own initialization}}
452 namespace in_class_initializers {
454 S() : a(a + 1) {} // expected-warning{{field 'a' is uninitialized when used here}}
455 int a = 42; // Note: because a is in a member initializer list, this initialization is ignored.
459 T() : b(a + 1) {} // No-warning.
465 U() : a(b + 1), b(a + 1) {} // expected-warning{{field 'b' is uninitialized when used here}}
466 int a = 42; // Note: because a and b are in the member initializer list, these initializers are ignored.
471 namespace references {
472 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
473 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
474 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
475 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
478 S() : a(a) {} // expected-warning{{reference 'a' is not yet bound to a value when used here}}
483 int &a = a; // expected-warning{{reference 'a' is not yet bound to a value when used within its own initialization}}
484 int &b(b); // expected-warning{{reference 'b' is not yet bound to a value when used within its own initialization}}
485 int &c = a ? b : c; // expected-warning{{reference 'c' is not yet bound to a value when used within its own initialization}}
486 int &d{d}; // expected-warning{{reference 'd' is not yet bound to a value when used within its own initialization}}
490 T() // expected-note{{during field initialization in this constructor}}
491 : a(b), b(a) {} // expected-warning{{reference 'b' is not yet bound to a value when used here}}
493 int &c = c; // expected-warning{{reference 'c' is not yet bound to a value when used here}}
498 U() : b(a) {} // No-warning.
504 namespace operators {
512 A a1 = a1 = makeA(); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
513 A a2 = a2 == a1; // expected-warning{{variable 'a2' is uninitialized when used within its own initialization}}
514 A a3 = a2 == a3; // expected-warning{{variable 'a3' is uninitialized when used within its own initialization}}
521 template<typename T> A(T) {}
524 A a0([] { return a0.x; }); // ok
526 A a1([=] { return a1.x; }); // expected-warning{{variable 'a1' is uninitialized when used within its own initialization}}
527 A a2([&] { return a2.x; }); // ok
531 namespace record_fields {
537 static A something(A&);
541 A const_ref(const A&);
547 B(char (*)[1]) : a(a) {} // expected-warning {{uninitialized}}
548 B(char (*)[2]) : a(a.get()) {} // expected-warning {{uninitialized}}
549 B(char (*)[3]) : a(a.num()) {}
550 B(char (*)[4]) : a(a.copy(a)) {} // expected-warning {{uninitialized}}
551 B(char (*)[5]) : a(a.something(a)) {}
552 B(char (*)[6]) : a(ref(a)) {}
553 B(char (*)[7]) : a(const_ref(a)) {}
554 B(char (*)[8]) : a(pointer(&a)) {}
555 B(char (*)[9]) : a(normal(a)) {} // expected-warning {{uninitialized}}
558 C() {} // expected-note4{{in this constructor}}
559 A a1 = a1; // expected-warning {{uninitialized}}
560 A a2 = a2.get(); // expected-warning {{uninitialized}}
562 A a4 = a4.copy(a4); // expected-warning {{uninitialized}}
563 A a5 = a5.something(a5);
565 A a7 = const_ref(a7);
567 A a9 = normal(a9); // expected-warning {{uninitialized}}
569 struct D { // expected-note4{{in the implicit default constructor}}
570 A a1 = a1; // expected-warning {{uninitialized}}
571 A a2 = a2.get(); // expected-warning {{uninitialized}}
573 A a4 = a4.copy(a4); // expected-warning {{uninitialized}}
574 A a5 = a5.something(a5);
576 A a7 = const_ref(a7);
578 A a9 = normal(a9); // expected-warning {{uninitialized}}
586 A a5 = a5.something(a5);
588 A a7 = const_ref(a7);
594 namespace cross_field_warnings {
598 A(char (*)[1]) : b(a) {} // expected-warning{{field 'a' is uninitialized when used here}}
599 A(char (*)[2]) : a(b) {} // expected-warning{{field 'b' is uninitialized when used here}}
603 int a = b; // expected-warning{{field 'b' is uninitialized when used here}}
605 B() {} // expected-note{{during field initialization in this constructor}}
610 int b = a; // expected-warning{{field 'a' is uninitialized when used here}}
611 C(char (*)[1]) : a(5) {}
612 C(char (*)[2]) {} // expected-note{{during field initialization in this constructor}}
635 F(char (*)[1]) : a(e.get()) {} // expected-warning{{field 'e' is uninitialized when used here}}
636 F(char (*)[2]) : a(e.num()) {}
637 F(char (*)[3]) : e(a) {} // expected-warning{{field 'a' is uninitialized when used here}}
638 F(char (*)[4]) : a(4), e(a) {}
639 F(char (*)[5]) : e(b) {} // expected-warning{{field 'b' is uninitialized when used here}}
640 F(char (*)[6]) : e(b), b(4) {} // expected-warning{{field 'b' is uninitialized when used here}}
659 struct J : public I {
663 J() : I((a = new int(5))), b(a), c(*a) {}
673 int b = b + 5; // expected-warning{{field 'b' is uninitialized when used here}}
674 L() : a(5) {} // expected-note{{during field initialization in this constructor}}
679 struct N : public M {
682 N() : b(a) { } // expected-warning{{field 'a' is uninitialized when used here}}
687 int get() { return x; }
701 a(c = 5), // expected-warning{{reference 'c' is not yet bound to a value when used here}}
702 b(c), // expected-warning{{reference 'c' is not yet bound to a value when used here}}
711 R() : a(c = 5), b(c), c(a) {}
715 namespace base_class {
720 struct B : public A {
722 B() : A(x) {} // expected-warning{{field 'x' is uninitialized when used here}}
725 struct C : public A {
728 C() : A(y = 4), x(y) {}