blob: 9e3f736d77049b3ad6b66253dfd7efba8982e9b6 [file] [log] [blame]
Tom Caredf4ca422010-07-16 20:41:41 +00001// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
Ted Kremenekebd42f42010-03-18 01:22:39 +00002// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
3// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
4// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
5// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
Ted Kremenek8aefcbf2007-11-19 06:38:23 +00006
Ted Kremenek49a2fd22008-04-14 15:56:17 +00007void f1() {
Ted Kremenekebd42f42010-03-18 01:22:39 +00008 int k, y; // expected-warning{{unused variable 'k'}} expected-warning{{unused variable 'y'}}
Ted Kremenek0fdf06e2008-03-19 07:31:52 +00009 int abc=1;
Ted Kremenekebd42f42010-03-18 01:22:39 +000010 long idx=abc+3*5; // expected-warning {{never read}} expected-warning{{unused variable 'idx'}}
Ted Kremenekaa395ba2007-11-18 20:06:35 +000011}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000012
Ted Kremenek49a2fd22008-04-14 15:56:17 +000013void f2(void *b) {
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000014 char *c = (char*)b; // no-warning
Ted Kremenekebd42f42010-03-18 01:22:39 +000015 char *d = b+1; // expected-warning {{never read}} expected-warning{{unused variable 'd'}}
Chris Lattner58f9e132010-09-05 00:04:01 +000016 printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (const char *, ...)'}} \
Douglas Gregora316e7b2009-02-14 00:32:47 +000017 // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
Ted Kremenek8aefcbf2007-11-19 06:38:23 +000018}
Ted Kremenek74c43a02007-11-20 03:03:00 +000019
Chris Lattnere0303582010-01-09 20:43:19 +000020int f();
21
Ted Kremenek49a2fd22008-04-14 15:56:17 +000022void f3() {
Ted Kremenek0fdf06e2008-03-19 07:31:52 +000023 int r;
24 if ((r = f()) != 0) { // no-warning
25 int y = r; // no-warning
26 printf("the error is: %d\n", y);
27 }
Ted Kremenek74c43a02007-11-20 03:03:00 +000028}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000029
30void f4(int k) {
31
32 k = 1;
33
34 if (k)
35 f1();
36
Ted Kremenek1a654b62008-06-20 21:45:25 +000037 k = 2; // expected-warning {{never read}}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000038}
Ted Kremenek93fab7c2009-11-22 20:26:21 +000039
Ted Kremenekf87821c2008-04-15 18:37:29 +000040void f5() {
41
42 int x = 4; // no-warning
Ted Kremenekebd42f42010-03-18 01:22:39 +000043 int *p = &x; // expected-warning{{never read}} expected-warning{{unused variable 'p'}}
Ted Kremenekf87821c2008-04-15 18:37:29 +000044
Ted Kremeneka23157e2008-05-05 23:12:21 +000045}
46
47int f6() {
48
49 int x = 4;
Ted Kremenek1a654b62008-06-20 21:45:25 +000050 ++x; // expected-warning{{never read}}
Ted Kremeneka23157e2008-05-05 23:12:21 +000051 return 1;
52}
Ted Kremenek1a654b62008-06-20 21:45:25 +000053
54int f7(int *p) {
55 // This is allowed for defensive programming.
56 p = 0; // no-warning
57 return 1;
58}
59
Ted Kremenek93fab7c2009-11-22 20:26:21 +000060int f7b(int *p) {
61 // This is allowed for defensive programming.
62 p = (0); // no-warning
63 return 1;
64}
65
66int f7c(int *p) {
67 // This is allowed for defensive programming.
68 p = (void*) 0; // no-warning
69 return 1;
70}
71
72int f7d(int *p) {
73 // This is allowed for defensive programming.
74 p = (void*) (0); // no-warning
75 return 1;
76}
77
Ted Kremenek56b1f712011-01-13 20:58:56 +000078// Don't warn for dead stores in nested expressions. We have yet
79// to see a real bug in this scenario.
Ted Kremenek1a654b62008-06-20 21:45:25 +000080int f8(int *p) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000081 extern int *baz();
Ted Kremenek56b1f712011-01-13 20:58:56 +000082 if ((p = baz())) // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000083 return 1;
84 return 0;
85}
86
Ted Kremenek2cfac222008-07-23 21:16:38 +000087int f9() {
88 int x = 4;
89 x = x + 10; // expected-warning{{never read}}
90 return 1;
91}
92
Ted Kremenek2cfac222008-07-23 21:16:38 +000093int f10() {
94 int x = 4;
95 x = 10 + x; // expected-warning{{never read}}
96 return 1;
97}
98
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000099int f11() {
100 int x = 4;
Ted Kremenek380277e2008-10-15 05:23:41 +0000101 return x++; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +0000102}
103
Ted Kremenek380277e2008-10-15 05:23:41 +0000104int f11b() {
105 int x = 4;
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000106 return ((((++x)))); // no-warning
Ted Kremenek380277e2008-10-15 05:23:41 +0000107}
108
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000109int f12a(int y) {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000110 int x = y; // expected-warning{{unused variable 'x'}}
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000111 return 1;
112}
113int f12b(int y) {
114 int x __attribute__((unused)) = y; // no-warning
115 return 1;
116}
Ted Kremenekebd42f42010-03-18 01:22:39 +0000117int f12c(int y) {
118 // Allow initialiation of scalar variables by parameters as a form of
119 // defensive programming.
120 int x = y; // no-warning
121 x = 1;
122 return x;
123}
Ted Kremenek2cfac222008-07-23 21:16:38 +0000124
Ted Kremenekefe88f52008-08-06 23:26:31 +0000125// Filed with PR 2630. This code should produce no warnings.
126int f13(void)
127{
128 int a = 1;
129 int b, c = b = a + a;
130
131 if (b > 0)
132 return (0);
133
134 return (a + b + c);
135}
136
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000137// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000138int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000139 int index, nextLineIndex;
140 for (index = 0; index < count; index = nextLineIndex+1) {
141 nextLineIndex = index+1; // no-warning
142 continue;
143 }
144 return index;
145}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000146
147// Test case for <rdar://problem/6248086>
148void f15(unsigned x, unsigned y) {
149 int count = x * y; // no-warning
Ted Kremenekebd42f42010-03-18 01:22:39 +0000150 int z[count]; // expected-warning{{unused variable 'z'}}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000151}
152
Ted Kremenek56b1f712011-01-13 20:58:56 +0000153// Don't warn for dead stores in nested expressions. We have yet
154// to see a real bug in this scenario.
Ted Kremenek610a09e2008-09-26 22:58:57 +0000155int f16(int x) {
156 x = x * 2;
Ted Kremenek56b1f712011-01-13 20:58:56 +0000157 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{The left operand to '+' is always 0}} expected-warning{{The left operand to '*' is always 1}}
Ted Kremenekd2025e22008-09-26 23:05:47 +0000158 ? 5 : 8;
Ted Kremenek610a09e2008-09-26 22:58:57 +0000159 return x;
160}
161
Ted Kremenek3b587862009-01-09 22:15:01 +0000162// Self-assignments should not be flagged as dead stores.
Mike Stumpa5495ea2009-07-21 19:01:31 +0000163void f17() {
Ted Kremenek3b587862009-01-09 22:15:01 +0000164 int x = 1;
Tom Careef52bcb2010-08-24 21:09:07 +0000165 x = x;
Ted Kremenek3b587862009-01-09 22:15:01 +0000166}
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000167
168// <rdar://problem/6506065>
169// The values of dead stores are only "consumed" in an enclosing expression
Mike Stumpcd7bf232009-07-17 01:04:31 +0000170// what that value is actually used. In other words, don't say "Although the
171// value stored to 'x' is used...".
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000172int f18() {
173 int x = 0; // no-warning
174 if (1)
175 x = 10; // expected-warning{{Value stored to 'x' is never read}}
176 while (1)
177 x = 10; // expected-warning{{Value stored to 'x' is never read}}
Ted Kremenek848ec832011-02-11 23:24:26 +0000178 // unreachable.
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000179 do
Ted Kremenek848ec832011-02-11 23:24:26 +0000180 x = 10; // no-warning
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000181 while (1);
Ted Kremenek56b1f712011-01-13 20:58:56 +0000182 return (x = 10); // no-warning
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000183}
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000184
Ted Kremenek848ec832011-02-11 23:24:26 +0000185int f18_a() {
186 int x = 0; // no-warning
187 return (x = 10); // no-warning
188}
189
190void f18_b() {
191 int x = 0; // no-warning
192 if (1)
193 x = 10; // expected-warning{{Value stored to 'x' is never read}}
194}
195
196void f18_c() {
197 int x = 0;
198 while (1)
199 x = 10; // expected-warning{{Value stored to 'x' is never read}}
200}
201
202void f18_d() {
203 int x = 0; // no-warning
204 do
205 x = 10; // expected-warning{{Value stored to 'x' is never read}}
206 while (1);
207}
208
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000209// PR 3514: false positive `dead initialization` warning for init to global
210// http://llvm.org/bugs/show_bug.cgi?id=3514
211extern const int MyConstant;
212int f19(void) {
213 int x = MyConstant; // no-warning
214 x = 1;
215 return x;
216}
217
Ted Kremenek28433ff2009-03-31 03:34:38 +0000218int f19b(void) { // This case is the same as f19.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000219 const int MyConstant = 0;
Ted Kremenek28433ff2009-03-31 03:34:38 +0000220 int x = MyConstant; // no-warning
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000221 x = 1;
222 return x;
223}
Ted Kremenek632d1ec2009-03-23 22:30:58 +0000224
225void f20(void) {
226 int x = 1; // no-warning
227#pragma unused(x)
228}
229
Mike Stumpcd7bf232009-07-17 01:04:31 +0000230void halt() __attribute__((noreturn));
231int f21() {
232 int x = 4;
233
234 ++x; // expected-warning{{never read}}
235 if (1) {
236 halt();
237 (void)x;
238 }
239 return 1;
240}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000241
242int j;
243void f22() {
244 int x = 4;
245 int y1 = 4;
246 int y2 = 4;
247 int y3 = 4;
248 int y4 = 4;
249 int y5 = 4;
250 int y6 = 4;
251 int y7 = 4;
252 int y8 = 4;
253 int y9 = 4;
254 int y10 = 4;
Mike Stump5f203632009-07-21 00:38:52 +0000255 int y11 = 4;
256 int y12 = 4;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000257 int y13 = 4;
258 int y14 = 4;
259 int y15 = 4;
Mike Stump8f9893a2009-07-21 01:27:50 +0000260 int y16 = 4;
261 int y17 = 4;
262 int y18 = 4;
Mike Stump22cd6582009-07-21 01:46:17 +0000263 int y19 = 4;
264 int y20 = 4;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000265
266 ++x; // expected-warning{{never read}}
267 ++y1;
268 ++y2;
269 ++y3;
270 ++y4;
271 ++y5;
272 ++y6;
273 ++y7;
274 ++y8;
275 ++y9;
276 ++y10;
Mike Stump5f203632009-07-21 00:38:52 +0000277 ++y11;
278 ++y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000279 ++y13;
280 ++y14;
281 ++y15;
Mike Stump8f9893a2009-07-21 01:27:50 +0000282 ++y16;
283 ++y17;
284 ++y18;
Mike Stump22cd6582009-07-21 01:46:17 +0000285 ++y19;
286 ++y20;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000287
288 switch (j) {
289 case 1:
290 if (0)
291 (void)x;
292 if (1) {
293 (void)y1;
294 return;
295 }
296 (void)x;
297 break;
298 case 2:
299 if (0)
300 (void)x;
301 else {
302 (void)y2;
303 return;
304 }
305 (void)x;
306 break;
307 case 3:
308 if (1) {
309 (void)y3;
310 return;
311 } else
312 (void)x;
313 (void)x;
314 break;
315 case 4:
316 0 ? : ((void)y4, ({ return; }));
317 (void)x;
318 break;
319 case 5:
320 1 ? : (void)x;
321 0 ? (void)x : ((void)y5, ({ return; }));
322 (void)x;
323 break;
324 case 6:
325 1 ? ((void)y6, ({ return; })) : (void)x;
326 (void)x;
327 break;
328 case 7:
329 (void)(0 && x);
330 (void)y7;
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +0000331 (void)(0 || (y8, ({ return; }), 1)); // expected-warning {{expression result unused}}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000332 (void)x;
333 break;
334 case 8:
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +0000335 (void)(1 && (y9, ({ return; }), 1)); // expected-warning {{expression result unused}}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000336 (void)x;
337 break;
338 case 9:
339 (void)(1 || x);
340 (void)y10;
Mike Stump5f203632009-07-21 00:38:52 +0000341 break;
342 case 10:
Mike Stump5f203632009-07-21 00:38:52 +0000343 while (0) {
344 (void)x;
345 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000346 (void)y11;
Mike Stump5f203632009-07-21 00:38:52 +0000347 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000348 case 11:
349 while (1) {
350 (void)y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000351 }
352 (void)x;
353 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000354 case 12:
355 do {
356 (void)y13;
357 } while (0);
358 (void)y14;
359 break;
360 case 13:
361 do {
362 (void)y15;
363 } while (1);
364 (void)x;
365 break;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000366 case 14:
Mike Stump8f9893a2009-07-21 01:27:50 +0000367 for (;;) {
368 (void)y16;
369 }
370 (void)x;
371 break;
372 case 15:
373 for (;1;) {
374 (void)y17;
375 }
376 (void)x;
377 break;
378 case 16:
Mike Stumpfefb9f72009-07-21 01:12:51 +0000379 for (;0;) {
380 (void)x;
381 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000382 (void)y18;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000383 break;
Mike Stump22cd6582009-07-21 01:46:17 +0000384 case 17:
385 __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; })));
386 (void)x;
387 break;
388 case 19:
389 __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x);
390 (void)x;
391 break;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000392 }
393}
Ted Kremenek3a976342009-11-26 06:55:36 +0000394
395void f23_aux(const char* s);
396void f23(int argc, char **argv) {
397 int shouldLog = (argc > 1); // no-warning
398 ^{
399 if (shouldLog) f23_aux("I did too use it!\n");
400 else f23_aux("I shouldn't log. Wait.. d'oh!\n");
401 }();
402}
403
404void f23_pos(int argc, char **argv) {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000405 int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}} expected-warning{{unused variable 'shouldLog'}}
Ted Kremenek3a976342009-11-26 06:55:36 +0000406 ^{
407 f23_aux("I did too use it!\n");
408 }();
409}
Ted Kremenek9a0459c2009-12-01 23:04:14 +0000410
411void f24_A(int y) {
412 // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
413 int x = (y > 2); // no-warning
414 ^ {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000415 int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}} expected-warning{{unused variable 'z'}}
Ted Kremenek9a0459c2009-12-01 23:04:14 +0000416 }();
417}
418
419void f24_B(int y) {
420 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
421 __block int x = (y > 2); // no-warning
422 ^{
423 // FIXME: This should eventually be a dead store since it is never read either.
424 x = 5; // no-warning
425 }();
426}
427
428int f24_C(int y) {
429 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
430 __block int x = (y > 2); // no-warning
431 ^{
432 x = 5; // no-warning
433 }();
434 return x;
435}
436
437int f24_D(int y) {
438 __block int x = (y > 2); // no-warning
439 ^{
440 if (y > 4)
441 x = 5; // no-warning
442 }();
443 return x;
444}
445
Ted Kremenek74635d82009-12-03 00:46:16 +0000446// This example shows that writing to a variable captured by a block means that it might
447// not be dead.
448int f25(int y) {
449 __block int x = (y > 2);
450 __block int z = 0;
451 void (^foo)() = ^{ z = x + y; };
452 x = 4; // no-warning
453 foo();
454 return z;
455}
456
457// This test is mostly the same as 'f25', but shows that the heuristic of pruning out dead
458// stores for variables that are just marked '__block' is overly conservative.
459int f25_b(int y) {
460 // FIXME: we should eventually report a dead store here.
461 __block int x = (y > 2);
462 __block int z = 0;
463 x = 4; // no-warning
464 return z;
465}
466
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000467int f26_nestedblocks() {
468 int z;
469 z = 1;
470 __block int y = 0;
471 ^{
472 int k;
473 k = 1; // expected-warning{{Value stored to 'k' is never read}}
474 ^{
475 y = z + 1;
476 }();
477 }();
478 return y;
479}
480
Ted Kremenek3f64a0e2010-05-21 20:30:15 +0000481// The FOREACH macro in QT uses 'break' statements within statement expressions
482// placed within the increment code of for loops.
483void rdar8014335() {
484 for (int i = 0 ; i != 10 ; ({ break; })) {
485 for ( ; ; ({ ++i; break; })) ;
486 // Note that the next value stored to 'i' is never executed
487 // because the next statement to be executed is the 'break'
488 // in the increment code of the first loop.
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000489 i = i * 3; // expected-warning{{Value stored to 'i' is never read}} expected-warning{{The left operand to '*' is always 1}}
Ted Kremenek3f64a0e2010-05-21 20:30:15 +0000490 }
491}
492
Ted Kremenek334c1952010-08-17 21:00:06 +0000493// <rdar://problem/8320674> NullStmts followed by do...while() can lead to disconnected CFG
494//
495// This previously caused bogus dead-stores warnings because the body of the first do...while was
496// disconnected from the entry of the function.
497typedef struct { float r; float i; } s_rdar8320674;
498typedef struct { s_rdar8320674 x[1]; } s2_rdar8320674;
499
500void rdar8320674(s_rdar8320674 *z, unsigned y, s2_rdar8320674 *st, int m)
501{
502 s_rdar8320674 * z2;
503 s_rdar8320674 * tw1 = st->x;
504 s_rdar8320674 t;
505 z2 = z + m;
506 do{
507 ; ;
508 do{ (t).r = (*z2).r*(*tw1).r - (*z2).i*(*tw1).i; (t).i = (*z2).r*(*tw1).i + (*z2).i*(*tw1).r; }while(0);
509 tw1 += y;
510 do { (*z2).r=(*z).r-(t).r; (*z2).i=(*z).i-(t).i; }while(0);
511 do { (*z).r += (t).r; (*z).i += (t).i; }while(0);
512 ++z2;
513 ++z;
514 }while (--m);
515}
516
Ted Kremenek848ec832011-02-11 23:24:26 +0000517// Avoid dead stores resulting from an assignment (and use) being unreachable.
518void rdar8405222_aux(int i);
519void rdar8405222() {
520 const int show = 0;
521 int i = 0;
522
523 if (show)
524 i = 5; // no-warning
525
526 if (show)
527 rdar8405222_aux(i);
528}
529