Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / ptr-arith.c
blob40c8188704e811bad005ecf176fb94c30129b3a8
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.FixedAddr,alpha.core.PointerArithm,alpha.core.PointerSub,debug.ExprInspection -Wno-pointer-to-int-cast -verify -triple x86_64-apple-darwin9 -Wno-tautological-pointer-compare -analyzer-config eagerly-assume=false %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.FixedAddr,alpha.core.PointerArithm,alpha.core.PointerSub,debug.ExprInspection -Wno-pointer-to-int-cast -verify -triple i686-apple-darwin9 -Wno-tautological-pointer-compare -analyzer-config eagerly-assume=false %s
4 void clang_analyzer_eval(int);
5 void clang_analyzer_dump(int);
7 void f1(void) {
8 int a[10];
9 int *p = a;
10 ++p;
13 char* foo(void);
15 void f2(void) {
16 char *p = foo();
17 ++p;
20 // This test case checks if we get the right rvalue type of a TypedViewRegion.
21 // The ElementRegion's type depends on the array region's rvalue type. If it was
22 // a pointer type, we would get a loc::SymbolVal for '*p'.
23 void* memchr(const void *, int, __typeof__(sizeof(0)));
24 static int
25 domain_port (const char *domain_b, const char *domain_e,
26 const char **domain_e_ptr)
28 int port = 0;
30 const char *p;
31 const char *colon = memchr (domain_b, ':', domain_e - domain_b);
33 for (p = colon + 1; p < domain_e ; p++)
34 port = 10 * port + (*p - '0');
35 return port;
38 void f3(void) {
39 int x, y;
40 int d = &y - &x; // expected-warning{{Subtraction of two pointers that do not point to the same memory chunk may cause incorrect result}}
42 int a[10];
43 int *p = &a[2];
44 int *q = &a[8];
45 d = q-p; // no-warning
48 void f4(void) {
49 int *p;
50 p = (int*) 0x10000; // expected-warning{{Using a fixed address is not portable because that address will probably not be valid in all environments or platforms}}
53 void f5(void) {
54 int x, y;
55 int *p;
56 p = &x + 1; // expected-warning{{Pointer arithmetic on non-array variables relies on memory layout, which is dangerous}}
58 int a[10];
59 p = a + 1; // no-warning
62 // Allow arithmetic on different symbolic regions.
63 void f6(int *p, int *q) {
64 int d = q - p; // no-warning
67 void null_operand(int *a) {
68 start:
69 // LHS is a label, RHS is NULL
70 clang_analyzer_eval(&&start != 0); // expected-warning{{TRUE}}
71 clang_analyzer_eval(&&start >= 0); // expected-warning{{TRUE}}
72 clang_analyzer_eval(&&start > 0); // expected-warning{{TRUE}}
73 clang_analyzer_eval((&&start - 0) != 0); // expected-warning{{TRUE}}
75 // LHS is a non-symbolic value, RHS is NULL
76 clang_analyzer_eval(&a != 0); // expected-warning{{TRUE}}
77 clang_analyzer_eval(&a >= 0); // expected-warning{{TRUE}}
78 clang_analyzer_eval(&a > 0); // expected-warning{{TRUE}}
79 clang_analyzer_eval((&a - 0) != 0); // expected-warning{{TRUE}}
81 // LHS is NULL, RHS is non-symbolic
82 // The same code is used for labels and non-symbolic values.
83 clang_analyzer_eval(0 != &a); // expected-warning{{TRUE}}
84 clang_analyzer_eval(0 <= &a); // expected-warning{{TRUE}}
85 clang_analyzer_eval(0 < &a); // expected-warning{{TRUE}}
87 // LHS is a symbolic value, RHS is NULL
88 clang_analyzer_eval(a != 0); // expected-warning{{UNKNOWN}}
89 clang_analyzer_eval(a >= 0); // expected-warning{{TRUE}}
90 clang_analyzer_eval(a <= 0); // expected-warning{{UNKNOWN}}
91 clang_analyzer_eval((a - 0) != 0); // expected-warning{{UNKNOWN}}
93 // LHS is NULL, RHS is a symbolic value
94 clang_analyzer_eval(0 != a); // expected-warning{{UNKNOWN}}
95 clang_analyzer_eval(0 <= a); // expected-warning{{TRUE}}
96 clang_analyzer_eval(0 < a); // expected-warning{{UNKNOWN}}
99 void const_locs(void) {
100 char *a = (char*)0x1000;
101 char *b = (char*)0x1100;
102 start:
103 clang_analyzer_eval(a != b); // expected-warning{{TRUE}}
104 clang_analyzer_eval(a < b); // expected-warning{{TRUE}}
105 clang_analyzer_eval(a <= b); // expected-warning{{TRUE}}
106 clang_analyzer_eval((b-a) == 0x100); // expected-warning{{TRUE}}
108 clang_analyzer_eval(&&start == a); // expected-warning{{UNKNOWN}}
109 clang_analyzer_eval(a == &&start); // expected-warning{{UNKNOWN}}
110 clang_analyzer_eval(&a == (char**)a); // expected-warning{{UNKNOWN}}
111 clang_analyzer_eval((char**)a == &a); // expected-warning{{UNKNOWN}}
114 void array_matching_types(void) {
115 int array[10];
116 int *a = &array[2];
117 int *b = &array[5];
119 clang_analyzer_eval(a != b); // expected-warning{{TRUE}}
120 clang_analyzer_eval(a < b); // expected-warning{{TRUE}}
121 clang_analyzer_eval(a <= b); // expected-warning{{TRUE}}
122 clang_analyzer_eval((b-a) != 0); // expected-warning{{TRUE}}
125 // This takes a different code path than array_matching_types()
126 void array_different_types(void) {
127 int array[10];
128 int *a = &array[2];
129 char *b = (char*)&array[5];
131 clang_analyzer_eval(a != b); // expected-warning{{TRUE}} expected-warning{{comparison of distinct pointer types}}
132 clang_analyzer_eval(a < b); // expected-warning{{TRUE}} expected-warning{{comparison of distinct pointer types}}
133 clang_analyzer_eval(a <= b); // expected-warning{{TRUE}} expected-warning{{comparison of distinct pointer types}}
136 struct test { int x; int y; };
137 void struct_fields(void) {
138 struct test a, b;
140 clang_analyzer_eval(&a.x != &a.y); // expected-warning{{TRUE}}
141 clang_analyzer_eval(&a.x < &a.y); // expected-warning{{TRUE}}
142 clang_analyzer_eval(&a.x <= &a.y); // expected-warning{{TRUE}}
144 clang_analyzer_eval(&a.x != &b.x); // expected-warning{{TRUE}}
145 clang_analyzer_eval(&a.x > &b.x); // expected-warning{{UNKNOWN}}
146 clang_analyzer_eval(&a.x >= &b.x); // expected-warning{{UNKNOWN}}
149 void mixed_region_types(void) {
150 struct test s;
151 int array[2];
152 void *a = &array, *b = &s;
154 clang_analyzer_eval(&a != &b); // expected-warning{{TRUE}}
155 clang_analyzer_eval(&a > &b); // expected-warning{{UNKNOWN}}
156 clang_analyzer_eval(&a >= &b); // expected-warning{{UNKNOWN}}
159 void symbolic_region(int *p) {
160 int a;
162 clang_analyzer_eval(&a != p); // expected-warning{{TRUE}}
163 clang_analyzer_eval(&a > p); // expected-warning{{UNKNOWN}}
164 clang_analyzer_eval(&a >= p); // expected-warning{{UNKNOWN}}
167 void PR7527 (int *p) {
168 if (((int) p) & 1) // not crash
169 return;
172 void use_symbols(int *lhs, int *rhs) {
173 clang_analyzer_eval(lhs < rhs); // expected-warning{{UNKNOWN}}
174 if (lhs < rhs)
175 return;
176 clang_analyzer_eval(lhs < rhs); // expected-warning{{FALSE}}
178 clang_analyzer_eval(lhs - rhs); // expected-warning{{UNKNOWN}}
179 if ((lhs - rhs) != 5)
180 return;
181 clang_analyzer_eval((lhs - rhs) == 5); // expected-warning{{TRUE}}
184 void equal_implies_zero(int *lhs, int *rhs) {
185 clang_analyzer_eval(lhs == rhs); // expected-warning{{UNKNOWN}}
186 if (lhs == rhs) {
187 clang_analyzer_eval(lhs != rhs); // expected-warning{{FALSE}}
188 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{TRUE}}
189 return;
191 clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
192 clang_analyzer_eval(lhs != rhs); // expected-warning{{TRUE}}
193 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{FALSE}}
196 void zero_implies_equal(int *lhs, int *rhs) {
197 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{UNKNOWN}}
198 if ((rhs - lhs) == 0) {
199 clang_analyzer_eval(lhs != rhs); // expected-warning{{FALSE}}
200 clang_analyzer_eval(lhs == rhs); // expected-warning{{TRUE}}
201 return;
203 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{FALSE}}
204 clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
205 clang_analyzer_eval(lhs != rhs); // expected-warning{{TRUE}}
208 void comparisons_imply_size(int *lhs, int *rhs) {
209 clang_analyzer_eval(lhs <= rhs); // expected-warning{{UNKNOWN}}
211 if (lhs > rhs) {
212 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{FALSE}}
213 return;
216 clang_analyzer_eval(lhs <= rhs); // expected-warning{{TRUE}}
217 // FIXME: In Z3ConstraintManager, ptrdiff_t is mapped to signed bitvector. However, this does not directly imply the unsigned comparison.
218 #ifdef ANALYZER_CM_Z3
219 clang_analyzer_eval((rhs - lhs) >= 0); // expected-warning{{UNKNOWN}}
220 #else
221 clang_analyzer_eval((rhs - lhs) >= 0); // expected-warning{{TRUE}}
222 #endif
223 clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{UNKNOWN}}
225 if (lhs >= rhs) {
226 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{TRUE}}
227 return;
230 clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
231 clang_analyzer_eval(lhs < rhs); // expected-warning{{TRUE}}
232 #ifdef ANALYZER_CM_Z3
233 clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{UNKNOWN}}
234 #else
235 clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{TRUE}}
236 #endif
239 void size_implies_comparison(int *lhs, int *rhs) {
240 clang_analyzer_eval(lhs <= rhs); // expected-warning{{UNKNOWN}}
242 if ((rhs - lhs) < 0) {
243 clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
244 return;
247 #ifdef ANALYZER_CM_Z3
248 clang_analyzer_eval(lhs <= rhs); // expected-warning{{UNKNOWN}}
249 #else
250 clang_analyzer_eval(lhs <= rhs); // expected-warning{{TRUE}}
251 #endif
252 clang_analyzer_eval((rhs - lhs) >= 0); // expected-warning{{TRUE}}
253 clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{UNKNOWN}}
255 if ((rhs - lhs) <= 0) {
256 clang_analyzer_eval(lhs == rhs); // expected-warning{{TRUE}}
257 return;
260 clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
261 #ifdef ANALYZER_CM_Z3
262 clang_analyzer_eval(lhs < rhs); // expected-warning{{UNKNOWN}}
263 #else
264 clang_analyzer_eval(lhs < rhs); // expected-warning{{TRUE}}
265 #endif
266 clang_analyzer_eval((rhs - lhs) > 0); // expected-warning{{TRUE}}
269 void zero_implies_reversed_equal(int *lhs, int *rhs) {
270 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{UNKNOWN}}
271 if ((rhs - lhs) == 0) {
272 clang_analyzer_eval(rhs != lhs); // expected-warning{{FALSE}}
273 clang_analyzer_eval(rhs == lhs); // expected-warning{{TRUE}}
274 return;
276 clang_analyzer_eval((rhs - lhs) == 0); // expected-warning{{FALSE}}
277 clang_analyzer_eval(rhs == lhs); // expected-warning{{FALSE}}
278 clang_analyzer_eval(rhs != lhs); // expected-warning{{TRUE}}
281 void canonical_equal(int *lhs, int *rhs) {
282 clang_analyzer_eval(lhs == rhs); // expected-warning{{UNKNOWN}}
283 if (lhs == rhs) {
284 clang_analyzer_eval(rhs == lhs); // expected-warning{{TRUE}}
285 return;
287 clang_analyzer_eval(lhs == rhs); // expected-warning{{FALSE}}
288 clang_analyzer_eval(rhs == lhs); // expected-warning{{FALSE}}
291 void compare_element_region_and_base(int *p) {
292 int *q = p - 1;
293 clang_analyzer_eval(p == q); // expected-warning{{FALSE}}
296 struct Point {
297 int x;
298 int y;
300 void symbolicFieldRegion(struct Point *points, int i, int j) {
301 clang_analyzer_eval(&points[i].x == &points[j].x);// expected-warning{{UNKNOWN}}
302 clang_analyzer_eval(&points[i].x == &points[i].y);// expected-warning{{FALSE}}
303 clang_analyzer_eval(&points[i].x < &points[i].y);// expected-warning{{TRUE}}
306 void negativeIndex(char *str) {
307 *(str + 1) = 'a';
308 clang_analyzer_eval(*(str + 1) == 'a'); // expected-warning{{TRUE}}
309 clang_analyzer_eval(*(str - 1) == 'a'); // expected-warning{{UNKNOWN}}
311 char *ptr1 = str - 1;
312 clang_analyzer_eval(*ptr1 == 'a'); // expected-warning{{UNKNOWN}}
314 char *ptr2 = str;
315 ptr2 -= 1;
316 clang_analyzer_eval(*ptr2 == 'a'); // expected-warning{{UNKNOWN}}
318 char *ptr3 = str;
319 --ptr3;
320 clang_analyzer_eval(*ptr3 == 'a'); // expected-warning{{UNKNOWN}}
323 void test_no_crash_on_pointer_to_label(void) {
324 char *a = &&label;
325 a[0] = 0;
326 label:;
329 typedef __attribute__((__ext_vector_type__(2))) float simd_float2;
330 float test_nowarning_on_vector_deref(void) {
331 simd_float2 x = {0, 1};
332 return x[1]; // no-warning
335 struct s {
336 int v;
339 // These three expressions should produce the same sym vals.
340 void struct_pointer_canon(struct s *ps) {
341 struct s ss = *ps;
342 clang_analyzer_dump((*ps).v);
343 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<struct s * ps>},0 S64b,struct s}.v>}}
344 clang_analyzer_dump(ps[0].v);
345 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<struct s * ps>},0 S64b,struct s}.v>}}
346 clang_analyzer_dump(ps->v);
347 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<struct s * ps>},0 S64b,struct s}.v>}}
348 clang_analyzer_eval((*ps).v == ps[0].v); // expected-warning{{TRUE}}
349 clang_analyzer_eval((*ps).v == ps->v); // expected-warning{{TRUE}}
350 clang_analyzer_eval(ps[0].v == ps->v); // expected-warning{{TRUE}}
353 void struct_pointer_canon_bidim(struct s **ps) {
354 struct s ss = **ps;
355 clang_analyzer_eval(&(ps[0][0].v) == &((*ps)->v)); // expected-warning{{TRUE}}
358 typedef struct s T1;
359 typedef struct s T2;
360 void struct_pointer_canon_typedef(T1 *ps) {
361 T2 ss = *ps;
362 clang_analyzer_dump((*ps).v);
363 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<T1 * ps>},0 S64b,struct s}.v>}}
364 clang_analyzer_dump(ps[0].v);
365 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<T1 * ps>},0 S64b,struct s}.v>}}
366 clang_analyzer_dump(ps->v);
367 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<T1 * ps>},0 S64b,struct s}.v>}}
368 clang_analyzer_eval((*ps).v == ps[0].v); // expected-warning{{TRUE}}
369 clang_analyzer_eval((*ps).v == ps->v); // expected-warning{{TRUE}}
370 clang_analyzer_eval(ps[0].v == ps->v); // expected-warning{{TRUE}}
373 void struct_pointer_canon_bidim_typedef(T1 **ps) {
374 T2 ss = **ps;
375 clang_analyzer_eval(&(ps[0][0].v) == &((*ps)->v)); // expected-warning{{TRUE}}
378 void struct_pointer_canon_const(const struct s *ps) {
379 struct s ss = *ps;
380 clang_analyzer_dump((*ps).v);
381 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<const struct s * ps>},0 S64b,struct s}.v>}}
382 clang_analyzer_dump(ps[0].v);
383 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<const struct s * ps>},0 S64b,struct s}.v>}}
384 clang_analyzer_dump(ps->v);
385 // expected-warning-re@-1{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<const struct s * ps>},0 S64b,struct s}.v>}}
386 clang_analyzer_eval((*ps).v == ps[0].v); // expected-warning{{TRUE}}
387 clang_analyzer_eval((*ps).v == ps->v); // expected-warning{{TRUE}}
388 clang_analyzer_eval(ps[0].v == ps->v); // expected-warning{{TRUE}}