[PATCH] D55212: Handle alloc_size attribute on function pointers
Alexander Richardson via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 3 08:02:50 PST 2018
arichardson marked 5 inline comments as done.
arichardson added inline comments.
================
Comment at: include/clang/Basic/Attr.td:1072
def AllocSize : InheritableAttr {
let Spellings = [GCC<"alloc_size">];
+ let Subjects = SubjectList<[HasFunctionProto]>;
----------------
aaron.ballman wrote:
> Does GCC support writing `alloc_size` on function pointers?
Yes it does and it seems to be used by some projects. I first discovered this while compiling libxml2: https://github.com/GNOME/libxml2/blob/35e83488505d501864826125cfe6a7950d6cba78/include/libxml/xmlmemory.h#L66
================
Comment at: test/Sema/alloc-size.c:46
+// This typedef applies the alloc_size to the pointer to the function pointer and should not be allowed
+void *(**__attribute__((alloc_size(1, 2))) * allocator_function_typdef4)(int, int); // expected-warning{{'alloc_size' attribute only applies to non-K&R-style functions}}
----------------
aaron.ballman wrote:
> What should happen in these situations?
> ```
> typedef void * (__attribute__((alloc_size(1))) * my_malloc_fn_pointer_type)(int);
> typedef void * (* my_other_malloc_fn_pointer_type)(int);
>
> void *fn(int i);
> __attribute__((alloc_size(1))) void *fn2(int i);
>
> int main() {
> my_malloc_fn_pointer_type f = fn; // ???
> my_other_malloc_fn_pointer_type f2 = fn2; // ???
> }
> ```
> Should this code do something special?
> ```
> typedef void * (__attribute__((alloc_size(1))) * my_malloc_fn_pointer_type)(int);
> typedef void * (* my_other_malloc_fn_pointer_type)(int);
>
> void overloaded(my_malloc_fn_pointer_type fn);
> void overloaded(my_other_malloc_fn_pointer_type fn);
>
> void *fn(int i);
> __attribute__((alloc_size(1))) void *fn2(int i);
>
> int main() {
> overloaded(fn);
> overloaded(fn2);
> }
>
> ```
If I define two overloaded functions that only differ on the attribute GCC gives me the following error:
```
<source>:14:6: error: redefinition of 'overloaded'
14 | void overloaded(my_other_malloc_fn_pointer_type fn) {
| ^~~~~~~~~~
<source>:11:6: note: previous definition of 'overloaded' was here
11 | void overloaded(my_malloc_fn_pointer_type fn) {
| ^~~~~~~~~~
```
Assigning function pointers with and without the attribute seems to work just fine:
https://godbolt.org/z/-i5zUK
Repository:
rC Clang
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D55212/new/
https://reviews.llvm.org/D55212
More information about the cfe-commits
mailing list