From: Devin Coughlin Date: Fri, 15 Jan 2016 21:35:40 +0000 (+0000) Subject: [analyzer] Check for return of nil in ObjC methods with nonnull return type. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ebf2d81849a20fb009b64c3686be6b5eeb06a20d;p=clang [analyzer] Check for return of nil in ObjC methods with nonnull return type. Update NullabilityChecker so that it checks return statements in ObjC methods. Previously it was returning early because methods do not have a function type. Also update detection of violated parameter _Nonnull preconditions to handle ObjC methods. rdar://problem/24200560 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257938 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp b/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp index bb86ea401d..01c7287c97 100644 --- a/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp @@ -366,24 +366,20 @@ static bool checkPreconditionViolation(ProgramStateRef State, ExplodedNode *N, if (!D) return false; - if (const auto *BlockD = dyn_cast(D)) { - if (checkParamsForPreconditionViolation(BlockD->parameters(), State, - LocCtxt)) { - if (!N->isSink()) - C.addTransition(State->set(true), N); - return true; - } + ArrayRef Params; + if (const auto *BD = dyn_cast(D)) + Params = BD->parameters(); + else if (const auto *FD = dyn_cast(D)) + Params = FD->parameters(); + else if (const auto *MD = dyn_cast(D)) + Params = MD->parameters(); + else return false; - } - if (const auto *FuncDecl = dyn_cast(D)) { - if (checkParamsForPreconditionViolation(FuncDecl->parameters(), State, - LocCtxt)) { - if (!N->isSink()) - C.addTransition(State->set(true), N); - return true; - } - return false; + if (checkParamsForPreconditionViolation(Params, State, LocCtxt)) { + if (!N->isSink()) + C.addTransition(State->set(true), N); + return true; } return false; } @@ -484,16 +480,20 @@ void NullabilityChecker::checkPreStmt(const ReturnStmt *S, if (!RetSVal) return; + QualType RequiredRetType; AnalysisDeclContext *DeclCtxt = C.getLocationContext()->getAnalysisDeclContext(); - const FunctionType *FuncType = DeclCtxt->getDecl()->getFunctionType(); - if (!FuncType) + const Decl *D = DeclCtxt->getDecl(); + if (auto *MD = dyn_cast(D)) + RequiredRetType = MD->getReturnType(); + else if (auto *FD = dyn_cast(D)) + RequiredRetType = FD->getReturnType(); + else return; NullConstraint Nullness = getNullConstraint(*RetSVal, State); - Nullability RequiredNullability = - getNullabilityAnnotation(FuncType->getReturnType()); + Nullability RequiredNullability = getNullabilityAnnotation(RequiredRetType); // If the returned value is null but the type of the expression // generating it is nonnull then we will suppress the diagnostic. diff --git a/test/Analysis/nullability_nullonly.mm b/test/Analysis/nullability_nullonly.mm index 56b3f9e144..6479d67bda 100644 --- a/test/Analysis/nullability_nullonly.mm +++ b/test/Analysis/nullability_nullonly.mm @@ -1,5 +1,21 @@ // RUN: %clang_cc1 -analyze -analyzer-checker=core,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull -verify %s +#define nil 0 +#define BOOL int + +@protocol NSObject ++ (id)alloc; +- (id)init; +@end + +@protocol NSCopying +@end + +__attribute__((objc_root_class)) +@interface +NSObject +@end + int getRandom(); typedef struct Dummy { int val; } Dummy; @@ -85,3 +101,27 @@ Dummy *_Nonnull testDefensiveInlineChecks(Dummy * p) { takesNonnull(p); return p; } + + +@interface SomeClass : NSObject +@end + +@implementation SomeClass (MethodReturn) +- (SomeClass * _Nonnull)testReturnsNilInNonnull { + SomeClass *local = nil; + return local; // expected-warning {{Null is returned from a function that is expected to return a non-null value}} +} + +- (SomeClass * _Nonnull)testReturnsCastSuppressedNilInNonnull { + SomeClass *local = nil; + return (SomeClass * _Nonnull)local; // no-warning +} + +- (SomeClass * _Nonnull)testReturnsNilInNonnullWhenPreconditionViolated:(SomeClass * _Nonnull) p { + SomeClass *local = nil; + if (!p) // Pre-condition violated here. + return local; // no-warning + else + return p; // no-warning +} +@end