Handle alloc_size attribute on function pointers
authorAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Thu, 6 Dec 2018 12:22:08 +0000 (12:22 +0000)
committerAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Fri, 9 Apr 2021 17:49:38 +0000 (18:49 +0100)
commitdc4abca7662b74467c2b6bceb7e8b046ae04b8a0
tree0695b60b450da5c3e5759044cdd4366068665285
parented0bf875a93e184c234510717b22fb4c8367ed82
Handle alloc_size attribute on function pointers

I have been trying to statically find and analyze all calls to heap
allocation functions to determine how many of them use sizes known at
compile time vs only at runtime. While doing so I saw that quite a few
projects use replaceable function pointers for heap allocation and noticed
that clang was not able to annotate functions pointers with alloc_size.
I have changed the Sema checks to allow alloc_size on all function pointers
and typedefs for function pointers now and added checks that these
attributes are propagated to the LLVM IR correctly.

With this patch we can also compute __builtin_object_size() for calls to
allocation function pointers with the alloc_size attribute.

Reviewed By: aaron.ballman, erik.pilkington
Differential Revision: https://reviews.llvm.org/D55212
clang/include/clang/Basic/Attr.td
clang/lib/AST/ExprConstant.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclAttr.cpp
clang/test/CodeGen/alloc-size-fnptr.c [new file with mode: 0644]
clang/test/CodeGen/alloc-size.c
clang/test/Misc/pragma-attribute-supported-attributes-list.test
clang/test/Sema/alloc-size.c