dead-stores.c revision 9a0459c0f59a09ac7287ca1f49083fc6b31e4142
1// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -warn-dead-stores -fblocks -verify %s
2// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -warn-dead-stores -fblocks -verify %s
3// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=basic -analyzer-constraints=range -warn-dead-stores -fblocks -verify %s
4// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=basic -warn-dead-stores -fblocks -verify %s
5// RUN: clang-cc -analyze -analyzer-experimental-internal-checks -checker-cfref -analyzer-store=region -analyzer-constraints=range -warn-dead-stores -fblocks -verify %s
6
7void f1() {
8  int k, y;
9  int abc=1;
10  long idx=abc+3*5; // expected-warning {{never read}}
11}
12
13void f2(void *b) {
14 char *c = (char*)b; // no-warning
15 char *d = b+1; // expected-warning {{never read}}
16 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'}}
18}
19
20void f3() {
21  int r;
22  if ((r = f()) != 0) { // no-warning
23    int y = r; // no-warning
24    printf("the error is: %d\n", y);
25  }
26}
27
28void f4(int k) {
29
30  k = 1;
31
32  if (k)
33    f1();
34
35  k = 2;  // expected-warning {{never read}}
36}
37
38void f5() {
39
40  int x = 4; // no-warning
41  int *p = &x; // expected-warning{{never read}}
42
43}
44
45int f6() {
46
47  int x = 4;
48  ++x; // expected-warning{{never read}}
49  return 1;
50}
51
52int f7(int *p) {
53  // This is allowed for defensive programming.
54  p = 0; // no-warning
55  return 1;
56}
57
58int 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
76int f8(int *p) {
77  extern int *baz();
78  if ((p = baz())) // expected-warning{{Although the value}}
79    return 1;
80  return 0;
81}
82
83int f9() {
84  int x = 4;
85  x = x + 10; // expected-warning{{never read}}
86  return 1;
87}
88
89int f10() {
90  int x = 4;
91  x = 10 + x; // expected-warning{{never read}}
92  return 1;
93}
94
95int f11() {
96  int x = 4;
97  return x++; // expected-warning{{never read}}
98}
99
100int f11b() {
101  int x = 4;
102  return ((((++x)))); // no-warning
103}
104
105int 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}
113
114// 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
126// Filed with PR 2763.
127int f14(int count) {
128  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}
135
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
142int f16(int x) {
143  x = x * 2;
144  x = sizeof(int [x = (x || x + 1) * 2]) // expected-warning{{Although the value stored to 'x' is used}}
145      ? 5 : 8;
146  return x;
147}
148
149// Self-assignments should not be flagged as dead stores.
150void f17() {
151  int x = 1;
152  x = x; // no-warning
153}
154
155// <rdar://problem/6506065>
156// The values of dead stores are only "consumed" in an enclosing expression
157// what that value is actually used.  In other words, don't say "Although the
158// value stored to 'x' is used...".
159int 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}
171
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
181int f19b(void) { // This case is the same as f19.
182  const int MyConstant = 0;
183  int x = MyConstant; // no-warning
184  x = 1;
185  return x;
186}
187
188void f20(void) {
189  int x = 1; // no-warning
190#pragma unused(x)
191}
192
193void 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}
204
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;
218  int y11 = 4;
219  int y12 = 4;
220  int y13 = 4;
221  int y14 = 4;
222  int y15 = 4;
223  int y16 = 4;
224  int y17 = 4;
225  int y18 = 4;
226  int y19 = 4;
227  int y20 = 4;
228
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;
240  ++y11;
241  ++y12;
242  ++y13;
243  ++y14;
244  ++y15;
245  ++y16;
246  ++y17;
247  ++y18;
248  ++y19;
249  ++y20;
250
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;
304    break;
305  case 10:
306    while (0) {
307      (void)x;
308    }
309    (void)y11;
310    break;
311  case 11:
312    while (1) {
313      (void)y12;
314    }
315    (void)x;
316    break;
317  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;
329  case 14:
330    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:
342    for (;0;) {
343      (void)x;
344    }
345    (void)y18;
346    break;
347  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;
355  }
356}
357
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}
373
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
409