blob: 973e504f634dcf290fa1d0f917cc1c3ff4aff3d7 [file] [log] [blame]
Ted Kremenek1b528442011-02-07 17:38:38 +00001// RUN: %clang_cc1 -fsyntax-only -Wuninitialized-experimental -fsyntax-only -fblocks %s -verify
Ted Kremenek610068c2011-01-15 02:58:47 +00002
3int test1() {
Ted Kremenek609e3172011-02-02 23:35:53 +00004 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
5 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +00006}
7
8int test2() {
9 int x = 0;
10 return x; // no-warning
11}
12
13int test3() {
14 int x;
15 x = 0;
16 return x; // no-warning
17}
18
19int test4() {
Ted Kremenek609e3172011-02-02 23:35:53 +000020 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
21 ++x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000022 return x;
23}
24
25int test5() {
Ted Kremenek609e3172011-02-02 23:35:53 +000026 int x, y; // expected-note{{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
27 x = y; // expected-warning{{variable 'y' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000028 return x;
29}
30
31int test6() {
Ted Kremenek609e3172011-02-02 23:35:53 +000032 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
33 x += 2; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000034 return x;
35}
36
37int test7(int y) {
Ted Kremenek609e3172011-02-02 23:35:53 +000038 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000039 if (y)
40 x = 1;
Ted Kremenek609e3172011-02-02 23:35:53 +000041 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000042}
43
44int test8(int y) {
45 int x;
46 if (y)
47 x = 1;
48 else
49 x = 0;
Ted Kremenek609e3172011-02-02 23:35:53 +000050 return x;
Ted Kremenek610068c2011-01-15 02:58:47 +000051}
52
53int test9(int n) {
Ted Kremenek609e3172011-02-02 23:35:53 +000054 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000055 for (unsigned i = 0 ; i < n; ++i) {
56 if (i == n - 1)
57 break;
Ted Kremenek94b1b4d2011-01-21 19:41:41 +000058 x = 1;
Ted Kremenek610068c2011-01-15 02:58:47 +000059 }
Ted Kremenek609e3172011-02-02 23:35:53 +000060 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000061}
62
63int test10(unsigned n) {
Ted Kremenek609e3172011-02-02 23:35:53 +000064 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000065 for (unsigned i = 0 ; i < n; ++i) {
66 x = 1;
67 }
Ted Kremenek609e3172011-02-02 23:35:53 +000068 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000069}
70
71int test11(unsigned n) {
Ted Kremenek609e3172011-02-02 23:35:53 +000072 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000073 for (unsigned i = 0 ; i <= n; ++i) {
74 x = 1;
75 }
Ted Kremenek609e3172011-02-02 23:35:53 +000076 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000077}
78
79void test12(unsigned n) {
Ted Kremenek609e3172011-02-02 23:35:53 +000080 for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is possibly uninitialized when used here}} expected-note{{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek610068c2011-01-15 02:58:47 +000081}
82
83int test13() {
84 static int i;
85 return i; // no-warning
86}
87
Ted Kremenekc104e532011-01-18 04:53:25 +000088// Simply don't crash on this test case.
89void test14() {
90 const char *p = 0;
91 for (;;) {}
92}
Ted Kremenek610068c2011-01-15 02:58:47 +000093
Ted Kremenekc104e532011-01-18 04:53:25 +000094void test15() {
Ted Kremenek609e3172011-02-02 23:35:53 +000095 int x = x; // expected-warning{{variable 'x' is possibly uninitialized when used here}} expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenekc104e532011-01-18 04:53:25 +000096}
97
98// Don't warn in the following example; shows dataflow confluence.
99char *test16_aux();
100void test16() {
101 char *p = test16_aux();
102 for (unsigned i = 0 ; i < 100 ; i++)
103 p[i] = 'a'; // no-warning
104}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000105
106void test17() {
107 // Don't warn multiple times about the same uninitialized variable
108 // along the same path.
Ted Kremenek609e3172011-02-02 23:35:53 +0000109 int *x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
110 *x = 1; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000111 *x = 1; // no-warning
112}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000113
114int test18(int x, int y) {
115 int z;
116 if (x && y && (z = 1)) {
117 return z; // no-warning
118 }
119 return 0;
120}
121
122int test19_aux1();
123int test19_aux2();
124int test19_aux3(int *x);
125int test19() {
126 int z;
127 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
128 return z; // no-warning
129 return 0;
130}
131
132int test20() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000133 int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000134 if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z))
Ted Kremenek609e3172011-02-02 23:35:53 +0000135 return z; // expected-warning{{variable 'z' is possibly uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000136 return 0;
137}
138
139int test21(int x, int y) {
Ted Kremenek609e3172011-02-02 23:35:53 +0000140 int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000141 if ((x && y) || test19_aux3(&z) || test19_aux2())
Ted Kremenek609e3172011-02-02 23:35:53 +0000142 return z; // expected-warning{{variable 'z' is possibly uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000143 return 0;
144}
145
146int test22() {
147 int z;
148 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
149 return z; // no-warning
150 return 0;
151}
152
153int test23() {
154 int z;
155 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
156 return z; // no-warning
157 return 0;
158}
159
160// The basic uninitialized value analysis doesn't have enough path-sensitivity
161// to catch initializations relying on control-dependencies spanning multiple
162// conditionals. This possibly can be handled by making the CFG itself
163// represent such control-dependencies, but it is a niche case.
164int test24(int flag) {
Ted Kremenek609e3172011-02-02 23:35:53 +0000165 unsigned val; // expected-note{{variable 'val' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000166 if (flag)
167 val = 1;
168 if (!flag)
169 val = 1;
Ted Kremenek609e3172011-02-02 23:35:53 +0000170 return val; // expected-warning{{variable 'val' is possibly uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000171}
172
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000173float test25() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000174 float x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
175 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000176}
177
178typedef int MyInt;
179MyInt test26() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000180 MyInt x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
181 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000182}
Ted Kremenek96608032011-01-23 17:53:04 +0000183
184// Test handling of sizeof().
185int test27() {
186 struct test_27 { int x; } *y;
187 return sizeof(y->x); // no-warning
188}
189
190int test28() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000191 int len; // expected-note{{variable 'len' is declared here}} expected-note{{add initialization to silence this warning}}
192 return sizeof(int[len]); // expected-warning{{variable 'len' is possibly uninitialized when used here}}
Ted Kremenek96608032011-01-23 17:53:04 +0000193}
194
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000195void test29() {
Ted Kremenek55330452011-02-03 06:51:50 +0000196 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
197 (void) ^{ (void) x; }; // expected-warning{{variable 'x' is possibly uninitialized when captured by block}}
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000198}
199
200void test30() {
201 static int x; // no-warning
202 (void) ^{ (void) x; };
203}
204
205void test31() {
206 __block int x; // no-warning
207 (void) ^{ (void) x; };
208}
209
210int test32_x;
211void test32() {
212 (void) ^{ (void) test32_x; }; // no-warning
213}
214
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000215void test_33() {
216 int x; // no-warning
217 (void) x;
218}
219
220int test_34() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000221 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000222 (void) x;
Ted Kremenek609e3172011-02-02 23:35:53 +0000223 return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000224}
225
Ted Kremenek40900ee2011-01-27 02:29:34 +0000226// Test that this case doesn't crash.
227void test35(int x) {
228 __block int y = 0;
229 ^{ y = (x == 0); }();
230}
231
Ted Kremenek96554fd2011-01-27 18:51:39 +0000232// Test handling of indirect goto.
233void test36()
234{
Ted Kremenek609e3172011-02-02 23:35:53 +0000235 void **pc; // expected-note{{variable 'pc' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000236 void *dummy[] = { &&L1, &&L2 };
237 L1:
Ted Kremenek609e3172011-02-02 23:35:53 +0000238 goto *pc; // expected-warning{{variable 'pc' is possibly uninitialized when used here}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000239 L2:
240 goto *pc;
241}
242
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000243// Test && nested in ||.
244int test37_a();
245int test37_b();
246int test37()
247{
248 int identifier;
249 if ((test37_a() && (identifier = 1)) ||
250 (test37_b() && (identifier = 2))) {
251 return identifier; // no-warning
252 }
253 return 0;
254}
255
256// Test merging of path-specific dataflow values (without asserting).
257int test38(int r, int x, int y)
258{
259 int z;
260 return ((r < 0) || ((r == 0) && (x < y)));
261}
262