From f22c410efa1ec345881996ebf987a83da4552803 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Fri, 14 Aug 2009 22:03:27 +0000 Subject: [PATCH] Per Eli Friedman's feedback, handle attribute 'malloc' being applied to declarations of function pointers. llvm-svn: 79053 --- clang/lib/Sema/SemaDeclAttr.cpp | 10 +++++----- clang/test/Sema/attr-malloc.c | 8 +++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index e65b3aa6fdfc..23fe4010fb0b 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/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/clang/test/Sema/attr-malloc.c b/clang/test/Sema/attr-malloc.c index 8841d6993a20..36e8b1d69947 100644 --- a/clang/test/Sema/attr-malloc.c +++ b/clang/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__))