fix codegen support for alloc_size attribute for static C++ methods
authorNuno Lopes <nunoplopes@sapo.pt>
Fri, 25 May 2012 21:45:08 +0000 (21:45 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Fri, 25 May 2012 21:45:08 +0000 (21:45 +0000)
add test case for C++ codegen

llvm-svn: 157500

clang/lib/CodeGen/CGCall.cpp
clang/test/CodeGenCXX/alloc_size.cpp [new file with mode: 0644]

index 650fe8b..cd01fa2 100644 (file)
@@ -2090,9 +2090,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
   // add metadata for __attribute__((alloc_size(foo)))
   if (TargetDecl) {
     if (const AllocSizeAttr* Attr = TargetDecl->getAttr<AllocSizeAttr>()) {
-      std::vector<llvm::Value*> Args;
+      SmallVector<llvm::Value*, 4> Args;
       llvm::IntegerType *Ty = llvm::IntegerType::getInt32Ty(getLLVMContext());
-      bool isMethod = isa<CXXMethodDecl>(TargetDecl);
+      bool isMethod = false;
+      if (const CXXMethodDecl *MDecl = dyn_cast<CXXMethodDecl>(TargetDecl))
+        isMethod = MDecl->isInstance();
 
       for (AllocSizeAttr::args_iterator I = Attr->args_begin(),
            E = Attr->args_end(); I != E; ++I) {
diff --git a/clang/test/CodeGenCXX/alloc_size.cpp b/clang/test/CodeGenCXX/alloc_size.cpp
new file mode 100644 (file)
index 0000000..90273cc
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+struct foo {
+  void *my_alloc(unsigned) __attribute__((alloc_size(2)));
+  static void* static_alloc(unsigned) __attribute__((alloc_size(1)));
+};
+
+
+void* f(bool a) {
+  // CHECK: call i8* {{.*}}alloc{{.*}}, !alloc_size !0
+  // CHECK: call i8* {{.*}}static_alloc{{.*}}, !alloc_size !1
+  foo obj;
+  return a ? obj.my_alloc(2) :
+             foo::static_alloc(42);
+}
+
+// CHECK: !0 = metadata !{i32 1}
+// CHECK: !1 = metadata !{i32 0}