Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-8.c
blobf2d88f9d591eb08991687ebf2b5935f0643472c7
1 /* { dg-do run } */
2 /* { dg-options "-O2" } */
4 typedef __SIZE_TYPE__ size_t;
5 extern void *malloc (size_t);
6 extern void free (void *);
7 extern void abort (void);
9 union A
11 int a1;
12 char a2[3];
15 union B
17 long long b1;
18 union A b2;
21 struct C
23 int c1;
24 union A c2;
27 struct D
29 int d1;
30 union B d2;
33 union E
35 struct C e1;
36 char e2[3];
39 union F
41 int f1;
42 struct D f2;
45 struct G
47 union A g1;
48 char g2;
51 struct H
53 int h1;
54 union E h2;
57 #define T(X, S0, S1) \
58 if (__builtin_object_size (X, 0) != (S0)) \
59 abort (); \
60 if (__builtin_object_size (X, 1) != (S1)) \
61 abort (); \
62 if (__builtin_object_size (X, 2) != (S0)) \
63 abort (); \
64 if (__builtin_object_size (X, 3) != (S1)) \
65 abort ()
66 #define TS(X, S0) T(&X, S0, sizeof (X))
67 #define TA(X, S0, S1) \
68 T(X, S0, S1); T(&X[0], S0, S1); T(&X[1], (S0) - 1, (S1) - 1)
69 #define TF(X, S0) TA(X, S0, S0)
71 int
72 main (void)
74 size_t s, o, o2;
76 s = sizeof (union A);
77 o = 0;
78 union A *a1 = malloc (s);
79 union A *a2 = malloc (o + 212);
80 TS (a1->a1, s);
81 TF (a1->a2, s);
82 s = o + 212;
83 TS (a2->a1, s);
84 TF (a2->a2, s);
85 free (a2);
86 free (a1);
88 s = sizeof (union B);
89 o = 0;
90 union B *b1 = malloc (s);
91 union B *b2 = malloc (o + 212);
92 TS (b1->b1, s);
93 TS (b1->b2.a1, s);
94 TF (b1->b2.a2, s);
95 s = o + 212;
96 TS (b2->b1, s);
97 TS (b2->b2.a1, s);
98 TF (b2->b2.a2, s);
99 free (b2);
100 free (b1);
102 s = sizeof (struct C);
103 o = __builtin_offsetof (struct C, c2);
104 struct C *c1 = malloc (s);
105 struct C *c2 = malloc (o + 212);
106 TS (c1->c1, s);
107 TS (c1->c2.a1, s - o);
108 TF (c1->c2.a2, s - o);
109 s = o + 212;
110 TS (c2->c1, s);
111 TS (c2->c2.a1, s - o);
112 TF (c2->c2.a2, s - o);
113 free (c2);
114 free (c1);
116 s = sizeof (struct D);
117 o = __builtin_offsetof (struct D, d2);
118 struct D *d1 = malloc (s);
119 struct D *d2 = malloc (o + 212);
120 TS (d1->d1, s);
121 TS (d1->d2.b1, s - o);
122 TS (d1->d2.b2.a1, s - o);
123 TF (d1->d2.b2.a2, s - o);
124 s = o + 212;
125 TS (d2->d1, s);
126 TS (d2->d2.b1, s - o);
127 TS (d2->d2.b2.a1, s - o);
128 TF (d2->d2.b2.a2, s - o);
129 free (d2);
130 free (d1);
132 s = sizeof (union E);
133 o = __builtin_offsetof (union E, e1.c2);
134 union E *e1 = malloc (s);
135 union E *e2 = malloc (o + 212);
136 TS (e1->e1.c1, s);
137 TS (e1->e1.c2.a1, s - o);
138 TF (e1->e1.c2.a2, s - o);
139 TF (e1->e2, s);
140 s = o + 212;
141 TS (e2->e1.c1, s);
142 TS (e2->e1.c2.a1, s - o);
143 TF (e2->e1.c2.a2, s - o);
144 TF (e2->e2, s);
145 free (e2);
146 free (e1);
148 s = sizeof (union F);
149 o = __builtin_offsetof (union F, f2.d2);
150 union F *f1 = malloc (s);
151 union F *f2 = malloc (o + 212);
152 TS (f1->f1, s);
153 TS (f1->f2.d1, s);
154 TS (f1->f2.d2.b1, s - o);
155 TS (f1->f2.d2.b2.a1, s - o);
156 TF (f1->f2.d2.b2.a2, s - o);
157 s = o + 212;
158 TS (f2->f1, s);
159 TS (f2->f2.d1, s);
160 TS (f2->f2.d2.b1, s - o);
161 TS (f2->f2.d2.b2.a1, s - o);
162 TF (f2->f2.d2.b2.a2, s - o);
163 free (f2);
164 free (f1);
166 s = sizeof (struct G);
167 o = __builtin_offsetof (struct G, g2);
168 struct G *g1 = malloc (s);
169 struct G *g2 = malloc (o + 212);
170 TS (g1->g1.a1, s);
171 TA (g1->g1.a2, s, sizeof (g1->g1.a2));
172 TS (g1->g2, s - o);
173 s = o + 212;
174 TS (g2->g1.a1, s);
175 TA (g2->g1.a2, s, sizeof (g1->g1.a2));
176 TS (g2->g2, s - o);
177 free (g2);
178 free (g1);
180 s = sizeof (struct H);
181 o = __builtin_offsetof (struct H, h2);
182 o2 = __builtin_offsetof (struct H, h2.e1.c2);
183 struct H *h1 = malloc (s);
184 struct H *h2 = malloc (o2 + 212);
185 TS (h1->h1, s);
186 TS (h1->h2.e1.c1, s - o);
187 TS (h1->h2.e1.c2.a1, s - o2);
188 TA (h1->h2.e1.c2.a2, s - o2, sizeof (h1->h2.e1.c2.a2));
189 TF (h1->h2.e2, s - o);
190 s = o2 + 212;
191 TS (h2->h1, s);
192 TS (h2->h2.e1.c1, s - o);
193 TS (h2->h2.e1.c2.a1, s - o2);
194 TA (h2->h2.e1.c2.a2, s - o2, sizeof (h2->h2.e1.c2.a2));
195 TF (h2->h2.e2, s - o);
196 free (h2);
197 free (h1);
199 return 0;