2 /* { dg-options "-O2" } */
3 /* { dg-require-effective-target alloca } */
5 typedef __SIZE_TYPE__
size_t;
6 extern void abort (void);
7 extern void exit (int);
8 extern void *malloc (size_t);
9 extern void *calloc (size_t, size_t);
10 extern void *alloca (size_t);
11 extern void *memcpy (void *, const void *, size_t);
12 extern void *memset (void *, int, size_t);
13 extern char *strcpy (char *, const char *);
24 extern struct A zerol
[0];
27 __attribute__ ((noinline
))
28 test1 (void *q
, int x
)
31 void *p
= &a
.a
[3], *r
;
37 if (__builtin_object_size (p
, 0)
38 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 3)
40 if (__builtin_object_size (&a
.c
[9], 0)
41 != sizeof (a
) - __builtin_offsetof (struct A
, c
) - 9)
43 if (__builtin_object_size (q
, 0) != (size_t) -1)
45 if (__builtin_object_size (r
, 0)
46 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 9)
52 if (__builtin_object_size (&y
, 0)
55 if (__builtin_object_size (w
, 0)
58 if (__builtin_object_size (&y
.b
, 0)
59 != sizeof (a
) - __builtin_offsetof (struct A
, b
))
61 if (__builtin_object_size (r
, 0)
62 != 2 * sizeof (w
[0]) - __builtin_offsetof (struct A
, a
) - 1)
68 /* We may duplicate this test onto the two exit paths. On one path
69 the size will be 32, the other it will be 30. If we don't duplicate
70 this test, then the size will be 32. */
71 if (__builtin_object_size (r
, 0) != 2 * 16
72 && __builtin_object_size (r
, 0) != 30)
78 if (__builtin_object_size (r
, 0) != 30)
81 r
= malloc (sizeof (a
));
84 if (__builtin_object_size (r
, 0) != sizeof (a
))
86 r
= memcpy (r
, "a", 2);
87 if (__builtin_object_size (r
, 0) != sizeof (a
))
89 r
= memcpy (r
+ 2, "b", 2) + 2;
90 if (__builtin_object_size (r
, 0) != sizeof (a
) - 4)
93 r
= memset (r
, 'a', 2);
94 if (__builtin_object_size (r
, 0)
95 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 4)
97 r
= memset (r
+ 2, 'b', 2) + 2;
98 if (__builtin_object_size (r
, 0)
99 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 8)
102 r
= strcpy (r
, "ab");
103 if (__builtin_object_size (r
, 0)
104 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 1)
106 r
= strcpy (r
+ 2, "cd") + 2;
107 if (__builtin_object_size (r
, 0)
108 != sizeof (a
) - __builtin_offsetof (struct A
, a
) - 5)
110 if (__builtin_object_size (exta
, 0) != (size_t) -1)
112 if (__builtin_object_size (exta
+ 10, 0) != (size_t) -1)
114 if (__builtin_object_size (&exta
[5], 0) != (size_t) -1)
116 if (__builtin_object_size (extb
, 0) != sizeof (extb
))
118 if (__builtin_object_size (extb
+ 10, 0) != sizeof (extb
) - 10)
120 if (__builtin_object_size (&extb
[5], 0) != sizeof (extb
) - 5)
122 if (__builtin_object_size (var
, 0) != (size_t) -1)
124 if (__builtin_object_size (var
+ 10, 0) != (size_t) -1)
126 if (__builtin_object_size (&var
[5], 0) != (size_t) -1)
128 if (__builtin_object_size (zerol
, 0) != 0)
130 if (__builtin_object_size (&zerol
, 0) != 0)
132 if (__builtin_object_size (&zerol
[0], 0) != 0)
134 if (__builtin_object_size (zerol
[0].a
, 0) != 0)
136 if (__builtin_object_size (&zerol
[0].a
[0], 0) != 0)
138 if (__builtin_object_size (&zerol
[0].b
, 0) != 0)
140 if (__builtin_object_size ("abcdefg", 0) != sizeof ("abcdefg"))
142 if (__builtin_object_size ("abcd\0efg", 0) != sizeof ("abcd\0efg"))
144 if (__builtin_object_size (&"abcd\0efg", 0) != sizeof ("abcd\0efg"))
146 if (__builtin_object_size (&"abcd\0efg"[0], 0) != sizeof ("abcd\0efg"))
148 if (__builtin_object_size (&"abcd\0efg"[4], 0) != sizeof ("abcd\0efg") - 4)
150 if (__builtin_object_size ("abcd\0efg" + 5, 0) != sizeof ("abcd\0efg") - 5)
152 if (__builtin_object_size (L
"abcdefg", 0) != sizeof (L
"abcdefg"))
154 r
= (char *) L
"abcd\0efg";
155 if (__builtin_object_size (r
+ 2, 0) != sizeof (L
"abcd\0efg") - 2)
162 __attribute__ ((noinline
))
165 struct B
{ char buf1
[10]; char buf2
[10]; } a
;
169 if (sizeof (a
) != 20)
173 for (i
= 0; i
< 4; ++i
)
179 else if (i
== l1
+ 1)
181 else if (i
== l1
+ 2)
184 if (__builtin_object_size (r
, 0) != 20)
187 for (i
= 0; i
< 4; ++i
)
193 else if (i
== l1
+ 1)
195 else if (i
== l1
+ 2)
198 if (__builtin_object_size (r
, 0) != 15)
201 if (__builtin_object_size (r
, 0) != 7)
203 if (__builtin_object_size (r
+ 6, 0) != 1)
206 for (i
= 0; i
< 4; ++i
)
212 else if (i
== l1
+ 1)
214 else if (i
== l1
+ 2)
217 if (__builtin_object_size (r
+ 12, 0) != 4)
222 __attribute__ ((noinline
))
226 struct B
{ struct A a
[2]; struct A b
; char c
[4]; char d
; double e
;
227 _Complex
double f
; } x
;
232 if (__builtin_object_size (buf4
, 0) != sizeof (buf4
))
234 if (__builtin_object_size (&buf4
, 0) != sizeof (buf4
))
236 if (__builtin_object_size (&buf4
[0], 0) != sizeof (buf4
))
238 if (__builtin_object_size (&buf4
[1], 0) != sizeof (buf4
) - 1)
240 if (__builtin_object_size (&x
, 0) != sizeof (x
))
242 if (__builtin_object_size (&x
.a
, 0) != sizeof (x
))
244 if (__builtin_object_size (&x
.a
[0], 0) != sizeof (x
))
246 if (__builtin_object_size (&x
.a
[0].a
, 0) != sizeof (x
))
248 if (__builtin_object_size (&x
.a
[0].a
[0], 0) != sizeof (x
))
250 if (__builtin_object_size (&x
.a
[0].a
[3], 0) != sizeof (x
) - 3)
252 if (__builtin_object_size (&x
.a
[0].b
, 0)
253 != sizeof (x
) - __builtin_offsetof (struct A
, b
))
255 if (__builtin_object_size (&x
.a
[1].c
, 0)
256 != sizeof (x
) - sizeof (struct A
) - __builtin_offsetof (struct A
, c
))
258 if (__builtin_object_size (&x
.a
[1].c
[0], 0)
259 != sizeof (x
) - sizeof (struct A
) - __builtin_offsetof (struct A
, c
))
261 if (__builtin_object_size (&x
.a
[1].c
[3], 0)
262 != sizeof (x
) - sizeof (struct A
) - __builtin_offsetof (struct A
, c
) - 3)
264 if (__builtin_object_size (&x
.b
, 0)
265 != sizeof (x
) - __builtin_offsetof (struct B
, b
))
267 if (__builtin_object_size (&x
.b
.a
, 0)
268 != sizeof (x
) - __builtin_offsetof (struct B
, b
))
270 if (__builtin_object_size (&x
.b
.a
[0], 0)
271 != sizeof (x
) - __builtin_offsetof (struct B
, b
))
273 if (__builtin_object_size (&x
.b
.a
[3], 0)
274 != sizeof (x
) - __builtin_offsetof (struct B
, b
) - 3)
276 if (__builtin_object_size (&x
.b
.b
, 0)
277 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
278 - __builtin_offsetof (struct A
, b
))
280 if (__builtin_object_size (&x
.b
.c
, 0)
281 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
282 - __builtin_offsetof (struct A
, c
))
284 if (__builtin_object_size (&x
.b
.c
[0], 0)
285 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
286 - __builtin_offsetof (struct A
, c
))
288 if (__builtin_object_size (&x
.b
.c
[3], 0)
289 != sizeof (x
) - __builtin_offsetof (struct B
, b
)
290 - __builtin_offsetof (struct A
, c
) - 3)
292 if (__builtin_object_size (&x
.c
, 0)
293 != sizeof (x
) - __builtin_offsetof (struct B
, c
))
295 if (__builtin_object_size (&x
.c
[0], 0)
296 != sizeof (x
) - __builtin_offsetof (struct B
, c
))
298 if (__builtin_object_size (&x
.c
[1], 0)
299 != sizeof (x
) - __builtin_offsetof (struct B
, c
) - 1)
301 if (__builtin_object_size (&x
.d
, 0)
302 != sizeof (x
) - __builtin_offsetof (struct B
, d
))
304 if (__builtin_object_size (&x
.e
, 0)
305 != sizeof (x
) - __builtin_offsetof (struct B
, e
))
307 if (__builtin_object_size (&x
.f
, 0)
308 != sizeof (x
) - __builtin_offsetof (struct B
, f
))
311 if (__builtin_object_size (dp
, 0)
312 != sizeof (x
) - __builtin_offsetof (struct B
, f
))
315 if (__builtin_object_size (dp
, 0)
316 != sizeof (x
) - __builtin_offsetof (struct B
, f
)
320 if (__builtin_object_size (dp
, 0) != sizeof (y
))
322 if (__builtin_object_size (&z
, 0) != sizeof (z
))
325 if (__builtin_object_size (dp
, 0) != sizeof (z
))
328 if (__builtin_object_size (dp
, 0) != sizeof (z
) / 2)
332 struct S
{ unsigned int a
; };
335 __attribute__ ((noinline
))
336 test4 (char *x
, int y
)
341 for (i
= 0; i
< y
; i
++)
345 if (__builtin_object_size (p
, 0) != (size_t) -1)
352 __attribute__ ((noinline
))
359 for (i
= 0; i
< x
; ++i
)
361 /* My understanding of ISO C99 6.5.6 is that a conforming
362 program will not end up with p equal to &buf[0]
363 through &buf[7], i.e. calling this function with say
364 UINTPTR_MAX / 4 results in undefined behavior.
365 If that's true, then the maximum number of remaining
366 bytes from p until end of the object is 56, otherwise
367 it would be 64 (or conservative (size_t) -1 == unknown). */
368 if (__builtin_object_size (p
, 0) != sizeof (buf
) - 8)
370 memset (p
, ' ', sizeof (buf
) - 8 - 4 * 4);
374 __attribute__ ((noinline
))
377 struct T
{ char buf
[64]; char buf2
[64]; } t
;
381 for (i
= 0; i
< x
; ++i
)
383 if (__builtin_object_size (p
, 0) != sizeof (t
) - 8)
385 memset (p
, ' ', sizeof (t
) - 8 - 4 * 4);
389 __attribute__ ((noinline
))
393 struct T
{ char buf
[64]; char buf2
[64]; } t
;
394 char *p
= &buf
[64], *q
= &t
.buf
[64];
396 if (__builtin_object_size (p
+ 64, 0) != 0)
398 if (__builtin_object_size (q
+ 63, 0) != sizeof (t
) - 64 - 63)
400 if (__builtin_object_size (q
+ 64, 0) != sizeof (t
) - 64 - 64)
402 if (__builtin_object_size (q
+ 256, 0) != 0)
407 __attribute__ ((noinline
))
410 struct T
{ char buf
[10]; char buf2
[10]; } t
;
411 char *p
= &t
.buf2
[-4];
412 char *q
= &t
.buf2
[0];
413 if (__builtin_object_size (p
, 0) != sizeof (t
) - 10 + 4)
415 if (__builtin_object_size (q
, 0) != sizeof (t
) - 10)
417 /* GCC only handles additions, not subtractions. */
419 if (__builtin_object_size (q
, 0) != (size_t) -1
420 && __builtin_object_size (q
, 0) != sizeof (t
) - 10 + 8)
423 if (__builtin_object_size (p
, 0) != 0)
431 __asm ("" : "=r" (l1
) : "0" (l1
));
435 test4 ((char *) s
, 10);