blob: e602d5f5276b5f0ecb679637317988dfe5716f64 [file] [log] [blame]
Daniel Dunbarffd408a2009-03-24 02:24:46 +00001// RUN: clang-cc -analyze -checker-simple -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
2// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
Ted Kremenekaf81ece2009-05-04 06:18:28 +00003// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s
4
5// RegionStore now has an infinite recursion with this test case.
6// NOWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
7// NOWORK: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
Zhongxing Xubcec9d52008-10-17 05:19:52 +00008
Zhongxing Xu84b043e2008-10-24 08:51:58 +00009struct s {
10 int data;
11 int data_array[10];
12};
Zhongxing Xubcec9d52008-10-17 05:19:52 +000013
Zhongxing Xu305ea6c2008-10-27 09:19:25 +000014typedef struct {
15 int data;
16} STYPE;
17
Zhongxing Xu29c87a22008-11-02 13:17:44 +000018void g1(struct s* p);
19
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000020// Array to pointer conversion. Array in the struct field.
Zhongxing Xubcec9d52008-10-17 05:19:52 +000021void f(void) {
22 int a[10];
23 int (*p)[10];
24 p = &a;
25 (*p)[3] = 1;
26
27 struct s d;
28 struct s *q;
29 q = &d;
Zhongxing Xu84b043e2008-10-24 08:51:58 +000030 q->data = 3;
31 d.data_array[9] = 17;
Zhongxing Xubcec9d52008-10-17 05:19:52 +000032}
Zhongxing Xubbef7052008-10-25 14:11:23 +000033
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000034// StringLiteral in lvalue context and pointer to array type.
35// p: ElementRegion, q: StringRegion
Zhongxing Xubbef7052008-10-25 14:11:23 +000036void f2() {
37 char *p = "/usr/local";
38 char (*q)[4];
39 q = &"abc";
40}
Zhongxing Xu305ea6c2008-10-27 09:19:25 +000041
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000042// Typedef'ed struct definition.
Zhongxing Xu305ea6c2008-10-27 09:19:25 +000043void f3() {
44 STYPE s;
45}
Zhongxing Xuaeef0612008-10-31 10:23:14 +000046
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000047// Initialize array with InitExprList.
Zhongxing Xuaeef0612008-10-31 10:23:14 +000048void f4() {
49 int a[] = { 1, 2, 3};
50 int b[3] = { 1, 2 };
Zhongxing Xuf84c31b2009-01-23 10:23:13 +000051 struct s c[] = {{1,{1}}};
Zhongxing Xuaeef0612008-10-31 10:23:14 +000052}
Zhongxing Xu29c87a22008-11-02 13:17:44 +000053
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000054// Struct variable in lvalue context.
Zhongxing Xu85adc922009-01-13 01:49:57 +000055// Assign UnknownVal to the whole struct.
Zhongxing Xu29c87a22008-11-02 13:17:44 +000056void f5() {
57 struct s data;
58 g1(&data);
59}
Zhongxing Xu05ce9aa2008-11-13 07:59:15 +000060
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000061// AllocaRegion test.
Zhongxing Xu05ce9aa2008-11-13 07:59:15 +000062void f6() {
63 char *p;
64 p = __builtin_alloca(10);
65 p[1] = 'a';
Zhongxing Xufaacaf32009-05-20 09:03:10 +000066 // Test if RegionStore::EvalBinOp converts the alloca region to element
67 // region.
Zhongxing Xuc890e332009-05-20 09:00:16 +000068 p += 2;
Zhongxing Xu05ce9aa2008-11-13 07:59:15 +000069}
Zhongxing Xu54f2e182008-11-13 08:44:52 +000070
71struct s2;
72
73void g2(struct s2 *p);
74
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000075// Incomplete struct pointer used as function argument.
Zhongxing Xu54f2e182008-11-13 08:44:52 +000076void f7() {
77 struct s2 *p = __builtin_alloca(10);
78 g2(p);
79}
Zhongxing Xu59501122008-11-13 09:20:05 +000080
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000081// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu59501122008-11-13 09:20:05 +000082void f8() {
83 int a[10];
Zhongxing Xu150824e2008-11-24 23:45:56 +000084 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu59501122008-11-13 09:20:05 +000085}
Zhongxing Xuaeb9cc92008-11-18 13:30:46 +000086
Zhongxing Xucb9ca8d2008-11-25 01:45:11 +000087// Initialization of struct array elements.
Zhongxing Xuaeb9cc92008-11-18 13:30:46 +000088void f9() {
89 struct s a[10];
90}
Zhongxing Xuc8f73c52008-11-30 05:51:19 +000091
92// Initializing array with string literal.
93void f10() {
94 char a1[4] = "abc";
Zhongxing Xuc8f73c52008-11-30 05:51:19 +000095 char a3[6] = "abc";
96}
Zhongxing Xu99234992009-01-23 11:22:12 +000097
98// Retrieve the default value of element/field region.
99void f11() {
100 struct s a;
101 g(&a);
102 if (a.data == 0) // no-warning
103 a.data = 1;
104}
Zhongxing Xu002839e2009-02-19 08:42:43 +0000105
106// Convert unsigned offset to signed when creating ElementRegion from
107// SymbolicRegion.
108void f12(int *list) {
109 unsigned i = 0;
110 list[i] = 1;
111}
Zhongxing Xu52e23d62009-03-18 02:07:30 +0000112
113struct s1 {
114 struct s2 {
115 int d;
116 } e;
117};
118
119// The binding of a.e.d should not be removed. Test recursive subregion map
120// building: a->e, e->d. Only then 'a' could be added to live region roots.
121void f13(double timeout) {
122 struct s1 a;
123 a.e.d = (long) timeout;
124 if (a.e.d == 10)
125 a.e.d = 4;
126}
Zhongxing Xu0aca4242009-05-03 00:27:40 +0000127
128struct s3 {
129 int a[2];
130};
131
132static struct s3 opt;
133
134// Test if the embedded array is retrieved correctly.
135void f14() {
136 struct s3 my_opt = opt;
137}
Zhongxing Xu35edd9a2009-05-12 10:10:00 +0000138
139void bar(int*);
140
141// Test if the array is correctly invalidated.
142void f15() {
143 int a[10];
144 bar(a);
145 if (a[1]) // no-warning
146 1;
147}