blob: 9e3dd9d2875be285edbea3c5319b29f9d97c43cf [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}}
Richard Smith6cfa78f2012-07-17 01:27:33 +000036 x += 2; // expected-warning{{variable 'x' is uninitialized when used here}}
37 return x;
Ted Kremenek610068c2011-01-15 02:58:47 +000038}
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 Smithbdb97ff2012-05-26 06:20:46 +000042 if (y) // expected-warning{{variable 'x' is used uninitialized whenever 'if' condition is false}} \
43 // expected-note{{remove the 'if' if its condition is always true}}
Ted Kremenek610068c2011-01-15 02:58:47 +000044 x = 1;
Richard Smithbdb97ff2012-05-26 06:20:46 +000045 return x; // expected-note{{uninitialized use occurs here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000046}
47
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +000048int test7b(int y) {
49 int x = x; // expected-note{{variable 'x' is declared here}}
50 if (y)
51 x = 1;
Richard Smith2815e1a2012-05-25 02:17:09 +000052 // Warn with "may be uninitialized" here (not "is sometimes uninitialized"),
53 // since the self-initialization is intended to suppress a -Wuninitialized
54 // warning.
Matt Beaumont-Gay0d381812011-10-19 18:53:03 +000055 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
56}
57
Ted Kremenek610068c2011-01-15 02:58:47 +000058int test8(int y) {
59 int x;
60 if (y)
61 x = 1;
62 else
63 x = 0;
Ted Kremenek609e3172011-02-02 23:35:53 +000064 return x;
Ted Kremenek610068c2011-01-15 02:58:47 +000065}
66
67int test9(int n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000068 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000069 for (unsigned i = 0 ; i < n; ++i) {
70 if (i == n - 1)
71 break;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +000072 x = 1;
Ted Kremenek610068c2011-01-15 02:58:47 +000073 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000074 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000075}
76
77int test10(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000078 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000079 for (unsigned i = 0 ; i < n; ++i) {
80 x = 1;
81 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000082 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000083}
84
85int test11(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000086 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000087 for (unsigned i = 0 ; i <= n; ++i) {
88 x = 1;
89 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000090 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000091}
92
93void test12(unsigned n) {
David Blaikie4f4f3492011-09-10 05:35:08 +000094 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 +000095}
96
97int test13() {
98 static int i;
99 return i; // no-warning
100}
101
Ted Kremenekc104e532011-01-18 04:53:25 +0000102// Simply don't crash on this test case.
103void test14() {
104 const char *p = 0;
105 for (;;) {}
106}
Ted Kremenek610068c2011-01-15 02:58:47 +0000107
Ted Kremenek9e761722011-10-13 18:50:06 +0000108void test15() {
109 int x = x; // no-warning: signals intended lack of initialization.
110}
111
112int test15b() {
113 // Warn here with the self-init, since it does result in a use of
114 // an unintialized variable and this is the root cause.
115 int x = x; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
116 return x;
Ted Kremenekc104e532011-01-18 04:53:25 +0000117}
118
119// Don't warn in the following example; shows dataflow confluence.
120char *test16_aux();
121void test16() {
122 char *p = test16_aux();
123 for (unsigned i = 0 ; i < 100 ; i++)
124 p[i] = 'a'; // no-warning
125}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000126
127void test17() {
128 // Don't warn multiple times about the same uninitialized variable
129 // along the same path.
David Blaikie4f4f3492011-09-10 05:35:08 +0000130 int *x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000131 *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000132 *x = 1; // no-warning
133}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000134
135int test18(int x, int y) {
136 int z;
137 if (x && y && (z = 1)) {
138 return z; // no-warning
139 }
140 return 0;
141}
142
143int test19_aux1();
144int test19_aux2();
145int test19_aux3(int *x);
146int test19() {
147 int z;
148 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
149 return z; // no-warning
150 return 0;
151}
152
153int test20() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000154 int z; // expected-note{{initialize the variable 'z' to silence this warning}}
Ted Kremenek3f635c02012-07-14 05:04:10 +0000155 if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z)) // expected-warning {{variable 'z' is used uninitialized whenever '||' condition is true}} expected-note {{remove the '||' if its condition is always false}}
156 return z; // expected-note {{uninitialized use occurs here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000157 return 0;
158}
159
160int test21(int x, int y) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000161 int z; // expected-note{{initialize the variable 'z' to silence this warning}}
Ted Kremenek3f635c02012-07-14 05:04:10 +0000162 if ((x && y) || test19_aux3(&z) || test19_aux2()) // expected-warning {{variable 'z' is used uninitialized whenever '||' condition is true}} expected-note {{remove the '||' if its condition is always false}}
163 return z; // expected-note {{uninitialized use occurs here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000164 return 0;
165}
166
167int test22() {
168 int z;
169 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
170 return z; // no-warning
171 return 0;
172}
173
174int test23() {
175 int z;
176 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
177 return z; // no-warning
178 return 0;
179}
180
181// The basic uninitialized value analysis doesn't have enough path-sensitivity
182// to catch initializations relying on control-dependencies spanning multiple
183// conditionals. This possibly can be handled by making the CFG itself
184// represent such control-dependencies, but it is a niche case.
185int test24(int flag) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000186 unsigned val; // expected-note{{initialize the variable 'val' to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000187 if (flag)
188 val = 1;
189 if (!flag)
190 val = 1;
Chandler Carruth584b9d62011-04-08 06:47:15 +0000191 return val; // expected-warning{{variable 'val' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000192}
193
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000194float test25() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000195 float x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000196 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000197}
198
199typedef int MyInt;
200MyInt test26() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000201 MyInt x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000202 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000203}
Ted Kremenek96608032011-01-23 17:53:04 +0000204
205// Test handling of sizeof().
206int test27() {
207 struct test_27 { int x; } *y;
208 return sizeof(y->x); // no-warning
209}
210
211int test28() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000212 int len; // expected-note{{initialize the variable 'len' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000213 return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}}
Ted Kremenek96608032011-01-23 17:53:04 +0000214}
215
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000216void test29() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000217 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000218 (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}}
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000219}
220
221void test30() {
222 static int x; // no-warning
223 (void) ^{ (void) x; };
224}
225
226void test31() {
227 __block int x; // no-warning
228 (void) ^{ (void) x; };
229}
230
231int test32_x;
232void test32() {
233 (void) ^{ (void) test32_x; }; // no-warning
234}
235
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000236void test_33() {
237 int x; // no-warning
238 (void) x;
239}
240
241int test_34() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000242 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000243 (void) x;
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000244 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000245}
246
Ted Kremenek40900ee2011-01-27 02:29:34 +0000247// Test that this case doesn't crash.
248void test35(int x) {
249 __block int y = 0;
250 ^{ y = (x == 0); }();
251}
252
Ted Kremenek96554fd2011-01-27 18:51:39 +0000253// Test handling of indirect goto.
254void test36()
255{
David Blaikie4f4f3492011-09-10 05:35:08 +0000256 void **pc; // expected-note{{initialize the variable 'pc' to silence this warning}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000257 void *dummy[] = { &&L1, &&L2 };
258 L1:
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000259 goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000260 L2:
261 goto *pc;
262}
263
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000264// Test && nested in ||.
265int test37_a();
266int test37_b();
267int test37()
268{
269 int identifier;
270 if ((test37_a() && (identifier = 1)) ||
271 (test37_b() && (identifier = 2))) {
272 return identifier; // no-warning
273 }
274 return 0;
275}
276
277// Test merging of path-specific dataflow values (without asserting).
278int test38(int r, int x, int y)
279{
280 int z;
281 return ((r < 0) || ((r == 0) && (x < y)));
282}
283
Ted Kremenekf3f53792011-03-15 03:17:01 +0000284int test39(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000285 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000286 int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000287 return z;
288}
289
290
291int test40(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000292 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000293 return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000294}
295
296int test41(int x) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000297 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Richard Smithbdb97ff2012-05-26 06:20:46 +0000298 if (x) y = 1; // expected-warning{{variable 'y' is used uninitialized whenever 'if' condition is false}} \
299 // expected-note{{remove the 'if' if its condition is always true}}
300 return y; // expected-note{{uninitialized use occurs here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000301}
302
303void test42() {
304 int a;
305 a = 30; // no-warning
306}
307
308void test43_aux(int x);
309void test43(int i) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000310 int x; // expected-note{{initialize the variable 'x' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000311 for (i = 0 ; i < 10; i++)
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000312 test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000313}
314
315void test44(int i) {
316 int x = i;
David Blaikie4f4f3492011-09-10 05:35:08 +0000317 int y; // expected-note{{initialize the variable 'y' to silence this warning}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000318 for (i = 0; i < 10; i++ ) {
319 test43_aux(x++); // no-warning
Chandler Carruthd837c0d2011-07-22 05:27:52 +0000320 x += y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000321 }
322}
323
324int test45(int j) {
325 int x = 1, y = x + 1;
326 if (y) // no-warning
327 return x;
328 return y;
329}
330
331void test46()
332{
David Blaikie4f4f3492011-09-10 05:35:08 +0000333 int i; // expected-note{{initialize the variable 'i' to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000334 int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000335}
336
337void *test47(int *i)
338{
339 return i ? : 0; // no-warning
340}
341
342void *test49(int *i)
343{
344 int a;
345 return &a ? : i; // no-warning
346}
347
348void test50()
349{
350 char c[1 ? : 2]; // no-warning
351}
352
Ted Kremenekbc8b44c2011-03-31 22:32:41 +0000353int test51(void)
354{
355 __block int a;
356 ^(void) {
357 a = 42;
358 }();
359 return a; // no-warning
360}
361
Ted Kremeneke6c28032011-05-10 22:10:35 +0000362// FIXME: This is a false positive, but it tests logical operations in switch statements.
363int test52(int a, int b) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000364 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
Ted Kremeneke6c28032011-05-10 22:10:35 +0000365 switch (a || b) { // expected-warning {{switch condition has boolean value}}
366 case 0:
367 x = 1;
368 break;
369 case 1:
370 x = 2;
371 break;
372 }
373 return x; // expected-warning {{variable 'x' may be uninitialized when used here}}
374}
Chandler Carruth84350692011-07-16 22:27:02 +0000375
Ted Kremenekd626ec42011-07-19 20:33:49 +0000376void test53() {
David Blaikie4f4f3492011-09-10 05:35:08 +0000377 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
Ted Kremenek62d126e2011-07-19 21:41:51 +0000378 int y = (x); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekd626ec42011-07-19 20:33:49 +0000379}
380
Chandler Carruth84350692011-07-16 22:27:02 +0000381// This CFG caused the uninitialized values warning to inf-loop.
382extern int PR10379_g();
383void PR10379_f(int *len) {
David Blaikie4f4f3492011-09-10 05:35:08 +0000384 int new_len; // expected-note{{initialize the variable 'new_len' to silence this warning}}
Chandler Carruth84350692011-07-16 22:27:02 +0000385 for (int i = 0; i < 42 && PR10379_g() == 0; i++) {
386 if (PR10379_g() == 1)
387 continue;
388 if (PR10379_g() == 2)
389 PR10379_f(&new_len);
390 else if (PR10379_g() == 3)
391 PR10379_f(&new_len);
392 *len += new_len; // expected-warning {{variable 'new_len' may be uninitialized when used here}}
393 }
394}
Ted Kremenek540dda62011-08-23 20:30:50 +0000395
396// Test that sizeof(VLA) doesn't trigger a warning.
397void test_vla_sizeof(int x) {
398 double (*memory)[2][x] = malloc(sizeof(*memory)); // no-warning
399}
400
Ted Kremenek6f275422011-09-02 19:39:26 +0000401// Test absurd case of deadcode + use of blocks. This previously was a false positive
402// due to an analysis bug.
403int test_block_and_dead_code() {
404 __block int x;
405 ^{ x = 1; }();
406 if (0)
407 return x;
408 return x; // no-warning
409}
410
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000411// This previously triggered an infinite loop in the analysis.
412void PR11069(int a, int b) {
413 unsigned long flags;
414 for (;;) {
415 if (a && !b)
416 break;
417 }
418 for (;;) {
419 // This does not trigger a warning because it isn't a real use.
420 (void)(flags); // no-warning
421 }
422}
423
Ted Kremenekaa2176b2011-10-07 00:52:56 +0000424// Test uninitialized value used in loop condition.
425void rdar9432305(float *P) {
426 int i; // expected-note {{initialize the variable 'i' to silence this warning}}
427 for (; i < 10000; ++i) // expected-warning {{variable 'i' is uninitialized when used here}}
428 P[i] = 0.0f;
429}
Richard Trieu7b0a3e32012-05-03 01:09:59 +0000430
431// Test that fixits are not emitted inside macros.
432#define UNINIT(T, x, y) T x; T y = x;
433#define ASSIGN(T, x, y) T y = x;
434void test54() {
435 UNINIT(int, a, b); // expected-warning {{variable 'a' is uninitialized when used here}} \
436 // expected-note {{variable 'a' is declared here}}
437 int c; // expected-note {{initialize the variable 'c' to silence this warning}}
438 ASSIGN(int, c, d); // expected-warning {{variable 'c' is uninitialized when used here}}
439}
Richard Smith8f40dcc2012-06-16 23:34:14 +0000440
Joerg Sonnenberger7e58ad52012-06-17 23:10:39 +0000441// Taking the address is fine
442struct { struct { void *p; } a; } test55 = { { &test55.a }}; // no-warning
443struct { struct { void *p; } a; } test56 = { { &(test56.a) }}; // no-warning
444
Richard Smith8f40dcc2012-06-16 23:34:14 +0000445void uninit_in_loop() {
446 int produce(void);
447 void consume(int);
448 for (int n = 0; n < 100; ++n) {
449 int k; // expected-note {{initialize}}
450 consume(k); // expected-warning {{variable 'k' is uninitialized}}
451 k = produce();
452 }
453}
454
455void uninit_in_loop_goto() {
456 int produce(void);
457 void consume(int);
458 for (int n = 0; n < 100; ++n) {
459 goto skip_decl;
460 int k; // expected-note {{initialize}}
461skip_decl:
462 // FIXME: This should produce the 'is uninitialized' diagnostic, but we
463 // don't have enough information in the CFG to easily tell that the
464 // variable's scope has been left and re-entered.
465 consume(k); // expected-warning {{variable 'k' may be uninitialized}}
466 k = produce();
467 }
468}
Richard Smitha9e8b9e2012-07-02 23:23:04 +0000469
470typedef char jmp_buf[256];
471extern int setjmp(jmp_buf env); // implicitly returns_twice
472
473void do_stuff_and_longjmp(jmp_buf env, int *result) __attribute__((noreturn));
474
475int returns_twice() {
476 int a; // expected-note {{initialize}}
477 if (!a) { // expected-warning {{variable 'a' is uninitialized}}
478 jmp_buf env;
479 int b;
480 if (setjmp(env) == 0) {
481 do_stuff_and_longjmp(env, &b);
482 } else {
483 a = b; // no warning
484 }
485 }
486 return a;
487}
Richard Smith9532e0d2012-07-17 00:06:14 +0000488
489int compound_assign(int *arr, int n) {
490 int sum; // expected-note {{initialize}}
491 for (int i = 0; i < n; ++i)
Richard Smith6cfa78f2012-07-17 01:27:33 +0000492 sum += arr[i]; // expected-warning {{variable 'sum' is uninitialized}}
493 return sum / n;
Richard Smith9532e0d2012-07-17 00:06:14 +0000494}
495
Richard Smith6cfa78f2012-07-17 01:27:33 +0000496int compound_assign_2() {
497 int x; // expected-note {{initialize}}
498 return x += 1; // expected-warning {{variable 'x' is uninitialized}}
499}
500
501int compound_assign_3() {
502 int x; // expected-note {{initialize}}
503 x *= 0; // expected-warning {{variable 'x' is uninitialized}}
504 return x;
Richard Smith9532e0d2012-07-17 00:06:14 +0000505}
Richard Smith56df4a92012-07-24 21:02:14 +0000506
507int self_init_in_cond(int *p) {
508 int n = ((p && (0 || 1)) && (n = *p)) ? n : -1; // ok
509 return n;
510}
Ted Kremenek44ca53f2012-09-12 05:53:43 +0000511
512void test_analyzer_noreturn_aux() __attribute__((analyzer_noreturn));
513
514void test_analyzer_noreturn(int y) {
515 int x; // expected-note {{initialize the variable 'x' to silence this warning}}
516 if (y) {
517 test_analyzer_noreturn_aux();
518 ++x; // no-warning
519 }
520 else {
521 ++x; // expected-warning {{variable 'x' is uninitialized when used here}}
522 }
523}
524void test_analyzer_noreturn_2(int y) {
525 int x;
526 if (y) {
527 test_analyzer_noreturn_aux();
528 }
529 else {
530 x = 1;
531 }
532 ++x; // no-warning
533}