blob: 0ce6afcc0c0ab154a94575a884b05544fae76948 [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 &&
3// RUN: clang-cc -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s &&
4// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
5// RUN: clang-cc -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00006
Zhongxing Xu72e16822008-10-24 08:51:58 +00007struct s {
8 int data;
9 int data_array[10];
10};
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000011
Zhongxing Xu234a7d22008-10-27 09:19:25 +000012typedef struct {
13 int data;
14} STYPE;
15
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000016void g1(struct s* p);
17
Zhongxing Xu661fc392008-11-25 01:45:11 +000018// Array to pointer conversion. Array in the struct field.
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000019void f(void) {
20 int a[10];
21 int (*p)[10];
22 p = &a;
23 (*p)[3] = 1;
24
25 struct s d;
26 struct s *q;
27 q = &d;
Zhongxing Xu72e16822008-10-24 08:51:58 +000028 q->data = 3;
29 d.data_array[9] = 17;
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000030}
Zhongxing Xu2e971202008-10-25 14:11:23 +000031
Zhongxing Xu661fc392008-11-25 01:45:11 +000032// StringLiteral in lvalue context and pointer to array type.
33// p: ElementRegion, q: StringRegion
Zhongxing Xu2e971202008-10-25 14:11:23 +000034void f2() {
35 char *p = "/usr/local";
36 char (*q)[4];
37 q = &"abc";
38}
Zhongxing Xu234a7d22008-10-27 09:19:25 +000039
Zhongxing Xu661fc392008-11-25 01:45:11 +000040// Typedef'ed struct definition.
Zhongxing Xu234a7d22008-10-27 09:19:25 +000041void f3() {
42 STYPE s;
43}
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000044
Zhongxing Xu661fc392008-11-25 01:45:11 +000045// Initialize array with InitExprList.
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000046void f4() {
47 int a[] = { 1, 2, 3};
48 int b[3] = { 1, 2 };
Zhongxing Xub61f49c2009-01-23 10:23:13 +000049 struct s c[] = {{1,{1}}};
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000050}
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000051
Zhongxing Xu661fc392008-11-25 01:45:11 +000052// Struct variable in lvalue context.
Zhongxing Xu5834ed62009-01-13 01:49:57 +000053// Assign UnknownVal to the whole struct.
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000054void f5() {
55 struct s data;
56 g1(&data);
57}
Zhongxing Xub6701332008-11-13 07:59:15 +000058
Zhongxing Xu661fc392008-11-25 01:45:11 +000059// AllocaRegion test.
Zhongxing Xub6701332008-11-13 07:59:15 +000060void f6() {
61 char *p;
62 p = __builtin_alloca(10);
63 p[1] = 'a';
64}
Zhongxing Xufb75b252008-11-13 08:44:52 +000065
66struct s2;
67
68void g2(struct s2 *p);
69
Zhongxing Xu661fc392008-11-25 01:45:11 +000070// Incomplete struct pointer used as function argument.
Zhongxing Xufb75b252008-11-13 08:44:52 +000071void f7() {
72 struct s2 *p = __builtin_alloca(10);
73 g2(p);
74}
Zhongxing Xu26134a12008-11-13 09:20:05 +000075
Zhongxing Xu661fc392008-11-25 01:45:11 +000076// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu26134a12008-11-13 09:20:05 +000077void f8() {
78 int a[10];
Zhongxing Xu33d7cbf2008-11-24 23:45:56 +000079 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu26134a12008-11-13 09:20:05 +000080}
Zhongxing Xu617ff312008-11-18 13:30:46 +000081
Zhongxing Xu661fc392008-11-25 01:45:11 +000082// Initialization of struct array elements.
Zhongxing Xu617ff312008-11-18 13:30:46 +000083void f9() {
84 struct s a[10];
85}
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000086
87// Initializing array with string literal.
88void f10() {
89 char a1[4] = "abc";
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000090 char a3[6] = "abc";
91}
Zhongxing Xu562c4d92009-01-23 11:22:12 +000092
93// Retrieve the default value of element/field region.
94void f11() {
95 struct s a;
96 g(&a);
97 if (a.data == 0) // no-warning
98 a.data = 1;
99}
Zhongxing Xu3450a552009-02-19 08:42:43 +0000100
101// Convert unsigned offset to signed when creating ElementRegion from
102// SymbolicRegion.
103void f12(int *list) {
104 unsigned i = 0;
105 list[i] = 1;
106}
Zhongxing Xuc57bc592009-03-18 02:07:30 +0000107
108struct s1 {
109 struct s2 {
110 int d;
111 } e;
112};
113
114// The binding of a.e.d should not be removed. Test recursive subregion map
115// building: a->e, e->d. Only then 'a' could be added to live region roots.
116void f13(double timeout) {
117 struct s1 a;
118 a.e.d = (long) timeout;
119 if (a.e.d == 10)
120 a.e.d = 4;
121}
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000122
123struct s3 {
124 int a[2];
125};
126
127static struct s3 opt;
128
129// Test if the embedded array is retrieved correctly.
130void f14() {
131 struct s3 my_opt = opt;
132}