xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/attr-malloc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -verify -fsyntax-only %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -emit-llvm -o %t %s
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc #include <stddef.h>
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc // Declare malloc here explicitly so we don't depend on system headers.
7f4a2713aSLionel Sambuc void * malloc(size_t) __attribute((malloc));
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc int no_vars __attribute((malloc)); // expected-warning {{functions returning a pointer type}}
10f4a2713aSLionel Sambuc 
11f4a2713aSLionel Sambuc void  returns_void  (void) __attribute((malloc)); // expected-warning {{functions returning a pointer type}}
12f4a2713aSLionel Sambuc int   returns_int   (void) __attribute((malloc)); // expected-warning {{functions returning a pointer type}}
13f4a2713aSLionel Sambuc int * returns_intptr(void) __attribute((malloc)); // no-warning
14f4a2713aSLionel Sambuc typedef int * iptr;
15f4a2713aSLionel Sambuc iptr  returns_iptr  (void) __attribute((malloc)); // no-warning
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc __attribute((malloc)) void *(*f)(); //  expected-warning{{'malloc' attribute only applies to functions returning a pointer type}}
18f4a2713aSLionel Sambuc __attribute((malloc)) int (*g)(); // expected-warning{{'malloc' attribute only applies to functions returning a pointer type}}
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc __attribute((malloc))
xalloc(unsigned n)21f4a2713aSLionel Sambuc void * xalloc(unsigned n) { return malloc(n); } // no-warning
22f4a2713aSLionel Sambuc // RUN: grep 'define noalias .* @xalloc(' %t
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc #define malloc_like __attribute((__malloc__))
25f4a2713aSLionel Sambuc void * xalloc2(unsigned) malloc_like;
xalloc2(unsigned n)26f4a2713aSLionel Sambuc void * xalloc2(unsigned n) { return malloc(n); }
27f4a2713aSLionel Sambuc // RUN: grep 'define noalias .* @xalloc2(' %t
28f4a2713aSLionel Sambuc 
29