diff options
author | Ryan Flynn <pizza@parseerror.com> | 2009-08-09 22:36:29 +0000 |
---|---|---|
committer | Ryan Flynn <pizza@parseerror.com> | 2009-08-09 22:36:29 +0000 |
commit | fd6ad3cf9c8fc6904bd5f33212207aa69743fd45 (patch) | |
tree | 4c3454a4bcb8731ac5ca025d8fb51e998f03e2a5 | |
parent | 76168e289ca4b307259e3bc9b3353f03b05bb6b9 (diff) |
warn, as gcc does, if __attribute__((malloc)) applied to function returning non-pointer type
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78542 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 2 | ||||
-rw-r--r-- | lib/Sema/SemaDeclAttr.cpp | 7 | ||||
-rw-r--r-- | test/Sema/attr-malloc.c | 6 |
3 files changed, 15 insertions, 0 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 39bbe4e481..cc447385f5 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -605,6 +605,8 @@ def err_attr_wrong_decl : Error< "'%0' attribute invalid on this declaration, requires typedef or value">; 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">; 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 2f86fe1ffe..9d1b0849ef 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -437,6 +437,13 @@ static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { return; } + if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { + if (!FD->getResultType()->isPointerType()) { + S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); + return; + } + } + d->addAttr(::new (S.Context) MallocAttr()); } diff --git a/test/Sema/attr-malloc.c b/test/Sema/attr-malloc.c index 8603cc05a5..5b59bd5058 100644 --- a/test/Sema/attr-malloc.c +++ b/test/Sema/attr-malloc.c @@ -5,6 +5,12 @@ int no_vars __attribute((malloc)); // expected-warning {{only applies to function types}} +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)); +typedef int * iptr; +iptr returns_iptr (void) __attribute((malloc)); + __attribute((malloc)) void * xalloc(unsigned n) { return malloc(n); } // RUN: grep 'define noalias .* @xalloc(' %t && |