1 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core.experimental.IdempotentOps -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-checker=core.DeadStores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
2 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core.experimental.IdempotentOps -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-checker=core.DeadStores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
3 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core.experimental.IdempotentOps -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-checker=core.DeadStores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
4 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core.experimental.IdempotentOps -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-checker=core.DeadStores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
5 // RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-checker=core.experimental.IdempotentOps -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-checker=core.DeadStores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
8 int k
, y
; // expected-warning{{unused variable 'k'}} expected-warning{{unused variable 'y'}}
10 long idx
=abc
+3*5; // expected-warning {{never read}} expected-warning{{unused variable 'idx'}}
14 char *c
= (char*)b
; // no-warning
15 char *d
= b
+1; // expected-warning {{never read}} expected-warning{{unused variable 'd'}}
16 printf("%s", c
); // expected-warning{{implicitly declaring C library function 'printf' with type 'int (const char *, ...)'}} \
17 // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
24 if ((r
= f()) != 0) { // no-warning
25 int y
= r
; // no-warning
26 printf("the error is: %d\n", y
);
37 k
= 2; // expected-warning {{never read}}
42 int x
= 4; // no-warning
43 int *p
= &x
; // expected-warning{{never read}} expected-warning{{unused variable 'p'}}
56 // This is allowed for defensive programming.
62 // This is allowed for defensive programming.
63 p
= (0); // no-warning
68 // This is allowed for defensive programming.
69 p
= (void*) 0; // no-warning
74 // This is allowed for defensive programming.
75 p
= (void*) (0); // no-warning
79 // Don't warn for dead stores in nested expressions. We have yet
80 // to see a real bug in this scenario.
83 if ((p
= baz())) // no-warning
90 x
= x
+ 10; // expected-warning{{never read}}
96 x
= 10 + x
; // expected-warning{{never read}}
102 return x
++; // expected-warning{{never read}}
107 return ((((++x
)))); // no-warning
111 int x
= y
; // expected-warning{{unused variable 'x'}}
115 int x
__attribute__((unused
)) = y
; // no-warning
119 // Allow initialiation of scalar variables by parameters as a form of
120 // defensive programming.
121 int x
= y
; // no-warning
126 // Filed with PR 2630. This code should produce no warnings.
130 int b
, c
= b
= a
+ a
;
138 // Filed with PR 2763.
140 int index
, nextLineIndex
;
141 for (index
= 0; index
< count
; index
= nextLineIndex
+1) {
142 nextLineIndex
= index
+1; // no-warning
148 // Test case for <rdar://problem/6248086>
149 void f15(unsigned x
, unsigned y
) {
150 int count
= x
* y
; // no-warning
151 int z
[count
]; // expected-warning{{unused variable 'z'}}
154 // Don't warn for dead stores in nested expressions. We have yet
155 // to see a real bug in this scenario.
158 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}}
163 // Self-assignments should not be flagged as dead stores.
169 // <rdar://problem/6506065>
170 // The values of dead stores are only "consumed" in an enclosing expression
171 // what that value is actually used. In other words, don't say "Although the
172 // value stored to 'x' is used...".
174 int x
= 0; // no-warning
176 x
= 10; // expected-warning{{Value stored to 'x' is never read}}
178 x
= 10; // expected-warning{{Value stored to 'x' is never read}}
181 x
= 10; // no-warning
183 return (x
= 10); // no-warning
187 int x
= 0; // no-warning
188 return (x
= 10); // no-warning
192 int x
= 0; // no-warning
194 x
= 10; // expected-warning{{Value stored to 'x' is never read}}
200 x
= 10; // expected-warning{{Value stored to 'x' is never read}}
204 int x
= 0; // no-warning
206 x
= 10; // expected-warning{{Value stored to 'x' is never read}}
210 // PR 3514: false positive `dead initialization` warning for init to global
211 // http://llvm.org/bugs/show_bug.cgi?id=3514
212 extern const int MyConstant
;
214 int x
= MyConstant
; // no-warning
219 int f19b(void) { // This case is the same as f19.
220 const int MyConstant
= 0;
221 int x
= MyConstant
; // no-warning
227 int x
= 1; // no-warning
231 void halt() __attribute__((noreturn
));
235 x
= x
+ 1; // expected-warning{{never read}}
267 x
= x
+ 1; // expected-warning{{never read}}
317 0 ? : ((void)y4
, ({ return; }));
322 0 ? (void)x
: ((void)y5
, ({ return; }));
326 1 ? ((void)y6
, ({ return; })) : (void)x
;
332 (void)(0 || (y8
, ({ return; }), 1)); // expected-warning {{expression result unused}}
336 (void)(1 && (y9
, ({ return; }), 1)); // expected-warning {{expression result unused}}
386 __builtin_choose_expr(0, (void)x
, ((void)y19
, ({ return; })));
390 __builtin_choose_expr(1, ((void)y20
, ({ return; })), (void)x
);
396 void f23_aux(const char* s
);
397 void f23(int argc
, char **argv
) {
398 int shouldLog
= (argc
> 1); // no-warning
400 if (shouldLog
) f23_aux("I did too use it!\n");
401 else f23_aux("I shouldn't log. Wait.. d'oh!\n");
405 void f23_pos(int argc
, char **argv
) {
406 int shouldLog
= (argc
> 1); // expected-warning{{Value stored to 'shouldLog' during its initialization is never read}} expected-warning{{unused variable 'shouldLog'}}
408 f23_aux("I did too use it!\n");
413 // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
414 int x
= (y
> 2); // no-warning
416 int z
= x
+ y
; // expected-warning{{Value stored to 'z' during its initialization is never read}} expected-warning{{unused variable 'z'}}
421 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
422 __block
int x
= (y
> 2); // no-warning
424 // FIXME: This should eventually be a dead store since it is never read either.
430 // FIXME: One day this should be reported as dead since 'x' is just overwritten.
431 __block
int x
= (y
> 2); // no-warning
439 __block
int x
= (y
> 2); // no-warning
447 // This example shows that writing to a variable captured by a block means that it might
450 __block
int x
= (y
> 2);
452 void (^foo
)() = ^{ z
= x
+ y
; };
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.
461 // FIXME: we should eventually report a dead store here.
462 __block
int x
= (y
> 2);
468 int f26_nestedblocks() {
474 k
= 1; // expected-warning{{Value stored to 'k' is never read}}
482 // The FOREACH macro in QT uses 'break' statements within statement expressions
483 // placed within the increment code of for loops.
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.
490 i
= i
* 3; // expected-warning{{Value stored to 'i' is never read}} expected-warning{{The left operand to '*' is always 1}}
494 // <rdar://problem/8320674> NullStmts followed by do...while() can lead to disconnected CFG
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.
498 typedef struct { float r
; float i
; } s_rdar8320674
;
499 typedef struct { s_rdar8320674 x
[1]; } s2_rdar8320674
;
501 void rdar8320674(s_rdar8320674
*z
, unsigned y
, s2_rdar8320674
*st
, int m
)
504 s_rdar8320674
* tw1
= st
->x
;
509 do{ (t
).r
= (*z2
).r
*(*tw1
).r
- (*z2
).i
*(*tw1
).i
; (t
).i
= (*z2
).r
*(*tw1
).i
+ (*z2
).i
*(*tw1
).r
; }while(0);
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);
518 // Avoid dead stores resulting from an assignment (and use) being unreachable.
519 void rdar8405222_aux(int i
);