blob: 9a266c9379392ca172f2dbe2c71162daa116350b [file] [log] [blame]
Ted Kremenek2cfe28b2010-03-10 00:18:11 +00001// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
2// RUN: %clang_cc1 -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 -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 -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 -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 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
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 Kremenek1a654b62008-06-20 21:45:25 +000043 int *p = &x; // expected-warning{{never read}}
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 Kremenek1a654b62008-06-20 21:45:25 +000078int f8(int *p) {
Daniel Dunbar4489fe12008-08-05 00:07:51 +000079 extern int *baz();
John McCallf66d5cd2009-10-13 17:57:23 +000080 if ((p = baz())) // expected-warning{{Although the value}}
Ted Kremenek1a654b62008-06-20 21:45:25 +000081 return 1;
82 return 0;
83}
84
Ted Kremenek2cfac222008-07-23 21:16:38 +000085int f9() {
86 int x = 4;
87 x = x + 10; // expected-warning{{never read}}
88 return 1;
89}
90
Ted Kremenek2cfac222008-07-23 21:16:38 +000091int f10() {
92 int x = 4;
93 x = 10 + x; // expected-warning{{never read}}
94 return 1;
95}
96
Ted Kremenek8b00b6e2008-07-23 23:18:43 +000097int f11() {
98 int x = 4;
Ted Kremenek380277e2008-10-15 05:23:41 +000099 return x++; // expected-warning{{never read}}
Ted Kremenek8b00b6e2008-07-23 23:18:43 +0000100}
101
Ted Kremenek380277e2008-10-15 05:23:41 +0000102int f11b() {
103 int x = 4;
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000104 return ((((++x)))); // no-warning
Ted Kremenek380277e2008-10-15 05:23:41 +0000105}
106
Ted Kremenekfc7ff552008-07-25 04:47:34 +0000107int f12a(int y) {
108 int x = y; // expected-warning{{never read}}
109 return 1;
110}
111int f12b(int y) {
112 int x __attribute__((unused)) = y; // no-warning
113 return 1;
114}
Ted Kremenek2cfac222008-07-23 21:16:38 +0000115
Ted Kremenekefe88f52008-08-06 23:26:31 +0000116// Filed with PR 2630. This code should produce no warnings.
117int f13(void)
118{
119 int a = 1;
120 int b, c = b = a + a;
121
122 if (b > 0)
123 return (0);
124
125 return (a + b + c);
126}
127
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000128// Filed with PR 2763.
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000129int f14(int count) {
Ted Kremenekb497ebd2008-09-04 21:52:52 +0000130 int index, nextLineIndex;
131 for (index = 0; index < count; index = nextLineIndex+1) {
132 nextLineIndex = index+1; // no-warning
133 continue;
134 }
135 return index;
136}
Ted Kremenek84fa6b92008-09-26 05:52:45 +0000137
138// Test case for <rdar://problem/6248086>
139void f15(unsigned x, unsigned y) {
140 int count = x * y; // no-warning
141 int z[count];
142}
143
Ted Kremenek610a09e2008-09-26 22:58:57 +0000144int f16(int x) {
145 x = x * 2;
Ted Kremenekd2025e22008-09-26 23:05:47 +0000146 x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}}
147 ? 5 : 8;
Ted Kremenek610a09e2008-09-26 22:58:57 +0000148 return x;
149}
150
Ted Kremenek3b587862009-01-09 22:15:01 +0000151// Self-assignments should not be flagged as dead stores.
Mike Stumpa5495ea2009-07-21 19:01:31 +0000152void f17() {
Ted Kremenek3b587862009-01-09 22:15:01 +0000153 int x = 1;
154 x = x; // no-warning
155}
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000156
157// <rdar://problem/6506065>
158// The values of dead stores are only "consumed" in an enclosing expression
Mike Stumpcd7bf232009-07-17 01:04:31 +0000159// what that value is actually used. In other words, don't say "Although the
160// value stored to 'x' is used...".
Ted Kremenek7f5fce72009-01-20 00:47:45 +0000161int f18() {
162 int x = 0; // no-warning
163 if (1)
164 x = 10; // expected-warning{{Value stored to 'x' is never read}}
165 while (1)
166 x = 10; // expected-warning{{Value stored to 'x' is never read}}
167 do
168 x = 10; // expected-warning{{Value stored to 'x' is never read}}
169 while (1);
170
171 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'}}
172}
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000173
174// PR 3514: false positive `dead initialization` warning for init to global
175// http://llvm.org/bugs/show_bug.cgi?id=3514
176extern const int MyConstant;
177int f19(void) {
178 int x = MyConstant; // no-warning
179 x = 1;
180 return x;
181}
182
Ted Kremenek28433ff2009-03-31 03:34:38 +0000183int f19b(void) { // This case is the same as f19.
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000184 const int MyConstant = 0;
Ted Kremenek28433ff2009-03-31 03:34:38 +0000185 int x = MyConstant; // no-warning
Ted Kremenekd3098ee2009-02-09 18:01:00 +0000186 x = 1;
187 return x;
188}
Ted Kremenek632d1ec2009-03-23 22:30:58 +0000189
190void f20(void) {
191 int x = 1; // no-warning
192#pragma unused(x)
193}
194
Mike Stumpcd7bf232009-07-17 01:04:31 +0000195void halt() __attribute__((noreturn));
196int f21() {
197 int x = 4;
198
199 ++x; // expected-warning{{never read}}
200 if (1) {
201 halt();
202 (void)x;
203 }
204 return 1;
205}
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000206
207int j;
208void f22() {
209 int x = 4;
210 int y1 = 4;
211 int y2 = 4;
212 int y3 = 4;
213 int y4 = 4;
214 int y5 = 4;
215 int y6 = 4;
216 int y7 = 4;
217 int y8 = 4;
218 int y9 = 4;
219 int y10 = 4;
Mike Stump5f203632009-07-21 00:38:52 +0000220 int y11 = 4;
221 int y12 = 4;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000222 int y13 = 4;
223 int y14 = 4;
224 int y15 = 4;
Mike Stump8f9893a2009-07-21 01:27:50 +0000225 int y16 = 4;
226 int y17 = 4;
227 int y18 = 4;
Mike Stump22cd6582009-07-21 01:46:17 +0000228 int y19 = 4;
229 int y20 = 4;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000230
231 ++x; // expected-warning{{never read}}
232 ++y1;
233 ++y2;
234 ++y3;
235 ++y4;
236 ++y5;
237 ++y6;
238 ++y7;
239 ++y8;
240 ++y9;
241 ++y10;
Mike Stump5f203632009-07-21 00:38:52 +0000242 ++y11;
243 ++y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000244 ++y13;
245 ++y14;
246 ++y15;
Mike Stump8f9893a2009-07-21 01:27:50 +0000247 ++y16;
248 ++y17;
249 ++y18;
Mike Stump22cd6582009-07-21 01:46:17 +0000250 ++y19;
251 ++y20;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000252
253 switch (j) {
254 case 1:
255 if (0)
256 (void)x;
257 if (1) {
258 (void)y1;
259 return;
260 }
261 (void)x;
262 break;
263 case 2:
264 if (0)
265 (void)x;
266 else {
267 (void)y2;
268 return;
269 }
270 (void)x;
271 break;
272 case 3:
273 if (1) {
274 (void)y3;
275 return;
276 } else
277 (void)x;
278 (void)x;
279 break;
280 case 4:
281 0 ? : ((void)y4, ({ return; }));
282 (void)x;
283 break;
284 case 5:
285 1 ? : (void)x;
286 0 ? (void)x : ((void)y5, ({ return; }));
287 (void)x;
288 break;
289 case 6:
290 1 ? ((void)y6, ({ return; })) : (void)x;
291 (void)x;
292 break;
293 case 7:
294 (void)(0 && x);
295 (void)y7;
296 (void)(0 || (y8, ({ return; }), 1));
297 (void)x;
298 break;
299 case 8:
300 (void)(1 && (y9, ({ return; }), 1));
301 (void)x;
302 break;
303 case 9:
304 (void)(1 || x);
305 (void)y10;
Mike Stump5f203632009-07-21 00:38:52 +0000306 break;
307 case 10:
Mike Stump5f203632009-07-21 00:38:52 +0000308 while (0) {
309 (void)x;
310 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000311 (void)y11;
Mike Stump5f203632009-07-21 00:38:52 +0000312 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000313 case 11:
314 while (1) {
315 (void)y12;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000316 }
317 (void)x;
318 break;
Mike Stump8f9893a2009-07-21 01:27:50 +0000319 case 12:
320 do {
321 (void)y13;
322 } while (0);
323 (void)y14;
324 break;
325 case 13:
326 do {
327 (void)y15;
328 } while (1);
329 (void)x;
330 break;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000331 case 14:
Mike Stump8f9893a2009-07-21 01:27:50 +0000332 for (;;) {
333 (void)y16;
334 }
335 (void)x;
336 break;
337 case 15:
338 for (;1;) {
339 (void)y17;
340 }
341 (void)x;
342 break;
343 case 16:
Mike Stumpfefb9f72009-07-21 01:12:51 +0000344 for (;0;) {
345 (void)x;
346 }
Mike Stump8f9893a2009-07-21 01:27:50 +0000347 (void)y18;
Mike Stumpfefb9f72009-07-21 01:12:51 +0000348 break;
Mike Stump22cd6582009-07-21 01:46:17 +0000349 case 17:
350 __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; })));
351 (void)x;
352 break;
353 case 19:
354 __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x);
355 (void)x;
356 break;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000357 }
358}
Ted Kremenek3a976342009-11-26 06:55:36 +0000359
360void f23_aux(const char* s);
361void f23(int argc, char **argv) {
362 int shouldLog = (argc > 1); // no-warning
363 ^{
364 if (shouldLog) f23_aux("I did too use it!\n");
365 else f23_aux("I shouldn't log. Wait.. d'oh!\n");
366 }();
367}
368
369void f23_pos(int argc, char **argv) {
370 int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}}
371 ^{
372 f23_aux("I did too use it!\n");
373 }();
374}
Ted Kremenek9a0459c2009-12-01 23:04:14 +0000375
376void f24_A(int y) {
377 // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
378 int x = (y > 2); // no-warning
379 ^ {
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000380 int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}}
Ted Kremenek9a0459c2009-12-01 23:04:14 +0000381 }();
382}
383
384void f24_B(int y) {
385 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
386 __block int x = (y > 2); // no-warning
387 ^{
388 // FIXME: This should eventually be a dead store since it is never read either.
389 x = 5; // no-warning
390 }();
391}
392
393int f24_C(int y) {
394 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
395 __block int x = (y > 2); // no-warning
396 ^{
397 x = 5; // no-warning
398 }();
399 return x;
400}
401
402int f24_D(int y) {
403 __block int x = (y > 2); // no-warning
404 ^{
405 if (y > 4)
406 x = 5; // no-warning
407 }();
408 return x;
409}
410
Ted Kremenek74635d82009-12-03 00:46:16 +0000411// This example shows that writing to a variable captured by a block means that it might
412// not be dead.
413int f25(int y) {
414 __block int x = (y > 2);
415 __block int z = 0;
416 void (^foo)() = ^{ z = x + y; };
417 x = 4; // no-warning
418 foo();
419 return z;
420}
421
422// This test is mostly the same as 'f25', but shows that the heuristic of pruning out dead
423// stores for variables that are just marked '__block' is overly conservative.
424int f25_b(int y) {
425 // FIXME: we should eventually report a dead store here.
426 __block int x = (y > 2);
427 __block int z = 0;
428 x = 4; // no-warning
429 return z;
430}
431
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000432int f26_nestedblocks() {
433 int z;
434 z = 1;
435 __block int y = 0;
436 ^{
437 int k;
438 k = 1; // expected-warning{{Value stored to 'k' is never read}}
439 ^{
440 y = z + 1;
441 }();
442 }();
443 return y;
444}
445