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 *);
23 extern struct A zerol[0];
26 __attribute__ ((noinline))
27 test1 (void *q, int x)
30 void *p = &a.a[3], *r;
36 if (__builtin_object_size (p, 0)
37 != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
39 if (__builtin_object_size (&a.c[9], 0)
40 != sizeof (a) - __builtin_offsetof (struct A, c) - 9)
42 if (__builtin_object_size (q, 0) != (size_t) -1)
44 if (__builtin_object_size (r, 0)
45 != sizeof (a) - __builtin_offsetof (struct A, a) - 9)
51 if (__builtin_object_size (&y, 0)
54 if (__builtin_object_size (w, 0)
57 if (__builtin_object_size (&y.b, 0)
58 != sizeof (a) - __builtin_offsetof (struct A, b))
60 if (__builtin_object_size (r, 0)
61 != 2 * sizeof (w[0]) - __builtin_offsetof (struct A, a) - 1)
67 /* We may duplicate this test onto the two exit paths. On one path
68 the size will be 32, the other it will be 30. If we don't duplicate
69 this test, then the size will be 32. */
70 if (__builtin_object_size (r, 0) != 2 * 16
71 && __builtin_object_size (r, 0) != 30)
77 if (__builtin_object_size (r, 0) != 30)
80 r = malloc (sizeof (a));
83 if (__builtin_object_size (r, 0) != sizeof (a))
85 r = memcpy (r, "a", 2);
86 if (__builtin_object_size (r, 0) != sizeof (a))
88 r = memcpy (r + 2, "b", 2) + 2;
89 if (__builtin_object_size (r, 0) != sizeof (a) - 4)
92 r = memset (r, 'a', 2);
93 if (__builtin_object_size (r, 0)
94 != sizeof (a) - __builtin_offsetof (struct A, a) - 4)
96 r = memset (r + 2, 'b', 2) + 2;
97 if (__builtin_object_size (r, 0)
98 != sizeof (a) - __builtin_offsetof (struct A, a) - 8)
101 r = strcpy (r, "ab");
102 if (__builtin_object_size (r, 0)
103 != sizeof (a) - __builtin_offsetof (struct A, a) - 1)
105 r = strcpy (r + 2, "cd") + 2;
106 if (__builtin_object_size (r, 0)
107 != sizeof (a) - __builtin_offsetof (struct A, a) - 5)
109 if (__builtin_object_size (exta, 0) != (size_t) -1)
111 if (__builtin_object_size (exta + 10, 0) != (size_t) -1)
113 if (__builtin_object_size (&exta[5], 0) != (size_t) -1)
115 if (__builtin_object_size (extb, 0) != sizeof (extb))
117 if (__builtin_object_size (extb + 10, 0) != sizeof (extb) - 10)
119 if (__builtin_object_size (&extb[5], 0) != sizeof (extb) - 5)
121 if (__builtin_object_size (var, 0) != (size_t) -1)
123 if (__builtin_object_size (var + 10, 0) != (size_t) -1)
125 if (__builtin_object_size (&var[5], 0) != (size_t) -1)
127 if (__builtin_object_size (zerol, 0) != 0)
129 if (__builtin_object_size (&zerol, 0) != 0)
131 if (__builtin_object_size (&zerol[0], 0) != 0)
133 if (__builtin_object_size (zerol[0].a, 0) != 0)
135 if (__builtin_object_size (&zerol[0].a[0], 0) != 0)
137 if (__builtin_object_size (&zerol[0].b, 0) != 0)
139 if (__builtin_object_size ("abcdefg", 0) != sizeof ("abcdefg"))
141 if (__builtin_object_size ("abcd\0efg", 0) != sizeof ("abcd\0efg"))
143 if (__builtin_object_size (&"abcd\0efg", 0) != sizeof ("abcd\0efg"))
145 if (__builtin_object_size (&"abcd\0efg"[0], 0) != sizeof ("abcd\0efg"))
147 if (__builtin_object_size (&"abcd\0efg"[4], 0) != sizeof ("abcd\0efg") - 4)
149 if (__builtin_object_size ("abcd\0efg" + 5, 0) != sizeof ("abcd\0efg") - 5)
151 if (__builtin_object_size (L"abcdefg", 0) != sizeof (L"abcdefg"))
153 r = (char *) L"abcd\0efg";
154 if (__builtin_object_size (r + 2, 0) != sizeof (L"abcd\0efg") - 2)
161 __attribute__ ((noinline))
164 struct B { char buf1[10]; char buf2[10]; } a;
168 if (sizeof (a) != 20)
172 for (i = 0; i < 4; ++i)
178 else if (i == l1 + 1)
180 else if (i == l1 + 2)
183 if (__builtin_object_size (r, 0) != 20)
186 for (i = 0; i < 4; ++i)
192 else if (i == l1 + 1)
194 else if (i == l1 + 2)
197 if (__builtin_object_size (r, 0) != 15)
200 if (__builtin_object_size (r, 0) != 7)
202 if (__builtin_object_size (r + 6, 0) != 1)
205 for (i = 0; i < 4; ++i)
211 else if (i == l1 + 1)
213 else if (i == l1 + 2)
216 if (__builtin_object_size (r + 12, 0) != 4)
221 __attribute__ ((noinline))
225 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
226 _Complex double f; } x;
231 if (__builtin_object_size (buf4, 0) != sizeof (buf4))
233 if (__builtin_object_size (&buf4, 0) != sizeof (buf4))
235 if (__builtin_object_size (&buf4[0], 0) != sizeof (buf4))
237 if (__builtin_object_size (&buf4[1], 0) != sizeof (buf4) - 1)
239 if (__builtin_object_size (&x, 0) != sizeof (x))
241 if (__builtin_object_size (&x.a, 0) != sizeof (x))
243 if (__builtin_object_size (&x.a[0], 0) != sizeof (x))
245 if (__builtin_object_size (&x.a[0].a, 0) != sizeof (x))
247 if (__builtin_object_size (&x.a[0].a[0], 0) != sizeof (x))
249 if (__builtin_object_size (&x.a[0].a[3], 0) != sizeof (x) - 3)
251 if (__builtin_object_size (&x.a[0].b, 0)
252 != sizeof (x) - __builtin_offsetof (struct A, b))
254 if (__builtin_object_size (&x.a[1].c, 0)
255 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
257 if (__builtin_object_size (&x.a[1].c[0], 0)
258 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
260 if (__builtin_object_size (&x.a[1].c[3], 0)
261 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c) - 3)
263 if (__builtin_object_size (&x.b, 0)
264 != sizeof (x) - __builtin_offsetof (struct B, b))
266 if (__builtin_object_size (&x.b.a, 0)
267 != sizeof (x) - __builtin_offsetof (struct B, b))
269 if (__builtin_object_size (&x.b.a[0], 0)
270 != sizeof (x) - __builtin_offsetof (struct B, b))
272 if (__builtin_object_size (&x.b.a[3], 0)
273 != sizeof (x) - __builtin_offsetof (struct B, b) - 3)
275 if (__builtin_object_size (&x.b.b, 0)
276 != sizeof (x) - __builtin_offsetof (struct B, b)
277 - __builtin_offsetof (struct A, b))
279 if (__builtin_object_size (&x.b.c, 0)
280 != sizeof (x) - __builtin_offsetof (struct B, b)
281 - __builtin_offsetof (struct A, c))
283 if (__builtin_object_size (&x.b.c[0], 0)
284 != sizeof (x) - __builtin_offsetof (struct B, b)
285 - __builtin_offsetof (struct A, c))
287 if (__builtin_object_size (&x.b.c[3], 0)
288 != sizeof (x) - __builtin_offsetof (struct B, b)
289 - __builtin_offsetof (struct A, c) - 3)
291 if (__builtin_object_size (&x.c, 0)
292 != sizeof (x) - __builtin_offsetof (struct B, c))
294 if (__builtin_object_size (&x.c[0], 0)
295 != sizeof (x) - __builtin_offsetof (struct B, c))
297 if (__builtin_object_size (&x.c[1], 0)
298 != sizeof (x) - __builtin_offsetof (struct B, c) - 1)
300 if (__builtin_object_size (&x.d, 0)
301 != sizeof (x) - __builtin_offsetof (struct B, d))
303 if (__builtin_object_size (&x.e, 0)
304 != sizeof (x) - __builtin_offsetof (struct B, e))
306 if (__builtin_object_size (&x.f, 0)
307 != sizeof (x) - __builtin_offsetof (struct B, f))
310 if (__builtin_object_size (dp, 0)
311 != sizeof (x) - __builtin_offsetof (struct B, f))
314 if (__builtin_object_size (dp, 0)
315 != sizeof (x) - __builtin_offsetof (struct B, f)
319 if (__builtin_object_size (dp, 0) != sizeof (y))
321 if (__builtin_object_size (&z, 0) != sizeof (z))
324 if (__builtin_object_size (dp, 0) != sizeof (z))
327 if (__builtin_object_size (dp, 0) != sizeof (z) / 2)
331 struct S { unsigned int a; };
334 __attribute__ ((noinline))
335 test4 (char *x, int y)
340 for (i = 0; i < y; i++)
344 if (__builtin_object_size (p, 0) != (size_t) -1)
351 __attribute__ ((noinline))
358 for (i = 0; i < x; ++i)
360 /* My understanding of ISO C99 6.5.6 is that a conforming
361 program will not end up with p equal to &buf[0]
362 through &buf[7], i.e. calling this function with say
363 UINTPTR_MAX / 4 results in undefined behaviour.
364 If that's true, then the maximum number of remaining
365 bytes from p until end of the object is 56, otherwise
366 it would be 64 (or conservative (size_t) -1 == unknown). */
367 if (__builtin_object_size (p, 0) != sizeof (buf) - 8)
369 memset (p, ' ', sizeof (buf) - 8 - 4 * 4);
373 __attribute__ ((noinline))
376 struct T { char buf[64]; char buf2[64]; } t;
380 for (i = 0; i < x; ++i)
382 if (__builtin_object_size (p, 0) != sizeof (t) - 8)
384 memset (p, ' ', sizeof (t) - 8 - 4 * 4);
388 __attribute__ ((noinline))
392 struct T { char buf[64]; char buf2[64]; } t;
393 char *p = &buf[64], *q = &t.buf[64];
395 if (__builtin_object_size (p + 64, 0) != 0)
397 if (__builtin_object_size (q + 63, 0) != sizeof (t) - 64 - 63)
399 if (__builtin_object_size (q + 64, 0) != sizeof (t) - 64 - 64)
401 if (__builtin_object_size (q + 256, 0) != 0)
406 __attribute__ ((noinline))
409 struct T { char buf[10]; char buf2[10]; } t;
410 char *p = &t.buf2[-4];
411 char *q = &t.buf2[0];
412 if (__builtin_object_size (p, 0) != sizeof (t) - 10 + 4)
414 if (__builtin_object_size (q, 0) != sizeof (t) - 10)
416 /* GCC only handles additions, not subtractions. */
418 if (__builtin_object_size (q, 0) != (size_t) -1
419 && __builtin_object_size (q, 0) != sizeof (t) - 10 + 8)
422 if (__builtin_object_size (p, 0) != 0)
430 __asm ("" : "=r" (l1) : "0" (l1));
434 test4 ((char *) s, 10);