Add autoconf check for clock_gettime
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-2.c
blobc28d72eee9bfedce12b5de5940971223fd054aa6
1 /* { dg-do run } */
2 /* { dg-options "-O2 -Wno-stringop-overread" } */
3 /* { dg-require-effective-target alloca } */
4 /* { dg-additional-options "-DSKIP_STRNDUP" { target { ! strndup } } } */
6 #include "builtin-object-size-common.h"
8 struct A
10 char a[10];
11 int b;
12 char c[10];
13 } y, w[4];
15 extern char exta[];
16 extern char extb[30];
17 extern struct A extc[];
18 struct A zerol[0];
20 void
21 __attribute__ ((noinline))
22 test1 (void *q, int x)
24 struct A a;
25 void *p = &a.a[3], *r;
26 char var[x + 10];
27 struct A vara[x + 10];
28 if (x < 0)
29 r = &a.a[9];
30 else
31 r = &a.c[1];
32 if (__builtin_object_size (p, 1) != sizeof (a.a) - 3)
33 FAIL ();
34 if (__builtin_object_size (&a.c[9], 1)
35 != sizeof (a.c) - 9)
36 FAIL ();
37 if (__builtin_object_size (q, 1) != (size_t) -1)
38 FAIL ();
39 #ifdef __builtin_object_size
40 if (x < 0
41 ? __builtin_object_size (r, 1) != sizeof (a.a) - 9
42 : __builtin_object_size (r, 1) != sizeof (a.c) - 1)
43 FAIL ();
44 #else
45 if (__builtin_object_size (r, 1) != sizeof (a.c) - 1)
46 FAIL ();
47 #endif
48 if (x < 6)
49 r = &w[2].a[1];
50 else
51 r = &a.a[6];
52 if (__builtin_object_size (&y, 1) != sizeof (y))
53 FAIL ();
54 if (__builtin_object_size (w, 1) != sizeof (w))
55 FAIL ();
56 if (__builtin_object_size (&y.b, 1) != sizeof (a.b))
57 FAIL ();
58 #ifdef __builtin_object_size
59 if (x < 6
60 ? __builtin_object_size (r, 1) != sizeof (a.a) - 1
61 : __builtin_object_size (r, 1) != sizeof (a.a) - 6)
62 FAIL ();
63 #else
64 if (__builtin_object_size (r, 1) != sizeof (a.a) - 1)
65 FAIL ();
66 #endif
67 if (x < 20)
68 r = malloc (30);
69 else
70 r = calloc (2, 16);
71 #ifdef __builtin_object_size
72 if (__builtin_object_size (r, 1) != (x < 20 ? 30 : 2 * 16))
73 FAIL ();
74 #else
75 /* We may duplicate this test onto the two exit paths. On one path
76 the size will be 32, the other it will be 30. If we don't duplicate
77 this test, then the size will be 32. */
78 if (__builtin_object_size (r, 1) != 2 * 16
79 && __builtin_object_size (r, 1) != 30)
80 FAIL ();
81 #endif
82 if (x < 20)
83 r = malloc (30);
84 else
85 r = calloc (2, 14);
86 #ifdef __builtin_object_size
87 if (__builtin_object_size (r, 1) != (x < 20 ? 30 : 2 * 14))
88 FAIL ();
89 #else
90 if (__builtin_object_size (r, 1) != 30)
91 FAIL ();
92 #endif
93 if (x < 30)
94 r = malloc (sizeof (a));
95 else
96 r = &a.a[3];
97 #ifdef __builtin_object_size
98 if (__builtin_object_size (r, 1) != (x < 30 ? sizeof (a) : sizeof (a) - 3))
99 FAIL ();
100 #else
101 if (__builtin_object_size (r, 1) != sizeof (a))
102 FAIL ();
103 #endif
104 r = memcpy (r, "a", 2);
105 #ifdef __builtin_object_size
106 if (__builtin_object_size (r, 1) != (x < 30 ? sizeof (a) : sizeof (a) - 3))
107 FAIL ();
108 #else
109 if (__builtin_object_size (r, 1) != sizeof (a))
110 FAIL ();
111 #endif
112 r = memcpy (r + 2, "b", 2) + 2;
113 #ifdef __builtin_object_size
114 if (__builtin_object_size (r, 0)
115 != (x < 30 ? sizeof (a) - 4 : sizeof (a) - 7))
116 FAIL ();
117 #else
118 if (__builtin_object_size (r, 1) != sizeof (a) - 4)
119 FAIL ();
120 #endif
121 r = &a.a[4];
122 r = memset (r, 'a', 2);
123 if (__builtin_object_size (r, 1) != sizeof (a.a) - 4)
124 FAIL ();
125 r = memset (r + 2, 'b', 2) + 2;
126 if (__builtin_object_size (r, 1) != sizeof (a.a) - 8)
127 FAIL ();
128 r = &a.a[1];
129 r = strcpy (r, "ab");
130 if (__builtin_object_size (r, 1) != sizeof (a.a) - 1)
131 FAIL ();
132 r = strcpy (r + 2, "cd") + 2;
133 if (__builtin_object_size (r, 1) != sizeof (a.a) - 5)
134 FAIL ();
135 if (__builtin_object_size (exta, 1) != (size_t) -1)
136 FAIL ();
137 if (__builtin_object_size (exta + 10, 1) != (size_t) -1)
138 FAIL ();
139 if (__builtin_object_size (&exta[5], 1) != (size_t) -1)
140 FAIL ();
141 if (__builtin_object_size (extb, 1) != sizeof (extb))
142 FAIL ();
143 if (__builtin_object_size (extb + 10, 1) != sizeof (extb) - 10)
144 FAIL ();
145 if (__builtin_object_size (&extb[5], 1) != sizeof (extb) - 5)
146 FAIL ();
147 if (__builtin_object_size (extc, 1) != (size_t) -1)
148 FAIL ();
149 if (__builtin_object_size (extc + 10, 1) != (size_t) -1)
150 FAIL ();
151 if (__builtin_object_size (&extc[5], 1) != (size_t) -1)
152 FAIL ();
153 if (__builtin_object_size (&extc->a, 1) != (size_t) -1)
154 FAIL ();
155 if (__builtin_object_size (&(extc + 10)->b, 1) != (size_t) -1)
156 FAIL ();
157 if (__builtin_object_size (&extc[5].c[3], 1) != (size_t) -1)
158 FAIL ();
159 #ifdef __builtin_object_size
160 if (__builtin_object_size (var, 1) != x + 10)
161 FAIL ();
162 if (__builtin_object_size (var + 10, 1) != x)
163 FAIL ();
164 if (__builtin_object_size (&var[5], 1) != x + 5)
165 FAIL ();
166 if (__builtin_object_size (vara, 1) != (x + 10) * sizeof (struct A))
167 FAIL ();
168 if (__builtin_object_size (vara + 10, 1) != x * sizeof (struct A))
169 FAIL ();
170 if (__builtin_object_size (&vara[5], 1) != (x + 5) * sizeof (struct A))
171 FAIL ();
172 #else
173 if (__builtin_object_size (var, 1) != (size_t) -1)
174 FAIL ();
175 if (__builtin_object_size (var + 10, 1) != (size_t) -1)
176 FAIL ();
177 if (__builtin_object_size (&var[5], 1) != (size_t) -1)
178 FAIL ();
179 if (__builtin_object_size (vara, 1) != (size_t) -1)
180 FAIL ();
181 if (__builtin_object_size (vara + 10, 1) != (size_t) -1)
182 FAIL ();
183 if (__builtin_object_size (&vara[5], 1) != (size_t) -1)
184 FAIL ();
185 #endif
186 if (__builtin_object_size (&vara[0].a, 1) != sizeof (vara[0].a))
187 FAIL ();
188 if (__builtin_object_size (&vara[10].a[0], 1) != sizeof (vara[0].a))
189 FAIL ();
190 if (__builtin_object_size (&vara[5].a[4], 1) != sizeof (vara[0].a) - 4)
191 FAIL ();
192 if (__builtin_object_size (&vara[5].b, 1) != sizeof (vara[0].b))
193 FAIL ();
194 if (__builtin_object_size (&vara[7].c[7], 1) != sizeof (vara[0].c) - 7)
195 FAIL ();
196 if (__builtin_object_size (zerol, 1) != 0)
197 FAIL ();
198 if (__builtin_object_size (&zerol, 1) != 0)
199 FAIL ();
200 if (__builtin_object_size (&zerol[0], 1) != 0)
201 FAIL ();
202 if (__builtin_object_size (zerol[0].a, 1) != 0)
203 FAIL ();
204 if (__builtin_object_size (&zerol[0].a[0], 1) != 0)
205 FAIL ();
206 if (__builtin_object_size (&zerol[0].b, 1) != 0)
207 FAIL ();
208 if (__builtin_object_size ("abcdefg", 1) != sizeof ("abcdefg"))
209 FAIL ();
210 if (__builtin_object_size ("abcd\0efg", 1) != sizeof ("abcd\0efg"))
211 FAIL ();
212 if (__builtin_object_size (&"abcd\0efg", 1) != sizeof ("abcd\0efg"))
213 FAIL ();
214 if (__builtin_object_size (&"abcd\0efg"[0], 1) != sizeof ("abcd\0efg"))
215 FAIL ();
216 if (__builtin_object_size (&"abcd\0efg"[4], 1) != sizeof ("abcd\0efg") - 4)
217 FAIL ();
218 if (__builtin_object_size ("abcd\0efg" + 5, 1) != sizeof ("abcd\0efg") - 5)
219 FAIL ();
220 if (__builtin_object_size (L"abcdefg", 1) != sizeof (L"abcdefg"))
221 FAIL ();
222 r = (char *) L"abcd\0efg";
223 if (__builtin_object_size (r + 2, 1) != sizeof (L"abcd\0efg") - 2)
224 FAIL ();
227 size_t l1 = 1;
229 void
230 __attribute__ ((noinline))
231 test2 (void)
233 struct B { char buf1[10]; char buf2[10]; } a;
234 char *r, buf3[20];
235 int i;
236 #ifdef __builtin_object_size
237 size_t dyn_res;
238 #endif
240 if (sizeof (a) != 20)
241 return;
243 r = buf3;
244 for (i = 0; i < 4; ++i)
246 if (i == l1 - 1)
247 r = &a.buf1[1];
248 else if (i == l1)
249 r = &a.buf2[7];
250 else if (i == l1 + 1)
251 r = &buf3[5];
252 else if (i == l1 + 2)
253 r = &a.buf1[9];
255 #ifdef __builtin_object_size
256 dyn_res = sizeof (buf3);
258 for (i = 0; i < 4; ++i)
260 if (i == l1 - 1)
261 dyn_res = sizeof (a.buf1) - 1;
262 else if (i == l1)
263 dyn_res = sizeof (a.buf2) - 7;
264 else if (i == l1 + 1)
265 dyn_res = sizeof (buf3) - 5;
266 else if (i == l1 + 2)
267 dyn_res = sizeof (a.buf1) - 9;
269 if (__builtin_object_size (r, 1) != dyn_res)
270 FAIL ();
271 #else
272 if (__builtin_object_size (r, 1) != sizeof (buf3))
273 FAIL ();
274 #endif
275 r = &buf3[20];
276 for (i = 0; i < 4; ++i)
278 if (i == l1 - 1)
279 r = &a.buf1[7];
280 else if (i == l1)
281 r = &a.buf2[7];
282 else if (i == l1 + 1)
283 r = &buf3[5];
284 else if (i == l1 + 2)
285 r = &a.buf1[9];
287 #ifdef __builtin_object_size
288 dyn_res = sizeof (buf3) - 20;
290 for (i = 0; i < 4; ++i)
292 if (i == l1 - 1)
293 dyn_res = sizeof (a.buf1) - 7;
294 else if (i == l1)
295 dyn_res = sizeof (a.buf2) - 7;
296 else if (i == l1 + 1)
297 dyn_res = sizeof (buf3) - 5;
298 else if (i == l1 + 2)
299 dyn_res = sizeof (a.buf1) - 9;
301 if (__builtin_object_size (r, 1) != dyn_res)
302 FAIL ();
303 #else
304 if (__builtin_object_size (r, 1) != sizeof (buf3) - 5)
305 FAIL ();
306 #endif
307 r += 8;
308 #ifdef __builtin_object_size
309 if (dyn_res >= 8)
311 dyn_res -= 8;
312 if (__builtin_object_size (r, 1) != dyn_res)
313 FAIL ();
315 if (dyn_res >= 6)
317 if (__builtin_object_size (r + 6, 1) != dyn_res - 6)
318 FAIL ();
320 else if (__builtin_object_size (r + 6, 1) != 0)
321 FAIL ();
323 else if (__builtin_object_size (r, 1) != 0)
324 FAIL ();
325 #else
326 if (__builtin_object_size (r, 1) != sizeof (buf3) - 13)
327 FAIL ();
328 if (__builtin_object_size (r + 6, 1) != sizeof (buf3) - 19)
329 FAIL ();
330 #endif
333 void
334 __attribute__ ((noinline))
335 test3 (void)
337 char buf4[10];
338 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
339 _Complex double f; } x;
340 double y;
341 _Complex double z;
342 double *dp;
344 if (__builtin_object_size (buf4, 1) != sizeof (buf4))
345 FAIL ();
346 if (__builtin_object_size (&buf4, 1) != sizeof (buf4))
347 FAIL ();
348 if (__builtin_object_size (&buf4[0], 1) != sizeof (buf4))
349 FAIL ();
350 if (__builtin_object_size (&buf4[1], 1) != sizeof (buf4) - 1)
351 FAIL ();
352 if (__builtin_object_size (&x, 1) != sizeof (x))
353 FAIL ();
354 if (__builtin_object_size (&x.a, 1) != sizeof (x.a))
355 FAIL ();
356 if (__builtin_object_size (&x.a[0], 1) != sizeof (x.a))
357 FAIL ();
358 if (__builtin_object_size (&x.a[0].a, 1) != sizeof (x.a[0].a))
359 FAIL ();
360 if (__builtin_object_size (&x.a[0].a[0], 1) != sizeof (x.a[0].a))
361 FAIL ();
362 if (__builtin_object_size (&x.a[0].a[3], 1) != sizeof (x.a[0].a) - 3)
363 FAIL ();
364 if (__builtin_object_size (&x.a[0].b, 1) != sizeof (x.a[0].b))
365 FAIL ();
366 if (__builtin_object_size (&x.a[1].c, 1) != sizeof (x.a[1].c))
367 FAIL ();
368 if (__builtin_object_size (&x.a[1].c[0], 1) != sizeof (x.a[1].c))
369 FAIL ();
370 if (__builtin_object_size (&x.a[1].c[3], 1) != sizeof (x.a[1].c) - 3)
371 FAIL ();
372 if (__builtin_object_size (&x.b, 1) != sizeof (x.b))
373 FAIL ();
374 if (__builtin_object_size (&x.b.a, 1) != sizeof (x.b.a))
375 FAIL ();
376 if (__builtin_object_size (&x.b.a[0], 1) != sizeof (x.b.a))
377 FAIL ();
378 if (__builtin_object_size (&x.b.a[3], 1) != sizeof (x.b.a) - 3)
379 FAIL ();
380 if (__builtin_object_size (&x.b.b, 1) != sizeof (x.b.b))
381 FAIL ();
382 if (__builtin_object_size (&x.b.c, 1) != sizeof (x.b.c))
383 FAIL ();
384 if (__builtin_object_size (&x.b.c[0], 1) != sizeof (x.b.c))
385 FAIL ();
386 if (__builtin_object_size (&x.b.c[3], 1) != sizeof (x.b.c) - 3)
387 FAIL ();
388 if (__builtin_object_size (&x.c, 1) != sizeof (x.c))
389 FAIL ();
390 if (__builtin_object_size (&x.c[0], 1) != sizeof (x.c))
391 FAIL ();
392 if (__builtin_object_size (&x.c[1], 1) != sizeof (x.c) - 1)
393 FAIL ();
394 if (__builtin_object_size (&x.d, 1) != sizeof (x.d))
395 FAIL ();
396 if (__builtin_object_size (&x.e, 1) != sizeof (x.e))
397 FAIL ();
398 if (__builtin_object_size (&x.f, 1) != sizeof (x.f))
399 FAIL ();
400 dp = &__real__ x.f;
401 if (__builtin_object_size (dp, 1) != sizeof (x.f) / 2)
402 FAIL ();
403 dp = &__imag__ x.f;
404 if (__builtin_object_size (dp, 1) != sizeof (x.f) / 2)
405 FAIL ();
406 dp = &y;
407 if (__builtin_object_size (dp, 1) != sizeof (y))
408 FAIL ();
409 if (__builtin_object_size (&z, 1) != sizeof (z))
410 FAIL ();
411 dp = &__real__ z;
412 if (__builtin_object_size (dp, 1) != sizeof (z) / 2)
413 FAIL ();
414 dp = &__imag__ z;
415 if (__builtin_object_size (dp, 1) != sizeof (z) / 2)
416 FAIL ();
419 struct S { unsigned int a; };
421 char *
422 __attribute__ ((noinline))
423 test4 (char *x, int y)
425 register int i;
426 struct A *p;
428 for (i = 0; i < y; i++)
430 p = (struct A *) x;
431 x = (char *) &p[1];
432 if (__builtin_object_size (p, 1) != (size_t) -1)
433 FAIL ();
435 return x;
438 void
439 __attribute__ ((noinline))
440 test5 (size_t x)
442 struct T { char buf[64]; char buf2[64]; } t;
443 char *p = &t.buf[8];
444 size_t i;
446 for (i = 0; i < x; ++i)
447 p = p + 4;
448 #ifdef __builtin_object_size
449 if (__builtin_object_size (p, 1) != sizeof (t.buf) - 8 - 4 * x)
450 FAIL ();
451 #else
452 if (__builtin_object_size (p, 1) != sizeof (t.buf) - 8)
453 FAIL ();
454 #endif
455 memset (p, ' ', sizeof (t.buf) - 8 - 4 * 4);
458 void
459 __attribute__ ((noinline))
460 test6 (void)
462 char buf[64];
463 struct T { char buf[64]; char buf2[64]; } t;
464 char *p = &buf[64], *q = &t.buf[64];
466 if (__builtin_object_size (p + 64, 1) != 0)
467 FAIL ();
468 if (__builtin_object_size (q + 0, 1) != 0)
469 FAIL ();
470 if (__builtin_object_size (q + 64, 1) != 0)
471 FAIL ();
474 void
475 __attribute__ ((noinline))
476 test7 (void)
478 struct T { char buf[10]; char buf2[10]; } t;
479 char *p = &t.buf2[-4];
480 char *q = &t.buf2[0];
481 if (__builtin_object_size (p, 1) != 0)
482 FAIL ();
483 if (__builtin_object_size (q, 1) != sizeof (t.buf2))
484 FAIL ();
485 q = &t.buf[10];
486 if (__builtin_object_size (q, 1) != 0)
487 FAIL ();
488 q = &t.buf[11];
489 if (__builtin_object_size (q, 1) != 0)
490 FAIL ();
491 p = &t.buf[-4];
492 if (__builtin_object_size (p, 1) != 0)
493 FAIL ();
496 void
497 __attribute__ ((noinline))
498 test8 (unsigned cond)
500 char *buf2 = malloc (10);
501 char *p;
503 if (cond)
504 p = &buf2[8];
505 else
506 p = &buf2[4];
508 #ifdef __builtin_object_size
509 if (__builtin_object_size (&p[-4], 1) != (cond ? 6 : 10))
510 FAIL ();
511 #else
512 if (__builtin_object_size (&p[-4], 1) != 10)
513 FAIL ();
514 #endif
516 for (unsigned i = cond; i > 0; i--)
517 p--;
519 #ifdef __builtin_object_size
520 if (__builtin_object_size (p, 1) != ((cond ? 2 : 6) + cond))
521 FAIL ();
522 #else
523 if (__builtin_object_size (p, 1) != 10)
524 FAIL ();
525 #endif
527 p = &y.c[8];
528 for (unsigned i = cond; i > 0; i--)
529 p--;
531 #ifdef __builtin_object_size
532 if (__builtin_object_size (p, 1) != sizeof (y.c) - 8 + cond)
533 FAIL ();
534 #else
535 if (__builtin_object_size (p, 1) != sizeof (y.c))
536 FAIL ();
537 #endif
540 #ifndef SKIP_STRNDUP
541 /* Tests for strdup/strndup. */
542 size_t
543 __attribute__ ((noinline))
544 test9 (void)
546 const char *ptr = "abcdefghijklmnopqrstuvwxyz";
547 char *res = strndup (ptr, 21);
548 if (__builtin_object_size (res, 1) != 22)
549 FAIL ();
551 free (res);
553 res = strndup (ptr, 32);
554 if (__builtin_object_size (res, 1) != 27)
555 FAIL ();
557 free (res);
559 res = strdup (ptr);
560 if (__builtin_object_size (res, 1) != 27)
561 FAIL ();
563 free (res);
565 char *ptr2 = malloc (64);
566 strcpy (ptr2, ptr);
568 res = strndup (ptr2, 21);
569 if (__builtin_object_size (res, 1) != 22)
570 FAIL ();
572 free (res);
574 res = strndup (ptr2, 32);
575 if (__builtin_object_size (res, 1) != 33)
576 FAIL ();
578 free (res);
580 res = strndup (ptr2, 128);
581 if (__builtin_object_size (res, 1) != 64)
582 FAIL ();
584 free (res);
586 res = strdup (ptr2);
587 #ifdef __builtin_object_size
588 if (__builtin_object_size (res, 1) != 27)
589 #else
590 if (__builtin_object_size (res, 1) != (size_t) -1)
591 #endif
592 FAIL ();
594 free (res);
595 free (ptr2);
597 ptr = "abcd\0efghijklmnopqrstuvwxyz";
598 res = strdup (ptr);
599 if (__builtin_object_size (res, 1) != 5)
600 FAIL ();
601 free (res);
603 res = strndup (ptr, 24);
604 if (__builtin_object_size (res, 1) != 5)
605 FAIL ();
606 free (res);
608 res = strndup (ptr, 2);
609 if (__builtin_object_size (res, 1) != 3)
610 FAIL ();
611 free (res);
613 res = strdup (&ptr[4]);
614 if (__builtin_object_size (res, 1) != 1)
615 FAIL ();
616 free (res);
618 res = strndup (&ptr[4], 4);
619 if (__builtin_object_size (res, 1) != 1)
620 FAIL ();
621 free (res);
623 res = strndup (&ptr[4], 1);
624 if (__builtin_object_size (res, 1) != 1)
625 FAIL ();
626 free (res);
628 #endif
631 main (void)
633 struct S s[10];
634 __asm ("" : "=r" (l1) : "0" (l1));
635 test1 (main, 6);
636 test2 ();
637 test3 ();
638 test4 ((char *) s, 10);
639 test5 (4);
640 test6 ();
641 test7 ();
642 test8 (1);
643 #ifndef SKIP_STRNDUP
644 test9 ();
645 #endif
646 DONE ();