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