[cfe-commits] r55610 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/SemaDeclAttr.cpp test/Analysis/null-deref-ps.c test/Sema/nonnull.c

Ted Kremenek kremenek at apple.com
Mon Sep 1 12:57:53 PDT 2008


Author: kremenek
Date: Mon Sep  1 14:57:52 2008
New Revision: 55610

URL: http://llvm.org/viewvc/llvm-project?rev=55610&view=rev
Log:
Tidy up sema processing of attribute "nonull":
- warn about nonnull being applied to functions with no pointer arguments
- continue processing argument list in the attribute when we encounter a non-pointer parameter being marked as nonnull
- when no argument list is specified, only mark pointers as nonnull.  This fixes PR 2732 and radar 6188814.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/test/Analysis/null-deref-ps.c
    cfe/trunk/test/Sema/nonnull.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=55610&r1=55609&r2=55610&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Sep  1 14:57:52 2008
@@ -692,7 +692,9 @@
      "type of machine mode does not match type of base type")
 DIAG(err_attr_wrong_decl, ERROR,
      "'%0' attribute invalid on this declaration, requires typedef or value")
-     
+DIAG(warn_attribute_nonnull_no_pointers, WARNING,
+  "'nonnull' attribute applied to function with no pointer arguments")
+  
 // Clang-Specific Attributes
 DIAG(err_attribute_iboutlet_non_ivar, ERROR,
      "'iboutlet' attribute can only be applied to instance variables")

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=55610&r1=55609&r2=55610&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Mon Sep  1 14:57:52 2008
@@ -282,20 +282,32 @@
       // FIXME: Should also highlight argument in decl.
       S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only,
              "nonnull", Ex->getSourceRange());
-      return;    
+      continue;
     }
     
     NonNullArgs.push_back(x);
   }
   
-  if (!NonNullArgs.empty()) {
-    unsigned* start = &NonNullArgs[0];
-    unsigned size = NonNullArgs.size();
-    std::sort(start, start + size);
-    d->addAttr(new NonNullAttr(start, size));
+  // If no arguments were specified to __attribute__((nonnull)) then all
+  // pointer arguments have a nonnull attribute.
+  if (NonNullArgs.empty()) {
+    unsigned idx = 0;
+    
+    for (FunctionTypeProto::arg_type_iterator
+         I=proto->arg_type_begin(), E=proto->arg_type_end(); I!=E; ++I, ++idx)
+      if ((*I)->isPointerType())
+        NonNullArgs.push_back(idx);
+    
+    if (NonNullArgs.empty()) {
+      S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
+      return;
+    }
   }
-  else
-    d->addAttr(new NonNullAttr());
+
+  unsigned* start = &NonNullArgs[0];
+  unsigned size = NonNullArgs.size();
+  std::sort(start, start + size);
+  d->addAttr(new NonNullAttr(start, size));
 }
 
 static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {

Modified: cfe/trunk/test/Analysis/null-deref-ps.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/null-deref-ps.c?rev=55610&r1=55609&r2=55610&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/null-deref-ps.c (original)
+++ cfe/trunk/test/Analysis/null-deref-ps.c Mon Sep  1 14:57:52 2008
@@ -56,11 +56,12 @@
   return s[0]; // no-warning
 }
 
-int bar(int* p) __attribute__((nonnull));
+int bar(int* p, int q) __attribute__((nonnull));
 
 int f6(int *p) { 
-  return !p ? bar(p) : *p; // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
-} 
+  return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
+         : bar(p, 0);   // no-warning
+}
 
 int* qux();
 

Modified: cfe/trunk/test/Sema/nonnull.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/nonnull.c?rev=55610&r1=55609&r2=55610&view=diff

==============================================================================
--- cfe/trunk/test/Sema/nonnull.c (original)
+++ cfe/trunk/test/Sema/nonnull.c Mon Sep  1 14:57:52 2008
@@ -1,6 +1,6 @@
 // RUN: clang -fsyntax-only -verify %s
 
-int f1(int x) __attribute__((nonnull));
+int f1(int x) __attribute__((nonnull)); // expected-warning{{'nonnull' attribute applied to function with no pointer arguments}}
 int f2(int *x) __attribute__ ((nonnull (1)));
 int f3(int *x) __attribute__ ((nonnull (0))); // expected-error {{'nonnull' attribute parameter 1 is out of bounds}}
 int f4(int *x, int *y) __attribute__ ((nonnull (1,2)));





More information about the cfe-commits mailing list