blob: 06abc76019b2696e46bd5088f76750ee819a9c5d [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
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}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +00005 return x; // expected-warning{{variable 'x' is 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}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000021 ++x; // expected-warning{{variable 'x' is 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}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000027 x = y; // expected-warning{{variable 'y' is 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}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000033 x += 2; // expected-warning{{variable 'x' is 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;
Chandler Carruth584b9d62011-04-08 06:47:15 +000041 return x; // expected-warning{{variable 'x' may be 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 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000060 return x; // expected-warning{{variable 'x' may be 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 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000068 return x; // expected-warning{{variable 'x' may be 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 }
Chandler Carruth584b9d62011-04-08 06:47:15 +000076 return x; // expected-warning{{variable 'x' may be uninitialized when used here}}
Ted Kremenek610068c2011-01-15 02:58:47 +000077}
78
79void test12(unsigned n) {
Ted Kremenek05bcade2011-07-14 23:43:06 +000080 for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is 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
Chandler Carruthb88fb022011-04-05 21:36:30 +000094int test15() {
95 int x = x; // no-warning: signals intended lack of initialization. \
96 // expected-note{{variable 'x' is declared here}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +000097 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekc104e532011-01-18 04:53:25 +000098}
99
100// Don't warn in the following example; shows dataflow confluence.
101char *test16_aux();
102void test16() {
103 char *p = test16_aux();
104 for (unsigned i = 0 ; i < 100 ; i++)
105 p[i] = 'a'; // no-warning
106}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000107
108void test17() {
109 // Don't warn multiple times about the same uninitialized variable
110 // along the same path.
Ted Kremenek609e3172011-02-02 23:35:53 +0000111 int *x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000112 *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekc21fed32011-01-18 21:18:58 +0000113 *x = 1; // no-warning
114}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000115
116int test18(int x, int y) {
117 int z;
118 if (x && y && (z = 1)) {
119 return z; // no-warning
120 }
121 return 0;
122}
123
124int test19_aux1();
125int test19_aux2();
126int test19_aux3(int *x);
127int test19() {
128 int z;
129 if (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
130 return z; // no-warning
131 return 0;
132}
133
134int test20() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000135 int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000136 if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z))
Chandler Carruth584b9d62011-04-08 06:47:15 +0000137 return z; // expected-warning{{variable 'z' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000138 return 0;
139}
140
141int test21(int x, int y) {
Ted Kremenek609e3172011-02-02 23:35:53 +0000142 int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000143 if ((x && y) || test19_aux3(&z) || test19_aux2())
Chandler Carruth584b9d62011-04-08 06:47:15 +0000144 return z; // expected-warning{{variable 'z' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000145 return 0;
146}
147
148int test22() {
149 int z;
150 while (test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z))
151 return z; // no-warning
152 return 0;
153}
154
155int test23() {
156 int z;
157 for ( ; test19_aux1() + test19_aux2() && test19_aux1() && test19_aux3(&z) ; )
158 return z; // no-warning
159 return 0;
160}
161
162// The basic uninitialized value analysis doesn't have enough path-sensitivity
163// to catch initializations relying on control-dependencies spanning multiple
164// conditionals. This possibly can be handled by making the CFG itself
165// represent such control-dependencies, but it is a niche case.
166int test24(int flag) {
Ted Kremenek609e3172011-02-02 23:35:53 +0000167 unsigned val; // expected-note{{variable 'val' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000168 if (flag)
169 val = 1;
170 if (!flag)
171 val = 1;
Chandler Carruth584b9d62011-04-08 06:47:15 +0000172 return val; // expected-warning{{variable 'val' may be uninitialized when used here}}
Ted Kremenek13bd4232011-01-20 17:37:17 +0000173}
174
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000175float test25() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000176 float x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000177 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000178}
179
180typedef int MyInt;
181MyInt test26() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000182 MyInt x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000183 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdcfb3602011-01-21 22:49:49 +0000184}
Ted Kremenek96608032011-01-23 17:53:04 +0000185
186// Test handling of sizeof().
187int test27() {
188 struct test_27 { int x; } *y;
189 return sizeof(y->x); // no-warning
190}
191
192int test28() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000193 int len; // expected-note{{variable 'len' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000194 return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}}
Ted Kremenek96608032011-01-23 17:53:04 +0000195}
196
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000197void test29() {
Ted Kremenek55330452011-02-03 06:51:50 +0000198 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000199 (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}}
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000200}
201
202void test30() {
203 static int x; // no-warning
204 (void) ^{ (void) x; };
205}
206
207void test31() {
208 __block int x; // no-warning
209 (void) ^{ (void) x; };
210}
211
212int test32_x;
213void test32() {
214 (void) ^{ (void) test32_x; }; // no-warning
215}
216
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000217void test_33() {
218 int x; // no-warning
219 (void) x;
220}
221
222int test_34() {
Ted Kremenek609e3172011-02-02 23:35:53 +0000223 int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000224 (void) x;
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000225 return x; // expected-warning{{variable 'x' is uninitialized when used here}}
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000226}
227
Ted Kremenek40900ee2011-01-27 02:29:34 +0000228// Test that this case doesn't crash.
229void test35(int x) {
230 __block int y = 0;
231 ^{ y = (x == 0); }();
232}
233
Ted Kremenek96554fd2011-01-27 18:51:39 +0000234// Test handling of indirect goto.
235void test36()
236{
Ted Kremenek609e3172011-02-02 23:35:53 +0000237 void **pc; // expected-note{{variable 'pc' is declared here}} expected-note{{add initialization to silence this warning}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000238 void *dummy[] = { &&L1, &&L2 };
239 L1:
Ted Kremenek05bcade2011-07-14 23:43:06 +0000240 goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}}
Ted Kremenek96554fd2011-01-27 18:51:39 +0000241 L2:
242 goto *pc;
243}
244
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000245// Test && nested in ||.
246int test37_a();
247int test37_b();
248int test37()
249{
250 int identifier;
251 if ((test37_a() && (identifier = 1)) ||
252 (test37_b() && (identifier = 2))) {
253 return identifier; // no-warning
254 }
255 return 0;
256}
257
258// Test merging of path-specific dataflow values (without asserting).
259int test38(int r, int x, int y)
260{
261 int z;
262 return ((r < 0) || ((r == 0) && (x < y)));
263}
264
Ted Kremenekf3f53792011-03-15 03:17:01 +0000265int test39(int x) {
266 int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000267 int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000268 return z;
269}
270
271
272int test40(int x) {
273 int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000274 return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000275}
276
277int test41(int x) {
278 int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
279 if (x) y = 1; // no-warning
Chandler Carruth584b9d62011-04-08 06:47:15 +0000280 return y; // expected-warning {{variable 'y' may be uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000281}
282
283void test42() {
284 int a;
285 a = 30; // no-warning
286}
287
288void test43_aux(int x);
289void test43(int i) {
290 int x; // expected-note {{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}}
291 for (i = 0 ; i < 10; i++)
Ted Kremenek05bcade2011-07-14 23:43:06 +0000292 test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000293}
294
295void test44(int i) {
296 int x = i;
297 int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}}
298 for (i = 0; i < 10; i++ ) {
299 test43_aux(x++); // no-warning
Ted Kremenek05bcade2011-07-14 23:43:06 +0000300 x += y; // expected-warning {{variable 'y' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000301 }
302}
303
304int test45(int j) {
305 int x = 1, y = x + 1;
306 if (y) // no-warning
307 return x;
308 return y;
309}
310
311void test46()
312{
313 int i; // expected-note {{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}}
Chandler Carruthf04eb2d2011-04-08 06:33:38 +0000314 int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}}
Ted Kremenekf3f53792011-03-15 03:17:01 +0000315}
316
317void *test47(int *i)
318{
319 return i ? : 0; // no-warning
320}
321
322void *test49(int *i)
323{
324 int a;
325 return &a ? : i; // no-warning
326}
327
328void test50()
329{
330 char c[1 ? : 2]; // no-warning
331}
332
Ted Kremenekbc8b44c2011-03-31 22:32:41 +0000333int test51(void)
334{
335 __block int a;
336 ^(void) {
337 a = 42;
338 }();
339 return a; // no-warning
340}
341
Ted Kremeneke6c28032011-05-10 22:10:35 +0000342// FIXME: This is a false positive, but it tests logical operations in switch statements.
343int test52(int a, int b) {
344 int x; // expected-note {{variable 'x' is declared here}} expected-note {{add initialization to silence this warning}}
345 switch (a || b) { // expected-warning {{switch condition has boolean value}}
346 case 0:
347 x = 1;
348 break;
349 case 1:
350 x = 2;
351 break;
352 }
353 return x; // expected-warning {{variable 'x' may be uninitialized when used here}}
354}