From 2813420dd3be04fc0c44074c3f1af1aada83ad99 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Fri, 25 May 2012 17:04:42 +0000 Subject: [PATCH] add CodeGen support for the alloc_size attribute git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@157483 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCall.cpp | 17 +++++++++++++++++ test/CodeGen/alloc_size.c | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/CodeGen/alloc_size.c diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 3ad1df1782..650fe8b623 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -2087,6 +2087,23 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, CS.setAttributes(Attrs); CS.setCallingConv(static_cast(CallingConv)); + // add metadata for __attribute__((alloc_size(foo))) + if (TargetDecl) { + if (const AllocSizeAttr* Attr = TargetDecl->getAttr()) { + std::vector Args; + llvm::IntegerType *Ty = llvm::IntegerType::getInt32Ty(getLLVMContext()); + bool isMethod = isa(TargetDecl); + + for (AllocSizeAttr::args_iterator I = Attr->args_begin(), + E = Attr->args_end(); I != E; ++I) { + Args.push_back(llvm::ConstantInt::get(Ty, *I + isMethod)); + } + + llvm::MDNode *MD = llvm::MDNode::get(getLLVMContext(), Args); + CS.getInstruction()->setMetadata("alloc_size", MD); + } + } + // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC // optimizer it can aggressively ignore unwind edges. if (CGM.getLangOpts().ObjCAutoRefCount) diff --git a/test/CodeGen/alloc_size.c b/test/CodeGen/alloc_size.c new file mode 100644 index 0000000000..05aa8456ee --- /dev/null +++ b/test/CodeGen/alloc_size.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s + +void *my_recalloc(void *, unsigned, unsigned) __attribute__((alloc_size(2,3))); + +// CHECK: @f +void* f() { + // CHECK: call i8* @my_recalloc{{.*}}, !alloc_size !0 + return my_recalloc(0, 11, 27); +} + +// CHECK: !0 = metadata !{i32 1, i32 2} -- 2.40.0