blob: c0e1d8b7e39f7107af1100f6e0aa749b60668fb9 [file] [log] [blame]
Daniel Dunbard7d5f022009-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 Kremenekf936f452009-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 Xuef8b28e2008-10-17 05:19:52 +00008
Zhongxing Xu72e16822008-10-24 08:51:58 +00009struct s {
10 int data;
11 int data_array[10];
12};
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000013
Zhongxing Xu234a7d22008-10-27 09:19:25 +000014typedef struct {
15 int data;
16} STYPE;
17
Zhongxing Xu91844122009-05-20 09:18:48 +000018void g(char *p);
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000019void g1(struct s* p);
20
Zhongxing Xu661fc392008-11-25 01:45:11 +000021// Array to pointer conversion. Array in the struct field.
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000022void f(void) {
23 int a[10];
24 int (*p)[10];
25 p = &a;
26 (*p)[3] = 1;
27
28 struct s d;
29 struct s *q;
30 q = &d;
Zhongxing Xu72e16822008-10-24 08:51:58 +000031 q->data = 3;
32 d.data_array[9] = 17;
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000033}
Zhongxing Xu2e971202008-10-25 14:11:23 +000034
Zhongxing Xu661fc392008-11-25 01:45:11 +000035// StringLiteral in lvalue context and pointer to array type.
36// p: ElementRegion, q: StringRegion
Zhongxing Xu2e971202008-10-25 14:11:23 +000037void f2() {
38 char *p = "/usr/local";
39 char (*q)[4];
40 q = &"abc";
41}
Zhongxing Xu234a7d22008-10-27 09:19:25 +000042
Zhongxing Xu661fc392008-11-25 01:45:11 +000043// Typedef'ed struct definition.
Zhongxing Xu234a7d22008-10-27 09:19:25 +000044void f3() {
45 STYPE s;
46}
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000047
Zhongxing Xu661fc392008-11-25 01:45:11 +000048// Initialize array with InitExprList.
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000049void f4() {
50 int a[] = { 1, 2, 3};
51 int b[3] = { 1, 2 };
Zhongxing Xub61f49c2009-01-23 10:23:13 +000052 struct s c[] = {{1,{1}}};
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000053}
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000054
Zhongxing Xu661fc392008-11-25 01:45:11 +000055// Struct variable in lvalue context.
Zhongxing Xu5834ed62009-01-13 01:49:57 +000056// Assign UnknownVal to the whole struct.
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000057void f5() {
58 struct s data;
59 g1(&data);
60}
Zhongxing Xub6701332008-11-13 07:59:15 +000061
Zhongxing Xu661fc392008-11-25 01:45:11 +000062// AllocaRegion test.
Zhongxing Xub6701332008-11-13 07:59:15 +000063void f6() {
64 char *p;
65 p = __builtin_alloca(10);
Zhongxing Xu91844122009-05-20 09:18:48 +000066 g(p);
67 char c = *p;
Zhongxing Xub6701332008-11-13 07:59:15 +000068 p[1] = 'a';
Zhongxing Xu2acc3992009-05-20 09:03:10 +000069 // Test if RegionStore::EvalBinOp converts the alloca region to element
70 // region.
Zhongxing Xu262fd032009-05-20 09:00:16 +000071 p += 2;
Zhongxing Xub6701332008-11-13 07:59:15 +000072}
Zhongxing Xufb75b252008-11-13 08:44:52 +000073
74struct s2;
75
76void g2(struct s2 *p);
77
Zhongxing Xu661fc392008-11-25 01:45:11 +000078// Incomplete struct pointer used as function argument.
Zhongxing Xufb75b252008-11-13 08:44:52 +000079void f7() {
80 struct s2 *p = __builtin_alloca(10);
81 g2(p);
82}
Zhongxing Xu26134a12008-11-13 09:20:05 +000083
Zhongxing Xu661fc392008-11-25 01:45:11 +000084// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu26134a12008-11-13 09:20:05 +000085void f8() {
86 int a[10];
Zhongxing Xu33d7cbf2008-11-24 23:45:56 +000087 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu26134a12008-11-13 09:20:05 +000088}
Zhongxing Xu617ff312008-11-18 13:30:46 +000089
Zhongxing Xu661fc392008-11-25 01:45:11 +000090// Initialization of struct array elements.
Zhongxing Xu617ff312008-11-18 13:30:46 +000091void f9() {
92 struct s a[10];
93}
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000094
95// Initializing array with string literal.
96void f10() {
97 char a1[4] = "abc";
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000098 char a3[6] = "abc";
99}
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000100
101// Retrieve the default value of element/field region.
102void f11() {
103 struct s a;
Zhongxing Xu91844122009-05-20 09:18:48 +0000104 g1(&a);
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000105 if (a.data == 0) // no-warning
106 a.data = 1;
107}
Zhongxing Xu3450a552009-02-19 08:42:43 +0000108
109// Convert unsigned offset to signed when creating ElementRegion from
110// SymbolicRegion.
111void f12(int *list) {
112 unsigned i = 0;
113 list[i] = 1;
114}
Zhongxing Xuc57bc592009-03-18 02:07:30 +0000115
116struct s1 {
117 struct s2 {
118 int d;
119 } e;
120};
121
122// The binding of a.e.d should not be removed. Test recursive subregion map
123// building: a->e, e->d. Only then 'a' could be added to live region roots.
124void f13(double timeout) {
125 struct s1 a;
126 a.e.d = (long) timeout;
127 if (a.e.d == 10)
128 a.e.d = 4;
129}
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000130
131struct s3 {
132 int a[2];
133};
134
135static struct s3 opt;
136
137// Test if the embedded array is retrieved correctly.
138void f14() {
139 struct s3 my_opt = opt;
140}
Zhongxing Xu264e9372009-05-12 10:10:00 +0000141
142void bar(int*);
143
144// Test if the array is correctly invalidated.
145void f15() {
146 int a[10];
147 bar(a);
148 if (a[1]) // no-warning
149 1;
150}