From a93d8a5d2c31d36239588b0427fccbd5fc277a33 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Fri, 17 Jan 2014 07:15:35 +0000 Subject: [PATCH] [analyzer] Teach NonNullParamChecker about 'nonnull' attributes on parameters. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@199473 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp | 8 +++++++- test/Analysis/nonnull.m | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp b/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp index 273a7a3882..bde902707e 100644 --- a/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp @@ -43,7 +43,7 @@ public: } // end anonymous namespace void NonNullParamChecker::checkPreCall(const CallEvent &Call, - CheckerContext &C) const { + CheckerContext &C) const { const Decl *FD = Call.getDecl(); if (!FD) return; @@ -66,6 +66,12 @@ void NonNullParamChecker::checkPreCall(const CallEvent &Call, } bool haveAttrNonNull = Att && Att->isNonNull(idx); + if (!haveAttrNonNull) { + // Check if the parameter is also marked 'nonnull'. + ArrayRef parms = Call.parameters(); + if (idx < parms.size()) + haveAttrNonNull = parms[idx]->hasAttr(); + } if (!haveRefTypeParam && !haveAttrNonNull) continue; diff --git a/test/Analysis/nonnull.m b/test/Analysis/nonnull.m index c32a7f780e..a4955ca0a4 100644 --- a/test/Analysis/nonnull.m +++ b/test/Analysis/nonnull.m @@ -2,6 +2,8 @@ @interface MyObject - (void)takePointer:(void *)ptr __attribute__((nonnull(1))); +- (void)takePointerArg:(void *)__attribute__((nonnull)) ptr; + @end void testNonNullMethod(int *p, MyObject *obj) { @@ -21,3 +23,10 @@ void testSubclass(int *p, Subclass *obj) { return; [obj takePointer:p]; // expected-warning{{nonnull}} } + +void testSubclassArg(int *p, Subclass *obj) { + if (p) + return; + [obj takePointerArg:p]; // expected-warning{{nonnull}} +} + -- 2.40.0