Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-3.c
blobd5ca5047ee9e6c7ae16e572240659359b001de90
1 /* { dg-do run } */
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 *);
15 struct A
17 char a[10];
18 int b;
19 char c[10];
20 } y, w[4];
22 extern char exta[];
23 extern char extb[30];
24 extern 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 if (x < 0)
34 r = &a.a[9];
35 else
36 r = &a.c[1];
37 if (__builtin_object_size (p, 2)
38 != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
39 abort ();
40 if (__builtin_object_size (&a.c[9], 2)
41 != sizeof (a) - __builtin_offsetof (struct A, c) - 9)
42 abort ();
43 if (__builtin_object_size (q, 2) != 0)
44 abort ();
45 if (__builtin_object_size (r, 2)
46 != sizeof (a) - __builtin_offsetof (struct A, c) - 1)
47 abort ();
48 if (x < 6)
49 r = &w[2].a[1];
50 else
51 r = &a.a[6];
52 if (__builtin_object_size (&y, 2)
53 != sizeof (y))
54 abort ();
55 if (__builtin_object_size (w, 2)
56 != sizeof (w))
57 abort ();
58 if (__builtin_object_size (&y.b, 2)
59 != sizeof (a) - __builtin_offsetof (struct A, b))
60 abort ();
61 if (__builtin_object_size (r, 2)
62 != sizeof (a) - __builtin_offsetof (struct A, a) - 6)
63 abort ();
64 if (x < 20)
65 r = malloc (30);
66 else
67 r = calloc (2, 16);
68 if (__builtin_object_size (r, 2) != 30)
69 abort ();
70 if (x < 20)
71 r = malloc (30);
72 else
73 r = calloc (2, 14);
74 if (__builtin_object_size (r, 2) != 2 * 14)
75 abort ();
76 if (x < 30)
77 r = malloc (sizeof (a));
78 else
79 r = &a.a[3];
80 if (__builtin_object_size (r, 2)
81 != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
82 abort ();
83 r = memcpy (r, "a", 2);
84 if (__builtin_object_size (r, 2)
85 != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
86 abort ();
87 r = memcpy (r + 2, "b", 2) + 2;
88 if (__builtin_object_size (r, 2)
89 != sizeof (a) - __builtin_offsetof (struct A, a) - 3 - 4)
90 abort ();
91 r = &a.a[4];
92 r = memset (r, 'a', 2);
93 if (__builtin_object_size (r, 2)
94 != sizeof (a) - __builtin_offsetof (struct A, a) - 4)
95 abort ();
96 r = memset (r + 2, 'b', 2) + 2;
97 if (__builtin_object_size (r, 2)
98 != sizeof (a) - __builtin_offsetof (struct A, a) - 8)
99 abort ();
100 r = &a.a[1];
101 r = strcpy (r, "ab");
102 if (__builtin_object_size (r, 2)
103 != sizeof (a) - __builtin_offsetof (struct A, a) - 1)
104 abort ();
105 r = strcpy (r + 2, "cd") + 2;
106 if (__builtin_object_size (r, 2)
107 != sizeof (a) - __builtin_offsetof (struct A, a) - 5)
108 abort ();
109 if (__builtin_object_size (exta, 2) != 0)
110 abort ();
111 if (__builtin_object_size (exta + 10, 2) != 0)
112 abort ();
113 if (__builtin_object_size (&exta[5], 2) != 0)
114 abort ();
115 if (__builtin_object_size (extb, 2) != sizeof (extb))
116 abort ();
117 if (__builtin_object_size (extb + 10, 2) != sizeof (extb) - 10)
118 abort ();
119 if (__builtin_object_size (&extb[5], 2) != sizeof (extb) - 5)
120 abort ();
121 if (__builtin_object_size (var, 2) != 0)
122 abort ();
123 if (__builtin_object_size (var + 10, 2) != 0)
124 abort ();
125 if (__builtin_object_size (&var[5], 2) != 0)
126 abort ();
127 if (__builtin_object_size (zerol, 2) != 0)
128 abort ();
129 if (__builtin_object_size (&zerol, 2) != 0)
130 abort ();
131 if (__builtin_object_size (&zerol[0], 2) != 0)
132 abort ();
133 if (__builtin_object_size (zerol[0].a, 2) != 0)
134 abort ();
135 if (__builtin_object_size (&zerol[0].a[0], 2) != 0)
136 abort ();
137 if (__builtin_object_size (&zerol[0].b, 2) != 0)
138 abort ();
139 if (__builtin_object_size ("abcdefg", 2) != sizeof ("abcdefg"))
140 abort ();
141 if (__builtin_object_size ("abcd\0efg", 2) != sizeof ("abcd\0efg"))
142 abort ();
143 if (__builtin_object_size (&"abcd\0efg", 2) != sizeof ("abcd\0efg"))
144 abort ();
145 if (__builtin_object_size (&"abcd\0efg"[0], 2) != sizeof ("abcd\0efg"))
146 abort ();
147 if (__builtin_object_size (&"abcd\0efg"[4], 2) != sizeof ("abcd\0efg") - 4)
148 abort ();
149 if (__builtin_object_size ("abcd\0efg" + 5, 2) != sizeof ("abcd\0efg") - 5)
150 abort ();
151 if (__builtin_object_size (L"abcdefg", 2) != sizeof (L"abcdefg"))
152 abort ();
153 r = (char *) L"abcd\0efg";
154 if (__builtin_object_size (r + 2, 2) != sizeof (L"abcd\0efg") - 2)
155 abort ();
158 size_t l1 = 1;
160 void
161 __attribute__ ((noinline))
162 test2 (void)
164 struct B { char buf1[10]; char buf2[10]; } a;
165 char *r, buf3[20];
166 int i;
168 if (sizeof (a) != 20)
169 return;
171 r = buf3;
172 for (i = 0; i < 4; ++i)
174 if (i == l1 - 1)
175 r = &a.buf1[1];
176 else if (i == l1)
177 r = &a.buf2[7];
178 else if (i == l1 + 1)
179 r = &buf3[5];
180 else if (i == l1 + 2)
181 r = &a.buf1[9];
183 if (__builtin_object_size (r, 2) != 3)
184 abort ();
185 r = &buf3[20];
186 for (i = 0; i < 4; ++i)
188 if (i == l1 - 1)
189 r = &a.buf1[7];
190 else if (i == l1)
191 r = &a.buf2[7];
192 else if (i == l1 + 1)
193 r = &buf3[5];
194 else if (i == l1 + 2)
195 r = &a.buf1[9];
197 if (__builtin_object_size (r, 2) != 0)
198 abort ();
199 r = &buf3[2];
200 for (i = 0; i < 4; ++i)
202 if (i == l1 - 1)
203 r = &a.buf1[1];
204 else if (i == l1)
205 r = &a.buf1[2];
206 else if (i == l1 + 1)
207 r = &buf3[5];
208 else if (i == l1 + 2)
209 r = &a.buf1[4];
211 if (__builtin_object_size (r, 2) != 15)
212 abort ();
213 r += 8;
214 if (__builtin_object_size (r, 2) != 7)
215 abort ();
216 if (__builtin_object_size (r + 6, 2) != 1)
217 abort ();
218 r = &buf3[18];
219 for (i = 0; i < 4; ++i)
221 if (i == l1 - 1)
222 r = &a.buf1[9];
223 else if (i == l1)
224 r = &a.buf2[9];
225 else if (i == l1 + 1)
226 r = &buf3[5];
227 else if (i == l1 + 2)
228 r = &a.buf1[4];
230 if (__builtin_object_size (r + 12, 2) != 0)
231 abort ();
234 void
235 __attribute__ ((noinline))
236 test3 (void)
238 char buf4[10];
239 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
240 _Complex double f; } x;
241 double y;
242 _Complex double z;
243 double *dp;
245 if (__builtin_object_size (buf4, 2) != sizeof (buf4))
246 abort ();
247 if (__builtin_object_size (&buf4, 2) != sizeof (buf4))
248 abort ();
249 if (__builtin_object_size (&buf4[0], 2) != sizeof (buf4))
250 abort ();
251 if (__builtin_object_size (&buf4[1], 2) != sizeof (buf4) - 1)
252 abort ();
253 if (__builtin_object_size (&x, 2) != sizeof (x))
254 abort ();
255 if (__builtin_object_size (&x.a, 2) != sizeof (x))
256 abort ();
257 if (__builtin_object_size (&x.a[0], 2) != sizeof (x))
258 abort ();
259 if (__builtin_object_size (&x.a[0].a, 2) != sizeof (x))
260 abort ();
261 if (__builtin_object_size (&x.a[0].a[0], 2) != sizeof (x))
262 abort ();
263 if (__builtin_object_size (&x.a[0].a[3], 2) != sizeof (x) - 3)
264 abort ();
265 if (__builtin_object_size (&x.a[0].b, 2)
266 != sizeof (x) - __builtin_offsetof (struct A, b))
267 abort ();
268 if (__builtin_object_size (&x.a[1].c, 2)
269 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
270 abort ();
271 if (__builtin_object_size (&x.a[1].c[0], 2)
272 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
273 abort ();
274 if (__builtin_object_size (&x.a[1].c[3], 2)
275 != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c) - 3)
276 abort ();
277 if (__builtin_object_size (&x.b, 2)
278 != sizeof (x) - __builtin_offsetof (struct B, b))
279 abort ();
280 if (__builtin_object_size (&x.b.a, 2)
281 != sizeof (x) - __builtin_offsetof (struct B, b))
282 abort ();
283 if (__builtin_object_size (&x.b.a[0], 2)
284 != sizeof (x) - __builtin_offsetof (struct B, b))
285 abort ();
286 if (__builtin_object_size (&x.b.a[3], 2)
287 != sizeof (x) - __builtin_offsetof (struct B, b) - 3)
288 abort ();
289 if (__builtin_object_size (&x.b.b, 2)
290 != sizeof (x) - __builtin_offsetof (struct B, b)
291 - __builtin_offsetof (struct A, b))
292 abort ();
293 if (__builtin_object_size (&x.b.c, 2)
294 != sizeof (x) - __builtin_offsetof (struct B, b)
295 - __builtin_offsetof (struct A, c))
296 abort ();
297 if (__builtin_object_size (&x.b.c[0], 2)
298 != sizeof (x) - __builtin_offsetof (struct B, b)
299 - __builtin_offsetof (struct A, c))
300 abort ();
301 if (__builtin_object_size (&x.b.c[3], 2)
302 != sizeof (x) - __builtin_offsetof (struct B, b)
303 - __builtin_offsetof (struct A, c) - 3)
304 abort ();
305 if (__builtin_object_size (&x.c, 2)
306 != sizeof (x) - __builtin_offsetof (struct B, c))
307 abort ();
308 if (__builtin_object_size (&x.c[0], 2)
309 != sizeof (x) - __builtin_offsetof (struct B, c))
310 abort ();
311 if (__builtin_object_size (&x.c[1], 2)
312 != sizeof (x) - __builtin_offsetof (struct B, c) - 1)
313 abort ();
314 if (__builtin_object_size (&x.d, 2)
315 != sizeof (x) - __builtin_offsetof (struct B, d))
316 abort ();
317 if (__builtin_object_size (&x.e, 2)
318 != sizeof (x) - __builtin_offsetof (struct B, e))
319 abort ();
320 if (__builtin_object_size (&x.f, 2)
321 != sizeof (x) - __builtin_offsetof (struct B, f))
322 abort ();
323 dp = &__real__ x.f;
324 if (__builtin_object_size (dp, 2)
325 != sizeof (x) - __builtin_offsetof (struct B, f))
326 abort ();
327 dp = &__imag__ x.f;
328 if (__builtin_object_size (dp, 2)
329 != sizeof (x) - __builtin_offsetof (struct B, f)
330 - sizeof (x.f) / 2)
331 abort ();
332 dp = &y;
333 if (__builtin_object_size (dp, 2) != sizeof (y))
334 abort ();
335 if (__builtin_object_size (&z, 2) != sizeof (z))
336 abort ();
337 dp = &__real__ z;
338 if (__builtin_object_size (dp, 2) != sizeof (z))
339 abort ();
340 dp = &__imag__ z;
341 if (__builtin_object_size (dp, 2) != sizeof (z) / 2)
342 abort ();
345 struct S { unsigned int a; };
347 char *
348 __attribute__ ((noinline))
349 test4 (char *x, int y)
351 register int i;
352 struct A *p;
354 for (i = 0; i < y; i++)
356 p = (struct A *) x;
357 x = (char *) &p[1];
358 if (__builtin_object_size (p, 2) != 0)
359 abort ();
361 return x;
364 void
365 __attribute__ ((noinline))
366 test5 (size_t x)
368 char buf[64];
369 char *p = &buf[8];
370 size_t i;
372 for (i = 0; i < x; ++i)
373 p = p + 4;
374 if (__builtin_object_size (p, 2) != 0)
375 abort ();
376 memset (p, ' ', sizeof (buf) - 8 - 4 * 4);
379 void
380 __attribute__ ((noinline))
381 test6 (size_t x)
383 struct T { char buf[64]; char buf2[64]; } t;
384 char *p = &t.buf[8];
385 size_t i;
387 for (i = 0; i < x; ++i)
388 p = p + 4;
389 if (__builtin_object_size (p, 2) != 0)
390 abort ();
391 memset (p, ' ', sizeof (t) - 8 - 4 * 4);
394 void
395 __attribute__ ((noinline))
396 test7 (void)
398 char buf[64];
399 struct T { char buf[64]; char buf2[64]; } t;
400 char *p = &buf[64], *q = &t.buf[64];
402 if (__builtin_object_size (p + 64, 2) != 0)
403 abort ();
404 if (__builtin_object_size (q + 63, 2) != sizeof (t) - 64 - 63)
405 abort ();
406 if (__builtin_object_size (q + 64, 2) != sizeof (t) - 64 - 64)
407 abort ();
408 if (__builtin_object_size (q + 256, 2) != 0)
409 abort ();
412 void
413 __attribute__ ((noinline))
414 test8 (void)
416 struct T { char buf[10]; char buf2[10]; } t;
417 char *p = &t.buf2[-4];
418 char *q = &t.buf2[0];
419 if (__builtin_object_size (p, 2) != sizeof (t) - 10 + 4)
420 abort ();
421 if (__builtin_object_size (q, 2) != sizeof (t) - 10)
422 abort ();
423 /* GCC only handles additions, not subtractions. */
424 q = q - 8;
425 if (__builtin_object_size (q, 2) != 0
426 && __builtin_object_size (q, 2) != sizeof (t) - 10 + 8)
427 abort ();
428 p = &t.buf[-4];
429 if (__builtin_object_size (p, 2) != 0)
430 abort ();
434 main (void)
436 struct S s[10];
437 __asm ("" : "=r" (l1) : "0" (l1));
438 test1 (main, 6);
439 test2 ();
440 test3 ();
441 test4 ((char *) s, 10);
442 test5 (4);
443 test6 (4);
444 test7 ();
445 test8 ();
446 exit (0);