2008-09-18 14:33:41 +08:00
|
|
|
//=- CheckNSError.cpp - Coding conventions for uses of NSError ---*- 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 a CheckNSError, a flow-insenstive check
|
|
|
|
// that determines if an Objective-C class interface correctly returns
|
|
|
|
// a non-void return type.
|
|
|
|
//
|
|
|
|
// File under feature request PR 2600.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-09-19 05:25:13 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
|
|
|
#include "BasicObjCFoundationChecks.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-09-18 14:33:41 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-09-19 05:25:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-09-18 14:33:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN NSErrorCheck : public BugTypeCacheLocation {
|
|
|
|
|
|
|
|
void EmitGRWarnings(GRBugReporter& BR);
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
void CheckSignature(ObjCMethodDecl& MD, QualType& ResultTy,
|
2008-10-02 07:24:09 +08:00
|
|
|
llvm::SmallVectorImpl<VarDecl*>& NSErrorParams,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& CFErrorParams,
|
|
|
|
IdentifierInfo* NSErrorII,
|
|
|
|
IdentifierInfo* CFErrorII);
|
|
|
|
|
|
|
|
void CheckSignature(FunctionDecl& MD, QualType& ResultTy,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& NSErrorParams,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& CFErrorParams,
|
|
|
|
IdentifierInfo* NSErrorII,
|
|
|
|
IdentifierInfo* CFErrorII);
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
bool CheckNSErrorArgument(QualType ArgTy, IdentifierInfo* NSErrorII);
|
|
|
|
bool CheckCFErrorArgument(QualType ArgTy, IdentifierInfo* CFErrorII);
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2008-09-19 07:09:54 +08:00
|
|
|
void CheckParamDeref(VarDecl* V, GRStateRef state, GRExprEngine& Eng,
|
2008-10-02 07:24:09 +08:00
|
|
|
GRBugReporter& BR, bool isNErrorWarning);
|
|
|
|
|
|
|
|
void EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl, bool isNSErrorWarning);
|
2008-09-19 07:09:54 +08:00
|
|
|
|
|
|
|
const char* desc;
|
2008-10-02 07:24:09 +08:00
|
|
|
const char* name;
|
2008-09-19 05:25:13 +08:00
|
|
|
public:
|
2008-09-19 07:09:54 +08:00
|
|
|
NSErrorCheck() : desc(0) {}
|
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
void EmitWarnings(BugReporter& BR) { EmitGRWarnings(cast<GRBugReporter>(BR));}
|
2008-10-02 07:24:09 +08:00
|
|
|
const char* getName() const { return name; }
|
2008-09-19 07:09:54 +08:00
|
|
|
const char* getDescription() const { return desc; }
|
2008-09-21 14:57:40 +08:00
|
|
|
const char* getCategory() const { return "Coding Conventions (Apple)"; }
|
2008-09-19 05:25:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
BugType* clang::CreateNSErrorCheck() {
|
|
|
|
return new NSErrorCheck();
|
|
|
|
}
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
void NSErrorCheck::EmitGRWarnings(GRBugReporter& BR) {
|
|
|
|
// Get the analysis engine and the exploded analysis graph.
|
|
|
|
GRExprEngine& Eng = BR.getEngine();
|
|
|
|
GRExprEngine::GraphTy& G = Eng.getGraph();
|
|
|
|
|
|
|
|
// Get the declaration of the method/function that was analyzed.
|
|
|
|
Decl& CodeDecl = G.getCodeDecl();
|
2008-10-02 07:24:09 +08:00
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
// Get the ASTContext, which is useful for querying type information.
|
|
|
|
ASTContext &Ctx = BR.getContext();
|
2008-09-18 14:33:41 +08:00
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
QualType ResultTy;
|
2008-10-02 07:24:09 +08:00
|
|
|
llvm::SmallVector<VarDecl*, 5> NSErrorParams;
|
|
|
|
llvm::SmallVector<VarDecl*, 5> CFErrorParams;
|
|
|
|
|
|
|
|
if (ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl))
|
|
|
|
CheckSignature(*MD, ResultTy, NSErrorParams, CFErrorParams,
|
|
|
|
&Ctx.Idents.get("NSError"), &Ctx.Idents.get("CFErrorRef"));
|
|
|
|
else if (FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl))
|
|
|
|
CheckSignature(*FD, ResultTy, NSErrorParams, CFErrorParams,
|
|
|
|
&Ctx.Idents.get("NSError"), &Ctx.Idents.get("CFErrorRef"));
|
|
|
|
else
|
|
|
|
return;
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
if (NSErrorParams.empty() && CFErrorParams.empty())
|
2008-09-19 05:25:13 +08:00
|
|
|
return;
|
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
if (ResultTy == Ctx.VoidTy) {
|
|
|
|
if (!NSErrorParams.empty())
|
|
|
|
EmitRetTyWarning(BR, CodeDecl, true);
|
|
|
|
if (!CFErrorParams.empty())
|
|
|
|
EmitRetTyWarning(BR, CodeDecl, false);
|
2008-09-18 14:33:41 +08:00
|
|
|
}
|
2008-09-19 07:09:54 +08:00
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
for (GRExprEngine::GraphTy::roots_iterator RI=G.roots_begin(),
|
|
|
|
RE=G.roots_end(); RI!=RE; ++RI) {
|
|
|
|
|
|
|
|
// Scan the NSError** parameters for an implicit null dereference.
|
|
|
|
for (llvm::SmallVectorImpl<VarDecl*>::iterator I=NSErrorParams.begin(),
|
|
|
|
E=NSErrorParams.end(); I!=E; ++I)
|
|
|
|
CheckParamDeref(*I, GRStateRef((*RI)->getState(), Eng.getStateManager()),
|
|
|
|
Eng, BR, true);
|
|
|
|
|
|
|
|
// Scan the CFErrorRef* parameters for an implicit null dereference.
|
|
|
|
for (llvm::SmallVectorImpl<VarDecl*>::iterator I=CFErrorParams.begin(),
|
|
|
|
E=CFErrorParams.end(); I!=E; ++I)
|
2008-09-19 07:09:54 +08:00
|
|
|
CheckParamDeref(*I, GRStateRef((*RI)->getState(), Eng.getStateManager()),
|
2008-10-02 07:24:09 +08:00
|
|
|
Eng, BR, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NSErrorCheck::EmitRetTyWarning(BugReporter& BR, Decl& CodeDecl,
|
|
|
|
bool isNSErrorWarning) {
|
|
|
|
|
|
|
|
std::string msg;
|
|
|
|
llvm::raw_string_ostream os(msg);
|
|
|
|
|
|
|
|
if (isa<ObjCMethodDecl>(CodeDecl))
|
|
|
|
os << "Method";
|
|
|
|
else
|
|
|
|
os << "Function";
|
|
|
|
|
|
|
|
os << " accepting ";
|
|
|
|
os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*");
|
|
|
|
os << " should have a non-void return value to indicate whether or not an "
|
|
|
|
"error occured.";
|
|
|
|
|
|
|
|
BR.EmitBasicReport(isNSErrorWarning
|
|
|
|
? "Bad return type when passing NSError**"
|
|
|
|
: "Bad return type when passing CFError*",
|
|
|
|
getCategory(), os.str().c_str(), CodeDecl.getLocation());
|
2008-09-18 14:33:41 +08:00
|
|
|
}
|
2008-09-19 05:25:13 +08:00
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
void
|
|
|
|
NSErrorCheck::CheckSignature(ObjCMethodDecl& M, QualType& ResultTy,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& NSErrorParams,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& CFErrorParams,
|
|
|
|
IdentifierInfo* NSErrorII,
|
|
|
|
IdentifierInfo* CFErrorII) {
|
2008-09-19 05:25:13 +08:00
|
|
|
|
|
|
|
ResultTy = M.getResultType();
|
|
|
|
|
|
|
|
for (ObjCMethodDecl::param_iterator I=M.param_begin(),
|
2008-10-02 07:24:09 +08:00
|
|
|
E=M.param_end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
QualType T = (*I)->getType();
|
|
|
|
|
|
|
|
if (CheckNSErrorArgument(T, NSErrorII))
|
|
|
|
NSErrorParams.push_back(*I);
|
|
|
|
else if (CheckCFErrorArgument(T, CFErrorII))
|
|
|
|
CFErrorParams.push_back(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NSErrorCheck::CheckSignature(FunctionDecl& F, QualType& ResultTy,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& NSErrorParams,
|
|
|
|
llvm::SmallVectorImpl<VarDecl*>& CFErrorParams,
|
|
|
|
IdentifierInfo* NSErrorII,
|
|
|
|
IdentifierInfo* CFErrorII) {
|
|
|
|
|
|
|
|
ResultTy = F.getResultType();
|
|
|
|
|
|
|
|
for (FunctionDecl::param_iterator I=F.param_begin(),
|
|
|
|
E=F.param_end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
QualType T = (*I)->getType();
|
|
|
|
|
|
|
|
if (CheckNSErrorArgument(T, NSErrorII))
|
|
|
|
NSErrorParams.push_back(*I);
|
|
|
|
else if (CheckCFErrorArgument(T, CFErrorII))
|
|
|
|
CFErrorParams.push_back(*I);
|
|
|
|
}
|
2008-09-19 05:25:13 +08:00
|
|
|
}
|
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
|
|
|
|
bool NSErrorCheck::CheckNSErrorArgument(QualType ArgTy,
|
|
|
|
IdentifierInfo* NSErrorII) {
|
|
|
|
|
2008-09-19 05:25:13 +08:00
|
|
|
const PointerType* PPT = ArgTy->getAsPointerType();
|
|
|
|
if (!PPT) return false;
|
|
|
|
|
|
|
|
const PointerType* PT = PPT->getPointeeType()->getAsPointerType();
|
|
|
|
if (!PT) return false;
|
|
|
|
|
|
|
|
const ObjCInterfaceType *IT =
|
|
|
|
PT->getPointeeType()->getAsObjCInterfaceType();
|
|
|
|
|
|
|
|
if (!IT) return false;
|
|
|
|
return IT->getDecl()->getIdentifier() == NSErrorII;
|
|
|
|
}
|
2008-09-19 07:09:54 +08:00
|
|
|
|
2008-10-02 07:24:09 +08:00
|
|
|
bool NSErrorCheck::CheckCFErrorArgument(QualType ArgTy,
|
|
|
|
IdentifierInfo* CFErrorII) {
|
|
|
|
|
|
|
|
const PointerType* PPT = ArgTy->getAsPointerType();
|
|
|
|
if (!PPT) return false;
|
|
|
|
|
|
|
|
const TypedefType* TT = PPT->getPointeeType()->getAsTypedefType();
|
|
|
|
if (!TT) return false;
|
|
|
|
|
|
|
|
return TT->getDecl()->getIdentifier() == CFErrorII;
|
|
|
|
}
|
|
|
|
|
2008-09-19 07:09:54 +08:00
|
|
|
void NSErrorCheck::CheckParamDeref(VarDecl* Param, GRStateRef rootState,
|
2008-10-02 07:24:09 +08:00
|
|
|
GRExprEngine& Eng, GRBugReporter& BR,
|
|
|
|
bool isNSErrorWarning) {
|
2008-09-19 07:09:54 +08:00
|
|
|
|
2008-10-17 08:51:01 +08:00
|
|
|
RVal ParamRVal = rootState.GetLValue(Param);
|
2008-09-19 07:09:54 +08:00
|
|
|
|
|
|
|
// FIXME: For now assume that ParamRVal is symbolic. We need to generalize
|
|
|
|
// this later.
|
|
|
|
lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&ParamRVal);
|
|
|
|
if (!SV) return;
|
|
|
|
|
|
|
|
// Iterate over the implicit-null dereferences.
|
|
|
|
for (GRExprEngine::null_deref_iterator I=Eng.implicit_null_derefs_begin(),
|
|
|
|
E=Eng.implicit_null_derefs_end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
GRStateRef state = GRStateRef((*I)->getState(), Eng.getStateManager());
|
|
|
|
const RVal* X = state.get<GRState::NullDerefTag>();
|
|
|
|
const lval::SymbolVal* SVX = dyn_cast_or_null<lval::SymbolVal>(X);
|
|
|
|
if (!SVX || SVX->getSymbol() != SV->getSymbol()) continue;
|
|
|
|
|
|
|
|
// Emit an error.
|
|
|
|
BugReport R(*this, *I);
|
2008-10-02 07:24:09 +08:00
|
|
|
|
|
|
|
name = isNSErrorWarning ? "NSError** null dereference"
|
|
|
|
: "CFErrorRef* null dereference";
|
2008-09-19 07:09:54 +08:00
|
|
|
|
|
|
|
std::string msg;
|
|
|
|
llvm::raw_string_ostream os(msg);
|
2008-10-02 07:24:09 +08:00
|
|
|
os << "Potential null dereference. According to coding standards ";
|
|
|
|
|
|
|
|
if (isNSErrorWarning)
|
|
|
|
os << "in 'Creating and Returning NSError Objects' the parameter '";
|
|
|
|
else
|
|
|
|
os << "documented in CoreFoundation/CFError.h the parameter '";
|
|
|
|
|
|
|
|
os << Param->getName() << "' may be null.";
|
2008-09-19 07:09:54 +08:00
|
|
|
desc = os.str().c_str();
|
|
|
|
|
2008-09-19 07:23:19 +08:00
|
|
|
BR.addNotableSymbol(SV->getSymbol());
|
2008-10-02 07:24:09 +08:00
|
|
|
BR.EmitWarning(R);
|
2008-09-19 07:09:54 +08:00
|
|
|
}
|
|
|
|
}
|