1 // RUN: %clang_cc1 -Wno-strict-prototypes %s -verify
3 void *fail1(int a
) __attribute__((alloc_size
)); //expected-error{{'alloc_size' attribute takes at least 1 argument}}
4 void *fail2(int a
) __attribute__((alloc_size())); //expected-error{{'alloc_size' attribute takes at least 1 argument}}
6 void *fail3(int a
) __attribute__((alloc_size(0))); //expected-error{{'alloc_size' attribute parameter 1 is out of bounds}}
7 void *fail4(int a
) __attribute__((alloc_size(2))); //expected-error{{'alloc_size' attribute parameter 1 is out of bounds}}
9 void *fail5(int a
, int b
) __attribute__((alloc_size(0, 1))); //expected-error{{'alloc_size' attribute parameter 1 is out of bounds}}
10 void *fail6(int a
, int b
) __attribute__((alloc_size(3, 1))); //expected-error{{'alloc_size' attribute parameter 1 is out of bounds}}
12 void *fail7(int a
, int b
) __attribute__((alloc_size(1, 0))); //expected-error{{'alloc_size' attribute parameter 2 is out of bounds}}
13 void *fail8(int a
, int b
) __attribute__((alloc_size(1, 3))); //expected-error{{'alloc_size' attribute parameter 2 is out of bounds}}
15 int fail9(int a
) __attribute__((alloc_size(1))); //expected-warning{{'alloc_size' attribute only applies to return values that are pointers}}
17 int fail10
__attribute__((alloc_size(1))); //expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
19 void *fail11(void *a
) __attribute__((alloc_size(1))); //expected-error{{'alloc_size' attribute argument may only refer to a function parameter of integer type}}
21 void *fail12(int a
) __attribute__((alloc_size("abc"))); //expected-error{{'alloc_size' attribute requires parameter 1 to be an integer constant}}
22 void *fail12(int a
) __attribute__((alloc_size(1, "abc"))); //expected-error{{'alloc_size' attribute requires parameter 2 to be an integer constant}}
23 void *fail13(int a
) __attribute__((alloc_size(1U<<31))); //expected-error{{integer constant expression evaluates to value 2147483648 that cannot be represented in a 32-bit signed integer type}}
25 void *(*PR31453
)(int)__attribute__((alloc_size(1)));
27 void *KR() __attribute__((alloc_size(1))); //expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
29 // Applying alloc_size to function pointers should work:
30 void *(__attribute__((alloc_size(1))) * func_ptr1
)(int);
31 void *(__attribute__((alloc_size(1, 2))) func_ptr2
)(int, int);
33 // TODO: according to GCC documentation the following should actually be the type
34 // “pointer to pointer to alloc_size attributed function returning void*” and should
35 // therefore be supported
36 void *(__attribute__((alloc_size(1))) **ptr_to_func_ptr
)(int); // expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
37 // The following definitions apply the attribute to the pointer to the function pointer which should not be possible
38 void *(*__attribute__((alloc_size(1))) * ptr_to_func_ptr2
)(int); // expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
39 void *(**__attribute__((alloc_size(1))) ptr_to_func_ptr2
)(int); // expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
41 // It should also work for typedefs:
42 typedef void *(__attribute__((alloc_size(1))) allocator_function_typdef
)(int);
43 typedef void *(__attribute__((alloc_size(1, 2))) * allocator_function_typdef2
)(int, int);
44 void *(__attribute__((alloc_size(1, 2))) * allocator_function_typdef3
)(int, int);
45 // This typedef applies the alloc_size to the pointer to the function pointer and should not be allowed
46 void *(**__attribute__((alloc_size(1, 2))) * allocator_function_typdef4
)(int, int); // expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
48 // We should not be warning when assigning function pointers with and without the alloc size attribute
49 // since it doesn't change the type of the function
50 typedef void *(__attribute__((alloc_size(1))) * my_malloc_fn_pointer_type
)(int);
51 typedef void *(*my_other_malloc_fn_pointer_type
)(int);
53 __attribute__((alloc_size(1))) void *fn2(int i
);
56 my_malloc_fn_pointer_type f
= fn
;
57 my_other_malloc_fn_pointer_type f2
= fn
;
58 my_malloc_fn_pointer_type f3
= fn2
;
59 my_other_malloc_fn_pointer_type f4
= fn2
;