1 // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -fsyntax-only -fblocks %s -verify
2 // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -ftrivial-auto-var-init=pattern -fsyntax-only -fblocks %s -verify
4 typedef __typeof(sizeof(int)) size_t;
8 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
9 return x
; // expected-warning{{variable 'x' is uninitialized when used here}}
14 return x
; // no-warning
20 return x
; // no-warning
24 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
25 ++x
; // expected-warning{{variable 'x' is uninitialized when used here}}
30 int x
, y
; // expected-note{{initialize the variable 'y' to silence this warning}}
31 x
= y
; // expected-warning{{variable 'y' is uninitialized when used here}}
36 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
37 x
+= 2; // expected-warning{{variable 'x' is uninitialized when used here}}
42 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
43 if (y
) // expected-warning{{variable 'x' is used uninitialized whenever 'if' condition is false}} \
44 // expected-note{{remove the 'if' if its condition is always true}}
46 return x
; // expected-note{{uninitialized use occurs here}}
50 int x
= x
; // expected-note{{variable 'x' is declared here}}
53 // Warn with "may be uninitialized" here (not "is sometimes uninitialized"),
54 // since the self-initialization is intended to suppress a -Wuninitialized
56 return x
; // expected-warning{{variable 'x' may be uninitialized when used here}}
69 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
70 for (unsigned i
= 0 ; i
< n
; ++i
) {
75 return x
; // expected-warning{{variable 'x' may be uninitialized when used here}}
78 int test10(unsigned n
) {
79 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
80 for (unsigned i
= 0 ; i
< n
; ++i
) {
83 return x
; // expected-warning{{variable 'x' may be uninitialized when used here}}
86 int test11(unsigned n
) {
87 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
88 for (unsigned i
= 0 ; i
<= n
; ++i
) {
91 return x
; // expected-warning{{variable 'x' may be uninitialized when used here}}
94 void test12(unsigned n
) {
95 for (unsigned i
; n
; ++i
) ; // expected-warning{{variable 'i' is uninitialized when used here}} expected-note{{initialize the variable 'i' to silence this warning}}
100 return i
; // no-warning
103 // Simply don't crash on this test case.
110 int x
= x
; // no-warning: signals intended lack of initialization.
114 // Warn here with the self-init, since it does result in a use of
115 // an uninitialized variable and this is the root cause.
116 int x
= x
; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
120 // Don't warn in the following example; shows dataflow confluence.
121 char *test16_aux(void);
123 char *p
= test16_aux();
124 for (unsigned i
= 0 ; i
< 100 ; i
++)
125 p
[i
] = 'a'; // no-warning
129 // Don't warn multiple times about the same uninitialized variable
130 // along the same path.
131 int *x
; // expected-note{{initialize the variable 'x' to silence this warning}}
132 *x
= 1; // expected-warning{{variable 'x' is uninitialized when used here}}
133 *x
= 1; // no-warning
136 int test18(int x
, int y
) {
138 if (x
&& y
&& (z
= 1)) {
139 return z
; // no-warning
144 int test19_aux1(void);
145 int test19_aux2(void);
146 int test19_aux3(int *x
);
149 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z
))
150 return z
; // no-warning
155 int z
; // expected-note{{initialize the variable 'z' to silence this warning}}
156 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}}
157 return z
; // expected-note {{uninitialized use occurs here}}
161 int test21(int x
, int y
) {
162 int z
; // expected-note{{initialize the variable 'z' to silence this warning}}
163 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}}
164 return z
; // expected-note {{uninitialized use occurs here}}
170 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z
))
171 return z
; // no-warning
177 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z
) ; )
178 return z
; // no-warning
182 // The basic uninitialized value analysis doesn't have enough path-sensitivity
183 // to catch initializations relying on control-dependencies spanning multiple
184 // conditionals. This possibly can be handled by making the CFG itself
185 // represent such control-dependencies, but it is a niche case.
186 int test24(int flag
) {
187 unsigned val
; // expected-note{{initialize the variable 'val' to silence this warning}}
192 return val
; // expected-warning{{variable 'val' may be uninitialized when used here}}
196 float x
; // expected-note{{initialize the variable 'x' to silence this warning}}
197 return x
; // expected-warning{{variable 'x' is uninitialized when used here}}
202 MyInt x
; // expected-note{{initialize the variable 'x' to silence this warning}}
203 return x
; // expected-warning{{variable 'x' is uninitialized when used here}}
206 // Test handling of sizeof().
208 struct test_27
{ int x
; } *y
;
209 return sizeof(y
->x
); // no-warning
213 int len
; // expected-note{{initialize the variable 'len' to silence this warning}}
214 return sizeof(int[len
]); // expected-warning{{variable 'len' is uninitialized when used here}}
218 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
219 (void) ^{ (void) x
; }; // expected-warning{{variable 'x' is uninitialized when captured by block}}
223 static int x
; // no-warning
224 (void) ^{ (void) x
; };
228 __block
int x
; // no-warning
229 (void) ^{ (void) x
; };
234 (void) ^{ (void) test32_x
; }; // no-warning
243 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
245 return x
; // expected-warning{{variable 'x' is uninitialized when used here}}
248 // Test that this case doesn't crash.
251 ^{ y
= (x
== 0); }();
254 // Test handling of indirect goto.
257 void **pc
; // expected-note{{initialize the variable 'pc' to silence this warning}}
258 void *dummy
[] = { &&L1
, &&L2
};
260 goto *pc
; // expected-warning{{variable 'pc' is uninitialized when used here}}
265 // Test && nested in ||.
271 if ((test37_a() && (identifier
= 1)) ||
272 (test37_b() && (identifier
= 2))) {
273 return identifier
; // no-warning
278 // Test merging of path-specific dataflow values (without asserting).
279 int test38(int r
, int x
, int y
)
282 return ((r
< 0) || ((r
== 0) && (x
< y
)));
286 int y
; // expected-note{{initialize the variable 'y' to silence this warning}}
287 int z
= x
+ y
; // expected-warning {{variable 'y' is uninitialized when used here}}
293 int y
; // expected-note{{initialize the variable 'y' to silence this warning}}
294 return x
? 1 : y
; // expected-warning {{variable 'y' is uninitialized when used here}}
298 int y
; // expected-note{{initialize the variable 'y' to silence this warning}}
299 if (x
) y
= 1; // expected-warning{{variable 'y' is used uninitialized whenever 'if' condition is false}} \
300 // expected-note{{remove the 'if' if its condition is always true}}
301 return y
; // expected-note{{uninitialized use occurs here}}
306 a
= 30; // no-warning
309 void test43_aux(int x
);
311 int x
; // expected-note{{initialize the variable 'x' to silence this warning}}
312 for (i
= 0 ; i
< 10; i
++)
313 test43_aux(x
++); // expected-warning {{variable 'x' is uninitialized when used here}}
318 int y
; // expected-note{{initialize the variable 'y' to silence this warning}}
319 for (i
= 0; i
< 10; i
++ ) {
320 test43_aux(x
++); // no-warning
321 x
+= y
; // expected-warning {{variable 'y' is uninitialized when used here}}
326 int x
= 1, y
= x
+ 1;
334 int i
; // expected-note{{initialize the variable 'i' to silence this warning}}
335 int j
= i
? : 1; // expected-warning {{variable 'i' is uninitialized when used here}}
340 return i
? : 0; // no-warning
346 return &a
? : i
; // no-warning
351 char c
[1 ? : 2]; // no-warning
360 return a
; // no-warning
363 // FIXME: This is a false positive, but it tests logical operations in switch statements.
364 int test52(int a
, int b
) {
365 int x
; // expected-note {{initialize the variable 'x' to silence this warning}}
366 switch (a
|| b
) { // expected-warning {{switch condition has boolean value}}
374 return x
; // expected-warning {{variable 'x' may be uninitialized when used here}}
378 int x
; // expected-note {{initialize the variable 'x' to silence this warning}}
379 int y
= (x
); // expected-warning {{variable 'x' is uninitialized when used here}}
382 // This CFG caused the uninitialized values warning to inf-loop.
383 extern int PR10379_g(void);
384 void PR10379_f(int *len
) {
385 int new_len
; // expected-note{{initialize the variable 'new_len' to silence this warning}}
386 for (int i
= 0; i
< 42 && PR10379_g() == 0; i
++) {
387 if (PR10379_g() == 1)
389 if (PR10379_g() == 2)
391 else if (PR10379_g() == 3)
393 *len
+= new_len
; // expected-warning {{variable 'new_len' may be uninitialized when used here}}
397 // Test that sizeof(VLA) doesn't trigger a warning.
398 void test_vla_sizeof(int x
) {
399 double (*memory
)[2][x
] = malloc(sizeof(*memory
)); // no-warning
402 // Test absurd case of deadcode + use of blocks. This previously was a false positive
403 // due to an analysis bug.
404 int test_block_and_dead_code(void) {
409 return x
; // no-warning
412 // This previously triggered an infinite loop in the analysis.
413 void PR11069(int a
, int b
) {
420 // This does not trigger a warning because it isn't a real use.
421 (void)(flags
); // no-warning
425 // Test uninitialized value used in loop condition.
426 void rdar9432305(float *P
) {
427 int i
; // expected-note {{initialize the variable 'i' to silence this warning}}
428 for (; i
< 10000; ++i
) // expected-warning {{variable 'i' is uninitialized when used here}}
432 // Test that fixits are not emitted inside macros.
433 #define UNINIT(T, x, y) T x; T y = x;
434 #define ASSIGN(T, x, y) T y = x;
436 UNINIT(int, a
, b
); // expected-warning {{variable 'a' is uninitialized when used here}} \
437 // expected-note {{variable 'a' is declared here}}
438 int c
; // expected-note {{initialize the variable 'c' to silence this warning}}
439 ASSIGN(int, c
, d
); // expected-warning {{variable 'c' is uninitialized when used here}}
442 // Taking the address is fine
443 struct { struct { void *p
; } a
; } test55
= { { &test55
.a
}}; // no-warning
444 struct { struct { void *p
; } a
; } test56
= { { &(test56
.a
) }}; // no-warning
446 void uninit_in_loop(void) {
449 for (int n
= 0; n
< 100; ++n
) {
450 int k
; // expected-note {{initialize}}
451 consume(k
); // expected-warning {{variable 'k' is uninitialized}}
456 void uninit_in_loop_goto(void) {
459 for (int n
= 0; n
< 100; ++n
) {
461 int k
; // expected-note {{initialize}}
463 // FIXME: This should produce the 'is uninitialized' diagnostic, but we
464 // don't have enough information in the CFG to easily tell that the
465 // variable's scope has been left and re-entered.
466 consume(k
); // expected-warning {{variable 'k' may be uninitialized}}
471 typedef char jmp_buf[256];
472 extern int setjmp(jmp_buf env
); // implicitly returns_twice
474 void do_stuff_and_longjmp(jmp_buf env
, int *result
) __attribute__((noreturn
));
476 int returns_twice(void) {
477 int a
; // expected-note {{initialize}}
478 if (!a
) { // expected-warning {{variable 'a' is uninitialized}}
481 if (setjmp(env
) == 0) {
482 do_stuff_and_longjmp(env
, &b
);
490 int compound_assign(int *arr
, int n
) {
491 int sum
; // expected-note {{initialize}}
492 for (int i
= 0; i
< n
; ++i
)
493 sum
+= arr
[i
]; // expected-warning {{variable 'sum' is uninitialized}}
497 int compound_assign_2(void) {
498 int x
; // expected-note {{initialize}}
499 return x
+= 1; // expected-warning {{variable 'x' is uninitialized}}
502 int compound_assign_3(void) {
503 int x
; // expected-note {{initialize}}
504 x
*= 0; // expected-warning {{variable 'x' is uninitialized}}
508 int self_init_in_cond(int *p
) {
509 int n
= ((p
&& (0 || 1)) && (n
= *p
)) ? n
: -1; // ok
513 void test_analyzer_noreturn_aux(void) __attribute__((analyzer_noreturn
));
515 void test_analyzer_noreturn(int y
) {
516 int x
; // expected-note {{initialize the variable 'x' to silence this warning}}
518 test_analyzer_noreturn_aux();
522 ++x
; // expected-warning {{variable 'x' is uninitialized when used here}}
525 void test_analyzer_noreturn_2(int y
) {
528 test_analyzer_noreturn_aux();