]> granicus.if.org Git - clang/commitdiff
Change handling of attribute 'malloc' to only accept the attribute on function
authorTed Kremenek <kremenek@apple.com>
Sat, 15 Aug 2009 00:51:46 +0000 (00:51 +0000)
committerTed Kremenek <kremenek@apple.com>
Sat, 15 Aug 2009 00:51:46 +0000 (00:51 +0000)
declarations (and not function pointers). This is consistent with GCC. Accepting
this attribute on function pointers means that the attribute should be treated
as a type qualifier, which apparently is not what GCC does. We obviously can
change this later should we desire to enhance the 'malloc' attribute in this
way.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79060 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/attr-malloc.c
test/SemaObjC/attr-malloc.m

index fa03ddc8ce58eb12037937d096604b2e427c0bd9..2ebcfd36f5b9cab67937e82de83f840ced0fdf98 100644 (file)
@@ -612,7 +612,7 @@ def err_attr_wrong_decl : Error<
 def warn_attribute_nonnull_no_pointers : Warning<
   "'nonnull' attribute applied to function with no pointer arguments">;
 def warn_attribute_malloc_pointer_only : Warning<
-  "'malloc' attribute only applies to functions returning pointer type">;
+  "'malloc' attribute only applies to functions returning pointer type">;
 def warn_transparent_union_nonpointer : Warning<
   "'transparent_union' attribute support incomplete; only supported for "
   "pointer unions">;
index 23fe4010fb0b6cda1739861b421d5eb95fa29e4c..76b99bb33f13ec56b84ff8afa7907c76e959435d 100644 (file)
@@ -438,22 +438,15 @@ static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
     return;
   }
   
-  const FunctionType *FT = getFunctionType(d, false);
-  
-  if (!FT) {
-    S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
-      << Attr.getName() << 0 /*function*/;
-    return;
-  }
-
-  QualType RetTy = FT->getResultType();
-  
-  if (!(RetTy->isAnyPointerType() || RetTy->isBlockPointerType())) {
-    S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
-    return;
+  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
+    QualType RetTy = FD->getResultType();  
+    if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
+      d->addAttr(::new (S.Context) MallocAttr());
+      return;
+    }
   }
 
-  d->addAttr(::new (S.Context) MallocAttr());
+  S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
 }
 
 static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
index 2ad71efebd04e4210f5be799eb9e24d56f72798a..1adcf074a499a320b3537a415edbd3449715472b 100644 (file)
@@ -3,16 +3,16 @@
 
 #include <stdlib.h>
 
-int no_vars __attribute((malloc)); // expected-warning {{only applies to function types}}
+int no_vars __attribute((malloc)); // expected-warning {{functions returning a pointer type}}
 
-void  returns_void  (void) __attribute((malloc)); // expected-warning {{functions returning pointer type}}
-int   returns_int   (void) __attribute((malloc)); // expected-warning {{functions returning pointer type}}
+void  returns_void  (void) __attribute((malloc)); // expected-warning {{functions returning pointer type}}
+int   returns_int   (void) __attribute((malloc)); // expected-warning {{functions returning pointer type}}
 int * returns_intptr(void) __attribute((malloc)); // no-warning
 typedef int * iptr;
 iptr  returns_iptr  (void) __attribute((malloc)); // no-warning
 
-__attribute((malloc)) void *(*f)(); // no-warning
-__attribute((malloc)) int (*g)(); // expected-warning{{'malloc' attribute only applies to functions returning pointer type}}
+__attribute((malloc)) void *(*f)(); //  expected-warning{{'malloc' attribute only applies to functions returning a pointer type}}
+__attribute((malloc)) int (*g)(); // expected-warning{{'malloc' attribute only applies to functions returning pointer type}}
 
 __attribute((malloc))
 void * xalloc(unsigned n) { return malloc(n); } // no-warning
index f18388f4e64c2e602f50d89d179e840e3b1de2f0..6cd6be00a8cf1bd0a32fa141367ae02a0e5e7738 100644 (file)
@@ -1,8 +1,8 @@
 // RUN: clang-cc -verify -fsyntax-only -fblocks %s
 
 @interface TestAttrMallocOnMethods {}
-- (id) test1 __attribute((malloc)); // expected-warning{{'malloc' attribute only applies to function types}}
-- (int) test2 __attribute((malloc)); // expected-warning{{'malloc' attribute only applies to function types}}
+- (id) test1 __attribute((malloc)); //  expected-warning {{functions returning a pointer type}}
+- (int) test2 __attribute((malloc)); //  expected-warning {{functions returning a pointer type}}
 @end
 
 id bar(void) __attribute((malloc)); // no-warning
@@ -10,6 +10,7 @@ id bar(void) __attribute((malloc)); // no-warning
 typedef void (^bptr)(void);
 bptr baz(void) __attribute((malloc)); // no-warning
 
-__attribute((malloc)) id (*f)(); // no-warning
-__attribute((malloc)) bptr (*g)(); // no-warning
+__attribute((malloc)) id (*f)(); //  expected-warning {{functions returning a pointer type}}
+__attribute((malloc)) bptr (*g)(); //  expected-warning {{functions returning a pointer type}}
+__attribute((malloc)) void *(^h)(); //  expected-warning {{functions returning a pointer type}}