From: Ted Kremenek Date: Fri, 14 Aug 2009 22:03:27 +0000 (+0000) Subject: Per Eli Friedman's feedback, handle attribute 'malloc' being applied to X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b109069995b44f3ef182bcd1b02ad05e9ea9d21d;p=clang Per Eli Friedman's feedback, handle attribute 'malloc' being applied to declarations of function pointers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79053 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index e65b3aa6fd..23fe4010fb 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -437,16 +437,16 @@ static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; return; } - - const FunctionDecl *FD = dyn_cast(d); - - if (!FD) { + + 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 = FD->getResultType(); + QualType RetTy = FT->getResultType(); if (!(RetTy->isAnyPointerType() || RetTy->isBlockPointerType())) { S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); diff --git a/test/Sema/attr-malloc.c b/test/Sema/attr-malloc.c index 8841d6993a..36e8b1d699 100644 --- a/test/Sema/attr-malloc.c +++ b/test/Sema/attr-malloc.c @@ -7,12 +7,14 @@ int no_vars __attribute((malloc)); // expected-warning {{only applies to functio 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)); +int * returns_intptr(void) __attribute((malloc)); // no-warning typedef int * iptr; -iptr returns_iptr (void) __attribute((malloc)); +iptr returns_iptr (void) __attribute((malloc)); // no-warning + +__attribute((malloc)) void *(*f)(); // no-warning __attribute((malloc)) -void * xalloc(unsigned n) { return malloc(n); } +void * xalloc(unsigned n) { return malloc(n); } // no-warning // RUN: grep 'define noalias .* @xalloc(' %t && #define malloc_like __attribute((__malloc__))