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 if (__builtin_object_size (r, 0) != 2 * 16)
73 if (__builtin_object_size (r, 0) != 30)
76 r = malloc (sizeof (a));
79 if (__builtin_object_size (r, 0) != sizeof (a))
81 r = memcpy (r, "a", 2);
82 if (__builtin_object_size (r, 0) != sizeof (a))
84 r = memcpy (r + 2, "b", 2) + 2;
85 if (__builtin_object_size (r, 0) != sizeof (a) - 4)
88 r = memset (r, 'a', 2);
89 if (__builtin_object_size (r, 0)
90 != sizeof (a) - __builtin_offsetof (struct A, a) - 4)
92 r = memset (r + 2, 'b', 2) + 2;
93 if (__builtin_object_size (r, 0)
94 != sizeof (a) - __builtin_offsetof (struct A, a) - 8)
98 if (__builtin_object_size (r, 0)
99 != sizeof (a) - __builtin_offsetof (struct A, a) - 1)
101 r = strcpy (r + 2, "cd") + 2;
102 if (__builtin_object_size (r, 0)
103 != sizeof (a) - __builtin_offsetof (struct A, a) - 5)
105 if (__builtin_object_size (exta, 0) != (size_t) -1)
107 if (__builtin_object_size (exta + 10, 0) != (size_t) -1)
109 if (__builtin_object_size (&exta[5], 0) != (size_t) -1)
111 if (__builtin_object_size (extb, 0) != sizeof (extb))
113 if (__builtin_object_size (extb + 10, 0) != sizeof (extb) - 10)
115 if (__builtin_object_size (&extb[5], 0) != sizeof (extb) - 5)
117 if (__builtin_object_size (var, 0) != (size_t) -1)
119 if (__builtin_object_size (var + 10, 0) != (size_t) -1)
121 if (__builtin_object_size (&var[5], 0) != (size_t) -1)
123 if (__builtin_object_size (zerol, 0) != 0)
125 if (__builtin_object_size (&zerol, 0) != 0)
127 if (__builtin_object_size (&zerol[0], 0) != 0)
129 if (__builtin_object_size (zerol[0].a, 0) != 0)
131 if (__builtin_object_size (&zerol[0].a[0], 0) != 0)
133 if (__builtin_object_size (&zerol[0].b, 0) != 0)
135 if (__builtin_object_size ("abcdefg", 0) != sizeof ("abcdefg"))
137 if (__builtin_object_size ("abcd\0efg", 0) != sizeof ("abcd\0efg"))
139 if (__builtin_object_size (&"abcd\0efg", 0) != sizeof ("abcd\0efg"))
141 if (__builtin_object_size (&"abcd\0efg"[0], 0) != sizeof ("abcd\0efg"))
143 if (__builtin_object_size (&"abcd\0efg"[4], 0) != sizeof ("abcd\0efg") - 4)
145 if (__builtin_object_size ("abcd\0efg" + 5, 0) != sizeof ("abcd\0efg") - 5)
147 if (__builtin_object_size (L"abcdefg", 0) != sizeof (L"abcdefg"))
149 r = (char *) L"abcd\0efg";
150 if (__builtin_object_size (r + 2, 0) != sizeof (L"abcd\0efg") - 2)
157 __attribute__ ((noinline))
160 struct B { char buf1[10]; char buf2[10]; } a;
164 if (sizeof (a) != 20)
168 for (i = 0; i < 4; ++i)
174 else if (i == l1 + 1)
176 else if (i == l1 + 2)
179 if (__builtin_object_size (r, 0) != 20)
182 for (i = 0; i < 4; ++i)
188 else if (i == l1 + 1)
190 else if (i == l1 + 2)
193 if (__builtin_object_size (r, 0) != 15)
196 if (__builtin_object_size (r, 0) != 7)
198 if (__builtin_object_size (r + 6, 0) != 1)
201 for (i = 0; i < 4; ++i)
207 else if (i == l1 + 1)
209 else if (i == l1 + 2)
212 if (__builtin_object_size (r + 12, 0) != 4)
217 __attribute__ ((noinline))
221 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
222 _Complex double f; } x;
227 if (__builtin_object_size (buf4, 0) != sizeof (buf4))
229 if (__builtin_object_size (&buf4, 0) != sizeof (buf4))
231 if (__builtin_object_size (&buf4[0], 0) != sizeof (buf4))
233 if (__builtin_object_size (&buf4[1], 0) != sizeof (buf4) - 1)
235 if (__builtin_object_size (&x, 0) != sizeof (x))
237 if (__builtin_object_size (&x.a, 0) != sizeof (x))
239 if (__builtin_object_size (&x.a[0], 0) != sizeof (x))
241 if (__builtin_object_size (&x.a[0].a, 0) != sizeof (x))
243 if (__builtin_object_size (&x.a[0].a[0], 0) != sizeof (x))
245 if (__builtin_object_size (&x.a[0].a[3], 0) != sizeof (x) - 3)
247 if (__builtin_object_size (&x.a[0].b, 0)
248 != sizeof (x) - __builtin_offsetof (struct A, b))
250 if (__builtin_object_size (&x.a[1].c, 0)
251 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
253 if (__builtin_object_size (&x.a[1].c[0], 0)
254 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
256 if (__builtin_object_size (&x.a[1].c[3], 0)
257 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c) - 3)
259 if (__builtin_object_size (&x.b, 0)
260 != sizeof (x) - __builtin_offsetof (struct B, b))
262 if (__builtin_object_size (&x.b.a, 0)
263 != sizeof (x) - __builtin_offsetof (struct B, b))
265 if (__builtin_object_size (&x.b.a[0], 0)
266 != sizeof (x) - __builtin_offsetof (struct B, b))
268 if (__builtin_object_size (&x.b.a[3], 0)
269 != sizeof (x) - __builtin_offsetof (struct B, b) - 3)
271 if (__builtin_object_size (&x.b.b, 0)
272 != sizeof (x) - __builtin_offsetof (struct B, b)
273 - __builtin_offsetof (struct A, b))
275 if (__builtin_object_size (&x.b.c, 0)
276 != sizeof (x) - __builtin_offsetof (struct B, b)
277 - __builtin_offsetof (struct A, c))
279 if (__builtin_object_size (&x.b.c[0], 0)
280 != sizeof (x) - __builtin_offsetof (struct B, b)
281 - __builtin_offsetof (struct A, c))
283 if (__builtin_object_size (&x.b.c[3], 0)
284 != sizeof (x) - __builtin_offsetof (struct B, b)
285 - __builtin_offsetof (struct A, c) - 3)
287 if (__builtin_object_size (&x.c, 0)
288 != sizeof (x) - __builtin_offsetof (struct B, c))
290 if (__builtin_object_size (&x.c[0], 0)
291 != sizeof (x) - __builtin_offsetof (struct B, c))
293 if (__builtin_object_size (&x.c[1], 0)
294 != sizeof (x) - __builtin_offsetof (struct B, c) - 1)
296 if (__builtin_object_size (&x.d, 0)
297 != sizeof (x) - __builtin_offsetof (struct B, d))
299 if (__builtin_object_size (&x.e, 0)
300 != sizeof (x) - __builtin_offsetof (struct B, e))
302 if (__builtin_object_size (&x.f, 0)
303 != sizeof (x) - __builtin_offsetof (struct B, f))
306 if (__builtin_object_size (dp, 0)
307 != sizeof (x) - __builtin_offsetof (struct B, f))
310 if (__builtin_object_size (dp, 0)
311 != sizeof (x) - __builtin_offsetof (struct B, f)
315 if (__builtin_object_size (dp, 0) != sizeof (y))
317 if (__builtin_object_size (&z, 0) != sizeof (z))
320 if (__builtin_object_size (dp, 0) != sizeof (z))
323 if (__builtin_object_size (dp, 0) != sizeof (z) / 2)
327 struct S { unsigned int a; };
330 __attribute__ ((noinline))
331 test4 (char *x, int y)
336 for (i = 0; i < y; i++)
340 if (__builtin_object_size (p, 0) != (size_t) -1)
347 __attribute__ ((noinline))
354 for (i = 0; i < x; ++i)
356 /* My understanding of ISO C99 6.5.6 is that a conforming
357 program will not end up with p equal to &buf[0]
358 through &buf[7], i.e. calling this function with say
359 UINTPTR_MAX / 4 results in undefined behaviour.
360 If that's true, then the maximum number of remaining
361 bytes from p until end of the object is 56, otherwise
362 it would be 64 (or conservative (size_t) -1 == unknown). */
363 if (__builtin_object_size (p, 0) != sizeof (buf) - 8)
365 memset (p, ' ', sizeof (buf) - 8 - 4 * 4);
369 __attribute__ ((noinline))
372 struct T { char buf[64]; char buf2[64]; } t;
376 for (i = 0; i < x; ++i)
378 if (__builtin_object_size (p, 0) != sizeof (t) - 8)
380 memset (p, ' ', sizeof (t) - 8 - 4 * 4);
384 __attribute__ ((noinline))
388 struct T { char buf[64]; char buf2[64]; } t;
389 char *p = &buf[64], *q = &t.buf[64];
391 if (__builtin_object_size (p + 64, 0) != 0)
393 if (__builtin_object_size (q + 63, 0) != sizeof (t) - 64 - 63)
395 if (__builtin_object_size (q + 64, 0) != sizeof (t) - 64 - 64)
397 if (__builtin_object_size (q + 256, 0) != 0)
402 __attribute__ ((noinline))
405 struct T { char buf[10]; char buf2[10]; } t;
406 char *p = &t.buf2[-4];
407 char *q = &t.buf2[0];
408 if (__builtin_object_size (p, 0) != sizeof (t) - 10 + 4)
410 if (__builtin_object_size (q, 0) != sizeof (t) - 10)
412 /* GCC only handles additions, not subtractions. */
414 if (__builtin_object_size (q, 0) != (size_t) -1
415 && __builtin_object_size (q, 0) != sizeof (t) - 10 + 8)
418 if (__builtin_object_size (p, 0) != 0)
426 __asm ("" : "=r" (l1) : "0" (l1));
430 test4 ((char *) s, 10);