[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 06:12:43 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]>;
----------------
Does GCC support writing `alloc_size` on function pointers?


================
Comment at: lib/Sema/SemaDecl.cpp:6269
+static void copyAttrFromTypedefToDecl(Sema &S, Decl *D, const TypedefType *TT) {
+  TypedefNameDecl *TND = TT->getDecl();
+  if (AttrTy *Attribute = TND->getAttr<AttrTy>()) {
----------------
`const TypedefNameDecl *` ?


================
Comment at: lib/Sema/SemaDecl.cpp:6270
+  TypedefNameDecl *TND = TT->getDecl();
+  if (AttrTy *Attribute = TND->getAttr<AttrTy>()) {
+    AttrTy *Clone = Attribute->clone(S.Context);
----------------
`const auto *`


================
Comment at: lib/Sema/SemaDecl.cpp:6723
+  if (R->isFunctionPointerType()) {
+    if (auto TT = R->getAs<TypedefType>()) {
+      copyAttrFromTypedefToDecl<AllocSizeAttr>(*this, NewVD, TT);
----------------
`const auto *` and can elide the braces.

This should probably be generalized at some point into something declarative in Attr.td, though the type checking may be hard to do that for (I'm not certain). It doesn't need to be done as part of this patch, though.


================
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}}
----------------
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);
}

```


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