From: Ted Kremenek Date: Sat, 15 Aug 2009 00:51:46 +0000 (+0000) Subject: Change handling of attribute 'malloc' to only accept the attribute on function X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2cff7d16fe58e6d6447ec9cad2af083beb20d6b5;p=clang Change handling of attribute 'malloc' to only accept the attribute on function 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 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index fa03ddc8ce..2ebcfd36f5 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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 a pointer type">; def warn_transparent_union_nonpointer : Warning< "'transparent_union' attribute support incomplete; only supported for " "pointer unions">; diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 23fe4010fb..76b99bb33f 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -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(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, diff --git a/test/Sema/attr-malloc.c b/test/Sema/attr-malloc.c index 2ad71efebd..1adcf074a4 100644 --- a/test/Sema/attr-malloc.c +++ b/test/Sema/attr-malloc.c @@ -3,16 +3,16 @@ #include -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 a pointer type}} +int returns_int (void) __attribute((malloc)); // expected-warning {{functions returning a 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 a pointer type}} __attribute((malloc)) void * xalloc(unsigned n) { return malloc(n); } // no-warning diff --git a/test/SemaObjC/attr-malloc.m b/test/SemaObjC/attr-malloc.m index f18388f4e6..6cd6be00a8 100644 --- a/test/SemaObjC/attr-malloc.m +++ b/test/SemaObjC/attr-malloc.m @@ -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}}