1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -fsyntax-only -fblocks %s -verify
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc typedef __typeof(sizeof(int)) size_t;
4*f4a2713aSLionel Sambuc void *malloc(size_t);
5*f4a2713aSLionel Sambuc
test1()6*f4a2713aSLionel Sambuc int test1() {
7*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
8*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' is uninitialized when used here}}
9*f4a2713aSLionel Sambuc }
10*f4a2713aSLionel Sambuc
test2()11*f4a2713aSLionel Sambuc int test2() {
12*f4a2713aSLionel Sambuc int x = 0;
13*f4a2713aSLionel Sambuc return x; // no-warning
14*f4a2713aSLionel Sambuc }
15*f4a2713aSLionel Sambuc
test3()16*f4a2713aSLionel Sambuc int test3() {
17*f4a2713aSLionel Sambuc int x;
18*f4a2713aSLionel Sambuc x = 0;
19*f4a2713aSLionel Sambuc return x; // no-warning
20*f4a2713aSLionel Sambuc }
21*f4a2713aSLionel Sambuc
test4()22*f4a2713aSLionel Sambuc int test4() {
23*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
24*f4a2713aSLionel Sambuc ++x; // expected-warning{{variable 'x' is uninitialized when used here}}
25*f4a2713aSLionel Sambuc return x;
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc
test5()28*f4a2713aSLionel Sambuc int test5() {
29*f4a2713aSLionel Sambuc int x, y; // expected-note{{initialize the variable 'y' to silence this warning}}
30*f4a2713aSLionel Sambuc x = y; // expected-warning{{variable 'y' is uninitialized when used here}}
31*f4a2713aSLionel Sambuc return x;
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc
test6()34*f4a2713aSLionel Sambuc int test6() {
35*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
36*f4a2713aSLionel Sambuc x += 2; // expected-warning{{variable 'x' is uninitialized when used here}}
37*f4a2713aSLionel Sambuc return x;
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc
test7(int y)40*f4a2713aSLionel Sambuc int test7(int y) {
41*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
42*f4a2713aSLionel Sambuc if (y) // expected-warning{{variable 'x' is used uninitialized whenever 'if' condition is false}} \
43*f4a2713aSLionel Sambuc // expected-note{{remove the 'if' if its condition is always true}}
44*f4a2713aSLionel Sambuc x = 1;
45*f4a2713aSLionel Sambuc return x; // expected-note{{uninitialized use occurs here}}
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc
test7b(int y)48*f4a2713aSLionel Sambuc int test7b(int y) {
49*f4a2713aSLionel Sambuc int x = x; // expected-note{{variable 'x' is declared here}}
50*f4a2713aSLionel Sambuc if (y)
51*f4a2713aSLionel Sambuc x = 1;
52*f4a2713aSLionel Sambuc // Warn with "may be uninitialized" here (not "is sometimes uninitialized"),
53*f4a2713aSLionel Sambuc // since the self-initialization is intended to suppress a -Wuninitialized
54*f4a2713aSLionel Sambuc // warning.
55*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
56*f4a2713aSLionel Sambuc }
57*f4a2713aSLionel Sambuc
test8(int y)58*f4a2713aSLionel Sambuc int test8(int y) {
59*f4a2713aSLionel Sambuc int x;
60*f4a2713aSLionel Sambuc if (y)
61*f4a2713aSLionel Sambuc x = 1;
62*f4a2713aSLionel Sambuc else
63*f4a2713aSLionel Sambuc x = 0;
64*f4a2713aSLionel Sambuc return x;
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc
test9(int n)67*f4a2713aSLionel Sambuc int test9(int n) {
68*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
69*f4a2713aSLionel Sambuc for (unsigned i = 0 ; i < n; ++i) {
70*f4a2713aSLionel Sambuc if (i == n - 1)
71*f4a2713aSLionel Sambuc break;
72*f4a2713aSLionel Sambuc x = 1;
73*f4a2713aSLionel Sambuc }
74*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
75*f4a2713aSLionel Sambuc }
76*f4a2713aSLionel Sambuc
test10(unsigned n)77*f4a2713aSLionel Sambuc int test10(unsigned n) {
78*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
79*f4a2713aSLionel Sambuc for (unsigned i = 0 ; i < n; ++i) {
80*f4a2713aSLionel Sambuc x = 1;
81*f4a2713aSLionel Sambuc }
82*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
83*f4a2713aSLionel Sambuc }
84*f4a2713aSLionel Sambuc
test11(unsigned n)85*f4a2713aSLionel Sambuc int test11(unsigned n) {
86*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
87*f4a2713aSLionel Sambuc for (unsigned i = 0 ; i <= n; ++i) {
88*f4a2713aSLionel Sambuc x = 1;
89*f4a2713aSLionel Sambuc }
90*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc
test12(unsigned n)93*f4a2713aSLionel Sambuc void test12(unsigned n) {
94*f4a2713aSLionel Sambuc for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is uninitialized when used here}} expected-note{{initialize the variable 'i' to silence this warning}}
95*f4a2713aSLionel Sambuc }
96*f4a2713aSLionel Sambuc
test13()97*f4a2713aSLionel Sambuc int test13() {
98*f4a2713aSLionel Sambuc static int i;
99*f4a2713aSLionel Sambuc return i; // no-warning
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc
102*f4a2713aSLionel Sambuc // Simply don't crash on this test case.
test14()103*f4a2713aSLionel Sambuc void test14() {
104*f4a2713aSLionel Sambuc const char *p = 0;
105*f4a2713aSLionel Sambuc for (;;) {}
106*f4a2713aSLionel Sambuc }
107*f4a2713aSLionel Sambuc
test15()108*f4a2713aSLionel Sambuc void test15() {
109*f4a2713aSLionel Sambuc int x = x; // no-warning: signals intended lack of initialization.
110*f4a2713aSLionel Sambuc }
111*f4a2713aSLionel Sambuc
test15b()112*f4a2713aSLionel Sambuc int test15b() {
113*f4a2713aSLionel Sambuc // Warn here with the self-init, since it does result in a use of
114*f4a2713aSLionel Sambuc // an unintialized variable and this is the root cause.
115*f4a2713aSLionel Sambuc int x = x; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}}
116*f4a2713aSLionel Sambuc return x;
117*f4a2713aSLionel Sambuc }
118*f4a2713aSLionel Sambuc
119*f4a2713aSLionel Sambuc // Don't warn in the following example; shows dataflow confluence.
120*f4a2713aSLionel Sambuc char *test16_aux();
test16()121*f4a2713aSLionel Sambuc void test16() {
122*f4a2713aSLionel Sambuc char *p = test16_aux();
123*f4a2713aSLionel Sambuc for (unsigned i = 0 ; i < 100 ; i++)
124*f4a2713aSLionel Sambuc p[i] = 'a'; // no-warning
125*f4a2713aSLionel Sambuc }
126*f4a2713aSLionel Sambuc
test17()127*f4a2713aSLionel Sambuc void test17() {
128*f4a2713aSLionel Sambuc // Don't warn multiple times about the same uninitialized variable
129*f4a2713aSLionel Sambuc // along the same path.
130*f4a2713aSLionel Sambuc int *x; // expected-note{{initialize the variable 'x' to silence this warning}}
131*f4a2713aSLionel Sambuc *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}}
132*f4a2713aSLionel Sambuc *x = 1; // no-warning
133*f4a2713aSLionel Sambuc }
134*f4a2713aSLionel Sambuc
test18(int x,int y)135*f4a2713aSLionel Sambuc int test18(int x, int y) {
136*f4a2713aSLionel Sambuc int z;
137*f4a2713aSLionel Sambuc if (x && y && (z = 1)) {
138*f4a2713aSLionel Sambuc return z; // no-warning
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc return 0;
141*f4a2713aSLionel Sambuc }
142*f4a2713aSLionel Sambuc
143*f4a2713aSLionel Sambuc int test19_aux1();
144*f4a2713aSLionel Sambuc int test19_aux2();
145*f4a2713aSLionel Sambuc int test19_aux3(int *x);
test19()146*f4a2713aSLionel Sambuc int test19() {
147*f4a2713aSLionel Sambuc int z;
148*f4a2713aSLionel Sambuc if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
149*f4a2713aSLionel Sambuc return z; // no-warning
150*f4a2713aSLionel Sambuc return 0;
151*f4a2713aSLionel Sambuc }
152*f4a2713aSLionel Sambuc
test20()153*f4a2713aSLionel Sambuc int test20() {
154*f4a2713aSLionel Sambuc int z; // expected-note{{initialize the variable 'z' to silence this warning}}
155*f4a2713aSLionel Sambuc 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*f4a2713aSLionel Sambuc return z; // expected-note {{uninitialized use occurs here}}
157*f4a2713aSLionel Sambuc return 0;
158*f4a2713aSLionel Sambuc }
159*f4a2713aSLionel Sambuc
test21(int x,int y)160*f4a2713aSLionel Sambuc int test21(int x, int y) {
161*f4a2713aSLionel Sambuc int z; // expected-note{{initialize the variable 'z' to silence this warning}}
162*f4a2713aSLionel Sambuc 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*f4a2713aSLionel Sambuc return z; // expected-note {{uninitialized use occurs here}}
164*f4a2713aSLionel Sambuc return 0;
165*f4a2713aSLionel Sambuc }
166*f4a2713aSLionel Sambuc
test22()167*f4a2713aSLionel Sambuc int test22() {
168*f4a2713aSLionel Sambuc int z;
169*f4a2713aSLionel Sambuc while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
170*f4a2713aSLionel Sambuc return z; // no-warning
171*f4a2713aSLionel Sambuc return 0;
172*f4a2713aSLionel Sambuc }
173*f4a2713aSLionel Sambuc
test23()174*f4a2713aSLionel Sambuc int test23() {
175*f4a2713aSLionel Sambuc int z;
176*f4a2713aSLionel Sambuc for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
177*f4a2713aSLionel Sambuc return z; // no-warning
178*f4a2713aSLionel Sambuc return 0;
179*f4a2713aSLionel Sambuc }
180*f4a2713aSLionel Sambuc
181*f4a2713aSLionel Sambuc // The basic uninitialized value analysis doesn't have enough path-sensitivity
182*f4a2713aSLionel Sambuc // to catch initializations relying on control-dependencies spanning multiple
183*f4a2713aSLionel Sambuc // conditionals. This possibly can be handled by making the CFG itself
184*f4a2713aSLionel Sambuc // represent such control-dependencies, but it is a niche case.
test24(int flag)185*f4a2713aSLionel Sambuc int test24(int flag) {
186*f4a2713aSLionel Sambuc unsigned val; // expected-note{{initialize the variable 'val' to silence this warning}}
187*f4a2713aSLionel Sambuc if (flag)
188*f4a2713aSLionel Sambuc val = 1;
189*f4a2713aSLionel Sambuc if (!flag)
190*f4a2713aSLionel Sambuc val = 1;
191*f4a2713aSLionel Sambuc return val; // expected-warning{{variable 'val' may be uninitialized when used here}}
192*f4a2713aSLionel Sambuc }
193*f4a2713aSLionel Sambuc
test25()194*f4a2713aSLionel Sambuc float test25() {
195*f4a2713aSLionel Sambuc float x; // expected-note{{initialize the variable 'x' to silence this warning}}
196*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' is uninitialized when used here}}
197*f4a2713aSLionel Sambuc }
198*f4a2713aSLionel Sambuc
199*f4a2713aSLionel Sambuc typedef int MyInt;
test26()200*f4a2713aSLionel Sambuc MyInt test26() {
201*f4a2713aSLionel Sambuc MyInt x; // expected-note{{initialize the variable 'x' to silence this warning}}
202*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' is uninitialized when used here}}
203*f4a2713aSLionel Sambuc }
204*f4a2713aSLionel Sambuc
205*f4a2713aSLionel Sambuc // Test handling of sizeof().
test27()206*f4a2713aSLionel Sambuc int test27() {
207*f4a2713aSLionel Sambuc struct test_27 { int x; } *y;
208*f4a2713aSLionel Sambuc return sizeof(y->x); // no-warning
209*f4a2713aSLionel Sambuc }
210*f4a2713aSLionel Sambuc
test28()211*f4a2713aSLionel Sambuc int test28() {
212*f4a2713aSLionel Sambuc int len; // expected-note{{initialize the variable 'len' to silence this warning}}
213*f4a2713aSLionel Sambuc return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}}
214*f4a2713aSLionel Sambuc }
215*f4a2713aSLionel Sambuc
test29()216*f4a2713aSLionel Sambuc void test29() {
217*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
218*f4a2713aSLionel Sambuc (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}}
219*f4a2713aSLionel Sambuc }
220*f4a2713aSLionel Sambuc
test30()221*f4a2713aSLionel Sambuc void test30() {
222*f4a2713aSLionel Sambuc static int x; // no-warning
223*f4a2713aSLionel Sambuc (void) ^{ (void) x; };
224*f4a2713aSLionel Sambuc }
225*f4a2713aSLionel Sambuc
test31()226*f4a2713aSLionel Sambuc void test31() {
227*f4a2713aSLionel Sambuc __block int x; // no-warning
228*f4a2713aSLionel Sambuc (void) ^{ (void) x; };
229*f4a2713aSLionel Sambuc }
230*f4a2713aSLionel Sambuc
231*f4a2713aSLionel Sambuc int test32_x;
test32()232*f4a2713aSLionel Sambuc void test32() {
233*f4a2713aSLionel Sambuc (void) ^{ (void) test32_x; }; // no-warning
234*f4a2713aSLionel Sambuc }
235*f4a2713aSLionel Sambuc
test_33()236*f4a2713aSLionel Sambuc void test_33() {
237*f4a2713aSLionel Sambuc int x; // no-warning
238*f4a2713aSLionel Sambuc (void) x;
239*f4a2713aSLionel Sambuc }
240*f4a2713aSLionel Sambuc
test_34()241*f4a2713aSLionel Sambuc int test_34() {
242*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
243*f4a2713aSLionel Sambuc (void) x;
244*f4a2713aSLionel Sambuc return x; // expected-warning{{variable 'x' is uninitialized when used here}}
245*f4a2713aSLionel Sambuc }
246*f4a2713aSLionel Sambuc
247*f4a2713aSLionel Sambuc // Test that this case doesn't crash.
test35(int x)248*f4a2713aSLionel Sambuc void test35(int x) {
249*f4a2713aSLionel Sambuc __block int y = 0;
250*f4a2713aSLionel Sambuc ^{ y = (x == 0); }();
251*f4a2713aSLionel Sambuc }
252*f4a2713aSLionel Sambuc
253*f4a2713aSLionel Sambuc // Test handling of indirect goto.
test36()254*f4a2713aSLionel Sambuc void test36()
255*f4a2713aSLionel Sambuc {
256*f4a2713aSLionel Sambuc void **pc; // expected-note{{initialize the variable 'pc' to silence this warning}}
257*f4a2713aSLionel Sambuc void *dummy[] = { &&L1, &&L2 };
258*f4a2713aSLionel Sambuc L1:
259*f4a2713aSLionel Sambuc goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}}
260*f4a2713aSLionel Sambuc L2:
261*f4a2713aSLionel Sambuc goto *pc;
262*f4a2713aSLionel Sambuc }
263*f4a2713aSLionel Sambuc
264*f4a2713aSLionel Sambuc // Test && nested in ||.
265*f4a2713aSLionel Sambuc int test37_a();
266*f4a2713aSLionel Sambuc int test37_b();
test37()267*f4a2713aSLionel Sambuc int test37()
268*f4a2713aSLionel Sambuc {
269*f4a2713aSLionel Sambuc int identifier;
270*f4a2713aSLionel Sambuc if ((test37_a() && (identifier = 1)) ||
271*f4a2713aSLionel Sambuc (test37_b() && (identifier = 2))) {
272*f4a2713aSLionel Sambuc return identifier; // no-warning
273*f4a2713aSLionel Sambuc }
274*f4a2713aSLionel Sambuc return 0;
275*f4a2713aSLionel Sambuc }
276*f4a2713aSLionel Sambuc
277*f4a2713aSLionel Sambuc // Test merging of path-specific dataflow values (without asserting).
test38(int r,int x,int y)278*f4a2713aSLionel Sambuc int test38(int r, int x, int y)
279*f4a2713aSLionel Sambuc {
280*f4a2713aSLionel Sambuc int z;
281*f4a2713aSLionel Sambuc return ((r < 0) || ((r == 0) && (x < y)));
282*f4a2713aSLionel Sambuc }
283*f4a2713aSLionel Sambuc
test39(int x)284*f4a2713aSLionel Sambuc int test39(int x) {
285*f4a2713aSLionel Sambuc int y; // expected-note{{initialize the variable 'y' to silence this warning}}
286*f4a2713aSLionel Sambuc int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}}
287*f4a2713aSLionel Sambuc return z;
288*f4a2713aSLionel Sambuc }
289*f4a2713aSLionel Sambuc
290*f4a2713aSLionel Sambuc
test40(int x)291*f4a2713aSLionel Sambuc int test40(int x) {
292*f4a2713aSLionel Sambuc int y; // expected-note{{initialize the variable 'y' to silence this warning}}
293*f4a2713aSLionel Sambuc return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}}
294*f4a2713aSLionel Sambuc }
295*f4a2713aSLionel Sambuc
test41(int x)296*f4a2713aSLionel Sambuc int test41(int x) {
297*f4a2713aSLionel Sambuc int y; // expected-note{{initialize the variable 'y' to silence this warning}}
298*f4a2713aSLionel Sambuc if (x) y = 1; // expected-warning{{variable 'y' is used uninitialized whenever 'if' condition is false}} \
299*f4a2713aSLionel Sambuc // expected-note{{remove the 'if' if its condition is always true}}
300*f4a2713aSLionel Sambuc return y; // expected-note{{uninitialized use occurs here}}
301*f4a2713aSLionel Sambuc }
302*f4a2713aSLionel Sambuc
test42()303*f4a2713aSLionel Sambuc void test42() {
304*f4a2713aSLionel Sambuc int a;
305*f4a2713aSLionel Sambuc a = 30; // no-warning
306*f4a2713aSLionel Sambuc }
307*f4a2713aSLionel Sambuc
308*f4a2713aSLionel Sambuc void test43_aux(int x);
test43(int i)309*f4a2713aSLionel Sambuc void test43(int i) {
310*f4a2713aSLionel Sambuc int x; // expected-note{{initialize the variable 'x' to silence this warning}}
311*f4a2713aSLionel Sambuc for (i = 0 ; i < 10; i++)
312*f4a2713aSLionel Sambuc test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}}
313*f4a2713aSLionel Sambuc }
314*f4a2713aSLionel Sambuc
test44(int i)315*f4a2713aSLionel Sambuc void test44(int i) {
316*f4a2713aSLionel Sambuc int x = i;
317*f4a2713aSLionel Sambuc int y; // expected-note{{initialize the variable 'y' to silence this warning}}
318*f4a2713aSLionel Sambuc for (i = 0; i < 10; i++ ) {
319*f4a2713aSLionel Sambuc test43_aux(x++); // no-warning
320*f4a2713aSLionel Sambuc x += y; // expected-warning {{variable 'y' is uninitialized when used here}}
321*f4a2713aSLionel Sambuc }
322*f4a2713aSLionel Sambuc }
323*f4a2713aSLionel Sambuc
test45(int j)324*f4a2713aSLionel Sambuc int test45(int j) {
325*f4a2713aSLionel Sambuc int x = 1, y = x + 1;
326*f4a2713aSLionel Sambuc if (y) // no-warning
327*f4a2713aSLionel Sambuc return x;
328*f4a2713aSLionel Sambuc return y;
329*f4a2713aSLionel Sambuc }
330*f4a2713aSLionel Sambuc
test46()331*f4a2713aSLionel Sambuc void test46()
332*f4a2713aSLionel Sambuc {
333*f4a2713aSLionel Sambuc int i; // expected-note{{initialize the variable 'i' to silence this warning}}
334*f4a2713aSLionel Sambuc int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}}
335*f4a2713aSLionel Sambuc }
336*f4a2713aSLionel Sambuc
test47(int * i)337*f4a2713aSLionel Sambuc void *test47(int *i)
338*f4a2713aSLionel Sambuc {
339*f4a2713aSLionel Sambuc return i ? : 0; // no-warning
340*f4a2713aSLionel Sambuc }
341*f4a2713aSLionel Sambuc
test49(int * i)342*f4a2713aSLionel Sambuc void *test49(int *i)
343*f4a2713aSLionel Sambuc {
344*f4a2713aSLionel Sambuc int a;
345*f4a2713aSLionel Sambuc return &a ? : i; // no-warning
346*f4a2713aSLionel Sambuc }
347*f4a2713aSLionel Sambuc
test50()348*f4a2713aSLionel Sambuc void test50()
349*f4a2713aSLionel Sambuc {
350*f4a2713aSLionel Sambuc char c[1 ? : 2]; // no-warning
351*f4a2713aSLionel Sambuc }
352*f4a2713aSLionel Sambuc
test51(void)353*f4a2713aSLionel Sambuc int test51(void)
354*f4a2713aSLionel Sambuc {
355*f4a2713aSLionel Sambuc __block int a;
356*f4a2713aSLionel Sambuc ^(void) {
357*f4a2713aSLionel Sambuc a = 42;
358*f4a2713aSLionel Sambuc }();
359*f4a2713aSLionel Sambuc return a; // no-warning
360*f4a2713aSLionel Sambuc }
361*f4a2713aSLionel Sambuc
362*f4a2713aSLionel Sambuc // FIXME: This is a false positive, but it tests logical operations in switch statements.
test52(int a,int b)363*f4a2713aSLionel Sambuc int test52(int a, int b) {
364*f4a2713aSLionel Sambuc int x; // expected-note {{initialize the variable 'x' to silence this warning}}
365*f4a2713aSLionel Sambuc switch (a || b) { // expected-warning {{switch condition has boolean value}}
366*f4a2713aSLionel Sambuc case 0:
367*f4a2713aSLionel Sambuc x = 1;
368*f4a2713aSLionel Sambuc break;
369*f4a2713aSLionel Sambuc case 1:
370*f4a2713aSLionel Sambuc x = 2;
371*f4a2713aSLionel Sambuc break;
372*f4a2713aSLionel Sambuc }
373*f4a2713aSLionel Sambuc return x; // expected-warning {{variable 'x' may be uninitialized when used here}}
374*f4a2713aSLionel Sambuc }
375*f4a2713aSLionel Sambuc
test53()376*f4a2713aSLionel Sambuc void test53() {
377*f4a2713aSLionel Sambuc int x; // expected-note {{initialize the variable 'x' to silence this warning}}
378*f4a2713aSLionel Sambuc int y = (x); // expected-warning {{variable 'x' is uninitialized when used here}}
379*f4a2713aSLionel Sambuc }
380*f4a2713aSLionel Sambuc
381*f4a2713aSLionel Sambuc // This CFG caused the uninitialized values warning to inf-loop.
382*f4a2713aSLionel Sambuc extern int PR10379_g();
PR10379_f(int * len)383*f4a2713aSLionel Sambuc void PR10379_f(int *len) {
384*f4a2713aSLionel Sambuc int new_len; // expected-note{{initialize the variable 'new_len' to silence this warning}}
385*f4a2713aSLionel Sambuc for (int i = 0; i < 42 && PR10379_g() == 0; i++) {
386*f4a2713aSLionel Sambuc if (PR10379_g() == 1)
387*f4a2713aSLionel Sambuc continue;
388*f4a2713aSLionel Sambuc if (PR10379_g() == 2)
389*f4a2713aSLionel Sambuc PR10379_f(&new_len);
390*f4a2713aSLionel Sambuc else if (PR10379_g() == 3)
391*f4a2713aSLionel Sambuc PR10379_f(&new_len);
392*f4a2713aSLionel Sambuc *len += new_len; // expected-warning {{variable 'new_len' may be uninitialized when used here}}
393*f4a2713aSLionel Sambuc }
394*f4a2713aSLionel Sambuc }
395*f4a2713aSLionel Sambuc
396*f4a2713aSLionel Sambuc // Test that sizeof(VLA) doesn't trigger a warning.
test_vla_sizeof(int x)397*f4a2713aSLionel Sambuc void test_vla_sizeof(int x) {
398*f4a2713aSLionel Sambuc double (*memory)[2][x] = malloc(sizeof(*memory)); // no-warning
399*f4a2713aSLionel Sambuc }
400*f4a2713aSLionel Sambuc
401*f4a2713aSLionel Sambuc // Test absurd case of deadcode + use of blocks. This previously was a false positive
402*f4a2713aSLionel Sambuc // due to an analysis bug.
test_block_and_dead_code()403*f4a2713aSLionel Sambuc int test_block_and_dead_code() {
404*f4a2713aSLionel Sambuc __block int x;
405*f4a2713aSLionel Sambuc ^{ x = 1; }();
406*f4a2713aSLionel Sambuc if (0)
407*f4a2713aSLionel Sambuc return x;
408*f4a2713aSLionel Sambuc return x; // no-warning
409*f4a2713aSLionel Sambuc }
410*f4a2713aSLionel Sambuc
411*f4a2713aSLionel Sambuc // This previously triggered an infinite loop in the analysis.
PR11069(int a,int b)412*f4a2713aSLionel Sambuc void PR11069(int a, int b) {
413*f4a2713aSLionel Sambuc unsigned long flags;
414*f4a2713aSLionel Sambuc for (;;) {
415*f4a2713aSLionel Sambuc if (a && !b)
416*f4a2713aSLionel Sambuc break;
417*f4a2713aSLionel Sambuc }
418*f4a2713aSLionel Sambuc for (;;) {
419*f4a2713aSLionel Sambuc // This does not trigger a warning because it isn't a real use.
420*f4a2713aSLionel Sambuc (void)(flags); // no-warning
421*f4a2713aSLionel Sambuc }
422*f4a2713aSLionel Sambuc }
423*f4a2713aSLionel Sambuc
424*f4a2713aSLionel Sambuc // Test uninitialized value used in loop condition.
rdar9432305(float * P)425*f4a2713aSLionel Sambuc void rdar9432305(float *P) {
426*f4a2713aSLionel Sambuc int i; // expected-note {{initialize the variable 'i' to silence this warning}}
427*f4a2713aSLionel Sambuc for (; i < 10000; ++i) // expected-warning {{variable 'i' is uninitialized when used here}}
428*f4a2713aSLionel Sambuc P[i] = 0.0f;
429*f4a2713aSLionel Sambuc }
430*f4a2713aSLionel Sambuc
431*f4a2713aSLionel Sambuc // Test that fixits are not emitted inside macros.
432*f4a2713aSLionel Sambuc #define UNINIT(T, x, y) T x; T y = x;
433*f4a2713aSLionel Sambuc #define ASSIGN(T, x, y) T y = x;
test54()434*f4a2713aSLionel Sambuc void test54() {
435*f4a2713aSLionel Sambuc UNINIT(int, a, b); // expected-warning {{variable 'a' is uninitialized when used here}} \
436*f4a2713aSLionel Sambuc // expected-note {{variable 'a' is declared here}}
437*f4a2713aSLionel Sambuc int c; // expected-note {{initialize the variable 'c' to silence this warning}}
438*f4a2713aSLionel Sambuc ASSIGN(int, c, d); // expected-warning {{variable 'c' is uninitialized when used here}}
439*f4a2713aSLionel Sambuc }
440*f4a2713aSLionel Sambuc
441*f4a2713aSLionel Sambuc // Taking the address is fine
442*f4a2713aSLionel Sambuc struct { struct { void *p; } a; } test55 = { { &test55.a }}; // no-warning
443*f4a2713aSLionel Sambuc struct { struct { void *p; } a; } test56 = { { &(test56.a) }}; // no-warning
444*f4a2713aSLionel Sambuc
uninit_in_loop()445*f4a2713aSLionel Sambuc void uninit_in_loop() {
446*f4a2713aSLionel Sambuc int produce(void);
447*f4a2713aSLionel Sambuc void consume(int);
448*f4a2713aSLionel Sambuc for (int n = 0; n < 100; ++n) {
449*f4a2713aSLionel Sambuc int k; // expected-note {{initialize}}
450*f4a2713aSLionel Sambuc consume(k); // expected-warning {{variable 'k' is uninitialized}}
451*f4a2713aSLionel Sambuc k = produce();
452*f4a2713aSLionel Sambuc }
453*f4a2713aSLionel Sambuc }
454*f4a2713aSLionel Sambuc
uninit_in_loop_goto()455*f4a2713aSLionel Sambuc void uninit_in_loop_goto() {
456*f4a2713aSLionel Sambuc int produce(void);
457*f4a2713aSLionel Sambuc void consume(int);
458*f4a2713aSLionel Sambuc for (int n = 0; n < 100; ++n) {
459*f4a2713aSLionel Sambuc goto skip_decl;
460*f4a2713aSLionel Sambuc int k; // expected-note {{initialize}}
461*f4a2713aSLionel Sambuc skip_decl:
462*f4a2713aSLionel Sambuc // FIXME: This should produce the 'is uninitialized' diagnostic, but we
463*f4a2713aSLionel Sambuc // don't have enough information in the CFG to easily tell that the
464*f4a2713aSLionel Sambuc // variable's scope has been left and re-entered.
465*f4a2713aSLionel Sambuc consume(k); // expected-warning {{variable 'k' may be uninitialized}}
466*f4a2713aSLionel Sambuc k = produce();
467*f4a2713aSLionel Sambuc }
468*f4a2713aSLionel Sambuc }
469*f4a2713aSLionel Sambuc
470*f4a2713aSLionel Sambuc typedef char jmp_buf[256];
471*f4a2713aSLionel Sambuc extern int setjmp(jmp_buf env); // implicitly returns_twice
472*f4a2713aSLionel Sambuc
473*f4a2713aSLionel Sambuc void do_stuff_and_longjmp(jmp_buf env, int *result) __attribute__((noreturn));
474*f4a2713aSLionel Sambuc
returns_twice()475*f4a2713aSLionel Sambuc int returns_twice() {
476*f4a2713aSLionel Sambuc int a; // expected-note {{initialize}}
477*f4a2713aSLionel Sambuc if (!a) { // expected-warning {{variable 'a' is uninitialized}}
478*f4a2713aSLionel Sambuc jmp_buf env;
479*f4a2713aSLionel Sambuc int b;
480*f4a2713aSLionel Sambuc if (setjmp(env) == 0) {
481*f4a2713aSLionel Sambuc do_stuff_and_longjmp(env, &b);
482*f4a2713aSLionel Sambuc } else {
483*f4a2713aSLionel Sambuc a = b; // no warning
484*f4a2713aSLionel Sambuc }
485*f4a2713aSLionel Sambuc }
486*f4a2713aSLionel Sambuc return a;
487*f4a2713aSLionel Sambuc }
488*f4a2713aSLionel Sambuc
compound_assign(int * arr,int n)489*f4a2713aSLionel Sambuc int compound_assign(int *arr, int n) {
490*f4a2713aSLionel Sambuc int sum; // expected-note {{initialize}}
491*f4a2713aSLionel Sambuc for (int i = 0; i < n; ++i)
492*f4a2713aSLionel Sambuc sum += arr[i]; // expected-warning {{variable 'sum' is uninitialized}}
493*f4a2713aSLionel Sambuc return sum / n;
494*f4a2713aSLionel Sambuc }
495*f4a2713aSLionel Sambuc
compound_assign_2()496*f4a2713aSLionel Sambuc int compound_assign_2() {
497*f4a2713aSLionel Sambuc int x; // expected-note {{initialize}}
498*f4a2713aSLionel Sambuc return x += 1; // expected-warning {{variable 'x' is uninitialized}}
499*f4a2713aSLionel Sambuc }
500*f4a2713aSLionel Sambuc
compound_assign_3()501*f4a2713aSLionel Sambuc int compound_assign_3() {
502*f4a2713aSLionel Sambuc int x; // expected-note {{initialize}}
503*f4a2713aSLionel Sambuc x *= 0; // expected-warning {{variable 'x' is uninitialized}}
504*f4a2713aSLionel Sambuc return x;
505*f4a2713aSLionel Sambuc }
506*f4a2713aSLionel Sambuc
self_init_in_cond(int * p)507*f4a2713aSLionel Sambuc int self_init_in_cond(int *p) {
508*f4a2713aSLionel Sambuc int n = ((p && (0 || 1)) && (n = *p)) ? n : -1; // ok
509*f4a2713aSLionel Sambuc return n;
510*f4a2713aSLionel Sambuc }
511*f4a2713aSLionel Sambuc
512*f4a2713aSLionel Sambuc void test_analyzer_noreturn_aux() __attribute__((analyzer_noreturn));
513*f4a2713aSLionel Sambuc
test_analyzer_noreturn(int y)514*f4a2713aSLionel Sambuc void test_analyzer_noreturn(int y) {
515*f4a2713aSLionel Sambuc int x; // expected-note {{initialize the variable 'x' to silence this warning}}
516*f4a2713aSLionel Sambuc if (y) {
517*f4a2713aSLionel Sambuc test_analyzer_noreturn_aux();
518*f4a2713aSLionel Sambuc ++x; // no-warning
519*f4a2713aSLionel Sambuc }
520*f4a2713aSLionel Sambuc else {
521*f4a2713aSLionel Sambuc ++x; // expected-warning {{variable 'x' is uninitialized when used here}}
522*f4a2713aSLionel Sambuc }
523*f4a2713aSLionel Sambuc }
test_analyzer_noreturn_2(int y)524*f4a2713aSLionel Sambuc void test_analyzer_noreturn_2(int y) {
525*f4a2713aSLionel Sambuc int x;
526*f4a2713aSLionel Sambuc if (y) {
527*f4a2713aSLionel Sambuc test_analyzer_noreturn_aux();
528*f4a2713aSLionel Sambuc }
529*f4a2713aSLionel Sambuc else {
530*f4a2713aSLionel Sambuc x = 1;
531*f4a2713aSLionel Sambuc }
532*f4a2713aSLionel Sambuc ++x; // no-warning
533*f4a2713aSLionel Sambuc }
534