Update concepts branch to revision 131834
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-2.c
blob4071c2516eec3fd3284b043d2e4edd32b7809289
1 /* { dg-do run } */
2 /* { dg-options "-O2" } */
4 typedef __SIZE_TYPE__ size_t;
5 extern void abort (void);
6 extern void exit (int);
7 extern void *malloc (size_t);
8 extern void *calloc (size_t, size_t);
9 extern void *alloca (size_t);
10 extern void *memcpy (void *, const void *, size_t);
11 extern void *memset (void *, int, size_t);
12 extern char *strcpy (char *, const char *);
14 struct A
16 char a[10];
17 int b;
18 char c[10];
19 } y, w[4];
21 extern char exta[];
22 extern char extb[30];
23 extern struct A extc[];
24 struct A zerol[0];
26 void
27 __attribute__ ((noinline))
28 test1 (void *q, int x)
30 struct A a;
31 void *p = &a.a[3], *r;
32 char var[x + 10];
33 struct A vara[x + 10];
34 if (x < 0)
35 r = &a.a[9];
36 else
37 r = &a.c[1];
38 if (__builtin_object_size (p, 1) != sizeof (a.a) - 3)
39 abort ();
40 if (__builtin_object_size (&a.c[9], 1)
41 != sizeof (a.c) - 9)
42 abort ();
43 if (__builtin_object_size (q, 1) != (size_t) -1)
44 abort ();
45 if (__builtin_object_size (r, 1) != sizeof (a.c) - 1)
46 abort ();
47 if (x < 6)
48 r = &w[2].a[1];
49 else
50 r = &a.a[6];
51 if (__builtin_object_size (&y, 1) != sizeof (y))
52 abort ();
53 if (__builtin_object_size (w, 1) != sizeof (w))
54 abort ();
55 if (__builtin_object_size (&y.b, 1) != sizeof (a.b))
56 abort ();
57 if (__builtin_object_size (r, 1) != sizeof (a.a) - 1)
58 abort ();
59 if (x < 20)
60 r = malloc (30);
61 else
62 r = calloc (2, 16);
63 if (__builtin_object_size (r, 1) != 2 * 16)
64 abort ();
65 if (x < 20)
66 r = malloc (30);
67 else
68 r = calloc (2, 14);
69 if (__builtin_object_size (r, 1) != 30)
70 abort ();
71 if (x < 30)
72 r = malloc (sizeof (a));
73 else
74 r = &a.a[3];
75 if (__builtin_object_size (r, 1) != sizeof (a))
76 abort ();
77 r = memcpy (r, "a", 2);
78 if (__builtin_object_size (r, 1) != sizeof (a))
79 abort ();
80 r = memcpy (r + 2, "b", 2) + 2;
81 if (__builtin_object_size (r, 1) != sizeof (a) - 4)
82 abort ();
83 r = &a.a[4];
84 r = memset (r, 'a', 2);
85 if (__builtin_object_size (r, 1) != sizeof (a.a) - 4)
86 abort ();
87 r = memset (r + 2, 'b', 2) + 2;
88 if (__builtin_object_size (r, 1) != sizeof (a.a) - 8)
89 abort ();
90 r = &a.a[1];
91 r = strcpy (r, "ab");
92 if (__builtin_object_size (r, 1) != sizeof (a.a) - 1)
93 abort ();
94 r = strcpy (r + 2, "cd") + 2;
95 if (__builtin_object_size (r, 1) != sizeof (a.a) - 5)
96 abort ();
97 if (__builtin_object_size (exta, 1) != (size_t) -1)
98 abort ();
99 if (__builtin_object_size (exta + 10, 1) != (size_t) -1)
100 abort ();
101 if (__builtin_object_size (&exta[5], 1) != (size_t) -1)
102 abort ();
103 if (__builtin_object_size (extb, 1) != sizeof (extb))
104 abort ();
105 if (__builtin_object_size (extb + 10, 1) != sizeof (extb) - 10)
106 abort ();
107 if (__builtin_object_size (&extb[5], 1) != sizeof (extb) - 5)
108 abort ();
109 if (__builtin_object_size (extc, 1) != (size_t) -1)
110 abort ();
111 if (__builtin_object_size (extc + 10, 1) != (size_t) -1)
112 abort ();
113 if (__builtin_object_size (&extc[5], 1) != (size_t) -1)
114 abort ();
115 if (__builtin_object_size (&extc->a, 1) != (size_t) -1)
116 abort ();
117 if (__builtin_object_size (&(extc + 10)->b, 1) != (size_t) -1)
118 abort ();
119 if (__builtin_object_size (&extc[5].c[3], 1) != (size_t) -1)
120 abort ();
121 if (__builtin_object_size (var, 1) != (size_t) -1)
122 abort ();
123 if (__builtin_object_size (var + 10, 1) != (size_t) -1)
124 abort ();
125 if (__builtin_object_size (&var[5], 1) != (size_t) -1)
126 abort ();
127 if (__builtin_object_size (vara, 1) != (size_t) -1)
128 abort ();
129 if (__builtin_object_size (vara + 10, 1) != (size_t) -1)
130 abort ();
131 if (__builtin_object_size (&vara[5], 1) != (size_t) -1)
132 abort ();
133 if (__builtin_object_size (&vara[0].a, 1) != (size_t) -1)
134 abort ();
135 if (__builtin_object_size (&vara[10].a[0], 1) != (size_t) -1)
136 abort ();
137 if (__builtin_object_size (&vara[5].a[4], 1) != (size_t) -1)
138 abort ();
139 if (__builtin_object_size (&vara[5].b, 1) != (size_t) -1)
140 abort ();
141 if (__builtin_object_size (&vara[7].c[7], 1) != (size_t) -1)
142 abort ();
143 if (__builtin_object_size (zerol, 1) != 0)
144 abort ();
145 if (__builtin_object_size (&zerol, 1) != 0)
146 abort ();
147 if (__builtin_object_size (&zerol[0], 1) != 0)
148 abort ();
149 if (__builtin_object_size (zerol[0].a, 1) != 0)
150 abort ();
151 if (__builtin_object_size (&zerol[0].a[0], 1) != 0)
152 abort ();
153 if (__builtin_object_size (&zerol[0].b, 1) != 0)
154 abort ();
155 if (__builtin_object_size ("abcdefg", 1) != sizeof ("abcdefg"))
156 abort ();
157 if (__builtin_object_size ("abcd\0efg", 1) != sizeof ("abcd\0efg"))
158 abort ();
159 if (__builtin_object_size (&"abcd\0efg", 1) != sizeof ("abcd\0efg"))
160 abort ();
161 if (__builtin_object_size (&"abcd\0efg"[0], 1) != sizeof ("abcd\0efg"))
162 abort ();
163 if (__builtin_object_size (&"abcd\0efg"[4], 1) != sizeof ("abcd\0efg") - 4)
164 abort ();
165 if (__builtin_object_size ("abcd\0efg" + 5, 1) != sizeof ("abcd\0efg") - 5)
166 abort ();
167 if (__builtin_object_size (L"abcdefg", 1) != sizeof (L"abcdefg"))
168 abort ();
169 r = (char *) L"abcd\0efg";
170 if (__builtin_object_size (r + 2, 1) != sizeof (L"abcd\0efg") - 2)
171 abort ();
174 size_t l1 = 1;
176 void
177 __attribute__ ((noinline))
178 test2 (void)
180 struct B { char buf1[10]; char buf2[10]; } a;
181 char *r, buf3[20];
182 int i;
184 if (sizeof (a) != 20)
185 return;
187 r = buf3;
188 for (i = 0; i < 4; ++i)
190 if (i == l1 - 1)
191 r = &a.buf1[1];
192 else if (i == l1)
193 r = &a.buf2[7];
194 else if (i == l1 + 1)
195 r = &buf3[5];
196 else if (i == l1 + 2)
197 r = &a.buf1[9];
199 if (__builtin_object_size (r, 1) != sizeof (buf3))
200 abort ();
201 r = &buf3[20];
202 for (i = 0; i < 4; ++i)
204 if (i == l1 - 1)
205 r = &a.buf1[7];
206 else if (i == l1)
207 r = &a.buf2[7];
208 else if (i == l1 + 1)
209 r = &buf3[5];
210 else if (i == l1 + 2)
211 r = &a.buf1[9];
213 if (__builtin_object_size (r, 1) != sizeof (buf3) - 5)
214 abort ();
215 r += 8;
216 if (__builtin_object_size (r, 1) != sizeof (buf3) - 13)
217 abort ();
218 if (__builtin_object_size (r + 6, 1) != sizeof (buf3) - 19)
219 abort ();
222 void
223 __attribute__ ((noinline))
224 test3 (void)
226 char buf4[10];
227 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
228 _Complex double f; } x;
229 double y;
230 _Complex double z;
231 double *dp;
233 if (__builtin_object_size (buf4, 1) != sizeof (buf4))
234 abort ();
235 if (__builtin_object_size (&buf4, 1) != sizeof (buf4))
236 abort ();
237 if (__builtin_object_size (&buf4[0], 1) != sizeof (buf4))
238 abort ();
239 if (__builtin_object_size (&buf4[1], 1) != sizeof (buf4) - 1)
240 abort ();
241 if (__builtin_object_size (&x, 1) != sizeof (x))
242 abort ();
243 if (__builtin_object_size (&x.a, 1) != sizeof (x.a))
244 abort ();
245 if (__builtin_object_size (&x.a[0], 1) != sizeof (x.a))
246 abort ();
247 if (__builtin_object_size (&x.a[0].a, 1) != sizeof (x.a[0].a))
248 abort ();
249 if (__builtin_object_size (&x.a[0].a[0], 1) != sizeof (x.a[0].a))
250 abort ();
251 if (__builtin_object_size (&x.a[0].a[3], 1) != sizeof (x.a[0].a) - 3)
252 abort ();
253 if (__builtin_object_size (&x.a[0].b, 1) != sizeof (x.a[0].b))
254 abort ();
255 if (__builtin_object_size (&x.a[1].c, 1) != sizeof (x.a[1].c))
256 abort ();
257 if (__builtin_object_size (&x.a[1].c[0], 1) != sizeof (x.a[1].c))
258 abort ();
259 if (__builtin_object_size (&x.a[1].c[3], 1) != sizeof (x.a[1].c) - 3)
260 abort ();
261 if (__builtin_object_size (&x.b, 1) != sizeof (x.b))
262 abort ();
263 if (__builtin_object_size (&x.b.a, 1) != sizeof (x.b.a))
264 abort ();
265 if (__builtin_object_size (&x.b.a[0], 1) != sizeof (x.b.a))
266 abort ();
267 if (__builtin_object_size (&x.b.a[3], 1) != sizeof (x.b.a) - 3)
268 abort ();
269 if (__builtin_object_size (&x.b.b, 1) != sizeof (x.b.b))
270 abort ();
271 if (__builtin_object_size (&x.b.c, 1) != sizeof (x.b.c))
272 abort ();
273 if (__builtin_object_size (&x.b.c[0], 1) != sizeof (x.b.c))
274 abort ();
275 if (__builtin_object_size (&x.b.c[3], 1) != sizeof (x.b.c) - 3)
276 abort ();
277 if (__builtin_object_size (&x.c, 1) != sizeof (x.c))
278 abort ();
279 if (__builtin_object_size (&x.c[0], 1) != sizeof (x.c))
280 abort ();
281 if (__builtin_object_size (&x.c[1], 1) != sizeof (x.c) - 1)
282 abort ();
283 if (__builtin_object_size (&x.d, 1) != sizeof (x.d))
284 abort ();
285 if (__builtin_object_size (&x.e, 1) != sizeof (x.e))
286 abort ();
287 if (__builtin_object_size (&x.f, 1) != sizeof (x.f))
288 abort ();
289 dp = &__real__ x.f;
290 if (__builtin_object_size (dp, 1) != sizeof (x.f) / 2)
291 abort ();
292 dp = &__imag__ x.f;
293 if (__builtin_object_size (dp, 1) != sizeof (x.f) / 2)
294 abort ();
295 dp = &y;
296 if (__builtin_object_size (dp, 1) != sizeof (y))
297 abort ();
298 if (__builtin_object_size (&z, 1) != sizeof (z))
299 abort ();
300 dp = &__real__ z;
301 if (__builtin_object_size (dp, 1) != sizeof (z) / 2)
302 abort ();
303 dp = &__imag__ z;
304 if (__builtin_object_size (dp, 1) != sizeof (z) / 2)
305 abort ();
308 struct S { unsigned int a; };
310 char *
311 __attribute__ ((noinline))
312 test4 (char *x, int y)
314 register int i;
315 struct A *p;
317 for (i = 0; i < y; i++)
319 p = (struct A *) x;
320 x = (char *) &p[1];
321 if (__builtin_object_size (p, 1) != (size_t) -1)
322 abort ();
324 return x;
327 void
328 __attribute__ ((noinline))
329 test5 (size_t x)
331 struct T { char buf[64]; char buf2[64]; } t;
332 char *p = &t.buf[8];
333 size_t i;
335 for (i = 0; i < x; ++i)
336 p = p + 4;
337 if (__builtin_object_size (p, 1) != sizeof (t.buf) - 8)
338 abort ();
339 memset (p, ' ', sizeof (t.buf) - 8 - 4 * 4);
342 void
343 __attribute__ ((noinline))
344 test6 (void)
346 char buf[64];
347 struct T { char buf[64]; char buf2[64]; } t;
348 char *p = &buf[64], *q = &t.buf[64];
350 if (__builtin_object_size (p + 64, 1) != 0)
351 abort ();
352 if (__builtin_object_size (q + 0, 1) != 0)
353 abort ();
354 if (__builtin_object_size (q + 64, 1) != 0)
355 abort ();
358 void
359 __attribute__ ((noinline))
360 test7 (void)
362 struct T { char buf[10]; char buf2[10]; } t;
363 char *p = &t.buf2[-4];
364 char *q = &t.buf2[0];
365 if (__builtin_object_size (p, 1) != 0)
366 abort ();
367 if (__builtin_object_size (q, 1) != sizeof (t.buf2))
368 abort ();
369 q = &t.buf[10];
370 if (__builtin_object_size (q, 1) != 0)
371 abort ();
372 q = &t.buf[11];
373 if (__builtin_object_size (q, 1) != 0)
374 abort ();
375 p = &t.buf[-4];
376 if (__builtin_object_size (p, 1) != 0)
377 abort ();
381 main (void)
383 struct S s[10];
384 __asm ("" : "=r" (l1) : "0" (l1));
385 test1 (main, 6);
386 test2 ();
387 test3 ();
388 test4 ((char *) s, 10);
389 test5 (4);
390 test6 ();
391 test7 ();
392 exit (0);