2016-12-21 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-alloc-size.c
blob5a40862faf844dad8ef07b5deeef05181ab4f1b6
1 /* PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size
2 Test to verify that memory allocation built-ins are decorated with
3 attribute alloc_size that __builtin_object_size can make use of (or
4 are treated as if they were for that purpose)..
5 { dg-do compile }
6 { dg-additional-options "-O2 -fdump-tree-optimized" } */
8 void sink (void*);
10 unsigned size (unsigned n)
12 return n;
15 void test_aligned_alloc (unsigned a)
17 unsigned n = size (7);
19 void *p = __builtin_aligned_alloc (n, a);
20 if (__builtin_object_size (p, 0) != n)
21 __builtin_abort ();
22 sink (p);
25 void test_alloca (void)
27 unsigned n = size (13);
29 void *p = __builtin_alloca (n);
31 /* Also verify that alloca is declared with attribute returns_nonnull
32 (or treated as it were as the case may be). */
33 if (!p)
34 __builtin_abort ();
36 if (__builtin_object_size (p, 0) != n)
37 __builtin_abort ();
38 sink (p);
41 void test_calloc (void)
43 unsigned m = size (19);
44 unsigned n = size (23);
46 void *p = __builtin_calloc (m, n);
47 if (__builtin_object_size (p, 0) != m * n)
48 __builtin_abort ();
49 sink (p);
52 void test_malloc (void)
54 unsigned n = size (17);
56 void *p = __builtin_malloc (n);
57 if (__builtin_object_size (p, 0) != n)
58 __builtin_abort ();
59 sink (p);
62 void test_realloc (void *p)
64 unsigned n = size (31);
66 p = __builtin_realloc (p, n);
67 if (__builtin_object_size (p, 0) != n)
68 __builtin_abort ();
69 sink (p);
72 /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */