blob: 197357f800e753078595d3dad85ff52f9e341c20 [file] [log] [blame]
Tom Caredb34ab72010-08-23 19:51:57 +00001// 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
Tom Caredb2fa8a2010-07-06 21:43:29 +00002
3// Basic tests
4
5extern void test(int i);
Ted Kremenek45329312010-07-17 00:40:32 +00006extern void test_f(float f);
Tom Caredb2fa8a2010-07-06 21:43:29 +00007
Tom Care245adab2010-08-18 21:17:24 +00008unsigned basic() {
Tom Caredb2fa8a2010-07-06 21:43:29 +00009 int x = 10, zero = 0, one = 1;
10
11 // x op x
Ted Kremenek3e5637f2010-07-27 18:49:08 +000012 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}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000015 x = 10; // no-warning
Ted Kremenek3e5637f2010-07-27 18:49:08 +000016 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}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000018 x = 10; // no-warning
Ted Kremenek3e5637f2010-07-27 18:49:08 +000019 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}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000023
24 // x op 1
Ted Kremenek3e5637f2010-07-27 18:49:08 +000025 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}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000029
30 // 1 op x
Ted Kremenek3e5637f2010-07-27 18:49:08 +000031 test(one * x); // expected-warning {{The left operand to '*' is always 1}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000032
33 // x op 0
Ted Kremenek3e5637f2010-07-27 18:49:08 +000034 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}}
Tom Caredb2fa8a2010-07-06 21:43:29 +000042
43 // 0 op x
Ted Kremenek3e5637f2010-07-27 18:49:08 +000044 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}}
Tom Care245adab2010-08-18 21:17:24 +000053
Tom Caredb34ab72010-08-23 19:51:57 +000054 // Overwrite the values so these aren't marked as Pseudoconstants
Tom Care245adab2010-08-18 21:17:24 +000055 x = 1;
56 zero = 2;
57 one = 3;
58
59 return x + zero + one;
Tom Caredb2fa8a2010-07-06 21:43:29 +000060}
Ted Kremenek45329312010-07-17 00:40:32 +000061
62void floats(float x) {
Tom Carea7a8a452010-08-12 22:45:47 +000063 test_f(x * 1.0); // no-warning
Ted Kremenek45329312010-07-17 00:40:32 +000064 test_f(x * 1.0F); // no-warning
65}
Tom Cared85770b2010-07-30 21:42:31 +000066
Tom Carea7a8a452010-08-12 22:45:47 +000067// Ensure that we don't report false poitives in complex loops
Tom Cared85770b2010-07-30 21:42:31 +000068void bailout() {
Tom Carea7a8a452010-08-12 22:45:47 +000069 int unused = 0, result = 4;
70 result = result; // expected-warning {{Assigned value is always the same as the existing value}}
Tom Cared85770b2010-07-30 21:42:31 +000071
Tom Carea7a8a452010-08-12 22:45:47 +000072 for (unsigned bg = 0; bg < 1024; bg ++) {
73 result = bg * result; // no-warning
Tom Cared85770b2010-07-30 21:42:31 +000074
75 for (int i = 0; i < 256; i++) {
Tom Carea7a8a452010-08-12 22:45:47 +000076 unused *= i; // no-warning
Tom Cared85770b2010-07-30 21:42:31 +000077 }
78 }
79}
Tom Carea7a8a452010-08-12 22:45:47 +000080
Tom Care6d0e6ce2010-08-27 22:46:32 +000081// Relaxed liveness - check that we don't kill liveness at assignments
82typedef unsigned uintptr_t;
83void kill_at_assign() {
84 short array[2];
85 uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}}
86 short *p = x; // expected-warning{{incompatible integer to pointer conversion}}
87
88 // The following branch should be infeasible.
89 if (!(p = &array[0])) { // expected-warning{{Assigned value is always the same as the existing value}}
90 p = 0;
91 *p = 1; // no-warning
92 }
93}
94
Tom Carea7a8a452010-08-12 22:45:47 +000095// False positive tests
96
97unsigned false1() {
Tom Care50e8ac22010-08-16 21:43:52 +000098 int a = 10;
99 return a * (5 - 2 - 3); // no-warning
Tom Carea7a8a452010-08-12 22:45:47 +0000100}
101
102enum testenum { enum1 = 0, enum2 };
103unsigned false2() {
Tom Care50e8ac22010-08-16 21:43:52 +0000104 int a = 1234;
105 return enum1 + a; // no-warning
Tom Carea7a8a452010-08-12 22:45:47 +0000106}
107
Tom Careef52bcb2010-08-24 21:09:07 +0000108// Self assignments of unused variables are common false positives
109unsigned false3(int param, int param2) {
Tom Care245adab2010-08-18 21:17:24 +0000110 param = param; // no-warning
111
Tom Careef52bcb2010-08-24 21:09:07 +0000112 // 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}}
114
Tom Care245adab2010-08-18 21:17:24 +0000115 unsigned nonparam = 5;
116
117 nonparam = nonparam; // expected-warning{{Assigned value is always the same as the existing value}}
118
Tom Careef52bcb2010-08-24 21:09:07 +0000119 return param2 + nonparam;
Tom Care245adab2010-08-18 21:17:24 +0000120}
121
Tom Caredb34ab72010-08-23 19:51:57 +0000122// Pseudo-constants (vars only read) and constants should not be reported
Tom Care245adab2010-08-18 21:17:24 +0000123unsigned false4() {
124 // Trivial constant
Tom Caredb34ab72010-08-23 19:51:57 +0000125 const int height = 1;
Tom Care82389412010-08-23 19:57:25 +0000126 int c = 42;
127 test(height * c); // no-warning
Tom Caredb34ab72010-08-23 19:51:57 +0000128
Tom Care82389412010-08-23 19:57:25 +0000129 // Pseudo-constant (never changes after decl)
130 int width = height;
131
Tom Care245adab2010-08-18 21:17:24 +0000132 return width * 10; // no-warning
133}
Tom Caredb34ab72010-08-23 19:51:57 +0000134
Tom Care967fea62010-08-25 22:37:26 +0000135// Block pseudoconstants
136void false4a() {
137 // Pseudo-constant
138 __block int a = 1;
139 int b = 10;
140 __block int c = 0;
141 b *= a; // no-warning
142
143 ^{
144 // Psuedoconstant block var
145 test(b * c); // no-warning
146
147 // Non-pseudoconstant block var
148 int d = 0;
149 test(b * d); // expected-warning{{The right operand to '*' is always 0}}
150 d = 5;
151 test(d);
152 }();
153
154 test(a + b);
155}
156
Tom Caredb34ab72010-08-23 19:51:57 +0000157// Static vars are common false positives
158int false5() {
159 static int test = 0;
160 int a = 56;
161 a *= test; // no-warning
162 test++;
163 return a;
164}
165
166// Non-local storage vars are considered false positives
167int globalInt = 1;
168int false6() {
169 int localInt = 23;
170
171 localInt /= globalInt;
172
173 return localInt;
174}
Tom Care6216dc02010-08-30 19:25:43 +0000175
176// Check that assignments filter out false positives correctly
177int false7() {
178 int zero = 0; // psuedo-constant
179 int one = 1;
180
181 int a = 55;
182 a = a; // expected-warning{{Assigned value is always the same as the existing value}}
183 a = enum1 * a; // no-warning
184
185 int b = 123;
186 b = b; // no-warning
187
188 return a;
189}
Tom Care84c24ed2010-09-07 20:27:56 +0000190
191// Check truncations do not flag as self-assignments
192void false8() {
193 int a = 10000000;
194 a = (short)a; // no-warning
195 test(a);
196}
Ted Kremenek02282ac2010-09-15 03:13:30 +0000197
198// This test case previously flagged a warning at 'b == c' because the
199// analyzer previously allowed 'UnknownVal' as the index for ElementRegions.
200typedef struct RDar8431728_F {
201 int RDar8431728_A;
202 unsigned char *RDar8431728_B;
203 int RDar8431728_E[6];
204} RDar8431728_D;
205static inline int RDar8431728_C(RDar8431728_D * s, int n,
206 unsigned char **RDar8431728_B_ptr) {
207 int xy, wrap, pred, a, b, c;
208
209 xy = s->RDar8431728_E[n];
210 wrap = s->RDar8431728_A;
211
212 a = s->RDar8431728_B[xy - 1];
213 b = s->RDar8431728_B[xy - 1 - wrap];
214 c = s->RDar8431728_B[xy - wrap];
215
216 if (b == c) { // no-warning
217 pred = a;
218 } else {
219 pred = c;
220 }
221
222 *RDar8431728_B_ptr = &s->RDar8431728_B[xy];
223
224 return pred;
225}
226
Ted Kremenek74faec22010-10-29 01:06:54 +0000227// <rdar://problem/8601243> - Don't warn on pointer arithmetic. This
228// is often idiomatic.
229unsigned rdar8601243_aux(unsigned n);
230void rdar8601243() {
231 char arr[100];
232 char *start = arr;
233 start = start + rdar8601243_aux(sizeof(arr) - (arr - start)); // no-warning
234 (void) start;
235}
236