1 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core,deadcode.DeadStores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
2 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core,deadcode.DeadStores -analyzer-store=region -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
5 int k, y; // expected-warning{{unused variable 'k'}} expected-warning{{unused variable 'y'}}
7 long idx=abc+3*5; // expected-warning {{never read}} expected-warning{{unused variable 'idx'}}
11 char *c = (char*)b; // no-warning
12 char *d = b+1; // expected-warning {{never read}} expected-warning{{unused variable 'd'}}
13 printf("%s", c); // expected-warning{{implicitly declaring library function 'printf' with type 'int (const char *, ...)'}} \
14 // expected-note{{include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
21 if ((r = f()) != 0) { // no-warning
22 int y = r; // no-warning
23 printf("the error is: %d\n", y);
34 k = 2; // expected-warning {{never read}}
39 int x = 4; // no-warning
40 int *p = &x; // expected-warning{{never read}} expected-warning{{unused variable 'p'}}
53 // This is allowed for defensive programming.
59 // This is allowed for defensive programming.
60 p = (0); // no-warning
65 // This is allowed for defensive programming.
66 p = (void*) 0; // no-warning
71 // This is allowed for defensive programming.
72 p = (void*) (0); // no-warning
76 // Don't warn for dead stores in nested expressions. We have yet
77 // to see a real bug in this scenario.
80 if ((p = baz())) // no-warning
87 x = x + 10; // expected-warning{{never read}}
93 x = 10 + x; // expected-warning{{never read}}
99 return x++; // expected-warning{{never read}}
104 return ((((++x)))); // no-warning
108 int x = y; // expected-warning{{unused variable 'x'}}
112 int x __attribute__((unused)) = y; // no-warning
116 // Allow initialiation of scalar variables by parameters as a form of
117 // defensive programming.
118 int x = y; // no-warning
123 // Filed with PR 2630. This code should produce no warnings.
127 int b, c = b = a + a;
135 // Filed with PR 2763.
137 int index, nextLineIndex;
138 for (index = 0; index < count; index = nextLineIndex+1) {
139 nextLineIndex = index+1; // no-warning
145 // Test case for <rdar://problem/6248086>
146 void f15(unsigned x, unsigned y) {
147 int count = x * y; // no-warning
148 int z[count]; // expected-warning{{unused variable 'z'}}
151 // Don't warn for dead stores in nested expressions. We have yet
152 // to see a real bug in this scenario.
155 x = sizeof(int [x = (x || x + 1) * 2])
160 // Self-assignments should not be flagged as dead stores.
166 // <rdar://problem/6506065>
167 // The values of dead stores are only "consumed" in an enclosing expression
168 // what that value is actually used. In other words, don't say "Although the
169 // value stored to 'x' is used...".
171 int x = 0; // no-warning
173 x = 10; // expected-warning{{Value stored to 'x' is never read}}
175 x = 10; // expected-warning{{Value stored to 'x' is never read}}
178 x = 10; // no-warning
180 return (x = 10); // no-warning
184 int x = 0; // no-warning
185 return (x = 10); // no-warning
189 int x = 0; // no-warning
191 x = 10; // expected-warning{{Value stored to 'x' is never read}}
197 x = 10; // expected-warning{{Value stored to 'x' is never read}}
201 int x = 0; // no-warning
203 x = 10; // expected-warning{{Value stored to 'x' is never read}}
207 // PR 3514: false positive `dead initialization` warning for init to global
208 // http://llvm.org/bugs/show_bug.cgi?id=3514
209 extern const int MyConstant;
211 int x = MyConstant; // no-warning
216 int f19b(void) { // This case is the same as f19.
217 const int MyConstant = 0;
218 int x = MyConstant; // no-warning
224 int x = 1; // no-warning
228 void halt() __attribute__((noreturn));
232 x = x + 1; // expected-warning{{never read}}
264 x = x + 1; // expected-warning{{never read}}
314 0 ? : ((void)y4, ({ return; }));
319 0 ? (void)x : ((void)y5, ({ return; }));
323 1 ? ((void)y6, ({ return; })) : (void)x;
329 (void)(0 || (y8, ({ return; }), 1)); // expected-warning {{expression result unused}}
333 (void)(1 && (y9, ({ return; }), 1)); // expected-warning {{expression result unused}}
383 __builtin_choose_expr(0, (void)x, ((void)y19, ({ return; })));
387 __builtin_choose_expr(1, ((void)y20, ({ return; })), (void)x);
393 void f23_aux(const char* s);
394 void f23(int argc, char **argv) {
395 int shouldLog = (argc > 1); // no-warning
397 if (shouldLog) f23_aux("I did too use it!\n");
398 else f23_aux("I shouldn't log. Wait.. d'oh!\n");
402 void f23_pos(int argc, char **argv) {
403 int shouldLog = (argc > 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}} expected-warning{{unused variable 'shouldLog'}}
405 f23_aux("I did too use it!\n");
410 // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
411 int x = (y > 2); // no-warning
413 int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}} expected-warning{{unused variable 'z'}}
418 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
419 __block int x = (y > 2); // no-warning
421 // FIXME: This should eventually be a dead store since it is never read either.
427 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
428 __block int x = (y > 2); // no-warning
436 __block int x = (y > 2); // no-warning
444 // This example shows that writing to a variable captured by a block means that it might
447 __block int x = (y > 2);
449 void (^foo)() = ^{ z = x + y; };
455 // This test is mostly the same as 'f25', but shows that the heuristic of pruning out dead
456 // stores for variables that are just marked '__block' is overly conservative.
458 // FIXME: we should eventually report a dead store here.
459 __block int x = (y > 2);
465 int f26_nestedblocks() {
471 k = 1; // expected-warning{{Value stored to 'k' is never read}}
479 // The FOREACH macro in QT uses 'break' statements within statement expressions
480 // placed within the increment code of for loops.
482 for (int i = 0 ; i != 10 ; ({ break; })) {
483 for ( ; ; ({ ++i; break; })) ; // expected-warning {{'break' is bound to current loop, GCC binds it to the enclosing loop}}
484 // Note that the next value stored to 'i' is never executed
485 // because the next statement to be executed is the 'break'
486 // in the increment code of the first loop.
487 i = i * 3; // expected-warning{{Value stored to 'i' is never read}}
491 // <rdar://problem/8320674> NullStmts followed by do...while() can lead to disconnected CFG
493 // This previously caused bogus dead-stores warnings because the body of the first do...while was
494 // disconnected from the entry of the function.
495 typedef struct { float r; float i; } s_rdar8320674;
496 typedef struct { s_rdar8320674 x[1]; } s2_rdar8320674;
498 void rdar8320674(s_rdar8320674 *z, unsigned y, s2_rdar8320674 *st, int m)
501 s_rdar8320674 * tw1 = st->x;
506 do{ (t).r = (*z2).r*(*tw1).r - (*z2).i*(*tw1).i; (t).i = (*z2).r*(*tw1).i + (*z2).i*(*tw1).r; }while(0);
508 do { (*z2).r=(*z).r-(t).r; (*z2).i=(*z).i-(t).i; }while(0);
509 do { (*z).r += (t).r; (*z).i += (t).i; }while(0);
515 // Avoid dead stores resulting from an assignment (and use) being unreachable.
516 void rdar8405222_aux(int i);
528 // Look through chains of assignements, e.g.: int x = y = 0, when employing
529 // silencing heuristics.
530 int radar11185138_foo() {
532 x = y = 0; // expected-warning {{never read}}
536 int rdar11185138_bar() {
538 int x = y = 0; // no-warning
544 int *radar11185138_baz() {
546 x = y = 0; // no-warning
553 int x0 = (getInt(), 0); // expected-warning{{unused variable 'x0'}}
554 int x1 = (getInt(), getInt()); // expected-warning {{Value stored to 'x1' during its initialization is never read}} // expected-warning{{unused variable 'x1'}}
555 int x2 = (getInt(), getInt(), getInt()); //expected-warning{{Value stored to 'x2' during its initialization is never read}} // expected-warning{{unused variable 'x2'}}
557 x3 = (getInt(), getInt(), 0); // expected-warning{{Value stored to 'x3' is never read}}
558 int x4 = (getInt(), (getInt(), 0)); // expected-warning{{unused variable 'x4'}}
560 int x5 = (getInt(), (y = 0)); // expected-warning{{unused variable 'x5'}}
561 int x6 = (getInt(), (y = getInt())); //expected-warning {{Value stored to 'x6' during its initialization is never read}} // expected-warning{{unused variable 'x6'}}
562 int x7 = 0, x8 = getInt(); //expected-warning {{Value stored to 'x8' during its initialization is never read}} // expected-warning{{unused variable 'x8'}} // expected-warning{{unused variable 'x7'}}
563 int x9 = getInt(), x10 = 0; //expected-warning {{Value stored to 'x9' during its initialization is never read}} // expected-warning{{unused variable 'x9'}} // expected-warning{{unused variable 'x10'}}
564 int m = getInt(), mm, mmm; //expected-warning {{Value stored to 'm' during its initialization is never read}} // expected-warning{{unused variable 'm'}} // expected-warning{{unused variable 'mm'}} // expected-warning{{unused variable 'mmm'}}
565 int n, nn = getInt(); //expected-warning {{Value stored to 'nn' during its initialization is never read}} // expected-warning{{unused variable 'n'}} // expected-warning{{unused variable 'nn'}}
568 p = (getPtr(), (int *)0); // no warning
572 void testVolatile() {