blob: a6045bd118a7c361ece40a20c81670714344647a [file] [log] [blame]
Ted Kremenekc02af352012-04-12 20:03:47 +00001// RUN: %clang_cc1 -fsyntax-only -Wall -Wuninitialized -std=c++11 -verify %s
Ted Kremenekc75f6182010-10-30 00:43:15 +00002
Chandler Carruth349894e2011-03-27 20:35:59 +00003int foo(int x);
4int bar(int* x);
5int boo(int& x);
6int far(const int& x);
7
8// Test self-references within initializers which are guaranteed to be
9// uninitialized.
Chandler Carruthb414c4f2011-04-05 17:41:31 +000010int a = a; // no-warning: used to signal intended lack of initialization.
Chandler Carruth349894e2011-03-27 20:35:59 +000011int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
12int c = (c + c); // expected-warning 2 {{variable 'c' is uninitialized when used within its own initialization}}
Chandler Carruth349894e2011-03-27 20:35:59 +000013int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
14int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
15
16// Thes don't warn as they don't require the value.
17int g = sizeof(g);
18void* ptr = &ptr;
19int h = bar(&h);
20int i = boo(i);
21int j = far(j);
22int k = __alignof__(k);
23
Richard Trieu7e9f8af2012-05-09 00:21:34 +000024int l = k ? l : l; // expected-warning 2{{variable 'l' is uninitialized when used within its own initialization}}
25int m = 1 + (k ? m : m); // expected-warning 2{{variable 'm' is uninitialized when used within its own initialization}}
26int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
Richard Trieu898267f2011-09-01 21:44:13 +000027
Richard Trieuf6278e52012-05-09 21:08:22 +000028void test () {
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}}
35
36 // Thes don't warn as they don't require the value.
37 int g = sizeof(g);
38 void* ptr = &ptr;
39 int h = bar(&h);
40 int i = boo(i);
41 int j = far(j);
42 int k = __alignof__(k);
43
44 int l = k ? l : l; // FIXME: warn here
45 int m = 1 + (k ? m : m); // FIXME: warn here
46 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
47
48 for (;;) {
49 int a = a; // no-warning: used to signal intended lack of initialization.
50 int b = b + 1; // expected-warning {{variable 'b' is uninitialized when used within its own initialization}}
51 int c = (c + c); // expected-warning {{variable 'c' is uninitialized when used within its own initialization}}
52 int d = ({ d + d ;}); // expected-warning {{variable 'd' is uninitialized when used within its own initialization}}
53 int e = static_cast<long>(e) + 1; // expected-warning {{variable 'e' is uninitialized when used within its own initialization}}
54 int f = foo(f); // expected-warning {{variable 'f' is uninitialized when used within its own initialization}}
55
56 // Thes don't warn as they don't require the value.
57 int g = sizeof(g);
58 void* ptr = &ptr;
59 int h = bar(&h);
60 int i = boo(i);
61 int j = far(j);
62 int k = __alignof__(k);
63
64 int l = k ? l : l; // FIXME: warn here
65 int m = 1 + (k ? m : m); // FIXME: warn here
66 int n = -n; // expected-warning {{variable 'n' is uninitialized when used within its own initialization}}
67 }
68}
69
Richard Trieu898267f2011-09-01 21:44:13 +000070// Test self-references with record types.
71class A {
72 // Non-POD class.
73 public:
74 enum count { ONE, TWO, THREE };
75 int num;
76 static int count;
77 int get() const { return num; }
Richard Trieuffea6b42012-03-08 01:15:31 +000078 int get2() { return num; }
Richard Trieu898267f2011-09-01 21:44:13 +000079 void set(int x) { num = x; }
80 static int zero() { return 0; }
81
82 A() {}
83 A(A const &a) {}
84 A(int x) {}
85 A(int *x) {}
86 A(A *a) {}
Rafael Espindola1a5d3552012-01-06 04:54:01 +000087 ~A();
Richard Trieu898267f2011-09-01 21:44:13 +000088};
89
90A getA() { return A(); }
91A getA(int x) { return A(); }
92A getA(A* a) { return A(); }
Richard Trieu7e9f8af2012-05-09 00:21:34 +000093A getA(A a) { return A(); }
Richard Trieu898267f2011-09-01 21:44:13 +000094
Richard Trieu7e9f8af2012-05-09 00:21:34 +000095void setupA(bool x) {
Richard Trieu898267f2011-09-01 21:44:13 +000096 A a1;
97 a1.set(a1.get());
98 A a2(a1.get());
99 A a3(a1);
100 A a4(&a4);
101 A a5(a5.zero());
102 A a6(a6.ONE);
103 A a7 = getA();
104 A a8 = getA(a8.TWO);
105 A a9 = getA(&a9);
106 A a10(a10.count);
107
108 A a11(a11); // expected-warning {{variable 'a11' is uninitialized when used within its own initialization}}
109 A a12(a12.get()); // expected-warning {{variable 'a12' is uninitialized when used within its own initialization}}
110 A a13(a13.num); // expected-warning {{variable 'a13' is uninitialized when used within its own initialization}}
111 A a14 = A(a14); // expected-warning {{variable 'a14' is uninitialized when used within its own initialization}}
112 A a15 = getA(a15.num); // expected-warning {{variable 'a15' is uninitialized when used within its own initialization}}
113 A a16(&a16.num); // expected-warning {{variable 'a16' is uninitialized when used within its own initialization}}
Richard Trieuffea6b42012-03-08 01:15:31 +0000114 A a17(a17.get2()); // expected-warning {{variable 'a17' is uninitialized when used within its own initialization}}
Richard Trieu7e9f8af2012-05-09 00:21:34 +0000115 A a18 = x ? a18 : a17; // expected-warning {{variable 'a18' is uninitialized when used within its own initialization}}
116 A a19 = getA(x ? a19 : a17); // expected-warning {{variable 'a19' is uninitialized when used within its own initialization}}
Richard Trieu898267f2011-09-01 21:44:13 +0000117}
118
119struct B {
120 // POD struct.
121 int x;
122 int *y;
123};
124
125B getB() { return B(); };
126B getB(int x) { return B(); };
127B getB(int *x) { return B(); };
128B getB(B *b) { return B(); };
129
130void setupB() {
131 B b1;
132 B b2(b1);
133 B b3 = { 5, &b3.x };
134 B b4 = getB();
135 B b5 = getB(&b5);
136 B b6 = getB(&b6.x);
137
138 // Silence unused warning
139 (void) b2;
140 (void) b4;
141
142 B b7(b7); // expected-warning {{variable 'b7' is uninitialized when used within its own initialization}}
143 B b8 = getB(b8.x); // expected-warning {{variable 'b8' is uninitialized when used within its own initialization}}
144 B b9 = getB(b9.y); // expected-warning {{variable 'b9' is uninitialized when used within its own initialization}}
Richard Trieu7e9f8af2012-05-09 00:21:34 +0000145 B b10 = getB(-b10.x); // expected-warning {{variable 'b10' is uninitialized when used within its own initialization}}
Richard Trieu898267f2011-09-01 21:44:13 +0000146}
147
Chandler Carruth349894e2011-03-27 20:35:59 +0000148// Also test similar constructs in a field's initializer.
149struct S {
150 int x;
151 void *ptr;
152
153 S(bool (*)[1]) : x(x) {} // expected-warning {{field is uninitialized when used here}}
154 S(bool (*)[2]) : x(x + 1) {} // expected-warning {{field is uninitialized when used here}}
155 S(bool (*)[3]) : x(x + x) {} // expected-warning {{field is uninitialized when used here}}
156 S(bool (*)[4]) : x(static_cast<long>(x) + 1) {} // expected-warning {{field is uninitialized when used here}}
157 S(bool (*)[5]) : x(foo(x)) {} // FIXME: This should warn!
158
159 // These don't actually require the value of x and so shouldn't warn.
160 S(char (*)[1]) : x(sizeof(x)) {} // rdar://8610363
161 S(char (*)[2]) : ptr(&ptr) {}
162 S(char (*)[3]) : x(__alignof__(x)) {}
163 S(char (*)[4]) : x(bar(&x)) {}
164 S(char (*)[5]) : x(boo(x)) {}
165 S(char (*)[6]) : x(far(x)) {}
Ted Kremenekc75f6182010-10-30 00:43:15 +0000166};
Richard Trieu47eb8982011-09-07 00:58:53 +0000167
168struct C { char a[100], *e; } car = { .e = car.a };
Douglas Gregor2d9eb212011-11-15 15:29:30 +0000169
170// <rdar://problem/10398199>
171namespace rdar10398199 {
172 class FooBase { protected: ~FooBase() {} };
173 class Foo : public FooBase {
174 public:
175 operator int&() const;
176 };
177 void stuff();
178 template <typename T> class FooImpl : public Foo {
179 T val;
180 public:
181 FooImpl(const T &x) : val(x) {}
182 ~FooImpl() { stuff(); }
183 };
184
185 template <typename T> FooImpl<T> makeFoo(const T& x) {
186 return FooImpl<T>(x);
187 }
188
189 void test() {
190 const Foo &x = makeFoo(42);
191 const int&y = makeFoo(42u);
192 (void)x;
193 (void)y;
194 };
195}
Ted Kremenek550f2232012-03-22 05:57:43 +0000196
197// PR 12325 - this was a false uninitialized value warning due to
198// a broken CFG.
199int pr12325(int params) {
200 int x = ({
201 while (false)
202 ;
203 int _v = params;
204 if (false)
205 ;
206 _v; // no-warning
207 });
208 return x;
209}
210
Ted Kremenekc02af352012-04-12 20:03:47 +0000211// Test lambda expressions with -Wuninitialized
212int test_lambda() {
213 auto f1 = [] (int x, int y) { int z; return x + y + z; }; // expected-warning {{C++11 requires lambda with omitted result type to consist of a single return statement}} expected-warning{{variable 'z' is uninitialized when used here}} expected-note {{initialize the variable 'z' to silence this warning}}
214 return f1(1, 2);
215}