2017-12-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / gcc.dg / builtin-alloc-size.c
blobcff3e8ad06f5607f90d76bd8ef41c0f05b70eb10
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-require-effective-target alloca }
7 { dg-additional-options "-O2 -fdump-tree-optimized" } */
9 void sink (void*);
11 unsigned size (unsigned n)
13 return n;
16 void test_aligned_alloc (unsigned a)
18 unsigned n = size (7);
20 void *p = __builtin_aligned_alloc (a, n);
21 if (__builtin_object_size (p, 0) != n)
22 __builtin_abort ();
23 sink (p);
26 void test_alloca (void)
28 unsigned n = size (13);
30 void *p = __builtin_alloca (n);
32 /* Also verify that alloca is declared with attribute returns_nonnull
33 (or treated as it were as the case may be). */
34 if (!p)
35 __builtin_abort ();
37 if (__builtin_object_size (p, 0) != n)
38 __builtin_abort ();
39 sink (p);
42 void test_calloc (void)
44 unsigned m = size (19);
45 unsigned n = size (23);
47 void *p = __builtin_calloc (m, n);
48 if (__builtin_object_size (p, 0) != m * n)
49 __builtin_abort ();
50 sink (p);
53 void test_malloc (void)
55 unsigned n = size (17);
57 void *p = __builtin_malloc (n);
58 if (__builtin_object_size (p, 0) != n)
59 __builtin_abort ();
60 sink (p);
63 void test_realloc (void *p)
65 unsigned n = size (31);
67 p = __builtin_realloc (p, n);
68 if (__builtin_object_size (p, 0) != n)
69 __builtin_abort ();
70 sink (p);
73 /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */