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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-28 01:17:22 +08:00
|
|
|
#include "BasicObjCFoundationChecks.h"
|
|
|
|
|
2008-03-27 15:25:52 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/ExplodedGraph.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRSimpleAPICheck.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/ValueState.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/AnnotatedPath.h"
|
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2008-03-28 06:05:32 +08:00
|
|
|
#include <sstream>
|
2008-03-27 15:25:52 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BasicObjCFoundationChecks : public GRSimpleAPICheck {
|
|
|
|
|
2008-03-28 05:15:17 +08:00
|
|
|
ASTContext &Ctx;
|
|
|
|
ValueStateManager* VMgr;
|
|
|
|
|
|
|
|
typedef std::list<AnnotatedPath<ValueState> > ErrorsTy;
|
|
|
|
ErrorsTy Errors;
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2008-03-28 05:15:17 +08:00
|
|
|
RVal GetRVal(ValueState* St, Expr* E) { return VMgr->GetRVal(St, E); }
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2008-03-28 05:15:17 +08:00
|
|
|
bool isNSString(ObjCInterfaceType* T, const char* suffix);
|
|
|
|
bool AuditNSString(NodeTy* N, ObjCMessageExpr* ME);
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
void Warn(NodeTy* N, Expr* E, const std::string& s);
|
|
|
|
void WarnNilArg(NodeTy* N, Expr* E);
|
|
|
|
|
|
|
|
bool CheckNilArg(NodeTy* N, unsigned Arg);
|
2008-03-27 15:25:52 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
BasicObjCFoundationChecks(ASTContext& ctx, ValueStateManager* vmgr)
|
|
|
|
: Ctx(ctx), VMgr(vmgr) {}
|
|
|
|
|
|
|
|
virtual ~BasicObjCFoundationChecks() {}
|
|
|
|
|
|
|
|
virtual bool Audit(ExplodedNode<ValueState>* N);
|
2008-03-28 05:15:17 +08:00
|
|
|
|
|
|
|
virtual void ReportResults(Diagnostic& D);
|
|
|
|
|
2008-03-27 15:25:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2008-03-28 01:17:22 +08:00
|
|
|
GRSimpleAPICheck*
|
|
|
|
clang::CreateBasicObjCFoundationChecks(ASTContext& Ctx,
|
|
|
|
ValueStateManager* VMgr) {
|
|
|
|
|
|
|
|
return new BasicObjCFoundationChecks(Ctx, VMgr);
|
|
|
|
}
|
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
static ObjCInterfaceType* GetReceiverType(ObjCMessageExpr* ME) {
|
2008-03-27 15:25:52 +08:00
|
|
|
Expr* Receiver = ME->getReceiver();
|
|
|
|
|
|
|
|
if (!Receiver)
|
2008-03-28 06:05:32 +08:00
|
|
|
return NULL;
|
2008-03-27 15:25:52 +08:00
|
|
|
|
|
|
|
assert (Receiver->getType()->isPointerType());
|
2008-03-28 06:05:32 +08:00
|
|
|
|
2008-03-27 15:25:52 +08:00
|
|
|
const PointerType* T = Receiver->getType()->getAsPointerType();
|
2008-03-28 06:05:32 +08:00
|
|
|
|
|
|
|
return dyn_cast<ObjCInterfaceType>(T->getPointeeType().getTypePtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* GetReceiverNameType(ObjCMessageExpr* ME) {
|
|
|
|
ObjCInterfaceType* ReceiverType = GetReceiverType(ME);
|
|
|
|
return ReceiverType ? ReceiverType->getDecl()->getIdentifier()->getName()
|
|
|
|
: NULL;
|
|
|
|
}
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
bool BasicObjCFoundationChecks::Audit(ExplodedNode<ValueState>* N) {
|
|
|
|
|
|
|
|
ObjCMessageExpr* ME =
|
|
|
|
cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
|
|
|
|
ObjCInterfaceType* ReceiverType = GetReceiverType(ME);
|
2008-03-27 15:25:52 +08:00
|
|
|
|
|
|
|
if (!ReceiverType)
|
2008-03-28 06:05:32 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
const char* name = ReceiverType->getDecl()->getIdentifier()->getName();
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
if (!name)
|
|
|
|
return false;
|
2008-03-27 15:25:52 +08:00
|
|
|
|
|
|
|
if (name[0] != 'N' || name[1] != 'S')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
name += 2;
|
|
|
|
|
|
|
|
// FIXME: Make all of this faster.
|
|
|
|
|
|
|
|
if (isNSString(ReceiverType, name))
|
|
|
|
return AuditNSString(N, ME);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-28 05:15:17 +08:00
|
|
|
static inline bool isNil(RVal X) {
|
|
|
|
return isa<lval::ConcreteInt>(X);
|
|
|
|
}
|
|
|
|
|
2008-03-27 15:25:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error reporting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
void BasicObjCFoundationChecks::Warn(NodeTy* N, Expr* E, const std::string& s) {
|
2008-03-27 15:25:52 +08:00
|
|
|
Errors.push_back(AnnotatedPath<ValueState>());
|
2008-03-28 06:05:32 +08:00
|
|
|
Errors.back().push_back(N, s, E);
|
2008-03-27 15:25:52 +08:00
|
|
|
}
|
|
|
|
|
2008-03-28 05:15:17 +08:00
|
|
|
void BasicObjCFoundationChecks::ReportResults(Diagnostic& D) {
|
|
|
|
|
|
|
|
// FIXME: Expand errors into paths. For now, just issue warnings.
|
|
|
|
|
|
|
|
for (ErrorsTy::iterator I=Errors.begin(), E=Errors.end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
AnnotatedNode<ValueState>& AN = I->back();
|
|
|
|
|
|
|
|
unsigned diag = D.getCustomDiagID(Diagnostic::Warning,
|
|
|
|
AN.getString().c_str());
|
|
|
|
|
|
|
|
Stmt* S = cast<PostStmt>(AN.getNode()->getLocation()).getStmt();
|
|
|
|
FullSourceLoc L(S->getLocStart(), Ctx.getSourceManager());
|
|
|
|
|
|
|
|
SourceRange R = AN.getExpr()->getSourceRange();
|
|
|
|
|
2008-03-28 05:23:57 +08:00
|
|
|
D.Report(L, diag, &AN.getString(), 1, &R, 1);
|
2008-03-28 05:15:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
void BasicObjCFoundationChecks::WarnNilArg(NodeTy* N, Expr* E) {
|
|
|
|
|
|
|
|
ObjCMessageExpr* ME =
|
|
|
|
cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
os << "Argument to '" << GetReceiverNameType(ME) << "' method '"
|
|
|
|
<< ME->getSelector().getName()
|
|
|
|
<< "' cannot be nil.";
|
|
|
|
|
|
|
|
Warn(N, E, os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasicObjCFoundationChecks::CheckNilArg(NodeTy* N, unsigned Arg) {
|
|
|
|
ObjCMessageExpr* ME =
|
|
|
|
cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
|
|
|
|
Expr * E = ME->getArg(Arg);
|
|
|
|
|
|
|
|
if (isNil(GetRVal(N->getState(), E))) {
|
|
|
|
WarnNilArg(N, E);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-27 15:25:52 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NSString checking.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool BasicObjCFoundationChecks::isNSString(ObjCInterfaceType* T,
|
|
|
|
const char* suffix) {
|
|
|
|
|
|
|
|
return !strcmp("String", suffix) || !strcmp("MutableString", suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasicObjCFoundationChecks::AuditNSString(NodeTy* N,
|
|
|
|
ObjCMessageExpr* ME) {
|
|
|
|
|
|
|
|
Selector S = ME->getSelector();
|
|
|
|
|
|
|
|
if (S.isUnarySelector())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: This is going to be really slow doing these checks with
|
|
|
|
// lexical comparisons.
|
|
|
|
|
|
|
|
std::string name = S.getName();
|
2008-03-28 05:23:57 +08:00
|
|
|
assert (!name.empty());
|
|
|
|
const char* cstr = &name[0];
|
|
|
|
unsigned len = name.size();
|
2008-03-28 06:05:32 +08:00
|
|
|
|
2008-03-28 05:23:57 +08:00
|
|
|
switch (len) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case 8:
|
2008-03-28 06:05:32 +08:00
|
|
|
|
|
|
|
if (!strcmp(cstr, "compare:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
2008-03-27 15:25:52 +08:00
|
|
|
|
2008-03-28 06:05:32 +08:00
|
|
|
case 16:
|
|
|
|
if (!strcmp(cstr, "compare:options:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 22:
|
|
|
|
if (!strcmp(cstr, "compare:options:range:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 23:
|
|
|
|
|
|
|
|
if (!strcmp(cstr, "caseInsensitiveCompare:"))
|
|
|
|
return CheckNilArg(N, 0);
|
2008-03-28 05:23:57 +08:00
|
|
|
|
|
|
|
break;
|
2008-03-28 06:05:32 +08:00
|
|
|
|
|
|
|
case 29:
|
|
|
|
if (!strcmp(cstr, "compare:options:range:locale:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 37:
|
|
|
|
if (!strcmp(cstr, "componentsSeparatedByCharactersInSet:"))
|
|
|
|
return CheckNilArg(N, 0);
|
|
|
|
|
|
|
|
break;
|
2008-03-27 15:25:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|