Add new test to verify that the array index, limit, and stride are
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-object-size-2.c
blob21aff5a958dfcfb2bee335d63bcef3023c8f8f3d
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, 1) != sizeof (a.a) - 3)
39 abort ();
40 if (__builtin_object_size (&a.c[9], 1)
41 != sizeof (a.c) - 9)
42 abort ();
43 if (__builtin_object_size (q, 1) != (size_t) -1)
44 abort ();
45 if (__builtin_object_size (r, 1) != sizeof (a.c) - 1)
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, 1) != sizeof (y))
52 abort ();
53 if (__builtin_object_size (w, 1) != sizeof (w))
54 abort ();
55 if (__builtin_object_size (&y.b, 1) != sizeof (a.b))
56 abort ();
57 if (__builtin_object_size (r, 1) != sizeof (a.a) - 1)
58 abort ();
59 if (x < 20)
60 r = malloc (30);
61 else
62 r = calloc (2, 16);
63 /* We may duplicate this test onto the two exit paths. On one path
64 the size will be 32, the other it will be 30. If we don't duplicate
65 this test, then the size will be 32. */
66 if (__builtin_object_size (r, 1) != 2 * 16
67 && __builtin_object_size (r, 1) != 30)
68 abort ();
69 if (x < 20)
70 r = malloc (30);
71 else
72 r = calloc (2, 14);
73 if (__builtin_object_size (r, 1) != 30)
74 abort ();
75 if (x < 30)
76 r = malloc (sizeof (a));
77 else
78 r = &a.a[3];
79 if (__builtin_object_size (r, 1) != sizeof (a))
80 abort ();
81 r = memcpy (r, "a", 2);
82 if (__builtin_object_size (r, 1) != sizeof (a))
83 abort ();
84 r = memcpy (r + 2, "b", 2) + 2;
85 if (__builtin_object_size (r, 1) != sizeof (a) - 4)
86 abort ();
87 r = &a.a[4];
88 r = memset (r, 'a', 2);
89 if (__builtin_object_size (r, 1) != sizeof (a.a) - 4)
90 abort ();
91 r = memset (r + 2, 'b', 2) + 2;
92 if (__builtin_object_size (r, 1) != sizeof (a.a) - 8)
93 abort ();
94 r = &a.a[1];
95 r = strcpy (r, "ab");
96 if (__builtin_object_size (r, 1) != sizeof (a.a) - 1)
97 abort ();
98 r = strcpy (r + 2, "cd") + 2;
99 if (__builtin_object_size (r, 1) != sizeof (a.a) - 5)
100 abort ();
101 if (__builtin_object_size (exta, 1) != (size_t) -1)
102 abort ();
103 if (__builtin_object_size (exta + 10, 1) != (size_t) -1)
104 abort ();
105 if (__builtin_object_size (&exta[5], 1) != (size_t) -1)
106 abort ();
107 if (__builtin_object_size (extb, 1) != sizeof (extb))
108 abort ();
109 if (__builtin_object_size (extb + 10, 1) != sizeof (extb) - 10)
110 abort ();
111 if (__builtin_object_size (&extb[5], 1) != sizeof (extb) - 5)
112 abort ();
113 if (__builtin_object_size (extc, 1) != (size_t) -1)
114 abort ();
115 if (__builtin_object_size (extc + 10, 1) != (size_t) -1)
116 abort ();
117 if (__builtin_object_size (&extc[5], 1) != (size_t) -1)
118 abort ();
119 if (__builtin_object_size (&extc->a, 1) != (size_t) -1)
120 abort ();
121 if (__builtin_object_size (&(extc + 10)->b, 1) != (size_t) -1)
122 abort ();
123 if (__builtin_object_size (&extc[5].c[3], 1) != (size_t) -1)
124 abort ();
125 if (__builtin_object_size (var, 1) != (size_t) -1)
126 abort ();
127 if (__builtin_object_size (var + 10, 1) != (size_t) -1)
128 abort ();
129 if (__builtin_object_size (&var[5], 1) != (size_t) -1)
130 abort ();
131 if (__builtin_object_size (vara, 1) != (size_t) -1)
132 abort ();
133 if (__builtin_object_size (vara + 10, 1) != (size_t) -1)
134 abort ();
135 if (__builtin_object_size (&vara[5], 1) != (size_t) -1)
136 abort ();
137 if (__builtin_object_size (&vara[0].a, 1) != sizeof (vara[0].a))
138 abort ();
139 if (__builtin_object_size (&vara[10].a[0], 1) != sizeof (vara[0].a))
140 abort ();
141 if (__builtin_object_size (&vara[5].a[4], 1) != sizeof (vara[0].a) - 4)
142 abort ();
143 if (__builtin_object_size (&vara[5].b, 1) != sizeof (vara[0].b))
144 abort ();
145 if (__builtin_object_size (&vara[7].c[7], 1) != sizeof (vara[0].c) - 7)
146 abort ();
147 if (__builtin_object_size (zerol, 1) != 0)
148 abort ();
149 if (__builtin_object_size (&zerol, 1) != 0)
150 abort ();
151 if (__builtin_object_size (&zerol[0], 1) != 0)
152 abort ();
153 if (__builtin_object_size (zerol[0].a, 1) != 0)
154 abort ();
155 if (__builtin_object_size (&zerol[0].a[0], 1) != 0)
156 abort ();
157 if (__builtin_object_size (&zerol[0].b, 1) != 0)
158 abort ();
159 if (__builtin_object_size ("abcdefg", 1) != sizeof ("abcdefg"))
160 abort ();
161 if (__builtin_object_size ("abcd\0efg", 1) != sizeof ("abcd\0efg"))
162 abort ();
163 if (__builtin_object_size (&"abcd\0efg", 1) != sizeof ("abcd\0efg"))
164 abort ();
165 if (__builtin_object_size (&"abcd\0efg"[0], 1) != sizeof ("abcd\0efg"))
166 abort ();
167 if (__builtin_object_size (&"abcd\0efg"[4], 1) != sizeof ("abcd\0efg") - 4)
168 abort ();
169 if (__builtin_object_size ("abcd\0efg" + 5, 1) != sizeof ("abcd\0efg") - 5)
170 abort ();
171 if (__builtin_object_size (L"abcdefg", 1) != sizeof (L"abcdefg"))
172 abort ();
173 r = (char *) L"abcd\0efg";
174 if (__builtin_object_size (r + 2, 1) != sizeof (L"abcd\0efg") - 2)
175 abort ();
178 size_t l1 = 1;
180 void
181 __attribute__ ((noinline))
182 test2 (void)
184 struct B { char buf1[10]; char buf2[10]; } a;
185 char *r, buf3[20];
186 int i;
188 if (sizeof (a) != 20)
189 return;
191 r = buf3;
192 for (i = 0; i < 4; ++i)
194 if (i == l1 - 1)
195 r = &a.buf1[1];
196 else if (i == l1)
197 r = &a.buf2[7];
198 else if (i == l1 + 1)
199 r = &buf3[5];
200 else if (i == l1 + 2)
201 r = &a.buf1[9];
203 if (__builtin_object_size (r, 1) != sizeof (buf3))
204 abort ();
205 r = &buf3[20];
206 for (i = 0; i < 4; ++i)
208 if (i == l1 - 1)
209 r = &a.buf1[7];
210 else if (i == l1)
211 r = &a.buf2[7];
212 else if (i == l1 + 1)
213 r = &buf3[5];
214 else if (i == l1 + 2)
215 r = &a.buf1[9];
217 if (__builtin_object_size (r, 1) != sizeof (buf3) - 5)
218 abort ();
219 r += 8;
220 if (__builtin_object_size (r, 1) != sizeof (buf3) - 13)
221 abort ();
222 if (__builtin_object_size (r + 6, 1) != sizeof (buf3) - 19)
223 abort ();
226 void
227 __attribute__ ((noinline))
228 test3 (void)
230 char buf4[10];
231 struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
232 _Complex double f; } x;
233 double y;
234 _Complex double z;
235 double *dp;
237 if (__builtin_object_size (buf4, 1) != sizeof (buf4))
238 abort ();
239 if (__builtin_object_size (&buf4, 1) != sizeof (buf4))
240 abort ();
241 if (__builtin_object_size (&buf4[0], 1) != sizeof (buf4))
242 abort ();
243 if (__builtin_object_size (&buf4[1], 1) != sizeof (buf4) - 1)
244 abort ();
245 if (__builtin_object_size (&x, 1) != sizeof (x))
246 abort ();
247 if (__builtin_object_size (&x.a, 1) != sizeof (x.a))
248 abort ();
249 if (__builtin_object_size (&x.a[0], 1) != sizeof (x.a))
250 abort ();
251 if (__builtin_object_size (&x.a[0].a, 1) != sizeof (x.a[0].a))
252 abort ();
253 if (__builtin_object_size (&x.a[0].a[0], 1) != sizeof (x.a[0].a))
254 abort ();
255 if (__builtin_object_size (&x.a[0].a[3], 1) != sizeof (x.a[0].a) - 3)
256 abort ();
257 if (__builtin_object_size (&x.a[0].b, 1) != sizeof (x.a[0].b))
258 abort ();
259 if (__builtin_object_size (&x.a[1].c, 1) != sizeof (x.a[1].c))
260 abort ();
261 if (__builtin_object_size (&x.a[1].c[0], 1) != sizeof (x.a[1].c))
262 abort ();
263 if (__builtin_object_size (&x.a[1].c[3], 1) != sizeof (x.a[1].c) - 3)
264 abort ();
265 if (__builtin_object_size (&x.b, 1) != sizeof (x.b))
266 abort ();
267 if (__builtin_object_size (&x.b.a, 1) != sizeof (x.b.a))
268 abort ();
269 if (__builtin_object_size (&x.b.a[0], 1) != sizeof (x.b.a))
270 abort ();
271 if (__builtin_object_size (&x.b.a[3], 1) != sizeof (x.b.a) - 3)
272 abort ();
273 if (__builtin_object_size (&x.b.b, 1) != sizeof (x.b.b))
274 abort ();
275 if (__builtin_object_size (&x.b.c, 1) != sizeof (x.b.c))
276 abort ();
277 if (__builtin_object_size (&x.b.c[0], 1) != sizeof (x.b.c))
278 abort ();
279 if (__builtin_object_size (&x.b.c[3], 1) != sizeof (x.b.c) - 3)
280 abort ();
281 if (__builtin_object_size (&x.c, 1) != sizeof (x.c))
282 abort ();
283 if (__builtin_object_size (&x.c[0], 1) != sizeof (x.c))
284 abort ();
285 if (__builtin_object_size (&x.c[1], 1) != sizeof (x.c) - 1)
286 abort ();
287 if (__builtin_object_size (&x.d, 1) != sizeof (x.d))
288 abort ();
289 if (__builtin_object_size (&x.e, 1) != sizeof (x.e))
290 abort ();
291 if (__builtin_object_size (&x.f, 1) != sizeof (x.f))
292 abort ();
293 dp = &__real__ x.f;
294 if (__builtin_object_size (dp, 1) != sizeof (x.f) / 2)
295 abort ();
296 dp = &__imag__ x.f;
297 if (__builtin_object_size (dp, 1) != sizeof (x.f) / 2)
298 abort ();
299 dp = &y;
300 if (__builtin_object_size (dp, 1) != sizeof (y))
301 abort ();
302 if (__builtin_object_size (&z, 1) != sizeof (z))
303 abort ();
304 dp = &__real__ z;
305 if (__builtin_object_size (dp, 1) != sizeof (z) / 2)
306 abort ();
307 dp = &__imag__ z;
308 if (__builtin_object_size (dp, 1) != sizeof (z) / 2)
309 abort ();
312 struct S { unsigned int a; };
314 char *
315 __attribute__ ((noinline))
316 test4 (char *x, int y)
318 register int i;
319 struct A *p;
321 for (i = 0; i < y; i++)
323 p = (struct A *) x;
324 x = (char *) &p[1];
325 if (__builtin_object_size (p, 1) != (size_t) -1)
326 abort ();
328 return x;
331 void
332 __attribute__ ((noinline))
333 test5 (size_t x)
335 struct T { char buf[64]; char buf2[64]; } t;
336 char *p = &t.buf[8];
337 size_t i;
339 for (i = 0; i < x; ++i)
340 p = p + 4;
341 if (__builtin_object_size (p, 1) != sizeof (t.buf) - 8)
342 abort ();
343 memset (p, ' ', sizeof (t.buf) - 8 - 4 * 4);
346 void
347 __attribute__ ((noinline))
348 test6 (void)
350 char buf[64];
351 struct T { char buf[64]; char buf2[64]; } t;
352 char *p = &buf[64], *q = &t.buf[64];
354 if (__builtin_object_size (p + 64, 1) != 0)
355 abort ();
356 if (__builtin_object_size (q + 0, 1) != 0)
357 abort ();
358 if (__builtin_object_size (q + 64, 1) != 0)
359 abort ();
362 void
363 __attribute__ ((noinline))
364 test7 (void)
366 struct T { char buf[10]; char buf2[10]; } t;
367 char *p = &t.buf2[-4];
368 char *q = &t.buf2[0];
369 if (__builtin_object_size (p, 1) != 0)
370 abort ();
371 if (__builtin_object_size (q, 1) != sizeof (t.buf2))
372 abort ();
373 q = &t.buf[10];
374 if (__builtin_object_size (q, 1) != 0)
375 abort ();
376 q = &t.buf[11];
377 if (__builtin_object_size (q, 1) != 0)
378 abort ();
379 p = &t.buf[-4];
380 if (__builtin_object_size (p, 1) != 0)
381 abort ();
385 main (void)
387 struct S s[10];
388 __asm ("" : "=r" (l1) : "0" (l1));
389 test1 (main, 6);
390 test2 ();
391 test3 ();
392 test4 ((char *) s, 10);
393 test5 (4);
394 test6 ();
395 test7 ();
396 exit (0);