* gcc.dg/20061124-1.c: Add exit() function prototype.
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-4.c
blob453c2d01921643691118c6dde18704e99ffe1a94
1 /* { dg-do run } */
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 *);
14 struct A
16 char a[10];
17 int b;
18 char c[10];
19 } y, w[4];
21 extern char exta[];
22 extern char extb[30];
23 extern struct A extc[];
24 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 struct A vara[x + 10];
34 if (x < 0)
35 r = &a.a[9];
36 else
37 r = &a.c[1];
38 if (__builtin_object_size (p, 3) != sizeof (a.a) - 3)
39 abort ();
40 if (__builtin_object_size (&a.c[9], 3)
41 != sizeof (a.c) - 9)
42 abort ();
43 if (__builtin_object_size (q, 3) != 0)
44 abort ();
45 if (__builtin_object_size (r, 3) != sizeof (a.a) - 9)
46 abort ();
47 if (x < 6)
48 r = &w[2].a[1];
49 else
50 r = &a.a[6];
51 if (__builtin_object_size (&y, 3) != sizeof (y))
52 abort ();
53 if (__builtin_object_size (w, 3) != sizeof (w))
54 abort ();
55 if (__builtin_object_size (&y.b, 3) != sizeof (a.b))
56 abort ();
57 if (__builtin_object_size (r, 3) != sizeof (a.a) - 6)
58 abort ();
59 if (x < 20)
60 r = malloc (30);
61 else
62 r = calloc (2, 16);
63 if (__builtin_object_size (r, 3) != 30)
64 abort ();
65 if (x < 20)
66 r = malloc (30);
67 else
68 r = calloc (2, 14);
69 if (__builtin_object_size (r, 3) != 2 * 14)
70 abort ();
71 if (x < 30)
72 r = malloc (sizeof (a));
73 else
74 r = &a.a[3];
75 if (__builtin_object_size (r, 3) != sizeof (a.a) - 3)
76 abort ();
77 r = memcpy (r, "a", 2);
78 if (__builtin_object_size (r, 3) != sizeof (a.a) - 3)
79 abort ();
80 r = memcpy (r + 2, "b", 2) + 2;
81 if (__builtin_object_size (r, 3) != sizeof (a.a) - 3 - 4)
82 abort ();
83 r = &a.a[4];
84 r = memset (r, 'a', 2);
85 if (__builtin_object_size (r, 3) != sizeof (a.a) - 4)
86 abort ();
87 r = memset (r + 2, 'b', 2) + 2;
88 if (__builtin_object_size (r, 3) != sizeof (a.a) - 8)
89 abort ();
90 r = &a.a[1];
91 r = strcpy (r, "ab");
92 if (__builtin_object_size (r, 3) != sizeof (a.a) - 1)
93 abort ();
94 r = strcpy (r + 2, "cd") + 2;
95 if (__builtin_object_size (r, 3) != sizeof (a.a) - 5)
96 abort ();
97 if (__builtin_object_size (exta, 3) != 0)
98 abort ();
99 if (__builtin_object_size (exta + 10, 3) != 0)
100 abort ();
101 if (__builtin_object_size (&exta[5], 3) != 0)
102 abort ();
103 if (__builtin_object_size (extb, 3) != sizeof (extb))
104 abort ();
105 if (__builtin_object_size (extb + 10, 3) != sizeof (extb) - 10)
106 abort ();
107 if (__builtin_object_size (&extb[5], 3) != sizeof (extb) - 5)
108 abort ();
109 if (__builtin_object_size (extc, 3) != 0)
110 abort ();
111 if (__builtin_object_size (extc + 10, 3) != 0)
112 abort ();
113 if (__builtin_object_size (&extc[5], 3) != 0)
114 abort ();
115 if (__builtin_object_size (&extc->a, 3) != 0)
116 abort ();
117 if (__builtin_object_size (&(extc + 10)->b, 3) != 0)
118 abort ();
119 if (__builtin_object_size (&extc[5].c[3], 3) != 0)
120 abort ();
121 if (__builtin_object_size (var, 3) != 0)
122 abort ();
123 if (__builtin_object_size (var + 10, 3) != 0)
124 abort ();
125 if (__builtin_object_size (&var[5], 3) != 0)
126 abort ();
127 if (__builtin_object_size (vara, 3) != 0)
128 abort ();
129 if (__builtin_object_size (vara + 10, 3) != 0)
130 abort ();
131 if (__builtin_object_size (&vara[5], 3) != 0)
132 abort ();
133 if (__builtin_object_size (&vara[0].a, 3) != 0)
134 abort ();
135 if (__builtin_object_size (&vara[10].a[0], 3) != 0)
136 abort ();
137 if (__builtin_object_size (&vara[5].a[4], 3) != 0)
138 abort ();
139 if (__builtin_object_size (&vara[5].b, 3) != 0)
140 abort ();
141 if (__builtin_object_size (&vara[7].c[7], 3) != 0)
142 abort ();
143 if (__builtin_object_size (zerol, 3) != 0)
144 abort ();
145 if (__builtin_object_size (&zerol, 3) != 0)
146 abort ();
147 if (__builtin_object_size (&zerol[0], 3) != 0)
148 abort ();
149 if (__builtin_object_size (zerol[0].a, 3) != 0)
150 abort ();
151 if (__builtin_object_size (&zerol[0].a[0], 3) != 0)
152 abort ();
153 if (__builtin_object_size (&zerol[0].b, 3) != 0)
154 abort ();
155 if (__builtin_object_size ("abcdefg", 3) != sizeof ("abcdefg"))
156 abort ();
157 if (__builtin_object_size ("abcd\0efg", 3) != sizeof ("abcd\0efg"))
158 abort ();
159 if (__builtin_object_size (&"abcd\0efg", 3) != sizeof ("abcd\0efg"))
160 abort ();
161 if (__builtin_object_size (&"abcd\0efg"[0], 3) != sizeof ("abcd\0efg"))
162 abort ();
163 if (__builtin_object_size (&"abcd\0efg"[4], 3) != sizeof ("abcd\0efg") - 4)
164 abort ();
165 if (__builtin_object_size ("abcd\0efg" + 5, 3) != sizeof ("abcd\0efg") - 5)
166 abort ();
167 if (__builtin_object_size (L"abcdefg", 3) != sizeof (L"abcdefg"))
168 abort ();
169 r = (char *) L"abcd\0efg";
170 if (__builtin_object_size (r + 2, 3) != sizeof (L"abcd\0efg") - 2)
171 abort ();
174 size_t l1 = 1;
176 void
177 __attribute__ ((noinline))
178 test2 (void)
180 struct B { char buf1[10]; char buf2[10]; } a;
181 char *r, buf3[20];
182 int i;
184 if (sizeof (a) != 20)
185 return;
187 r = buf3;
188 for (i = 0; i < 4; ++i)
190 if (i == l1 - 1)
191 r = &a.buf1[1];
192 else if (i == l1)
193 r = &a.buf2[7];
194 else if (i == l1 + 1)
195 r = &buf3[5];
196 else if (i == l1 + 2)
197 r = &a.buf1[9];
199 if (__builtin_object_size (r, 3) != sizeof (a.buf1) - 9)
200 abort ();
201 r = &buf3[20];
202 for (i = 0; i < 4; ++i)
204 if (i == l1 - 1)
205 r = &a.buf1[7];
206 else if (i == l1)
207 r = &a.buf2[7];
208 else if (i == l1 + 1)
209 r = &buf3[5];
210 else if (i == l1 + 2)
211 r = &a.buf1[9];
213 if (__builtin_object_size (r, 3) != 0)
214 abort ();
215 r = &buf3[1];
216 for (i = 0; i < 4; ++i)
218 if (i == l1 - 1)
219 r = &a.buf1[6];
220 else if (i == l1)
221 r = &a.buf2[4];
222 else if (i == l1 + 1)
223 r = &buf3[5];
224 else if (i == l1 + 2)
225 r = &a.buf1[2];
227 if (__builtin_object_size (r, 3) != sizeof (a.buf1) - 6)
228 abort ();
229 r += 2;
230 if (__builtin_object_size (r, 3) != sizeof (a.buf1) - 6 - 2)
231 abort ();
232 if (__builtin_object_size (r + 1, 3) != sizeof (a.buf1) - 6 - 3)
233 abort ();
236 void
237 __attribute__ ((noinline))
238 test3 (void)
240 char buf4[10];
241 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
242 _Complex double f; } x;
243 double y;
244 _Complex double z;
245 double *dp;
247 if (__builtin_object_size (buf4, 3) != sizeof (buf4))
248 abort ();
249 if (__builtin_object_size (&buf4, 3) != sizeof (buf4))
250 abort ();
251 if (__builtin_object_size (&buf4[0], 3) != sizeof (buf4))
252 abort ();
253 if (__builtin_object_size (&buf4[1], 3) != sizeof (buf4) - 1)
254 abort ();
255 if (__builtin_object_size (&x, 3) != sizeof (x))
256 abort ();
257 if (__builtin_object_size (&x.a, 3) != sizeof (x.a))
258 abort ();
259 if (__builtin_object_size (&x.a[0], 3) != sizeof (x.a))
260 abort ();
261 if (__builtin_object_size (&x.a[0].a, 3) != sizeof (x.a[0].a))
262 abort ();
263 if (__builtin_object_size (&x.a[0].a[0], 3) != sizeof (x.a[0].a))
264 abort ();
265 if (__builtin_object_size (&x.a[0].a[3], 3) != sizeof (x.a[0].a) - 3)
266 abort ();
267 if (__builtin_object_size (&x.a[0].b, 3) != sizeof (x.a[0].b))
268 abort ();
269 if (__builtin_object_size (&x.a[1].c, 3) != sizeof (x.a[1].c))
270 abort ();
271 if (__builtin_object_size (&x.a[1].c[0], 3) != sizeof (x.a[1].c))
272 abort ();
273 if (__builtin_object_size (&x.a[1].c[3], 3) != sizeof (x.a[1].c) - 3)
274 abort ();
275 if (__builtin_object_size (&x.b, 3) != sizeof (x.b))
276 abort ();
277 if (__builtin_object_size (&x.b.a, 3) != sizeof (x.b.a))
278 abort ();
279 if (__builtin_object_size (&x.b.a[0], 3) != sizeof (x.b.a))
280 abort ();
281 if (__builtin_object_size (&x.b.a[3], 3) != sizeof (x.b.a) - 3)
282 abort ();
283 if (__builtin_object_size (&x.b.b, 3) != sizeof (x.b.b))
284 abort ();
285 if (__builtin_object_size (&x.b.c, 3) != sizeof (x.b.c))
286 abort ();
287 if (__builtin_object_size (&x.b.c[0], 3) != sizeof (x.b.c))
288 abort ();
289 if (__builtin_object_size (&x.b.c[3], 3) != sizeof (x.b.c) - 3)
290 abort ();
291 if (__builtin_object_size (&x.c, 3) != sizeof (x.c))
292 abort ();
293 if (__builtin_object_size (&x.c[0], 3) != sizeof (x.c))
294 abort ();
295 if (__builtin_object_size (&x.c[1], 3) != sizeof (x.c) - 1)
296 abort ();
297 if (__builtin_object_size (&x.d, 3) != sizeof (x.d))
298 abort ();
299 if (__builtin_object_size (&x.e, 3) != sizeof (x.e))
300 abort ();
301 if (__builtin_object_size (&x.f, 3) != sizeof (x.f))
302 abort ();
303 dp = &__real__ x.f;
304 if (__builtin_object_size (dp, 3) != sizeof (x.f) / 2)
305 abort ();
306 dp = &__imag__ x.f;
307 if (__builtin_object_size (dp, 3) != sizeof (x.f) / 2)
308 abort ();
309 dp = &y;
310 if (__builtin_object_size (dp, 3) != sizeof (y))
311 abort ();
312 if (__builtin_object_size (&z, 3) != sizeof (z))
313 abort ();
314 dp = &__real__ z;
315 if (__builtin_object_size (dp, 3) != sizeof (z) / 2)
316 abort ();
317 dp = &__imag__ z;
318 if (__builtin_object_size (dp, 3) != sizeof (z) / 2)
319 abort ();
322 struct S { unsigned int a; };
324 char *
325 __attribute__ ((noinline))
326 test4 (char *x, int y)
328 register int i;
329 struct A *p;
331 for (i = 0; i < y; i++)
333 p = (struct A *) x;
334 x = (char *) &p[1];
335 if (__builtin_object_size (p, 3) != 0)
336 abort ();
338 return x;
341 void
342 __attribute__ ((noinline))
343 test5 (size_t x)
345 struct T { char buf[64]; char buf2[64]; } t;
346 char *p = &t.buf[8];
347 size_t i;
349 for (i = 0; i < x; ++i)
350 p = p + 4;
351 if (__builtin_object_size (p, 3) != 0)
352 abort ();
353 memset (p, ' ', sizeof (t.buf) - 8 - 4 * 4);
356 void
357 __attribute__ ((noinline))
358 test6 (void)
360 char buf[64];
361 struct T { char buf[64]; char buf2[64]; } t;
362 char *p = &buf[64], *q = &t.buf[64];
364 if (__builtin_object_size (p + 64, 3) != 0)
365 abort ();
366 if (__builtin_object_size (q + 0, 3) != 0)
367 abort ();
368 if (__builtin_object_size (q + 64, 3) != 0)
369 abort ();
372 void
373 __attribute__ ((noinline))
374 test7 (void)
376 struct T { char buf[10]; char buf2[10]; } t;
377 char *p = &t.buf2[-4];
378 char *q = &t.buf2[0];
379 if (__builtin_object_size (p, 3) != 0)
380 abort ();
381 if (__builtin_object_size (q, 3) != sizeof (t.buf2))
382 abort ();
383 q = &t.buf[10];
384 if (__builtin_object_size (q, 3) != 0)
385 abort ();
386 q = &t.buf[11];
387 if (__builtin_object_size (q, 3) != 0)
388 abort ();
389 p = &t.buf[-4];
390 if (__builtin_object_size (p, 3) != 0)
391 abort ();
395 main (void)
397 struct S s[10];
398 __asm ("" : "=r" (l1) : "0" (l1));
399 test1 (main, 6);
400 test2 ();
401 test3 ();
402 test4 ((char *) s, 10);
403 test5 (4);
404 test6 ();
405 test7 ();
406 exit (0);