libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Wmismatched-dealloc-3.c
blob302900662cecf2a590905a22975363219b01c699
1 /* Verify that Glibc <stdlib.h> declarations are handled correctly
2 { dg-do compile }
3 { dg-options "-Wall" } */
5 #define A(...) __attribute__ ((malloc (__VA_ARGS__), noipa))
7 typedef __SIZE_TYPE__ size_t;
9 /* All functions with the same standard deallocator are associated
10 with each other. */
11 void free (void*);
12 void* calloc (size_t, size_t);
13 void* malloc (size_t);
14 void* realloc (void*, size_t);
16 A (__builtin_free) void* aligned_alloc (size_t, size_t);
18 /* Like realloc(), reallocarray() is both an allocator and a deallocator.
19 It must be associated with both free() and with itself, but nothing
20 else. */
21 A (__builtin_free) void* reallocarray (void*, size_t, size_t);
22 A (reallocarray) void* reallocarray (void*, size_t, size_t);
24 A (__builtin_free) extern char *canonicalize_file_name (const char*);
27 void dealloc (void*);
28 A (dealloc) void* alloc (size_t);
31 void sink (void*);
32 void* source (void);
35 void test_builtin_aligned_alloc (void *p)
38 void *q = __builtin_aligned_alloc (1, 2);
39 sink (q);
40 __builtin_free (q);
44 void *q = __builtin_aligned_alloc (1, 2);
45 sink (q);
46 free (q);
50 void *q = __builtin_aligned_alloc (1, 2);
51 q = __builtin_realloc (q, 3);
52 sink (q);
53 free (q);
57 void *q = __builtin_aligned_alloc (1, 2);
58 q = realloc (q, 3);
59 sink (q);
60 free (q);
64 void *q;
65 q = __builtin_aligned_alloc (1, 2); // { dg-message "returned from '__builtin_aligned_alloc'" }
66 sink (q);
67 dealloc (q); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
72 void test_aligned_alloc (void *p)
75 void *q = aligned_alloc (1, 2);
76 sink (q);
77 __builtin_free (q);
81 void *q = aligned_alloc (1, 2);
82 sink (q);
83 free (q);
87 void *q = aligned_alloc (1, 2);
88 q = __builtin_realloc (q, 3);
89 sink (q);
90 free (q);
94 void *q = aligned_alloc (1, 2);
95 q = realloc (q, 3);
96 sink (q);
97 free (q);
101 void *q = aligned_alloc (1, 2); // { dg-message "returned from 'aligned_alloc'" }
102 sink (q);
103 dealloc (q); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
108 void test_reallocarray (void *p)
111 void *q = __builtin_aligned_alloc (1, 2);
112 q = reallocarray (q, 2, 3);
113 sink (q);
114 free (q);
118 void *q = aligned_alloc (1, 2);
119 q = reallocarray (q, 2, 3);
120 sink (q);
121 free (q);
125 void *q = __builtin_calloc (1, 2);
126 q = reallocarray (q, 2, 3);
127 sink (q);
128 free (q);
132 void *q = calloc (1, 2);
133 q = reallocarray (q, 2, 3);
134 sink (q);
135 free (q);
139 void *q = __builtin_malloc (1);
140 q = reallocarray (q, 2, 3);
141 sink (q);
142 free (q);
146 void *q = malloc (1);
147 q = reallocarray (q, 2, 3);
148 sink (q);
149 free (q);
153 void *q = __builtin_realloc (p, 1);
154 q = reallocarray (q, 2, 3);
155 sink (q);
156 free (q);
160 p = source ();
161 void *q = realloc (p, 1);
162 q = reallocarray (q, 2, 3);
163 sink (q);
164 free (q);
168 void *q = __builtin_strdup ("abc");
169 q = reallocarray (q, 3, 4);
170 sink (q);
171 free (q);
175 void *q = __builtin_strndup ("abcd", 3);
176 q = reallocarray (q, 4, 5);
177 sink (q);
178 free (q);
182 void *q = source ();
183 q = reallocarray (q, 5, 6);
184 sink (q);
185 free (q);
189 void *q = alloc (1); // { dg-message "returned from 'alloc'" }
190 q = reallocarray (q, 6, 7); // { dg-warning "'reallocarray' called on pointer returned from a mismatched allocation function" }
191 sink (q);
192 free (q);
196 p = source ();
197 void *q = reallocarray (p, 7, 8);
198 q = __builtin_realloc (q, 9);
199 sink (q);
200 free (q);
204 p = source ();
205 void *q = reallocarray (p, 7, 8);
206 q = realloc (q, 9);
207 sink (q);
208 free (q);
212 p = source ();
213 void *q = reallocarray (p, 8, 9);
214 q = reallocarray (q, 3, 4);
215 sink (q);
216 free (q);
220 p = source ();
221 void *q = reallocarray (p, 9, 10);
222 q = reallocarray (q, 3, 4);
223 sink (q);
224 dealloc (q); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }
229 void test_canonicalize_filename (void *p)
232 void *q = canonicalize_file_name ("a");
233 sink (q);
234 __builtin_free (q);
238 void *q = canonicalize_file_name ("b");
239 sink (q);
240 free (q);
244 void *q = canonicalize_file_name ("c");
245 q = __builtin_realloc (q, 2);
246 sink (q);
247 free (q);
251 void *q = canonicalize_file_name ("d");
252 q = realloc (q, 3);
253 sink (q);
254 free (q);
258 void *q = canonicalize_file_name ("e");
259 q = reallocarray (q, 4, 5);
260 sink (q);
261 free (q);
265 void *q;
266 q = canonicalize_file_name ("f"); // { dg-message "returned from 'canonicalize_file_name'" }
267 sink (q);
268 dealloc (q); // { dg-warning "'dealloc' called on pointer returned from a mismatched allocation function" }