array-struct.c revision 9457a800f1fea4db4bb595c77de277609913b1b3
1// RUN: clang -analyze -checker-simple -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
2// RUN: clang -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=basic -verify %s &&
3// RUN: clang -analyze -checker-cfref -analyzer-store=basic -analyzer-constraints=range -verify %s &&
4// RUN: clang -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=basic -verify %s &&
5// RUN: clang -analyze -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s
6
7struct s {
8  int data;
9  int data_array[10];
10};
11
12typedef struct {
13  int data;
14} STYPE;
15
16void g1(struct s* p);
17
18// Array to pointer conversion. Array in the struct field.
19void 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;
28  q->data = 3;
29  d.data_array[9] = 17;
30}
31
32// StringLiteral in lvalue context and pointer to array type.
33// p: ElementRegion, q: StringRegion
34void f2() {
35  char *p = "/usr/local";
36  char (*q)[4];
37  q = &"abc";
38}
39
40// Typedef'ed struct definition.
41void f3() {
42  STYPE s;
43}
44
45// Initialize array with InitExprList.
46void f4() {
47  int a[] = { 1, 2, 3};
48  int b[3] = { 1, 2 };
49  struct s c[] = {{1,{1}}};
50}
51
52// Struct variable in lvalue context.
53// Assign UnknownVal to the whole struct.
54void f5() {
55  struct s data;
56  g1(&data);
57}
58
59// AllocaRegion test.
60void f6() {
61  char *p;
62  p = __builtin_alloca(10);
63  p[1] = 'a';
64}
65
66struct s2;
67
68void g2(struct s2 *p);
69
70// Incomplete struct pointer used as function argument.
71void f7() {
72  struct s2 *p = __builtin_alloca(10);
73  g2(p);
74}
75
76// sizeof() is unsigned while -1 is signed in array index.
77void f8() {
78  int a[10];
79  a[sizeof(a)/sizeof(int) - 1] = 1; // no-warning
80}
81
82// Initialization of struct array elements.
83void f9() {
84  struct s a[10];
85}
86
87// Initializing array with string literal.
88void f10() {
89  char a1[4] = "abc";
90  char a3[6] = "abc";
91}
92
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}
100