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