blob: 942b80e70e13c7eab7f354f9b47cc2b5e06a2000 [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
Ted Kremenekf4e532b2011-02-12 00:17:19 +000047//
Ted Kremeneka23157e2008-05-05 23:12:21 +000048int f6() {
49
50 int x = 4;
Ted Kremenekf4e532b2011-02-12 00:17:19 +000051 ++x; // no-warning
Ted Kremeneka23157e2008-05-05 23:12:21 +000052 return 1;
53}
Ted Kremenek1a654b62008-06-20 21:45:25 +000054
55int f7(int *p) {
56 // This is allowed for defensive programming.
57 p = 0; // no-warning
58 return 1;
59}
60
Ted Kremenek93fab7c2009-11-22 20:26:21 +000061int f7b(int *p) {
62 // This is allowed for defensive programming.
63 p = (0); // no-warning
64 return 1;
65}
66
67int f7c(int *p) {
68 // This is allowed for defensive programming.
69 p = (void*) 0; // no-warning
70 return 1;
71}
72
73int f7d(int *p) {
74 // This is allowed for defensive programming.
75 p = (void*) (0); // no-warning
76 return 1;
77}
78
Ted Kremenek56b1f712011-01-13 20:58:56 +000079// Don't warn for dead stores in nested expressions. We have yet
80// to see a real bug in this scenario.
Ted Kremenek1a654b62008-06-20 21:45:25 +000081int f8(int *p) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000082 extern int *baz();
Ted Kremenek56b1f712011-01-13 20:58:56 +000083 if ((p = baz())) // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000084 return 1;
85 return 0;
86}
87
Ted Kremenek2cfac222008-07-23 21:16:38 +000088int f9() {
89 int x = 4;
90 x = x + 10; // expected-warning{{never read}}
91 return 1;
92}
93
Ted Kremenek2cfac222008-07-23 21:16:38 +000094int f10() {
95 int x = 4;
96 x = 10 + x; // expected-warning{{never read}}
97 return 1;
98}
99
Ted Kremenek8b00b6e2008-07-23 23:18:43 +0000100int f11() {
101 int x = 4;
Ted Kremenek380277e2008-10-15 05:23:41 +0000102 return x++; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +0000103}
104
Ted Kremenek380277e2008-10-15 05:23:41 +0000105int f11b() {
106 int x = 4;
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000107 return ((((++x)))); // no-warning
Ted Kremenek380277e2008-10-15 05:23:41 +0000108}
109
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000110int f12a(int y) {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000111 int x = y; // expected-warning{{unused variable 'x'}}
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000112 return 1;
113}
114int f12b(int y) {
115 int x __attribute__((unused)) = y; // no-warning
116 return 1;
117}
Ted Kremenekebd42f42010-03-18 01:22:39 +0000118int f12c(int y) {
119 // Allow initialiation of scalar variables by parameters as a form of
120 // defensive programming.
121 int x = y; // no-warning
122 x = 1;
123 return x;
124}
Ted Kremenek2cfac222008-07-23 21:16:38 +0000125
Ted Kremenekefe88f52008-08-06 23:26:31 +0000126// Filed with PR 2630. This code should produce no warnings.
127int f13(void)
128{
129 int a = 1;
130 int b, c = b = a + a;
131
132 if (b > 0)
133 return (0);
134
135 return (a + b + c);
136}
137
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000138// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000139int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000140 int index, nextLineIndex;
141 for (index = 0; index < count; index = nextLineIndex+1) {
142 nextLineIndex = index+1; // no-warning
143 continue;
144 }
145 return index;
146}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000147
148// Test case for <rdar://problem/6248086>
149void f15(unsigned x, unsigned y) {
150 int count = x * y; // no-warning
Ted Kremenekebd42f42010-03-18 01:22:39 +0000151 int z[count]; // expected-warning{{unused variable 'z'}}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000152}
153
Ted Kremenek56b1f712011-01-13 20:58:56 +0000154// Don't warn for dead stores in nested expressions. We have yet
155// to see a real bug in this scenario.
Ted Kremenek610a09e2008-09-26 22:58:57 +0000156int f16(int x) {
157 x = x * 2;
Ted Kremenek56b1f712011-01-13 20:58:56 +0000158 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 +0000159 ? 5 : 8;
Ted Kremenek610a09e2008-09-26 22:58:57 +0000160 return x;
161}
162
Ted Kremenek3b587862009-01-09 22:15:01 +0000163// Self-assignments should not be flagged as dead stores.
Mike Stumpa5495ea2009-07-21 19:01:31 +0000164void f17() {
Ted Kremenek3b587862009-01-09 22:15:01 +0000165 int x = 1;
Tom Careef52bcb2010-08-24 21:09:07 +0000166 x = x;
Ted Kremenek3b587862009-01-09 22:15:01 +0000167}
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000168
169// <rdar://problem/6506065>
170// The values of dead stores are only "consumed" in an enclosing expression
Mike Stumpcd7bf232009-07-17 01:04:31 +0000171// what that value is actually used. In other words, don't say "Although the
172// value stored to 'x' is used...".
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000173int f18() {
174 int x = 0; // no-warning
175 if (1)
176 x = 10; // expected-warning{{Value stored to 'x' is never read}}
177 while (1)
178 x = 10; // expected-warning{{Value stored to 'x' is never read}}
Ted Kremenek848ec832011-02-11 23:24:26 +0000179 // unreachable.
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000180 do
Ted Kremenek848ec832011-02-11 23:24:26 +0000181 x = 10; // no-warning
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000182 while (1);
Ted Kremenek56b1f712011-01-13 20:58:56 +0000183 return (x = 10); // no-warning
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000184}
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000185
Ted Kremenek848ec832011-02-11 23:24:26 +0000186int f18_a() {
187 int x = 0; // no-warning
188 return (x = 10); // no-warning
189}
190
191void f18_b() {
192 int x = 0; // no-warning
193 if (1)
194 x = 10; // expected-warning{{Value stored to 'x' is never read}}
195}
196
197void f18_c() {
198 int x = 0;
199 while (1)
200 x = 10; // expected-warning{{Value stored to 'x' is never read}}
201}
202
203void f18_d() {
204 int x = 0; // no-warning
205 do
206 x = 10; // expected-warning{{Value stored to 'x' is never read}}
207 while (1);
208}
209
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000210// PR 3514: false positive `dead initialization` warning for init to global
211// http://llvm.org/bugs/show_bug.cgi?id=3514
212extern const int MyConstant;
213int f19(void) {
214 int x = MyConstant; // no-warning
215 x = 1;
216 return x;
217}
218
Ted Kremenek28433ff2009-03-31 03:34:38 +0000219int f19b(void) { // This case is the same as f19.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000220 const int MyConstant = 0;
Ted Kremenek28433ff2009-03-31 03:34:38 +0000221 int x = MyConstant; // no-warning
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000222 x = 1;
223 return x;
224}
Ted Kremenek632d1ec2009-03-23 22:30:58 +0000225
226void f20(void) {
227 int x = 1; // no-warning
228#pragma unused(x)
229}
230
Mike Stumpcd7bf232009-07-17 01:04:31 +0000231void halt() __attribute__((noreturn));
232int f21() {
233 int x = 4;
234
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000235 x = x + 1; // expected-warning{{never read}}
Mike Stumpcd7bf232009-07-17 01:04:31 +0000236 if (1) {
237 halt();
238 (void)x;
239 }
240 return 1;
241}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000242
243int j;
244void f22() {
245 int x = 4;
246 int y1 = 4;
247 int y2 = 4;
248 int y3 = 4;
249 int y4 = 4;
250 int y5 = 4;
251 int y6 = 4;
252 int y7 = 4;
253 int y8 = 4;
254 int y9 = 4;
255 int y10 = 4;
Mike Stump5f203632009-07-21 00:38:52 +0000256 int y11 = 4;
257 int y12 = 4;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000258 int y13 = 4;
259 int y14 = 4;
260 int y15 = 4;
Mike Stump8f9893a2009-07-21 01:27:50 +0000261 int y16 = 4;
262 int y17 = 4;
263 int y18 = 4;
Mike Stump22cd6582009-07-21 01:46:17 +0000264 int y19 = 4;
265 int y20 = 4;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000266
Ted Kremenekf4e532b2011-02-12 00:17:19 +0000267 x = x + 1; // expected-warning{{never read}}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000268 ++y1;
269 ++y2;
270 ++y3;
271 ++y4;
272 ++y5;
273 ++y6;
274 ++y7;
275 ++y8;
276 ++y9;
277 ++y10;
Mike Stump5f203632009-07-21 00:38:52 +0000278 ++y11;
279 ++y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000280 ++y13;
281 ++y14;
282 ++y15;
Mike Stump8f9893a2009-07-21 01:27:50 +0000283 ++y16;
284 ++y17;
285 ++y18;
Mike Stump22cd6582009-07-21 01:46:17 +0000286 ++y19;
287 ++y20;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000288
289 switch (j) {
290 case 1:
291 if (0)
292 (void)x;
293 if (1) {
294 (void)y1;
295 return;
296 }
297 (void)x;
298 break;
299 case 2:
300 if (0)
301 (void)x;
302 else {
303 (void)y2;
304 return;
305 }
306 (void)x;
307 break;
308 case 3:
309 if (1) {
310 (void)y3;
311 return;
312 } else
313 (void)x;
314 (void)x;
315 break;
316 case 4:
317 0 ? : ((void)y4, ({ return; }));
318 (void)x;
319 break;
320 case 5:
321 1 ? : (void)x;
322 0 ? (void)x : ((void)y5, ({ return; }));
323 (void)x;
324 break;
325 case 6:
326 1 ? ((void)y6, ({ return; })) : (void)x;
327 (void)x;
328 break;
329 case 7:
330 (void)(0 && x);
331 (void)y7;
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +0000332 (void)(0 || (y8, ({ return; }), 1)); // expected-warning {{expression result unused}}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000333 (void)x;
334 break;
335 case 8:
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +0000336 (void)(1 && (y9, ({ return; }), 1)); // expected-warning {{expression result unused}}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000337 (void)x;
338 break;
339 case 9:
340 (void)(1 || x);
341 (void)y10;
Mike Stump5f203632009-07-21 00:38:52 +0000342 break;
343 case 10:
Mike Stump5f203632009-07-21 00:38:52 +0000344 while (0) {
345 (void)x;
346 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000347 (void)y11;
Mike Stump5f203632009-07-21 00:38:52 +0000348 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000349 case 11:
350 while (1) {
351 (void)y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000352 }
353 (void)x;
354 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000355 case 12:
356 do {
357 (void)y13;
358 } while (0);
359 (void)y14;
360 break;
361 case 13:
362 do {
363 (void)y15;
364 } while (1);
365 (void)x;
366 break;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000367 case 14:
Mike Stump8f9893a2009-07-21 01:27:50 +0000368 for (;;) {
369 (void)y16;
370 }
371 (void)x;
372 break;
373 case 15:
374 for (;1;) {
375 (void)y17;
376 }
377 (void)x;
378 break;
379 case 16:
Mike Stumpfefb9f72009-07-21 01:12:51 +0000380 for (;0;) {
381 (void)x;
382 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000383 (void)y18;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000384 break;
Mike Stump22cd6582009-07-21 01:46:17 +0000385 case 17:
386 __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; })));
387 (void)x;
388 break;
389 case 19:
390 __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x);
391 (void)x;
392 break;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000393 }
394}
Ted Kremenek3a976342009-11-26 06:55:36 +0000395
396void f23_aux(const char* s);
397void f23(int argc, char **argv) {
398 int shouldLog = (argc > 1); // no-warning
399 ^{
400 if (shouldLog) f23_aux("I did too use it!\n");
401 else f23_aux("I shouldn't log. Wait.. d'oh!\n");
402 }();
403}
404
405void f23_pos(int argc, char **argv) {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000406 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 +0000407 ^{
408 f23_aux("I did too use it!\n");
409 }();
410}
Ted Kremenek9a0459c2009-12-01 23:04:14 +0000411
412void f24_A(int y) {
413 // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
414 int x = (y > 2); // no-warning
415 ^ {
Ted Kremenekebd42f42010-03-18 01:22:39 +0000416 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 +0000417 }();
418}
419
420void f24_B(int y) {
421 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
422 __block int x = (y > 2); // no-warning
423 ^{
424 // FIXME: This should eventually be a dead store since it is never read either.
425 x = 5; // no-warning
426 }();
427}
428
429int f24_C(int y) {
430 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
431 __block int x = (y > 2); // no-warning
432 ^{
433 x = 5; // no-warning
434 }();
435 return x;
436}
437
438int f24_D(int y) {
439 __block int x = (y > 2); // no-warning
440 ^{
441 if (y > 4)
442 x = 5; // no-warning
443 }();
444 return x;
445}
446
Ted Kremenek74635d82009-12-03 00:46:16 +0000447// This example shows that writing to a variable captured by a block means that it might
448// not be dead.
449int f25(int y) {
450 __block int x = (y > 2);
451 __block int z = 0;
452 void (^foo)() = ^{ z = x + y; };
453 x = 4; // no-warning
454 foo();
455 return z;
456}
457
458// This test is mostly the same as 'f25', but shows that the heuristic of pruning out dead
459// stores for variables that are just marked '__block' is overly conservative.
460int f25_b(int y) {
461 // FIXME: we should eventually report a dead store here.
462 __block int x = (y > 2);
463 __block int z = 0;
464 x = 4; // no-warning
465 return z;
466}
467
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000468int f26_nestedblocks() {
469 int z;
470 z = 1;
471 __block int y = 0;
472 ^{
473 int k;
474 k = 1; // expected-warning{{Value stored to 'k' is never read}}
475 ^{
476 y = z + 1;
477 }();
478 }();
479 return y;
480}
481
Ted Kremenek3f64a0e2010-05-21 20:30:15 +0000482// The FOREACH macro in QT uses 'break' statements within statement expressions
483// placed within the increment code of for loops.
484void rdar8014335() {
485 for (int i = 0 ; i != 10 ; ({ break; })) {
486 for ( ; ; ({ ++i; break; })) ;
487 // Note that the next value stored to 'i' is never executed
488 // because the next statement to be executed is the 'break'
489 // in the increment code of the first loop.
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000490 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 +0000491 }
492}
493
Ted Kremenek334c1952010-08-17 21:00:06 +0000494// <rdar://problem/8320674> NullStmts followed by do...while() can lead to disconnected CFG
495//
496// This previously caused bogus dead-stores warnings because the body of the first do...while was
497// disconnected from the entry of the function.
498typedef struct { float r; float i; } s_rdar8320674;
499typedef struct { s_rdar8320674 x[1]; } s2_rdar8320674;
500
501void rdar8320674(s_rdar8320674 *z, unsigned y, s2_rdar8320674 *st, int m)
502{
503 s_rdar8320674 * z2;
504 s_rdar8320674 * tw1 = st->x;
505 s_rdar8320674 t;
506 z2 = z + m;
507 do{
508 ; ;
509 do{ (t).r = (*z2).r*(*tw1).r - (*z2).i*(*tw1).i; (t).i = (*z2).r*(*tw1).i + (*z2).i*(*tw1).r; }while(0);
510 tw1 += y;
511 do { (*z2).r=(*z).r-(t).r; (*z2).i=(*z).i-(t).i; }while(0);
512 do { (*z).r += (t).r; (*z).i += (t).i; }while(0);
513 ++z2;
514 ++z;
515 }while (--m);
516}
517
Ted Kremenek848ec832011-02-11 23:24:26 +0000518// Avoid dead stores resulting from an assignment (and use) being unreachable.
519void rdar8405222_aux(int i);
520void rdar8405222() {
521 const int show = 0;
522 int i = 0;
523
524 if (show)
525 i = 5; // no-warning
526
527 if (show)
528 rdar8405222_aux(i);
529}
530