]> granicus.if.org Git - clang/commitdiff
warn, as gcc does, if __attribute__((malloc)) applied to function returning non-point...
authorRyan Flynn <pizza@parseerror.com>
Sun, 9 Aug 2009 22:36:29 +0000 (22:36 +0000)
committerRyan Flynn <pizza@parseerror.com>
Sun, 9 Aug 2009 22:36:29 +0000 (22:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78542 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 39bbe4e481621e6d90911d7756b3b2bd55d66d27..cc447385f5e286f9b43c0dd6e85cc1867699f8e4 100644 (file)
@@ -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">;
index 2f86fe1ffe702d79ab9612a0a1ea651632499054..9d1b0849eff17ea2a87caca21e2db41655a01036 100644 (file)
@@ -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());
 }
 
index 8603cc05a54d90bb77e806ce0bae2d3d79c12b4b..5b59bd50580d715fc7e99fff4ca20dc0f0440826 100644 (file)
@@ -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 &&