blob: 1be8da814847d205a2df359ba0c5227a7c427290 [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}}
Richard Smith2815e1a2012-05-25 02:17:09 +000042 if (y) // expected-note{{uninitialized use occurs whenever 'if' condition is false}}
Ted Kremenek610068c2011-01-15 02:58:47 +000043 x = 1;
Richard Smith2815e1a2012-05-25 02:17:09 +000044 return x; // expected-warning{{variable 'x' is sometimes uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000045}
46
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +000047int test7b(int y) {
48 int x = x; // expected-note{{variable 'x' is declared here}}
49 if (y)
50 x = 1;
Richard Smith2815e1a2012-05-25 02:17:09 +000051 // Warn with "may be uninitialized" here (not "is sometimes uninitialized"),
52 // since the self-initialization is intended to suppress a -Wuninitialized
53 // warning.
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +000054 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
55}
56
Ted Kremenek610068c2011-01-15 02:58:47 +000057int test8(int y) {
58 int x;
59 if (y)
60 x = 1;
61 else
62 x = 0;
Ted Kremenek609e3172011-02-02 23:35:53 +000063 return x;
Ted Kremenek610068c2011-01-15 02:58:47 +000064}
65
66int test9(int 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 if (i == n - 1)
70 break;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +000071 x = 1;
Ted Kremenek610068c2011-01-15 02:58:47 +000072 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000073 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000074}
75
76int test10(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000077 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000078 for (unsigned i = 0 ; i < n; ++i) {
79 x = 1;
80 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000081 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000082}
83
84int test11(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000085 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000086 for (unsigned i = 0 ; i <= n; ++i) {
87 x = 1;
88 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000089 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000090}
91
92void test12(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000093 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 +000094}
95
96int test13() {
97 static int i;
98 return i; // no-warning
99}
100
Ted Kremenekc104e532011-01-18 04:53:25 +0000101// Simply don't crash on this test case.
102void test14() {
103 const char *p = 0;
104 for (;;) {}
105}
Ted Kremenek610068c2011-01-15 02:58:47 +0000106
Ted Kremenek9e761722011-10-13 18:50:06 +0000107void test15() {
108 int x = x; // no-warning: signals intended lack of initialization.
109}
110
111int test15b() {
112 // Warn here with the self-init, since it does result in a use of
113 // an unintialized variable and this is the root cause.
114 int x = x; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
115 return x;
Ted Kremenekc104e532011-01-18 04:53:25 +0000116}
117
118// Don't warn in the following example; shows dataflow confluence.
119char *test16_aux();
120void test16() {
121 char *p = test16_aux();
122 for (unsigned i = 0 ; i < 100 ; i++)
123 p[i] = 'a'; // no-warning
124}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000125
126void test17() {
127 // Don't warn multiple times about the same uninitialized variable
128 // along the same path.
David Blaikie4f4f3492011-09-10 05:35:08 +0000129 int *x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000130 *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000131 *x = 1; // no-warning
132}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000133
134int test18(int x, int y) {
135 int z;
136 if (x && y && (z = 1)) {
137 return z; // no-warning
138 }
139 return 0;
140}
141
142int test19_aux1();
143int test19_aux2();
144int test19_aux3(int *x);
145int test19() {
146 int z;
147 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
148 return z; // no-warning
149 return 0;
150}
151
152int test20() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000153 int z; // expected-note{{initialize the variable 'z' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000154 if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z))
Chandler Carruth584b9d62011-04-08 06:47:15 +0000155 return z; // expected-warning{{variable 'z' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000156 return 0;
157}
158
159int test21(int x, int y) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000160 int z; // expected-note{{initialize the variable 'z' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000161 if ((x && y) || test19_aux3(&z) || test19_aux2())
Chandler Carruth584b9d62011-04-08 06:47:15 +0000162 return z; // expected-warning{{variable 'z' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000163 return 0;
164}
165
166int test22() {
167 int z;
168 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
169 return z; // no-warning
170 return 0;
171}
172
173int test23() {
174 int z;
175 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
176 return z; // no-warning
177 return 0;
178}
179
180// The basic uninitialized value analysis doesn't have enough path-sensitivity
181// to catch initializations relying on control-dependencies spanning multiple
182// conditionals. This possibly can be handled by making the CFG itself
183// represent such control-dependencies, but it is a niche case.
184int test24(int flag) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000185 unsigned val; // expected-note{{initialize the variable 'val' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000186 if (flag)
187 val = 1;
188 if (!flag)
189 val = 1;
Chandler Carruth584b9d62011-04-08 06:47:15 +0000190 return val; // expected-warning{{variable 'val' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000191}
192
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000193float test25() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000194 float x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000195 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000196}
197
198typedef int MyInt;
199MyInt test26() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000200 MyInt x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000201 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000202}
Ted Kremenek96608032011-01-23 17:53:04 +0000203
204// Test handling of sizeof().
205int test27() {
206 struct test_27 { int x; } *y;
207 return sizeof(y->x); // no-warning
208}
209
210int test28() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000211 int len; // expected-note{{initialize the variable 'len' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000212 return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}}
Ted Kremenek96608032011-01-23 17:53:04 +0000213}
214
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000215void test29() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000216 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000217 (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}}
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000218}
219
220void test30() {
221 static int x; // no-warning
222 (void) ^{ (void) x; };
223}
224
225void test31() {
226 __block int x; // no-warning
227 (void) ^{ (void) x; };
228}
229
230int test32_x;
231void test32() {
232 (void) ^{ (void) test32_x; }; // no-warning
233}
234
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000235void test_33() {
236 int x; // no-warning
237 (void) x;
238}
239
240int test_34() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000241 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000242 (void) x;
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000243 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000244}
245
Ted Kremenek40900ee2011-01-27 02:29:34 +0000246// Test that this case doesn't crash.
247void test35(int x) {
248 __block int y = 0;
249 ^{ y = (x == 0); }();
250}
251
Ted Kremenek96554fd2011-01-27 18:51:39 +0000252// Test handling of indirect goto.
253void test36()
254{
David Blaikie4f4f3492011-09-10 05:35:08 +0000255 void **pc; // expected-note{{initialize the variable 'pc' to silence this warning}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000256 void *dummy[] = { &&L1, &&L2 };
257 L1:
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000258 goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000259 L2:
260 goto *pc;
261}
262
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000263// Test && nested in ||.
264int test37_a();
265int test37_b();
266int test37()
267{
268 int identifier;
269 if ((test37_a() && (identifier = 1)) ||
270 (test37_b() && (identifier = 2))) {
271 return identifier; // no-warning
272 }
273 return 0;
274}
275
276// Test merging of path-specific dataflow values (without asserting).
277int test38(int r, int x, int y)
278{
279 int z;
280 return ((r < 0) || ((r == 0) && (x < y)));
281}
282
Ted Kremenekf3f53792011-03-15 03:17:01 +0000283int test39(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000284 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000285 int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000286 return z;
287}
288
289
290int test40(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000291 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000292 return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000293}
294
295int test41(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000296 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Richard Smith2815e1a2012-05-25 02:17:09 +0000297 if (x) y = 1; // expected-note{{uninitialized use occurs whenever 'if' condition is false}}
298 return y; // expected-warning {{variable 'y' is sometimes uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000299}
300
301void test42() {
302 int a;
303 a = 30; // no-warning
304}
305
306void test43_aux(int x);
307void test43(int i) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000308 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000309 for (i = 0 ; i < 10; i++)
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000310 test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000311}
312
313void test44(int i) {
314 int x = i;
David Blaikie4f4f3492011-09-10 05:35:08 +0000315 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000316 for (i = 0; i < 10; i++ ) {
317 test43_aux(x++); // no-warning
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000318 x += y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000319 }
320}
321
322int test45(int j) {
323 int x = 1, y = x + 1;
324 if (y) // no-warning
325 return x;
326 return y;
327}
328
329void test46()
330{
David Blaikie4f4f3492011-09-10 05:35:08 +0000331 int i; // expected-note{{initialize the variable 'i' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000332 int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000333}
334
335void *test47(int *i)
336{
337 return i ? : 0; // no-warning
338}
339
340void *test49(int *i)
341{
342 int a;
343 return &a ? : i; // no-warning
344}
345
346void test50()
347{
348 char c[1 ? : 2]; // no-warning
349}
350
Ted Kremenekbc8b44c2011-03-31 22:32:41 +0000351int test51(void)
352{
353 __block int a;
354 ^(void) {
355 a = 42;
356 }();
357 return a; // no-warning
358}
359
Ted Kremeneke6c28032011-05-10 22:10:35 +0000360// FIXME: This is a false positive, but it tests logical operations in switch statements.
361int test52(int a, int b) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000362 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
Ted Kremeneke6c28032011-05-10 22:10:35 +0000363 switch (a || b) { // expected-warning {{switch condition has boolean value}}
364 case 0:
365 x = 1;
366 break;
367 case 1:
368 x = 2;
369 break;
370 }
371 return x; // expected-warning {{variable 'x' may be uninitialized when used here}}
372}
Chandler Carruth84350692011-07-16 22:27:02 +0000373
Ted Kremenekd626ec42011-07-19 20:33:49 +0000374void test53() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000375 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
Ted Kremenek62d126e2011-07-19 21:41:51 +0000376 int y = (x); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekd626ec42011-07-19 20:33:49 +0000377}
378
Chandler Carruth84350692011-07-16 22:27:02 +0000379// This CFG caused the uninitialized values warning to inf-loop.
380extern int PR10379_g();
381void PR10379_f(int *len) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000382 int new_len; // expected-note{{initialize the variable 'new_len' to silence this warning}}
Chandler Carruth84350692011-07-16 22:27:02 +0000383 for (int i = 0; i < 42 && PR10379_g() == 0; i++) {
384 if (PR10379_g() == 1)
385 continue;
386 if (PR10379_g() == 2)
387 PR10379_f(&new_len);
388 else if (PR10379_g() == 3)
389 PR10379_f(&new_len);
390 *len += new_len; // expected-warning {{variable 'new_len' may be uninitialized when used here}}
391 }
392}
Ted Kremenek540dda62011-08-23 20:30:50 +0000393
394// Test that sizeof(VLA) doesn't trigger a warning.
395void test_vla_sizeof(int x) {
396 double (*memory)[2][x] = malloc(sizeof(*memory)); // no-warning
397}
398
Ted Kremenek6f275422011-09-02 19:39:26 +0000399// Test absurd case of deadcode + use of blocks. This previously was a false positive
400// due to an analysis bug.
401int test_block_and_dead_code() {
402 __block int x;
403 ^{ x = 1; }();
404 if (0)
405 return x;
406 return x; // no-warning
407}
408
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000409// This previously triggered an infinite loop in the analysis.
410void PR11069(int a, int b) {
411 unsigned long flags;
412 for (;;) {
413 if (a && !b)
414 break;
415 }
416 for (;;) {
417 // This does not trigger a warning because it isn't a real use.
418 (void)(flags); // no-warning
419 }
420}
421
Ted Kremenekaa2176b2011-10-07 00:52:56 +0000422// Test uninitialized value used in loop condition.
423void rdar9432305(float *P) {
424 int i; // expected-note {{initialize the variable 'i' to silence this warning}}
425 for (; i < 10000; ++i) // expected-warning {{variable 'i' is uninitialized when used here}}
426 P[i] = 0.0f;
427}
Richard Trieu7b0a3e32012-05-03 01:09:59 +0000428
429// Test that fixits are not emitted inside macros.
430#define UNINIT(T, x, y) T x; T y = x;
431#define ASSIGN(T, x, y) T y = x;
432void test54() {
433 UNINIT(int, a, b); // expected-warning {{variable 'a' is uninitialized when used here}} \
434 // expected-note {{variable 'a' is declared here}}
435 int c; // expected-note {{initialize the variable 'c' to silence this warning}}
436 ASSIGN(int, c, d); // expected-warning {{variable 'c' is uninitialized when used here}}
437}