Debian Codegraph @

gcc/testsuite/gcc.dg/builtin-alloc-size.c

raw
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" } */
7
8 void sink (void*);
9
10 static unsigned size (unsigned n)
11 {
12 return n;
13 }
14
15 void test_aligned_alloc (unsigned a)
16 {
17 unsigned n = size (7);
18
19 void *p = __builtin_aligned_alloc (a, n);
20 if (__builtin_object_size (p, 0) != n)
21 __builtin_abort ();
22 sink (p);
23 }
24
25 void test_alloca (void)
26 {
27 unsigned n = size (13);
28
29 void *p = __builtin_alloca (n);
30
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 ();
35
36 if (__builtin_object_size (p, 0) != n)
37 __builtin_abort ();
38 sink (p);
39 }
40
41 void test_calloc (void)
42 {
43 unsigned m = size (19);
44 unsigned n = size (23);
45
46 void *p = __builtin_calloc (m, n);
47 if (__builtin_object_size (p, 0) != m * n)
48 __builtin_abort ();
49 sink (p);
50 }
51
52 void test_malloc (void)
53 {
54 unsigned n = size (17);
55
56 void *p = __builtin_malloc (n);
57 if (__builtin_object_size (p, 0) != n)
58 __builtin_abort ();
59 sink (p);
60 }
61
62 void test_realloc (void *p)
63 {
64 unsigned n = size (31);
65
66 p = __builtin_realloc (p, n);
67 if (__builtin_object_size (p, 0) != n)
68 __builtin_abort ();
69 sink (p);
70 }
71
72 /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */