Debian Codegraph @

gcc/testsuite/gcc.dg/flex-array-counted-by-2.c

raw
1 /* Test the code generation for the new attribute counted_by.
2 And also the offsetof operator on such array. */
3 /* { dg-do run } */
4 /* { dg-options "-O2 -fdump-tree-original" } */
5
6 #include <stdlib.h>
7
8 typedef __UINTPTR_TYPE__ uintptr_t;
9
10 struct annotated {
11 int b;
12 char c[] __attribute__ ((counted_by (b)));
13 } *array_annotated;
14
15 static struct annotated static_annotated = { sizeof "hello", "hello" };
16 static char *y = static_annotated.c;
17
18 struct flex {
19 int b;
20 char c[];
21 };
22
23 struct nested_annotated {
24 struct {
25 union {
26 int b;
27 float f;
28 };
29 int n;
30 };
31 char c[] __attribute__ ((counted_by (b)));
32 } *array_nested_annotated;
33
34 static struct nested_annotated nested_static_annotated
35 = { sizeof "hello1", 0, "hello1" };
36 static char *nested_y = nested_static_annotated.c;
37
38 struct nested_flex {
39 struct {
40 union {
41 int b;
42 float f;
43 };
44 int n;
45 };
46 char c[];
47 };
48
49 void __attribute__((__noinline__)) setup (int normal_count, int attr_count)
50 {
51 array_annotated
52 = (struct annotated *)malloc (sizeof (struct annotated)
53 + attr_count * sizeof (char));
54 array_annotated->b = attr_count;
55
56 array_nested_annotated
57 = (struct nested_annotated *)malloc (sizeof (struct nested_annotated)
58 + attr_count * sizeof (char));
59 array_nested_annotated->b = attr_count;
60
61 return;
62 }
63
64 void __attribute__((__noinline__)) test (char a, char b)
65 {
66 if (__builtin_offsetof (struct annotated, c[0])
67 != __builtin_offsetof (struct flex, c[0]))
68 abort ();
69 if (__builtin_offsetof (struct annotated, c[1])
70 != __builtin_offsetof (struct flex, c[1]))
71 abort ();
72 if (__builtin_offsetof (struct nested_annotated, c[0])
73 != __builtin_offsetof (struct nested_flex, c[0]))
74 abort ();
75 if (__builtin_offsetof (struct nested_annotated, c[1])
76 != __builtin_offsetof (struct nested_flex, c[1]))
77 abort ();
78
79 if (__builtin_types_compatible_p (typeof (array_annotated->c),
80 typeof (&(array_annotated->c)[0])))
81 abort ();
82 if (__builtin_types_compatible_p (typeof (array_nested_annotated->c),
83 typeof (&(array_nested_annotated->c)[0])))
84 abort ();
85
86 if (__alignof (array_annotated->c) != __alignof (char))
87 abort ();
88 if (__alignof (array_nested_annotated->c) != __alignof (char))
89 abort ();
90
91 if ((uintptr_t) array_annotated->c != (uintptr_t) &array_annotated->c)
92 abort ();
93 if ((uintptr_t) array_nested_annotated->c
94 != (uintptr_t) &array_nested_annotated->c)
95 abort ();
96
97 array_annotated->c[2] = a;
98 array_nested_annotated->c[3] = b;
99
100 if (y[2] != 'l') abort ();
101 if (nested_y[4] !='o') abort ();
102
103 }
104
105 int main(int argc, char *argv[])
106 {
107 setup (10,10);
108 test ('A', 'B');
109 if (array_annotated->c[2] != 'A') abort ();
110 if (array_nested_annotated->c[3] != 'B') abort ();
111 return 0;
112 }
113
114 /* { dg-final { scan-tree-dump-times "ACCESS_WITH_SIZE" 8 "original" } } */