Debian Codegraph @

gcc/testsuite/gcc.dg/builtin-counted-by-ref-1.c

raw
1 /* Test the code generation for the new __builtin_counted_by_ref. */
2 /* { dg-do run } */
3 /* { dg-options "-O2" } */
4 #include <stdio.h>
5
6 struct annotated {
7 char b;
8 int c[] __attribute ((counted_by (b)));
9 } *array_annotated;
10
11 struct flex {
12 short b;
13 int c[];
14 } *array_flex;
15
16 struct nested_annotated {
17 struct {
18 union {
19 int b;
20 float f;
21 };
22 int n;
23 };
24 char c[] __attribute__ ((counted_by (b)));
25 } *array_nested_annotated;
26
27 struct nested_flex {
28 struct {
29 union {
30 unsigned int b;
31 float f;
32 };
33 int n;
34 };
35 char c[];
36 } *array_nested_flex;
37
38 #define MAX(A, B) (A > B) ? (A) : (B)
39 #define MY_ALLOC(P, FAM, COUNT) ({ \
40 __auto_type __p = &(P); \
41 __auto_type __c = (COUNT); \
42 size_t __size = MAX (sizeof (*(*__p)),\
43 __builtin_offsetof (__typeof(*(*__p)),FAM) \
44 + sizeof (*((*__p)->FAM)) * __c); \
45 if ((*__p = __builtin_malloc(__size))) { \
46 __builtin_memset(*__p, 0, __size); \
47 __auto_type ret = __builtin_counted_by_ref((*__p)->FAM); \
48 *_Generic(ret, void *: &(size_t){0}, default: ret) = __c; \
49 if (sizeof (__builtin_counted_by_ref ((*__p)->FAM)) != sizeof (char *)) \
50 __builtin_abort (); \
51 } \
52 })
53
54 int count;
55
56 int main(int argc, char *argv[])
57 {
58 MY_ALLOC(array_annotated, c, 10);
59 if (array_annotated->b != 10)
60 __builtin_abort ();
61 if (__alignof (*__builtin_counted_by_ref (array_annotated->c))
62 != __alignof (array_annotated->b))
63 __builtin_abort ();
64 if (!__builtin_types_compatible_p
65 (__typeof (*__builtin_counted_by_ref (array_annotated->c)),
66 __typeof (array_annotated->b)))
67 __builtin_abort ();
68 if (!__builtin_types_compatible_p
69 (__typeof (char[__builtin_counted_by_ref (array_annotated->c)
70 == &array_annotated->b ? 1 : 10]),
71 __typeof (char[1])))
72 __builtin_abort ();
73
74 MY_ALLOC(array_flex, c, 20);
75 if (array_flex->b == 20)
76 __builtin_abort ();
77 if (!__builtin_types_compatible_p
78 (__typeof (char[__builtin_counted_by_ref (array_flex->c)
79 == &array_flex->b ? 1 : 10]),
80 __typeof (char[10])))
81 __builtin_abort ();
82
83 MY_ALLOC(array_nested_annotated, c, 30);
84 if (array_nested_annotated->b != 30)
85 __builtin_abort ();
86 if (__alignof (*__builtin_counted_by_ref (array_nested_annotated->c))
87 != __alignof (array_nested_annotated->b))
88 __builtin_abort ();
89 if (!__builtin_types_compatible_p
90 (__typeof (*__builtin_counted_by_ref (array_nested_annotated->c)),
91 __typeof (array_nested_annotated->b)))
92 __builtin_abort ();
93
94 MY_ALLOC(array_nested_flex, c, 40);
95 if (array_nested_flex->b == 40)
96 __builtin_abort ();
97
98 count = array_annotated->b * 2 + array_nested_annotated->b * 3;
99 struct annotated * annotated_p;
100 struct flex * flex_p;
101 struct nested_annotated * nested_annotated_p;
102 struct nested_flex * nested_flex_p;
103
104 MY_ALLOC(annotated_p, c, count);
105 if (annotated_p->b != count)
106 __builtin_abort ();
107 if (__alignof (*__builtin_counted_by_ref (annotated_p->c))
108 != __alignof (annotated_p->b))
109 __builtin_abort ();
110 if (!__builtin_types_compatible_p
111 (__typeof (*__builtin_counted_by_ref (annotated_p->c)),
112 __typeof (annotated_p->b)))
113 __builtin_abort ();
114
115 MY_ALLOC(flex_p, c, count * 2);
116 if (flex_p->b == count * 2)
117 __builtin_abort ();
118
119 MY_ALLOC(nested_annotated_p, c, count * 3);
120 if (nested_annotated_p->b != count * 3)
121 __builtin_abort ();
122 if (__alignof (*__builtin_counted_by_ref (nested_annotated_p->c))
123 != __alignof (nested_annotated_p->b))
124 __builtin_abort ();
125 if (!__builtin_types_compatible_p
126 (__typeof (*__builtin_counted_by_ref (nested_annotated_p->c)),
127 __typeof (nested_annotated_p->b)))
128 __builtin_abort ();
129
130 MY_ALLOC(nested_flex_p, c, count * 4);
131 if (nested_flex_p->b == count * 4)
132 __builtin_abort ();
133
134 return 0;
135 }