blob: 1d5e9448e92833a186a8fc1574b46c109ebd7e4a [file] [log] [blame]
Daniel Dunbard4270232009-01-20 23:17:32 +00001// RUN: clang -analyze -checker-simple -verify %s
2// DISABLE: clang -analyze -checker-simple -analyzer-store-region -verify %s
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00003
Zhongxing Xu72e16822008-10-24 08:51:58 +00004struct s {
5 int data;
6 int data_array[10];
7};
Zhongxing Xuef8b28e2008-10-17 05:19:52 +00008
Zhongxing Xu234a7d22008-10-27 09:19:25 +00009typedef struct {
10 int data;
11} STYPE;
12
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000013void g1(struct s* p);
14
Zhongxing Xu661fc392008-11-25 01:45:11 +000015// Array to pointer conversion. Array in the struct field.
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000016void f(void) {
17 int a[10];
18 int (*p)[10];
19 p = &a;
20 (*p)[3] = 1;
21
22 struct s d;
23 struct s *q;
24 q = &d;
Zhongxing Xu72e16822008-10-24 08:51:58 +000025 q->data = 3;
26 d.data_array[9] = 17;
Zhongxing Xuef8b28e2008-10-17 05:19:52 +000027}
Zhongxing Xu2e971202008-10-25 14:11:23 +000028
Zhongxing Xu661fc392008-11-25 01:45:11 +000029// StringLiteral in lvalue context and pointer to array type.
30// p: ElementRegion, q: StringRegion
Zhongxing Xu2e971202008-10-25 14:11:23 +000031void f2() {
32 char *p = "/usr/local";
33 char (*q)[4];
34 q = &"abc";
35}
Zhongxing Xu234a7d22008-10-27 09:19:25 +000036
Zhongxing Xu661fc392008-11-25 01:45:11 +000037// Typedef'ed struct definition.
Zhongxing Xu234a7d22008-10-27 09:19:25 +000038void f3() {
39 STYPE s;
40}
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000041
Zhongxing Xu661fc392008-11-25 01:45:11 +000042// Initialize array with InitExprList.
Zhongxing Xudf2aa1e2008-10-31 10:23:14 +000043void f4() {
44 int a[] = { 1, 2, 3};
45 int b[3] = { 1, 2 };
46}
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000047
Zhongxing Xu661fc392008-11-25 01:45:11 +000048// Struct variable in lvalue context.
Zhongxing Xu5834ed62009-01-13 01:49:57 +000049// Assign UnknownVal to the whole struct.
Zhongxing Xu04b90bc2008-11-02 13:17:44 +000050void f5() {
51 struct s data;
52 g1(&data);
53}
Zhongxing Xub6701332008-11-13 07:59:15 +000054
Zhongxing Xu661fc392008-11-25 01:45:11 +000055// AllocaRegion test.
Zhongxing Xub6701332008-11-13 07:59:15 +000056void f6() {
57 char *p;
58 p = __builtin_alloca(10);
59 p[1] = 'a';
60}
Zhongxing Xufb75b252008-11-13 08:44:52 +000061
62struct s2;
63
64void g2(struct s2 *p);
65
Zhongxing Xu661fc392008-11-25 01:45:11 +000066// Incomplete struct pointer used as function argument.
Zhongxing Xufb75b252008-11-13 08:44:52 +000067void f7() {
68 struct s2 *p = __builtin_alloca(10);
69 g2(p);
70}
Zhongxing Xu26134a12008-11-13 09:20:05 +000071
Zhongxing Xu661fc392008-11-25 01:45:11 +000072// sizeof() is unsigned while -1 is signed in array index.
Zhongxing Xu26134a12008-11-13 09:20:05 +000073void f8() {
74 int a[10];
Zhongxing Xu33d7cbf2008-11-24 23:45:56 +000075 a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
Zhongxing Xu26134a12008-11-13 09:20:05 +000076}
Zhongxing Xu617ff312008-11-18 13:30:46 +000077
Zhongxing Xu661fc392008-11-25 01:45:11 +000078// Initialization of struct array elements.
Zhongxing Xu617ff312008-11-18 13:30:46 +000079void f9() {
80 struct s a[10];
81}
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000082
83// Initializing array with string literal.
84void f10() {
85 char a1[4] = "abc";
Zhongxing Xu27cae9e2008-11-30 05:51:19 +000086 char a3[6] = "abc";
87}