blob: a8de8245cd0ab03ce119e6ea9cd8c84bc1ac6d62 [file] [log] [blame]
Ted Kremeneka7ac9442009-01-22 20:27:48 +00001// RUN: clang -analyze -checker-simple -analyzer-store-basic -verify %s &&
2// RUN: clang -analyze -checker-cfref -analyzer-store-basic -verify %s &&
3// RUN: clang -analyze -checker-cfref -analyzer-store-region -verify %s
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00004
Zhongxing Xu72e16822008-10-24 08:51:58 +00005struct s {
6 int data;
7 int data_array[10];
8};
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00009
Zhongxing Xu234a7d22008-10-27 09:19:25 +000010typedef struct {
11 int data;
12} STYPE;
13
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000014void g1(struct s* p);
15
Zhongxing Xu661fc392008-11-25 01:45:11 +000016// Array to pointer conversion. Array in the struct field.
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000017void f(void) {
18 int a[10];
19 int (*p)[10];
20 p = &a;
21 (*p)[3] = 1;
22
23 struct s d;
24 struct s *q;
25 q = &d;
Zhongxing Xu72e16822008-10-24 08:51:58 +000026 q->data = 3;
27 d.data_array[9] = 17;
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000028}
Zhongxing Xu2e971202008-10-25 14:11:23 +000029
Zhongxing Xu661fc392008-11-25 01:45:11 +000030// StringLiteral in lvalue context and pointer to array type.
31// p: ElementRegion, q: StringRegion
Zhongxing Xu2e971202008-10-25 14:11:23 +000032void f2() {
33 char *p = "/usr/local";
34 char (*q)[4];
35 q = &"abc";
36}
Zhongxing Xu234a7d22008-10-27 09:19:25 +000037
Zhongxing Xu661fc392008-11-25 01:45:11 +000038// Typedef'ed struct definition.
Zhongxing Xu234a7d22008-10-27 09:19:25 +000039void f3() {
40 STYPE s;
41}
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000042
Zhongxing Xu661fc392008-11-25 01:45:11 +000043// Initialize array with InitExprList.
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000044void f4() {
45 int a[] = { 1, 2, 3};
46 int b[3] = { 1, 2 };
Zhongxing Xub61f49c2009-01-23 10:23:13 +000047 struct s c[] = {{1,{1}}};
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000048}
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000049
Zhongxing Xu661fc392008-11-25 01:45:11 +000050// Struct variable in lvalue context.
Zhongxing Xu5834ed62009-01-13 01:49:57 +000051// Assign UnknownVal to the whole struct.
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000052void f5() {
53 struct s data;
54 g1(&data);
55}
Zhongxing Xub6701332008-11-13 07:59:15 +000056
Zhongxing Xu661fc392008-11-25 01:45:11 +000057// AllocaRegion test.
Zhongxing Xub6701332008-11-13 07:59:15 +000058void f6() {
59 char *p;
60 p = __builtin_alloca(10);
61 p[1] = 'a';
62}
Zhongxing Xufb75b252008-11-13 08:44:52 +000063
64struct s2;
65
66void g2(struct s2 *p);
67
Zhongxing Xu661fc392008-11-25 01:45:11 +000068// Incomplete struct pointer used as function argument.
Zhongxing Xufb75b252008-11-13 08:44:52 +000069void f7() {
70 struct s2 *p = __builtin_alloca(10);
71 g2(p);
72}
Zhongxing Xu26134a12008-11-13 09:20:05 +000073
Zhongxing Xu661fc392008-11-25 01:45:11 +000074// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu26134a12008-11-13 09:20:05 +000075void f8() {
76 int a[10];
Zhongxing Xu33d7cbf2008-11-24 23:45:56 +000077 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu26134a12008-11-13 09:20:05 +000078}
Zhongxing Xu617ff312008-11-18 13:30:46 +000079
Zhongxing Xu661fc392008-11-25 01:45:11 +000080// Initialization of struct array elements.
Zhongxing Xu617ff312008-11-18 13:30:46 +000081void f9() {
82 struct s a[10];
83}
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000084
85// Initializing array with string literal.
86void f10() {
87 char a1[4] = "abc";
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000088 char a3[6] = "abc";
89}
Zhongxing Xu562c4d92009-01-23 11:22:12 +000090
91// Retrieve the default value of element/field region.
92void f11() {
93 struct s a;
94 g(&a);
95 if (a.data == 0) // no-warning
96 a.data = 1;
97}