1 // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-constraints=range -fblocks -analyzer-opt-analyze-nested-blocks -analyzer-check-objc-mem -analyzer-check-idempotent-operations -verify %s
5 extern void test(int i
);
6 extern void test_f(float f
);
9 int x
= 10, zero
= 0, one
= 1;
12 x
= x
; // expected-warning {{Assigned value is always the same as the existing value}}
13 test(x
- x
); // expected-warning {{Both operands to '-' always have the same value}}
14 x
-= x
; // expected-warning {{Both operands to '-=' always have the same value}}
16 test(x
/ x
); // expected-warning {{Both operands to '/' always have the same value}}
17 x
/= x
; // expected-warning {{Both operands to '/=' always have the same value}}
19 test(x
& x
); // expected-warning {{Both operands to '&' always have the same value}}
20 x
&= x
; // expected-warning {{Both operands to '&=' always have the same value}}
21 test(x
| x
); // expected-warning {{Both operands to '|' always have the same value}}
22 x
|= x
; // expected-warning {{Both operands to '|=' always have the same value}}
25 test(x
* one
); // expected-warning {{The right operand to '*' is always 1}}
26 x
*= one
; // expected-warning {{The right operand to '*=' is always 1}}
27 test(x
/ one
); // expected-warning {{The right operand to '/' is always 1}}
28 x
/= one
; // expected-warning {{The right operand to '/=' is always 1}}
31 test(one
* x
); // expected-warning {{The left operand to '*' is always 1}}
34 test(x
+ zero
); // expected-warning {{The right operand to '+' is always 0}}
35 test(x
- zero
); // expected-warning {{The right operand to '-' is always 0}}
36 test(x
* zero
); // expected-warning {{The right operand to '*' is always 0}}
37 test(x
& zero
); // expected-warning {{The right operand to '&' is always 0}}
38 test(x
| zero
); // expected-warning {{The right operand to '|' is always 0}}
39 test(x
^ zero
); // expected-warning {{The right operand to '^' is always 0}}
40 test(x
<< zero
); // expected-warning {{The right operand to '<<' is always 0}}
41 test(x
>> zero
); // expected-warning {{The right operand to '>>' is always 0}}
44 test(zero
+ x
); // expected-warning {{The left operand to '+' is always 0}}
45 test(zero
- x
); // expected-warning {{The left operand to '-' is always 0}}
46 test(zero
/ x
); // expected-warning {{The left operand to '/' is always 0}}
47 test(zero
* x
); // expected-warning {{The left operand to '*' is always 0}}
48 test(zero
& x
); // expected-warning {{The left operand to '&' is always 0}}
49 test(zero
| x
); // expected-warning {{The left operand to '|' is always 0}}
50 test(zero
^ x
); // expected-warning {{The left operand to '^' is always 0}}
51 test(zero
<< x
); // expected-warning {{The left operand to '<<' is always 0}}
52 test(zero
>> x
); // expected-warning {{The left operand to '>>' is always 0}}
54 // Overwrite the values so these aren't marked as Pseudoconstants
59 return x
+ zero
+ one
;
62 void floats(float x
) {
63 test_f(x
* 1.0); // no-warning
64 test_f(x
* 1.0F
); // no-warning
67 // Ensure that we don't report false poitives in complex loops
69 int unused
= 0, result
= 4;
70 result
= result
; // expected-warning {{Assigned value is always the same as the existing value}}
72 for (unsigned bg
= 0; bg
< 1024; bg
++) {
73 result
= bg
* result
; // no-warning
75 for (int i
= 0; i
< 256; i
++) {
76 unused
*= i
; // no-warning
81 // Relaxed liveness - check that we don't kill liveness at assignments
82 typedef unsigned uintptr_t;
83 void kill_at_assign() {
85 uintptr_t x
= array
; // expected-warning{{incompatible pointer to integer conversion}}
86 short *p
= x
; // expected-warning{{incompatible integer to pointer conversion}}
88 // The following branch should be infeasible.
89 if (!(p
= &array
[0])) { // expected-warning{{Assigned value is always the same as the existing value}}
95 // False positive tests
99 return a
* (5 - 2 - 3); // no-warning
102 enum testenum
{ enum1
= 0, enum2
};
105 return enum1
+ a
; // no-warning
108 // Self assignments of unused variables are common false positives
109 unsigned false3(int param
, int param2
) {
110 param
= param
; // no-warning
112 // if a self assigned variable is used later, then it should be reported still
113 param2
= param2
; // expected-warning{{Assigned value is always the same as the existing value}}
115 unsigned nonparam
= 5;
117 nonparam
= nonparam
; // expected-warning{{Assigned value is always the same as the existing value}}
119 return param2
+ nonparam
;
122 // Pseudo-constants (vars only read) and constants should not be reported
125 const int height
= 1;
127 test(height
* c
); // no-warning
129 // Pseudo-constant (never changes after decl)
132 return width
* 10; // no-warning
135 // Block pseudoconstants
141 b
*= a
; // no-warning
144 // Psuedoconstant block var
145 test(b
* c
); // no-warning
147 // Non-pseudoconstant block var
149 test(b
* d
); // expected-warning{{The right operand to '*' is always 0}}
157 // Static vars are common false positives
161 a
*= test
; // no-warning
166 // Non-local storage vars are considered false positives
171 localInt
/= globalInt
;
176 // Check that assignments filter out false positives correctly
178 int zero
= 0; // psuedo-constant
182 a
= a
; // expected-warning{{Assigned value is always the same as the existing value}}
183 a
= enum1
* a
; // no-warning
191 // Check truncations do not flag as self-assignments
194 a
= (short)a
; // no-warning
198 // This test case previously flagged a warning at 'b == c' because the
199 // analyzer previously allowed 'UnknownVal' as the index for ElementRegions.
200 typedef struct RDar8431728_F
{
202 unsigned char *RDar8431728_B
;
203 int RDar8431728_E
[6];
205 static inline int RDar8431728_C(RDar8431728_D
* s
, int n
,
206 unsigned char **RDar8431728_B_ptr
) {
207 int xy
, wrap
, pred
, a
, b
, c
;
209 xy
= s
->RDar8431728_E
[n
];
210 wrap
= s
->RDar8431728_A
;
212 a
= s
->RDar8431728_B
[xy
- 1];
213 b
= s
->RDar8431728_B
[xy
- 1 - wrap
];
214 c
= s
->RDar8431728_B
[xy
- wrap
];
216 if (b
== c
) { // no-warning
222 *RDar8431728_B_ptr
= &s
->RDar8431728_B
[xy
];
227 // <rdar://problem/8601243> - Don't warn on pointer arithmetic. This
228 // is often idiomatic.
229 unsigned rdar8601243_aux(unsigned n
);
233 start
= start
+ rdar8601243_aux(sizeof(arr
) - (arr
- start
)); // no-warning