]> granicus.if.org Git - clang/commitdiff
Add test case for nonnull attribute.
authorTed Kremenek <kremenek@apple.com>
Mon, 21 Jul 2008 22:09:15 +0000 (22:09 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 21 Jul 2008 22:09:15 +0000 (22:09 +0000)
Fix indexing bug.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53882 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclAttr.cpp
test/Sema/nonnull.c [new file with mode: 0644]

index 3800fa3568178fc7c2c2572c5d2b92d86005438c..edf9a664e24b8bb849543596da0b313b6263de7f 100644 (file)
@@ -14,6 +14,7 @@
 #include "Sema.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/Basic/TargetInfo.h"
+#include <sstream>
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -266,10 +267,14 @@ static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
     unsigned x = (unsigned) ArgNum.getZExtValue();
         
     if (x < 1 || x > NumArgs) {
+      std::ostringstream os;
+      os << I.getArgNum();
       S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds,
-             "nonnull", Ex->getSourceRange());
+             "nonnull", os.str(), Ex->getSourceRange());
       return;
     }
+    
+    --x;
 
     // Is the function argument a pointer type?
     if (!proto->getArgType(x).getCanonicalType()->isPointerType()) {
diff --git a/test/Sema/nonnull.c b/test/Sema/nonnull.c
new file mode 100644 (file)
index 0000000..f8a2a0e
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: clang -fsyntax-only -verify %s
+
+int f1(int x) __attribute__((nonnull));
+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)));
+int f5(int *x, int *y) __attribute__ ((nonnull (2,1)));
+