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