[PATCH] D55212: Handle alloc_size attribute on function pointers

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 3 11:08:27 PST 2018


aaron.ballman added inline comments.


================
Comment at: include/clang/Basic/Attr.td:1072
 def AllocSize : InheritableAttr {
   let Spellings = [GCC<"alloc_size">];
+  let Subjects = SubjectList<[HasFunctionProto]>;
----------------
arichardson wrote:
> 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
Parsed and ignored is different than supported. For instance, I can't seem to get GCC to produce different behavior here: https://godbolt.org/z/MI5k_m

Am I missing something?


================
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}}
----------------
arichardson wrote:
> 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
Great, that's what I was hoping to hear. Can you add a C++ tests for both of those? The C test is a good start, but C++ is more finicky about type identity.


================
Comment at: test/Sema/alloc-size.c:48-49
+
+
+
+// We should not be warning when assigning function pointers with and without the alloc size attribute
----------------
Spurious newlines can be removed.


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