blob: 63c9d0d97a448b660de845d00cd78fe733543c82 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -warn-dead-stores -fblocks -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -warn-dead-stores -fblocks -verify %s
3// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -fblocks -verify %s
4// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=basic -warn-dead-stores -fblocks -verify %s
5// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=range -warn-dead-stores -fblocks -verify %s
Ted Kremenek8aefcbf2007-11-19 06:38:23 +00006
Ted Kremenek49a2fd22008-04-14 15:56:17 +00007void f1() {
Ted Kremenekaa395ba2007-11-18 20:06:35 +00008 int k, y;
Ted Kremenek0fdf06e2008-03-19 07:31:52 +00009 int abc=1;
Ted Kremenek1a654b62008-06-20 21:45:25 +000010 long idx=abc+3*5; // expected-warning {{never read}}
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 Kremenek1a654b62008-06-20 21:45:25 +000015 char *d = b+1; // expected-warning {{never read}}
Douglas Gregora316e7b2009-02-14 00:32:47 +000016 printf("%s", c); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \
17 // 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
Ted Kremenek49a2fd22008-04-14 15:56:17 +000020void f3() {
Ted Kremenek0fdf06e2008-03-19 07:31:52 +000021 int r;
22 if ((r = f()) != 0) { // no-warning
23 int y = r; // no-warning
24 printf("the error is: %d\n", y);
25 }
Ted Kremenek74c43a02007-11-20 03:03:00 +000026}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000027
28void f4(int k) {
29
30 k = 1;
31
32 if (k)
33 f1();
34
Ted Kremenek1a654b62008-06-20 21:45:25 +000035 k = 2; // expected-warning {{never read}}
Ted Kremenek49a2fd22008-04-14 15:56:17 +000036}
Ted Kremenek93fab7c2009-11-22 20:26:21 +000037
Ted Kremenekf87821c2008-04-15 18:37:29 +000038void f5() {
39
40 int x = 4; // no-warning
Ted Kremenek1a654b62008-06-20 21:45:25 +000041 int *p = &x; // expected-warning{{never read}}
Ted Kremenekf87821c2008-04-15 18:37:29 +000042
Ted Kremeneka23157e2008-05-05 23:12:21 +000043}
44
45int f6() {
46
47 int x = 4;
Ted Kremenek1a654b62008-06-20 21:45:25 +000048 ++x; // expected-warning{{never read}}
Ted Kremeneka23157e2008-05-05 23:12:21 +000049 return 1;
50}
Ted Kremenek1a654b62008-06-20 21:45:25 +000051
52int f7(int *p) {
53 // This is allowed for defensive programming.
54 p = 0; // no-warning
55 return 1;
56}
57
Ted Kremenek93fab7c2009-11-22 20:26:21 +000058int f7b(int *p) {
59 // This is allowed for defensive programming.
60 p = (0); // no-warning
61 return 1;
62}
63
64int f7c(int *p) {
65 // This is allowed for defensive programming.
66 p = (void*) 0; // no-warning
67 return 1;
68}
69
70int f7d(int *p) {
71 // This is allowed for defensive programming.
72 p = (void*) (0); // no-warning
73 return 1;
74}
75
Ted Kremenek1a654b62008-06-20 21:45:25 +000076int f8(int *p) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000077 extern int *baz();
John McCallf66d5cd2009-10-13 17:57:23 +000078 if ((p = baz())) // expected-warning{{Although the value}}
Ted Kremenek1a654b62008-06-20 21:45:25 +000079 return 1;
80 return 0;
81}
82
Ted Kremenek2cfac222008-07-23 21:16:38 +000083int f9() {
84 int x = 4;
85 x = x + 10; // expected-warning{{never read}}
86 return 1;
87}
88
Ted Kremenek2cfac222008-07-23 21:16:38 +000089int f10() {
90 int x = 4;
91 x = 10 + x; // expected-warning{{never read}}
92 return 1;
93}
94
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000095int f11() {
96 int x = 4;
Ted Kremenek380277e2008-10-15 05:23:41 +000097 return x++; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000098}
99
Ted Kremenek380277e2008-10-15 05:23:41 +0000100int f11b() {
101 int x = 4;
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000102 return ((((++x)))); // no-warning
Ted Kremenek380277e2008-10-15 05:23:41 +0000103}
104
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000105int f12a(int y) {
106 int x = y; // expected-warning{{never read}}
107 return 1;
108}
109int f12b(int y) {
110 int x __attribute__((unused)) = y; // no-warning
111 return 1;
112}
Ted Kremenek2cfac222008-07-23 21:16:38 +0000113
Ted Kremenekefe88f52008-08-06 23:26:31 +0000114// Filed with PR 2630. This code should produce no warnings.
115int f13(void)
116{
117 int a = 1;
118 int b, c = b = a + a;
119
120 if (b > 0)
121 return (0);
122
123 return (a + b + c);
124}
125
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000126// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000127int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000128 int index, nextLineIndex;
129 for (index = 0; index < count; index = nextLineIndex+1) {
130 nextLineIndex = index+1; // no-warning
131 continue;
132 }
133 return index;
134}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000135
136// Test case for <rdar://problem/6248086>
137void f15(unsigned x, unsigned y) {
138 int count = x * y; // no-warning
139 int z[count];
140}
141
Ted Kremenek610a09e2008-09-26 22:58:57 +0000142int f16(int x) {
143 x = x * 2;
Ted Kremenekd2025e22008-09-26 23:05:47 +0000144 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}}
145 ? 5 : 8;
Ted Kremenek610a09e2008-09-26 22:58:57 +0000146 return x;
147}
148
Ted Kremenek3b587862009-01-09 22:15:01 +0000149// Self-assignments should not be flagged as dead stores.
Mike Stumpa5495ea2009-07-21 19:01:31 +0000150void f17() {
Ted Kremenek3b587862009-01-09 22:15:01 +0000151 int x = 1;
152 x = x; // no-warning
153}
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000154
155// <rdar://problem/6506065>
156// The values of dead stores are only "consumed" in an enclosing expression
Mike Stumpcd7bf232009-07-17 01:04:31 +0000157// what that value is actually used. In other words, don't say "Although the
158// value stored to 'x' is used...".
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000159int f18() {
160 int x = 0; // no-warning
161 if (1)
162 x = 10; // expected-warning{{Value stored to 'x' is never read}}
163 while (1)
164 x = 10; // expected-warning{{Value stored to 'x' is never read}}
165 do
166 x = 10; // expected-warning{{Value stored to 'x' is never read}}
167 while (1);
168
169 return (x = 10); // expected-warning{{Although the value stored to 'x' is used in the enclosing expression, the value is never actually read from 'x'}}
170}
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000171
172// PR 3514: false positive `dead initialization` warning for init to global
173// http://llvm.org/bugs/show_bug.cgi?id=3514
174extern const int MyConstant;
175int f19(void) {
176 int x = MyConstant; // no-warning
177 x = 1;
178 return x;
179}
180
Ted Kremenek28433ff2009-03-31 03:34:38 +0000181int f19b(void) { // This case is the same as f19.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000182 const int MyConstant = 0;
Ted Kremenek28433ff2009-03-31 03:34:38 +0000183 int x = MyConstant; // no-warning
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000184 x = 1;
185 return x;
186}
Ted Kremenek632d1ec2009-03-23 22:30:58 +0000187
188void f20(void) {
189 int x = 1; // no-warning
190#pragma unused(x)
191}
192
Mike Stumpcd7bf232009-07-17 01:04:31 +0000193void halt() __attribute__((noreturn));
194int f21() {
195 int x = 4;
196
197 ++x; // expected-warning{{never read}}
198 if (1) {
199 halt();
200 (void)x;
201 }
202 return 1;
203}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000204
205int j;
206void f22() {
207 int x = 4;
208 int y1 = 4;
209 int y2 = 4;
210 int y3 = 4;
211 int y4 = 4;
212 int y5 = 4;
213 int y6 = 4;
214 int y7 = 4;
215 int y8 = 4;
216 int y9 = 4;
217 int y10 = 4;
Mike Stump5f203632009-07-21 00:38:52 +0000218 int y11 = 4;
219 int y12 = 4;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000220 int y13 = 4;
221 int y14 = 4;
222 int y15 = 4;
Mike Stump8f9893a2009-07-21 01:27:50 +0000223 int y16 = 4;
224 int y17 = 4;
225 int y18 = 4;
Mike Stump22cd6582009-07-21 01:46:17 +0000226 int y19 = 4;
227 int y20 = 4;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000228
229 ++x; // expected-warning{{never read}}
230 ++y1;
231 ++y2;
232 ++y3;
233 ++y4;
234 ++y5;
235 ++y6;
236 ++y7;
237 ++y8;
238 ++y9;
239 ++y10;
Mike Stump5f203632009-07-21 00:38:52 +0000240 ++y11;
241 ++y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000242 ++y13;
243 ++y14;
244 ++y15;
Mike Stump8f9893a2009-07-21 01:27:50 +0000245 ++y16;
246 ++y17;
247 ++y18;
Mike Stump22cd6582009-07-21 01:46:17 +0000248 ++y19;
249 ++y20;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000250
251 switch (j) {
252 case 1:
253 if (0)
254 (void)x;
255 if (1) {
256 (void)y1;
257 return;
258 }
259 (void)x;
260 break;
261 case 2:
262 if (0)
263 (void)x;
264 else {
265 (void)y2;
266 return;
267 }
268 (void)x;
269 break;
270 case 3:
271 if (1) {
272 (void)y3;
273 return;
274 } else
275 (void)x;
276 (void)x;
277 break;
278 case 4:
279 0 ? : ((void)y4, ({ return; }));
280 (void)x;
281 break;
282 case 5:
283 1 ? : (void)x;
284 0 ? (void)x : ((void)y5, ({ return; }));
285 (void)x;
286 break;
287 case 6:
288 1 ? ((void)y6, ({ return; })) : (void)x;
289 (void)x;
290 break;
291 case 7:
292 (void)(0 && x);
293 (void)y7;
294 (void)(0 || (y8, ({ return; }), 1));
295 (void)x;
296 break;
297 case 8:
298 (void)(1 && (y9, ({ return; }), 1));
299 (void)x;
300 break;
301 case 9:
302 (void)(1 || x);
303 (void)y10;
Mike Stump5f203632009-07-21 00:38:52 +0000304 break;
305 case 10:
Mike Stump5f203632009-07-21 00:38:52 +0000306 while (0) {
307 (void)x;
308 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000309 (void)y11;
Mike Stump5f203632009-07-21 00:38:52 +0000310 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000311 case 11:
312 while (1) {
313 (void)y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000314 }
315 (void)x;
316 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000317 case 12:
318 do {
319 (void)y13;
320 } while (0);
321 (void)y14;
322 break;
323 case 13:
324 do {
325 (void)y15;
326 } while (1);
327 (void)x;
328 break;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000329 case 14:
Mike Stump8f9893a2009-07-21 01:27:50 +0000330 for (;;) {
331 (void)y16;
332 }
333 (void)x;
334 break;
335 case 15:
336 for (;1;) {
337 (void)y17;
338 }
339 (void)x;
340 break;
341 case 16:
Mike Stumpfefb9f72009-07-21 01:12:51 +0000342 for (;0;) {
343 (void)x;
344 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000345 (void)y18;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000346 break;
Mike Stump22cd6582009-07-21 01:46:17 +0000347 case 17:
348 __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; })));
349 (void)x;
350 break;
351 case 19:
352 __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x);
353 (void)x;
354 break;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000355 }
356}
Ted Kremenek3a976342009-11-26 06:55:36 +0000357
358void f23_aux(const char* s);
359void f23(int argc, char **argv) {
360 int shouldLog = (argc > 1); // no-warning
361 ^{
362 if (shouldLog) f23_aux("I did too use it!\n");
363 else f23_aux("I shouldn't log. Wait.. d'oh!\n");
364 }();
365}
366
367void f23_pos(int argc, char **argv) {
368 int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}}
369 ^{
370 f23_aux("I did too use it!\n");
371 }();
372}
Ted Kremenek9a0459c2009-12-01 23:04:14 +0000373
374void f24_A(int y) {
375 // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
376 int x = (y > 2); // no-warning
377 ^ {
378 int z = x + y; // FIXME: Eventually this should be reported as a dead store.
379 }();
380}
381
382void f24_B(int y) {
383 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
384 __block int x = (y > 2); // no-warning
385 ^{
386 // FIXME: This should eventually be a dead store since it is never read either.
387 x = 5; // no-warning
388 }();
389}
390
391int f24_C(int y) {
392 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
393 __block int x = (y > 2); // no-warning
394 ^{
395 x = 5; // no-warning
396 }();
397 return x;
398}
399
400int f24_D(int y) {
401 __block int x = (y > 2); // no-warning
402 ^{
403 if (y > 4)
404 x = 5; // no-warning
405 }();
406 return x;
407}
408
Ted Kremenek74635d82009-12-03 00:46:16 +0000409// This example shows that writing to a variable captured by a block means that it might
410// not be dead.
411int f25(int y) {
412 __block int x = (y > 2);
413 __block int z = 0;
414 void (^foo)() = ^{ z = x + y; };
415 x = 4; // no-warning
416 foo();
417 return z;
418}
419
420// This test is mostly the same as 'f25', but shows that the heuristic of pruning out dead
421// stores for variables that are just marked '__block' is overly conservative.
422int f25_b(int y) {
423 // FIXME: we should eventually report a dead store here.
424 __block int x = (y > 2);
425 __block int z = 0;
426 x = 4; // no-warning
427 return z;
428}
429