blob: 2398504347d2720f5767b8b0221af692a837f4cc [file] [log] [blame]
Ted Kremenek0a65f942011-03-17 03:06:07 +00001// RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -fsyntax-only -fblocks %s -verify
Ted Kremenek610068c2011-01-15 02:58:47 +00002
Ted Kremenek540dda62011-08-23 20:30:50 +00003typedef __typeof(sizeof(int)) size_t;
4void *malloc(size_t);
5
Ted Kremenek610068c2011-01-15 02:58:47 +00006int test1() {
David Blaikie4f4f3492011-09-10 05:35:08 +00007 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +00008 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +00009}
10
11int test2() {
12 int x = 0;
13 return x; // no-warning
14}
15
16int test3() {
17 int x;
18 x = 0;
19 return x; // no-warning
20}
21
22int test4() {
David Blaikie4f4f3492011-09-10 05:35:08 +000023 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000024 ++x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000025 return x;
26}
27
28int test5() {
David Blaikie4f4f3492011-09-10 05:35:08 +000029 int x, y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000030 x = y; // expected-warning{{variable 'y' is uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000031 return x;
32}
33
34int test6() {
David Blaikie4f4f3492011-09-10 05:35:08 +000035 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000036 x += 2; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000037 return x;
38}
39
40int test7(int y) {
David Blaikie4f4f3492011-09-10 05:35:08 +000041 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000042 if (y)
43 x = 1;
Chandler Carruth584b9d62011-04-08 06:47:15 +000044 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000045}
46
47int test8(int y) {
48 int x;
49 if (y)
50 x = 1;
51 else
52 x = 0;
Ted Kremenek609e3172011-02-02 23:35:53 +000053 return x;
Ted Kremenek610068c2011-01-15 02:58:47 +000054}
55
56int test9(int n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000057 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000058 for (unsigned i = 0 ; i < n; ++i) {
59 if (i == n - 1)
60 break;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +000061 x = 1;
Ted Kremenek610068c2011-01-15 02:58:47 +000062 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000063 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000064}
65
66int test10(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000067 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000068 for (unsigned i = 0 ; i < n; ++i) {
69 x = 1;
70 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000071 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000072}
73
74int test11(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000075 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000076 for (unsigned i = 0 ; i <= n; ++i) {
77 x = 1;
78 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000079 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000080}
81
82void test12(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000083 for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is uninitialized when used here}} expected-note{{initialize the variable 'i' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000084}
85
86int test13() {
87 static int i;
88 return i; // no-warning
89}
90
Ted Kremenekc104e532011-01-18 04:53:25 +000091// Simply don't crash on this test case.
92void test14() {
93 const char *p = 0;
94 for (;;) {}
95}
Ted Kremenek610068c2011-01-15 02:58:47 +000096
Chandler Carruthb88fb022011-04-05 21:36:30 +000097int test15() {
98 int x = x; // no-warning: signals intended lack of initialization. \
99 // expected-note{{variable 'x' is declared here}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000100 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekc104e532011-01-18 04:53:25 +0000101}
102
103// Don't warn in the following example; shows dataflow confluence.
104char *test16_aux();
105void test16() {
106 char *p = test16_aux();
107 for (unsigned i = 0 ; i < 100 ; i++)
108 p[i] = 'a'; // no-warning
109}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000110
111void test17() {
112 // Don't warn multiple times about the same uninitialized variable
113 // along the same path.
David Blaikie4f4f3492011-09-10 05:35:08 +0000114 int *x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000115 *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000116 *x = 1; // no-warning
117}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000118
119int test18(int x, int y) {
120 int z;
121 if (x && y && (z = 1)) {
122 return z; // no-warning
123 }
124 return 0;
125}
126
127int test19_aux1();
128int test19_aux2();
129int test19_aux3(int *x);
130int test19() {
131 int z;
132 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
133 return z; // no-warning
134 return 0;
135}
136
137int test20() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000138 int z; // expected-note{{initialize the variable 'z' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000139 if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z))
Chandler Carruth584b9d62011-04-08 06:47:15 +0000140 return z; // expected-warning{{variable 'z' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000141 return 0;
142}
143
144int test21(int x, int y) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000145 int z; // expected-note{{initialize the variable 'z' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000146 if ((x && y) || test19_aux3(&z) || test19_aux2())
Chandler Carruth584b9d62011-04-08 06:47:15 +0000147 return z; // expected-warning{{variable 'z' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000148 return 0;
149}
150
151int test22() {
152 int z;
153 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
154 return z; // no-warning
155 return 0;
156}
157
158int test23() {
159 int z;
160 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
161 return z; // no-warning
162 return 0;
163}
164
165// The basic uninitialized value analysis doesn't have enough path-sensitivity
166// to catch initializations relying on control-dependencies spanning multiple
167// conditionals. This possibly can be handled by making the CFG itself
168// represent such control-dependencies, but it is a niche case.
169int test24(int flag) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000170 unsigned val; // expected-note{{initialize the variable 'val' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000171 if (flag)
172 val = 1;
173 if (!flag)
174 val = 1;
Chandler Carruth584b9d62011-04-08 06:47:15 +0000175 return val; // expected-warning{{variable 'val' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000176}
177
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000178float test25() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000179 float x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000180 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000181}
182
183typedef int MyInt;
184MyInt test26() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000185 MyInt x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000186 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000187}
Ted Kremenek96608032011-01-23 17:53:04 +0000188
189// Test handling of sizeof().
190int test27() {
191 struct test_27 { int x; } *y;
192 return sizeof(y->x); // no-warning
193}
194
195int test28() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000196 int len; // expected-note{{initialize the variable 'len' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000197 return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}}
Ted Kremenek96608032011-01-23 17:53:04 +0000198}
199
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000200void test29() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000201 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000202 (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}}
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000203}
204
205void test30() {
206 static int x; // no-warning
207 (void) ^{ (void) x; };
208}
209
210void test31() {
211 __block int x; // no-warning
212 (void) ^{ (void) x; };
213}
214
215int test32_x;
216void test32() {
217 (void) ^{ (void) test32_x; }; // no-warning
218}
219
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000220void test_33() {
221 int x; // no-warning
222 (void) x;
223}
224
225int test_34() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000226 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000227 (void) x;
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000228 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000229}
230
Ted Kremenek40900ee2011-01-27 02:29:34 +0000231// Test that this case doesn't crash.
232void test35(int x) {
233 __block int y = 0;
234 ^{ y = (x == 0); }();
235}
236
Ted Kremenek96554fd2011-01-27 18:51:39 +0000237// Test handling of indirect goto.
238void test36()
239{
David Blaikie4f4f3492011-09-10 05:35:08 +0000240 void **pc; // expected-note{{initialize the variable 'pc' to silence this warning}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000241 void *dummy[] = { &&L1, &&L2 };
242 L1:
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000243 goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000244 L2:
245 goto *pc;
246}
247
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000248// Test && nested in ||.
249int test37_a();
250int test37_b();
251int test37()
252{
253 int identifier;
254 if ((test37_a() && (identifier = 1)) ||
255 (test37_b() && (identifier = 2))) {
256 return identifier; // no-warning
257 }
258 return 0;
259}
260
261// Test merging of path-specific dataflow values (without asserting).
262int test38(int r, int x, int y)
263{
264 int z;
265 return ((r < 0) || ((r == 0) && (x < y)));
266}
267
Ted Kremenekf3f53792011-03-15 03:17:01 +0000268int test39(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000269 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000270 int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000271 return z;
272}
273
274
275int test40(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000276 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000277 return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000278}
279
280int test41(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000281 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000282 if (x) y = 1; // no-warning
Chandler Carruth584b9d62011-04-08 06:47:15 +0000283 return y; // expected-warning {{variable 'y' may be uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000284}
285
286void test42() {
287 int a;
288 a = 30; // no-warning
289}
290
291void test43_aux(int x);
292void test43(int i) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000293 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000294 for (i = 0 ; i < 10; i++)
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000295 test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000296}
297
298void test44(int i) {
299 int x = i;
David Blaikie4f4f3492011-09-10 05:35:08 +0000300 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000301 for (i = 0; i < 10; i++ ) {
302 test43_aux(x++); // no-warning
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000303 x += y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000304 }
305}
306
307int test45(int j) {
308 int x = 1, y = x + 1;
309 if (y) // no-warning
310 return x;
311 return y;
312}
313
314void test46()
315{
David Blaikie4f4f3492011-09-10 05:35:08 +0000316 int i; // expected-note{{initialize the variable 'i' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000317 int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000318}
319
320void *test47(int *i)
321{
322 return i ? : 0; // no-warning
323}
324
325void *test49(int *i)
326{
327 int a;
328 return &a ? : i; // no-warning
329}
330
331void test50()
332{
333 char c[1 ? : 2]; // no-warning
334}
335
Ted Kremenekbc8b44c2011-03-31 22:32:41 +0000336int test51(void)
337{
338 __block int a;
339 ^(void) {
340 a = 42;
341 }();
342 return a; // no-warning
343}
344
Ted Kremeneke6c28032011-05-10 22:10:35 +0000345// FIXME: This is a false positive, but it tests logical operations in switch statements.
346int test52(int a, int b) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000347 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
Ted Kremeneke6c28032011-05-10 22:10:35 +0000348 switch (a || b) { // expected-warning {{switch condition has boolean value}}
349 case 0:
350 x = 1;
351 break;
352 case 1:
353 x = 2;
354 break;
355 }
356 return x; // expected-warning {{variable 'x' may be uninitialized when used here}}
357}
Chandler Carruth84350692011-07-16 22:27:02 +0000358
Ted Kremenekd626ec42011-07-19 20:33:49 +0000359void test53() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000360 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
Ted Kremenek62d126e2011-07-19 21:41:51 +0000361 int y = (x); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekd626ec42011-07-19 20:33:49 +0000362}
363
Chandler Carruth84350692011-07-16 22:27:02 +0000364// This CFG caused the uninitialized values warning to inf-loop.
365extern int PR10379_g();
366void PR10379_f(int *len) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000367 int new_len; // expected-note{{initialize the variable 'new_len' to silence this warning}}
Chandler Carruth84350692011-07-16 22:27:02 +0000368 for (int i = 0; i < 42 && PR10379_g() == 0; i++) {
369 if (PR10379_g() == 1)
370 continue;
371 if (PR10379_g() == 2)
372 PR10379_f(&new_len);
373 else if (PR10379_g() == 3)
374 PR10379_f(&new_len);
375 *len += new_len; // expected-warning {{variable 'new_len' may be uninitialized when used here}}
376 }
377}
Ted Kremenek540dda62011-08-23 20:30:50 +0000378
379// Test that sizeof(VLA) doesn't trigger a warning.
380void test_vla_sizeof(int x) {
381 double (*memory)[2][x] = malloc(sizeof(*memory)); // no-warning
382}
383
Ted Kremenek6f275422011-09-02 19:39:26 +0000384// Test absurd case of deadcode + use of blocks. This previously was a false positive
385// due to an analysis bug.
386int test_block_and_dead_code() {
387 __block int x;
388 ^{ x = 1; }();
389 if (0)
390 return x;
391 return x; // no-warning
392}
393