2008-03-27 15:25:52 +08:00
|
|
|
//== BasicObjCFoundationChecks.cpp - Simple Apple-Foundation checks -*- C++ -*--
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines BasicObjCFoundationChecks, a class that encapsulates
|
|
|
|
// a set of simple checks to run on Objective-C code using Apple's Foundation
|
|
|
|
// classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-16 09:40:52 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-17 12:01:35 +08:00
|
|
|
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2012-07-03 03:28:04 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Calls.h"
|
2011-02-23 09:05:36 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-03-27 15:25:52 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2008-05-30 05:12:08 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2012-06-12 00:40:41 +08:00
|
|
|
#include "clang/AST/StmtObjC.h"
|
2008-03-27 15:25:52 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-06-12 00:40:37 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2008-03-27 15:25:52 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-04-04 01:57:38 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
namespace {
|
|
|
|
class APIMisuse : public BugType {
|
|
|
|
public:
|
|
|
|
APIMisuse(const char* name) : BugType(name, "API Misuse (Apple)") {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
static StringRef GetReceiverInterfaceName(const ObjCMethodCall &msg) {
|
2011-01-25 08:03:53 +08:00
|
|
|
if (const ObjCInterfaceDecl *ID = msg.getReceiverInterface())
|
2012-07-03 03:28:04 +08:00
|
|
|
return ID->getIdentifier()->getName();
|
|
|
|
return StringRef();
|
2008-04-04 01:57:38 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 00:40:37 +08:00
|
|
|
enum FoundationClass {
|
|
|
|
FC_None,
|
|
|
|
FC_NSArray,
|
|
|
|
FC_NSDictionary,
|
|
|
|
FC_NSEnumerator,
|
|
|
|
FC_NSOrderedSet,
|
|
|
|
FC_NSSet,
|
|
|
|
FC_NSString
|
|
|
|
};
|
|
|
|
|
|
|
|
static FoundationClass findKnownClass(const ObjCInterfaceDecl *ID) {
|
|
|
|
static llvm::StringMap<FoundationClass> Classes;
|
|
|
|
if (Classes.empty()) {
|
|
|
|
Classes["NSArray"] = FC_NSArray;
|
|
|
|
Classes["NSDictionary"] = FC_NSDictionary;
|
|
|
|
Classes["NSEnumerator"] = FC_NSEnumerator;
|
|
|
|
Classes["NSOrderedSet"] = FC_NSOrderedSet;
|
|
|
|
Classes["NSSet"] = FC_NSSet;
|
|
|
|
Classes["NSString"] = FC_NSString;
|
|
|
|
}
|
2011-03-09 04:05:26 +08:00
|
|
|
|
2012-06-12 00:40:37 +08:00
|
|
|
// FIXME: Should we cache this at all?
|
|
|
|
FoundationClass result = Classes.lookup(ID->getIdentifier()->getName());
|
|
|
|
if (result == FC_None)
|
|
|
|
if (const ObjCInterfaceDecl *Super = ID->getSuperClass())
|
|
|
|
return findKnownClass(Super);
|
2008-04-04 01:57:38 +08:00
|
|
|
|
2012-06-12 00:40:37 +08:00
|
|
|
return result;
|
2010-10-21 07:38:56 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
static inline bool isNil(SVal X) {
|
|
|
|
return isa<loc::ConcreteInt>(X);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NilArgChecker - Check for prohibited nil arguments to ObjC method calls.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2010-10-23 00:33:16 +08:00
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class NilArgChecker : public Checker<check::PreObjCMessage> {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<APIMisuse> BT;
|
2011-02-23 08:16:10 +08:00
|
|
|
|
|
|
|
void WarnNilArg(CheckerContext &C,
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &msg, unsigned Arg) const;
|
2011-02-23 08:16:10 +08:00
|
|
|
|
2010-10-23 00:33:16 +08:00
|
|
|
public:
|
2012-07-03 03:28:04 +08:00
|
|
|
void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
|
2010-10-23 00:33:16 +08:00
|
|
|
};
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
void NilArgChecker::WarnNilArg(CheckerContext &C,
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &msg,
|
2011-02-23 08:16:10 +08:00
|
|
|
unsigned int Arg) const
|
2010-10-21 07:38:56 +08:00
|
|
|
{
|
|
|
|
if (!BT)
|
2011-02-23 08:16:10 +08:00
|
|
|
BT.reset(new APIMisuse("nil argument"));
|
2010-10-21 07:38:56 +08:00
|
|
|
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> sbuf;
|
2010-10-21 07:38:56 +08:00
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
2012-07-03 03:28:04 +08:00
|
|
|
os << "Argument to '" << GetReceiverInterfaceName(msg) << "' method '"
|
2011-01-25 08:03:53 +08:00
|
|
|
<< msg.getSelector().getAsString() << "' cannot be nil";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *R = new BugReport(*BT, os.str(), N);
|
2011-01-25 08:03:53 +08:00
|
|
|
R->addRange(msg.getArgSourceRange(Arg));
|
2010-10-21 07:38:56 +08:00
|
|
|
C.EmitReport(R);
|
2008-04-04 01:57:38 +08:00
|
|
|
}
|
2008-03-28 01:17:22 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
void NilArgChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
|
2011-02-23 08:16:10 +08:00
|
|
|
CheckerContext &C) const {
|
2011-03-09 04:05:26 +08:00
|
|
|
const ObjCInterfaceDecl *ID = msg.getReceiverInterface();
|
|
|
|
if (!ID)
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
|
|
|
|
2012-06-12 00:40:37 +08:00
|
|
|
if (findKnownClass(ID) == FC_NSString) {
|
2011-01-25 08:03:53 +08:00
|
|
|
Selector S = msg.getSelector();
|
2010-10-21 07:38:56 +08:00
|
|
|
|
|
|
|
if (S.isUnarySelector())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: This is going to be really slow doing these checks with
|
|
|
|
// lexical comparisons.
|
|
|
|
|
|
|
|
std::string NameStr = S.getAsString();
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name(NameStr);
|
2010-10-21 07:38:56 +08:00
|
|
|
assert(!Name.empty());
|
|
|
|
|
|
|
|
// FIXME: Checking for initWithFormat: will not work in most cases
|
|
|
|
// yet because [NSString alloc] returns id, not NSString*. We will
|
|
|
|
// need support for tracking expected-type information in the analyzer
|
|
|
|
// to find these errors.
|
|
|
|
if (Name == "caseInsensitiveCompare:" ||
|
|
|
|
Name == "compare:" ||
|
|
|
|
Name == "compare:options:" ||
|
|
|
|
Name == "compare:options:range:" ||
|
|
|
|
Name == "compare:options:range:locale:" ||
|
|
|
|
Name == "componentsSeparatedByCharactersInSet:" ||
|
|
|
|
Name == "initWithFormat:") {
|
2012-07-03 03:28:04 +08:00
|
|
|
if (isNil(msg.getArgSVal(0)))
|
2011-01-25 08:03:53 +08:00
|
|
|
WarnNilArg(C, msg, 0);
|
2010-10-21 07:38:56 +08:00
|
|
|
}
|
2008-03-28 06:05:32 +08:00
|
|
|
}
|
2008-03-27 15:25:52 +08:00
|
|
|
}
|
2008-06-27 07:59:48 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error reporting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class CFNumberCreateChecker : public Checker< check::PreStmt<CallExpr> > {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<APIMisuse> BT;
|
2011-02-23 08:16:10 +08:00
|
|
|
mutable IdentifierInfo* II;
|
2008-06-27 07:59:48 +08:00
|
|
|
public:
|
2011-02-23 08:16:10 +08:00
|
|
|
CFNumberCreateChecker() : II(0) {}
|
|
|
|
|
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
private:
|
2011-08-13 07:37:29 +08:00
|
|
|
void EmitError(const TypedRegion* R, const Expr *Ex,
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t SourceSize, uint64_t TargetSize, uint64_t NumberKind);
|
2008-06-27 07:59:48 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
enum CFNumberType {
|
|
|
|
kCFNumberSInt8Type = 1,
|
|
|
|
kCFNumberSInt16Type = 2,
|
|
|
|
kCFNumberSInt32Type = 3,
|
|
|
|
kCFNumberSInt64Type = 4,
|
|
|
|
kCFNumberFloat32Type = 5,
|
|
|
|
kCFNumberFloat64Type = 6,
|
|
|
|
kCFNumberCharType = 7,
|
|
|
|
kCFNumberShortType = 8,
|
|
|
|
kCFNumberIntType = 9,
|
|
|
|
kCFNumberLongType = 10,
|
|
|
|
kCFNumberLongLongType = 11,
|
|
|
|
kCFNumberFloatType = 12,
|
|
|
|
kCFNumberDoubleType = 13,
|
|
|
|
kCFNumberCFIndexType = 14,
|
|
|
|
kCFNumberNSIntegerType = 15,
|
|
|
|
kCFNumberCGFloatType = 16
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
template<typename T>
|
|
|
|
class Optional {
|
|
|
|
bool IsKnown;
|
|
|
|
T Val;
|
|
|
|
public:
|
|
|
|
Optional() : IsKnown(false), Val(0) {}
|
|
|
|
Optional(const T& val) : IsKnown(true), Val(val) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
bool isKnown() const { return IsKnown; }
|
|
|
|
|
|
|
|
const T& getValue() const {
|
|
|
|
assert (isKnown());
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const T&() const {
|
|
|
|
return getValue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
static Optional<uint64_t> GetCFNumberSize(ASTContext &Ctx, uint64_t i) {
|
2009-12-24 01:49:57 +08:00
|
|
|
static const unsigned char FixedSize[] = { 8, 16, 32, 64, 32, 64 };
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
if (i < kCFNumberCharType)
|
|
|
|
return FixedSize[i-1];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
QualType T;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
switch (i) {
|
|
|
|
case kCFNumberCharType: T = Ctx.CharTy; break;
|
|
|
|
case kCFNumberShortType: T = Ctx.ShortTy; break;
|
|
|
|
case kCFNumberIntType: T = Ctx.IntTy; break;
|
|
|
|
case kCFNumberLongType: T = Ctx.LongTy; break;
|
|
|
|
case kCFNumberLongLongType: T = Ctx.LongLongTy; break;
|
|
|
|
case kCFNumberFloatType: T = Ctx.FloatTy; break;
|
|
|
|
case kCFNumberDoubleType: T = Ctx.DoubleTy; break;
|
|
|
|
case kCFNumberCFIndexType:
|
|
|
|
case kCFNumberNSIntegerType:
|
|
|
|
case kCFNumberCGFloatType:
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: We need a way to map from names to Type*.
|
2008-06-27 07:59:48 +08:00
|
|
|
default:
|
|
|
|
return Optional<uint64_t>();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
return Ctx.getTypeSize(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static const char* GetCFNumberTypeStr(uint64_t i) {
|
|
|
|
static const char* Names[] = {
|
|
|
|
"kCFNumberSInt8Type",
|
|
|
|
"kCFNumberSInt16Type",
|
|
|
|
"kCFNumberSInt32Type",
|
|
|
|
"kCFNumberSInt64Type",
|
|
|
|
"kCFNumberFloat32Type",
|
|
|
|
"kCFNumberFloat64Type",
|
|
|
|
"kCFNumberCharType",
|
|
|
|
"kCFNumberShortType",
|
|
|
|
"kCFNumberIntType",
|
|
|
|
"kCFNumberLongType",
|
|
|
|
"kCFNumberLongLongType",
|
|
|
|
"kCFNumberFloatType",
|
|
|
|
"kCFNumberDoubleType",
|
|
|
|
"kCFNumberCFIndexType",
|
|
|
|
"kCFNumberNSIntegerType",
|
|
|
|
"kCFNumberCGFloatType"
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
return i <= kCFNumberCGFloatType ? Names[i-1] : "Invalid CFNumberType";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-23 08:16:10 +08:00
|
|
|
void CFNumberCreateChecker::checkPreStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2010-10-21 07:38:56 +08:00
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
if (!II)
|
|
|
|
II = &Ctx.Idents.get("CFNumberCreate");
|
|
|
|
|
|
|
|
if (FD->getIdentifier() != II || CE->getNumArgs() != 3)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
// Get the value of the "theType" argument.
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
|
|
|
SVal TheTypeVal = state->getSVal(CE->getArg(1), LCtx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
// FIXME: We really should allow ranges of valid theType values, and
|
|
|
|
// bifurcate the state appropriately.
|
2008-10-17 13:57:07 +08:00
|
|
|
nonloc::ConcreteInt* V = dyn_cast<nonloc::ConcreteInt>(&TheTypeVal);
|
2008-06-27 07:59:48 +08:00
|
|
|
if (!V)
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
uint64_t NumberKind = V->getValue().getLimitedValue();
|
|
|
|
Optional<uint64_t> TargetSize = GetCFNumberSize(Ctx, NumberKind);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
// FIXME: In some cases we can emit an error.
|
|
|
|
if (!TargetSize.isKnown())
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
// Look at the value of the integer being passed by reference. Essentially
|
|
|
|
// we want to catch cases where the value passed in is not equal to the
|
|
|
|
// size of the type being created.
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal TheValueExpr = state->getSVal(CE->getArg(2), LCtx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
// FIXME: Eventually we should handle arbitrary locations. We can do this
|
|
|
|
// by having an enhanced memory model that does low-level typing.
|
2008-10-17 13:57:07 +08:00
|
|
|
loc::MemRegionVal* LV = dyn_cast<loc::MemRegionVal>(&TheValueExpr);
|
2008-06-27 07:59:48 +08:00
|
|
|
if (!LV)
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-08-13 04:02:48 +08:00
|
|
|
const TypedValueRegion* R = dyn_cast<TypedValueRegion>(LV->stripCasts());
|
2009-07-30 02:17:40 +08:00
|
|
|
if (!R)
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
2009-07-30 02:17:40 +08:00
|
|
|
|
2010-08-11 14:10:55 +08:00
|
|
|
QualType T = Ctx.getCanonicalType(R->getValueType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
// FIXME: If the pointee isn't an integer type, should we flag a warning?
|
|
|
|
// People can do weird stuff with pointers.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (!T->isIntegerType())
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
uint64_t SourceSize = Ctx.getTypeSize(T);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-27 07:59:48 +08:00
|
|
|
// CHECK: is SourceSize == TargetSize
|
|
|
|
if (SourceSize == TargetSize)
|
2010-10-21 07:38:56 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
// Generate an error. Only generate a sink if 'SourceSize < TargetSize';
|
|
|
|
// otherwise generate a regular node.
|
|
|
|
//
|
2008-06-27 07:59:48 +08:00
|
|
|
// FIXME: We can actually create an abstract "CFNumber" object that has
|
|
|
|
// the bits initialized to the provided values.
|
2010-10-21 07:38:56 +08:00
|
|
|
//
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = SourceSize < TargetSize ? C.generateSink()
|
2011-10-27 05:06:34 +08:00
|
|
|
: C.addTransition()) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> sbuf;
|
2010-10-21 07:38:56 +08:00
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
|
|
|
|
|
|
|
os << (SourceSize == 8 ? "An " : "A ")
|
|
|
|
<< SourceSize << " bit integer is used to initialize a CFNumber "
|
|
|
|
"object that represents "
|
|
|
|
<< (TargetSize == 8 ? "an " : "a ")
|
|
|
|
<< TargetSize << " bit integer. ";
|
|
|
|
|
|
|
|
if (SourceSize < TargetSize)
|
|
|
|
os << (TargetSize - SourceSize)
|
|
|
|
<< " bits of the CFNumber value will be garbage." ;
|
|
|
|
else
|
|
|
|
os << (SourceSize - TargetSize)
|
|
|
|
<< " bits of the input integer will be lost.";
|
2008-06-27 07:59:48 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
if (!BT)
|
2011-02-23 08:16:10 +08:00
|
|
|
BT.reset(new APIMisuse("Bad use of CFNumberCreate"));
|
2010-10-21 07:38:56 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT, os.str(), N);
|
2010-10-21 07:38:56 +08:00
|
|
|
report->addRange(CE->getArg(2)->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
2008-06-27 07:59:48 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 08:43:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-06 10:34:42 +08:00
|
|
|
// CFRetain/CFRelease checking for null arguments.
|
2009-07-14 08:43:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class CFRetainReleaseChecker : public Checker< check::PreStmt<CallExpr> > {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<APIMisuse> BT;
|
2011-02-23 08:16:10 +08:00
|
|
|
mutable IdentifierInfo *Retain, *Release;
|
2009-07-14 08:43:42 +08:00
|
|
|
public:
|
2011-02-23 08:16:10 +08:00
|
|
|
CFRetainReleaseChecker(): Retain(0), Release(0) {}
|
2011-08-13 07:37:29 +08:00
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
2009-07-14 08:43:42 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void CFRetainReleaseChecker::checkPreStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
2009-07-14 08:43:42 +08:00
|
|
|
// If the CallExpr doesn't have exactly 1 argument just give up checking.
|
|
|
|
if (CE->getNumArgs() != 1)
|
2010-07-06 10:34:42 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-12-01 13:57:37 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
2009-07-14 08:43:42 +08:00
|
|
|
if (!FD)
|
2010-07-06 10:34:42 +08:00
|
|
|
return;
|
2010-10-21 07:38:56 +08:00
|
|
|
|
|
|
|
if (!BT) {
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
Retain = &Ctx.Idents.get("CFRetain");
|
|
|
|
Release = &Ctx.Idents.get("CFRelease");
|
2011-02-23 08:16:10 +08:00
|
|
|
BT.reset(new APIMisuse("null passed to CFRetain/CFRelease"));
|
2010-10-21 07:38:56 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-06 10:34:42 +08:00
|
|
|
// Check if we called CFRetain/CFRelease.
|
2009-09-09 23:08:12 +08:00
|
|
|
const IdentifierInfo *FuncII = FD->getIdentifier();
|
2009-07-14 08:43:42 +08:00
|
|
|
if (!(FuncII == Retain || FuncII == Release))
|
2010-07-06 10:34:42 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: The rest of this just checks that the argument is non-null.
|
|
|
|
// It should probably be refactored and combined with AttrNonNullChecker.
|
|
|
|
|
|
|
|
// Get the argument's value.
|
|
|
|
const Expr *Arg = CE->getArg(0);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal ArgVal = state->getSVal(Arg, C.getLocationContext());
|
2010-07-06 10:34:42 +08:00
|
|
|
DefinedSVal *DefArgVal = dyn_cast<DefinedSVal>(&ArgVal);
|
|
|
|
if (!DefArgVal)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get a NULL value.
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
DefinedSVal zero = cast<DefinedSVal>(svalBuilder.makeZeroVal(Arg->getType()));
|
2010-07-06 10:34:42 +08:00
|
|
|
|
|
|
|
// Make an expression asserting that they're equal.
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal ArgIsNull = svalBuilder.evalEQ(state, zero, *DefArgVal);
|
2010-07-06 10:34:42 +08:00
|
|
|
|
|
|
|
// Are they equal?
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef stateTrue, stateFalse;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(stateTrue, stateFalse) = state->assume(ArgIsNull);
|
2010-07-06 10:34:42 +08:00
|
|
|
|
|
|
|
if (stateTrue && !stateFalse) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(stateTrue);
|
2010-07-06 10:34:42 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-14 08:43:42 +08:00
|
|
|
const char *description = (FuncII == Retain)
|
|
|
|
? "Null pointer argument in call to CFRetain"
|
|
|
|
: "Null pointer argument in call to CFRelease";
|
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT, description, N);
|
2010-07-06 10:34:42 +08:00
|
|
|
report->addRange(Arg->getSourceRange());
|
2012-03-09 09:13:14 +08:00
|
|
|
report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Arg,
|
|
|
|
report));
|
2010-07-06 10:34:42 +08:00
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-06 10:34:42 +08:00
|
|
|
// From here on, we know the argument is non-null.
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(stateFalse);
|
2009-07-14 08:43:42 +08:00
|
|
|
}
|
|
|
|
|
2009-11-20 13:27:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check for sending 'retain', 'release', or 'autorelease' directly to a Class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class ClassReleaseChecker : public Checker<check::PreObjCMessage> {
|
2011-02-23 08:16:10 +08:00
|
|
|
mutable Selector releaseS;
|
|
|
|
mutable Selector retainS;
|
|
|
|
mutable Selector autoreleaseS;
|
|
|
|
mutable Selector drainS;
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT;
|
2009-11-20 13:27:05 +08:00
|
|
|
|
2011-02-23 08:16:10 +08:00
|
|
|
public:
|
2012-07-03 03:28:04 +08:00
|
|
|
void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
|
2009-11-20 13:27:05 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
void ClassReleaseChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
|
2011-02-23 08:16:10 +08:00
|
|
|
CheckerContext &C) const {
|
2010-10-21 07:38:56 +08:00
|
|
|
|
|
|
|
if (!BT) {
|
2011-02-23 08:16:10 +08:00
|
|
|
BT.reset(new APIMisuse("message incorrectly sent to class instead of class "
|
|
|
|
"instance"));
|
2010-10-21 07:38:56 +08:00
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
releaseS = GetNullarySelector("release", Ctx);
|
|
|
|
retainS = GetNullarySelector("retain", Ctx);
|
|
|
|
autoreleaseS = GetNullarySelector("autorelease", Ctx);
|
|
|
|
drainS = GetNullarySelector("drain", Ctx);
|
|
|
|
}
|
|
|
|
|
2011-01-25 08:03:53 +08:00
|
|
|
if (msg.isInstanceMessage())
|
2009-11-20 13:27:05 +08:00
|
|
|
return;
|
2011-01-25 08:03:53 +08:00
|
|
|
const ObjCInterfaceDecl *Class = msg.getReceiverInterface();
|
|
|
|
assert(Class);
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
llvm-svn: 101972
2010-04-21 08:45:42 +08:00
|
|
|
|
2011-01-25 08:03:53 +08:00
|
|
|
Selector S = msg.getSelector();
|
2009-11-20 18:03:00 +08:00
|
|
|
if (!(S == releaseS || S == retainS || S == autoreleaseS || S == drainS))
|
2009-11-20 13:27:05 +08:00
|
|
|
return;
|
|
|
|
|
2011-10-27 05:06:34 +08:00
|
|
|
if (ExplodedNode *N = C.addTransition()) {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<200> buf;
|
2010-10-21 07:38:56 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
2009-11-20 13:27:05 +08:00
|
|
|
|
2010-10-21 07:38:56 +08:00
|
|
|
os << "The '" << S.getAsString() << "' message should be sent to instances "
|
|
|
|
"of class '" << Class->getName()
|
|
|
|
<< "' and not the class directly";
|
2009-11-20 13:27:05 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT, os.str(), N);
|
2011-01-25 08:03:53 +08:00
|
|
|
report->addRange(msg.getSourceRange());
|
2010-10-21 07:38:56 +08:00
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
2009-11-20 13:27:05 +08:00
|
|
|
}
|
|
|
|
|
2011-03-14 04:35:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check for passing non-Objective-C types to variadic methods that expect
|
|
|
|
// only Objective-C types.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VariadicMethodTypeChecker : public Checker<check::PreObjCMessage> {
|
|
|
|
mutable Selector arrayWithObjectsS;
|
|
|
|
mutable Selector dictionaryWithObjectsAndKeysS;
|
|
|
|
mutable Selector setWithObjectsS;
|
2012-04-07 03:06:01 +08:00
|
|
|
mutable Selector orderedSetWithObjectsS;
|
2011-03-14 04:35:21 +08:00
|
|
|
mutable Selector initWithObjectsS;
|
|
|
|
mutable Selector initWithObjectsAndKeysS;
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BugType> BT;
|
2011-03-14 04:35:21 +08:00
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
bool isVariadicMessage(const ObjCMethodCall &msg) const;
|
2011-03-14 04:35:21 +08:00
|
|
|
|
|
|
|
public:
|
2012-07-03 03:28:04 +08:00
|
|
|
void checkPreObjCMessage(const ObjCMethodCall &msg, CheckerContext &C) const;
|
2011-03-14 04:35:21 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isVariadicMessage - Returns whether the given message is a variadic message,
|
|
|
|
/// where all arguments must be Objective-C types.
|
|
|
|
bool
|
2012-07-03 03:28:04 +08:00
|
|
|
VariadicMethodTypeChecker::isVariadicMessage(const ObjCMethodCall &msg) const {
|
|
|
|
const ObjCMethodDecl *MD = msg.getDecl();
|
2011-04-13 05:47:05 +08:00
|
|
|
|
|
|
|
if (!MD || !MD->isVariadic() || isa<ObjCProtocolDecl>(MD->getDeclContext()))
|
2011-03-14 04:35:21 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Selector S = msg.getSelector();
|
|
|
|
|
|
|
|
if (msg.isInstanceMessage()) {
|
|
|
|
// FIXME: Ideally we'd look at the receiver interface here, but that's not
|
|
|
|
// useful for init, because alloc returns 'id'. In theory, this could lead
|
|
|
|
// to false positives, for example if there existed a class that had an
|
|
|
|
// initWithObjects: implementation that does accept non-Objective-C pointer
|
|
|
|
// types, but the chance of that happening is pretty small compared to the
|
|
|
|
// gains that this analysis gives.
|
|
|
|
const ObjCInterfaceDecl *Class = MD->getClassInterface();
|
|
|
|
|
2012-06-12 00:40:37 +08:00
|
|
|
switch (findKnownClass(Class)) {
|
|
|
|
case FC_NSArray:
|
|
|
|
case FC_NSOrderedSet:
|
|
|
|
case FC_NSSet:
|
|
|
|
return S == initWithObjectsS;
|
|
|
|
case FC_NSDictionary:
|
|
|
|
return S == initWithObjectsAndKeysS;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-14 04:35:21 +08:00
|
|
|
} else {
|
|
|
|
const ObjCInterfaceDecl *Class = msg.getReceiverInterface();
|
|
|
|
|
2012-06-12 00:40:37 +08:00
|
|
|
switch (findKnownClass(Class)) {
|
|
|
|
case FC_NSArray:
|
|
|
|
return S == arrayWithObjectsS;
|
|
|
|
case FC_NSOrderedSet:
|
|
|
|
return S == orderedSetWithObjectsS;
|
|
|
|
case FC_NSSet:
|
|
|
|
return S == setWithObjectsS;
|
|
|
|
case FC_NSDictionary:
|
|
|
|
return S == dictionaryWithObjectsAndKeysS;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-14 04:35:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
void VariadicMethodTypeChecker::checkPreObjCMessage(const ObjCMethodCall &msg,
|
2011-03-14 04:35:21 +08:00
|
|
|
CheckerContext &C) const {
|
|
|
|
if (!BT) {
|
|
|
|
BT.reset(new APIMisuse("Arguments passed to variadic method aren't all "
|
|
|
|
"Objective-C pointer types"));
|
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
arrayWithObjectsS = GetUnarySelector("arrayWithObjects", Ctx);
|
|
|
|
dictionaryWithObjectsAndKeysS =
|
|
|
|
GetUnarySelector("dictionaryWithObjectsAndKeys", Ctx);
|
|
|
|
setWithObjectsS = GetUnarySelector("setWithObjects", Ctx);
|
2012-04-07 03:06:01 +08:00
|
|
|
orderedSetWithObjectsS = GetUnarySelector("orderedSetWithObjects", Ctx);
|
2011-03-14 04:35:21 +08:00
|
|
|
|
|
|
|
initWithObjectsS = GetUnarySelector("initWithObjects", Ctx);
|
|
|
|
initWithObjectsAndKeysS = GetUnarySelector("initWithObjectsAndKeys", Ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isVariadicMessage(msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We are not interested in the selector arguments since they have
|
|
|
|
// well-defined types, so the compiler will issue a warning for them.
|
|
|
|
unsigned variadicArgsBegin = msg.getSelector().getNumArgs();
|
|
|
|
|
|
|
|
// We're not interested in the last argument since it has to be nil or the
|
|
|
|
// compiler would have issued a warning for it elsewhere.
|
|
|
|
unsigned variadicArgsEnd = msg.getNumArgs() - 1;
|
|
|
|
|
|
|
|
if (variadicArgsEnd <= variadicArgsBegin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Verify that all arguments have Objective-C types.
|
2011-03-15 03:50:37 +08:00
|
|
|
llvm::Optional<ExplodedNode*> errorNode;
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2011-03-15 03:50:37 +08:00
|
|
|
|
2011-03-14 04:35:21 +08:00
|
|
|
for (unsigned I = variadicArgsBegin; I != variadicArgsEnd; ++I) {
|
2012-07-03 03:28:04 +08:00
|
|
|
QualType ArgTy = msg.getArgExpr(I)->getType();
|
2011-03-14 04:35:21 +08:00
|
|
|
if (ArgTy->isObjCObjectPointerType())
|
|
|
|
continue;
|
|
|
|
|
2011-04-19 09:16:46 +08:00
|
|
|
// Block pointers are treaded as Objective-C pointers.
|
|
|
|
if (ArgTy->isBlockPointerType())
|
|
|
|
continue;
|
|
|
|
|
2011-03-16 08:22:51 +08:00
|
|
|
// Ignore pointer constants.
|
2012-07-03 03:28:04 +08:00
|
|
|
if (isa<loc::ConcreteInt>(msg.getArgSVal(I)))
|
2011-03-16 08:22:51 +08:00
|
|
|
continue;
|
2011-03-17 12:01:35 +08:00
|
|
|
|
2011-03-17 12:10:25 +08:00
|
|
|
// Ignore pointer types annotated with 'NSObject' attribute.
|
|
|
|
if (C.getASTContext().isObjCNSObjectType(ArgTy))
|
|
|
|
continue;
|
|
|
|
|
2011-03-17 12:01:35 +08:00
|
|
|
// Ignore CF references, which can be toll-free bridged.
|
2011-07-17 03:50:32 +08:00
|
|
|
if (coreFoundation::isCFObjectRef(ArgTy))
|
2011-03-17 12:01:35 +08:00
|
|
|
continue;
|
2011-03-16 08:22:51 +08:00
|
|
|
|
2011-03-15 03:50:37 +08:00
|
|
|
// Generate only one error node to use for all bug reports.
|
2012-07-03 03:28:04 +08:00
|
|
|
if (!errorNode.hasValue())
|
2011-10-27 05:06:34 +08:00
|
|
|
errorNode = C.addTransition();
|
2011-03-15 03:50:37 +08:00
|
|
|
|
|
|
|
if (!errorNode.getValue())
|
2011-03-14 04:35:21 +08:00
|
|
|
continue;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> sbuf;
|
2011-03-14 04:35:21 +08:00
|
|
|
llvm::raw_svector_ostream os(sbuf);
|
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
StringRef TypeName = GetReceiverInterfaceName(msg);
|
|
|
|
if (!TypeName.empty())
|
2011-03-14 04:35:21 +08:00
|
|
|
os << "Argument to '" << TypeName << "' method '";
|
|
|
|
else
|
|
|
|
os << "Argument to method '";
|
|
|
|
|
|
|
|
os << msg.getSelector().getAsString()
|
2012-07-03 03:28:04 +08:00
|
|
|
<< "' should be an Objective-C pointer type, not '";
|
|
|
|
ArgTy.print(os, C.getLangOpts());
|
|
|
|
os << "'";
|
2011-03-14 04:35:21 +08:00
|
|
|
|
2012-07-03 03:28:04 +08:00
|
|
|
BugReport *R = new BugReport(*BT, os.str(), errorNode.getValue());
|
2011-03-14 04:35:21 +08:00
|
|
|
R->addRange(msg.getArgSourceRange(I));
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 00:40:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Improves the modeling of loops over Cocoa collections.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ObjCLoopChecker
|
|
|
|
: public Checker<check::PostStmt<ObjCForCollectionStmt> > {
|
|
|
|
|
|
|
|
public:
|
|
|
|
void checkPostStmt(const ObjCForCollectionStmt *FCS, CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isKnownNonNilCollectionType(QualType T) {
|
|
|
|
const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
|
|
|
|
if (!PT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
|
|
|
|
if (!ID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (findKnownClass(ID)) {
|
|
|
|
case FC_NSArray:
|
|
|
|
case FC_NSDictionary:
|
|
|
|
case FC_NSEnumerator:
|
|
|
|
case FC_NSOrderedSet:
|
|
|
|
case FC_NSSet:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCLoopChecker::checkPostStmt(const ObjCForCollectionStmt *FCS,
|
|
|
|
CheckerContext &C) const {
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
|
|
|
|
// Check if this is the branch for the end of the loop.
|
|
|
|
SVal CollectionSentinel = State->getSVal(FCS, C.getLocationContext());
|
|
|
|
if (CollectionSentinel.isZeroConstant())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// See if the collection is one where we /know/ the elements are non-nil.
|
|
|
|
const Expr *Collection = FCS->getCollection();
|
|
|
|
if (!isKnownNonNilCollectionType(Collection->getType()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Copied from ExprEngineObjC.
|
|
|
|
const Stmt *Element = FCS->getElement();
|
|
|
|
SVal ElementVar;
|
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(Element)) {
|
|
|
|
const VarDecl *ElemDecl = cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
assert(ElemDecl->getInit() == 0);
|
|
|
|
ElementVar = State->getLValue(ElemDecl, C.getLocationContext());
|
|
|
|
} else {
|
|
|
|
ElementVar = State->getSVal(Element, C.getLocationContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isa<Loc>(ElementVar))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Go ahead and assume the value is non-nil.
|
|
|
|
SVal Val = State->getSVal(cast<Loc>(ElementVar));
|
|
|
|
State = State->assume(cast<DefinedOrUnknownSVal>(Val), true);
|
|
|
|
C.addTransition(State);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check registration.
|
2009-07-14 08:43:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-02-16 09:40:52 +08:00
|
|
|
|
2011-02-18 05:39:17 +08:00
|
|
|
void ento::registerNilArgChecker(CheckerManager &mgr) {
|
2011-02-23 08:16:10 +08:00
|
|
|
mgr.registerChecker<NilArgChecker>();
|
2011-02-16 09:40:52 +08:00
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:17 +08:00
|
|
|
void ento::registerCFNumberCreateChecker(CheckerManager &mgr) {
|
2011-02-23 08:16:10 +08:00
|
|
|
mgr.registerChecker<CFNumberCreateChecker>();
|
2011-02-16 09:40:52 +08:00
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:17 +08:00
|
|
|
void ento::registerCFRetainReleaseChecker(CheckerManager &mgr) {
|
2011-02-23 08:16:10 +08:00
|
|
|
mgr.registerChecker<CFRetainReleaseChecker>();
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
2011-02-18 05:39:17 +08:00
|
|
|
|
|
|
|
void ento::registerClassReleaseChecker(CheckerManager &mgr) {
|
2011-02-23 08:16:10 +08:00
|
|
|
mgr.registerChecker<ClassReleaseChecker>();
|
2011-02-18 05:39:17 +08:00
|
|
|
}
|
2011-03-14 04:35:21 +08:00
|
|
|
|
|
|
|
void ento::registerVariadicMethodTypeChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<VariadicMethodTypeChecker>();
|
|
|
|
}
|
2012-06-12 00:40:41 +08:00
|
|
|
|
|
|
|
void ento::registerObjCLoopChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ObjCLoopChecker>();
|
|
|
|
}
|