opnsense-src/lib/Checker/NSErrorChecker.cpp

238 lines
7.4 KiB
C++
Raw Normal View History

2009-11-04 10:04:32 -05:00
//=- NSErrorCheckerer.cpp - Coding conventions for uses of NSError -*- C++ -*-==//
2009-06-02 13:58:47 -04:00
//
// 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.
//
//===----------------------------------------------------------------------===//
2010-02-16 04:31:36 -05:00
#include "clang/Checker/Checkers/LocalCheckers.h"
#include "clang/Checker/BugReporter/BugReporter.h"
#include "clang/Checker/PathSensitive/GRExprEngine.h"
#include "clang/Checker/Checkers/DereferenceChecker.h"
2009-06-02 13:58:47 -04:00
#include "BasicObjCFoundationChecks.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Decl.h"
#include "llvm/ADT/SmallVector.h"
using namespace clang;
namespace {
2009-12-01 06:08:04 -05:00
class NSErrorChecker : public BugType {
2009-10-14 14:03:49 -04:00
const Decl &CodeDecl;
2009-06-02 13:58:47 -04:00
const bool isNSErrorWarning;
IdentifierInfo * const II;
GRExprEngine &Eng;
2009-10-14 14:03:49 -04:00
void CheckSignature(const ObjCMethodDecl& MD, QualType& ResultTy,
2009-06-02 13:58:47 -04:00
llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
2009-10-14 14:03:49 -04:00
void CheckSignature(const FunctionDecl& MD, QualType& ResultTy,
2009-06-02 13:58:47 -04:00
llvm::SmallVectorImpl<VarDecl*>& ErrorParams);
bool CheckNSErrorArgument(QualType ArgTy);
bool CheckCFErrorArgument(QualType ArgTy);
2009-10-14 14:03:49 -04:00
void CheckParamDeref(const VarDecl *V, const LocationContext *LC,
const GRState *state, BugReporter& BR);
void EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl);
2009-06-02 13:58:47 -04:00
public:
2009-11-04 10:04:32 -05:00
NSErrorChecker(const Decl &D, bool isNSError, GRExprEngine& eng)
2009-10-14 14:03:49 -04:00
: BugType(isNSError ? "NSError** null dereference"
: "CFErrorRef* null dereference",
"Coding conventions (Apple)"),
CodeDecl(D),
isNSErrorWarning(isNSError),
2009-06-02 13:58:47 -04:00
II(&eng.getContext().Idents.get(isNSErrorWarning ? "NSError":"CFErrorRef")),
Eng(eng) {}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
void FlushReports(BugReporter& BR);
2009-10-14 14:03:49 -04:00
};
2009-06-02 13:58:47 -04:00
} // end anonymous namespace
2009-10-14 14:03:49 -04:00
void clang::RegisterNSErrorChecks(BugReporter& BR, GRExprEngine &Eng,
const Decl &D) {
2009-11-04 10:04:32 -05:00
BR.Register(new NSErrorChecker(D, true, Eng));
BR.Register(new NSErrorChecker(D, false, Eng));
2009-06-02 13:58:47 -04:00
}
2009-11-04 10:04:32 -05:00
void NSErrorChecker::FlushReports(BugReporter& BR) {
2009-06-02 13:58:47 -04:00
// Get the analysis engine and the exploded analysis graph.
2009-10-14 14:03:49 -04:00
ExplodedGraph& G = Eng.getGraph();
2009-06-02 13:58:47 -04:00
// Get the ASTContext, which is useful for querying type information.
ASTContext &Ctx = BR.getContext();
QualType ResultTy;
llvm::SmallVector<VarDecl*, 5> ErrorParams;
2009-10-14 14:03:49 -04:00
if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CodeDecl))
2009-06-02 13:58:47 -04:00
CheckSignature(*MD, ResultTy, ErrorParams);
2009-10-14 14:03:49 -04:00
else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(&CodeDecl))
2009-06-02 13:58:47 -04:00
CheckSignature(*FD, ResultTy, ErrorParams);
else
return;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (ErrorParams.empty())
return;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (ResultTy == Ctx.VoidTy) EmitRetTyWarning(BR, CodeDecl);
2009-10-14 14:03:49 -04:00
for (ExplodedGraph::roots_iterator RI=G.roots_begin(), RE=G.roots_end();
RI!=RE; ++RI) {
2009-06-02 13:58:47 -04:00
// Scan the parameters for an implicit null dereference.
for (llvm::SmallVectorImpl<VarDecl*>::iterator I=ErrorParams.begin(),
2009-10-14 14:03:49 -04:00
E=ErrorParams.end(); I!=E; ++I)
CheckParamDeref(*I, (*RI)->getLocationContext(), (*RI)->getState(), BR);
2009-06-02 13:58:47 -04:00
}
}
2009-11-04 10:04:32 -05:00
void NSErrorChecker::EmitRetTyWarning(BugReporter& BR, const Decl& CodeDecl) {
2009-06-02 13:58:47 -04:00
std::string sbuf;
llvm::raw_string_ostream os(sbuf);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (isa<ObjCMethodDecl>(CodeDecl))
os << "Method";
else
2009-10-14 14:03:49 -04:00
os << "Function";
2009-06-02 13:58:47 -04:00
os << " accepting ";
os << (isNSErrorWarning ? "NSError**" : "CFErrorRef*");
os << " should have a non-void return value to indicate whether or not an "
2009-10-14 14:03:49 -04:00
"error occurred";
2009-06-02 13:58:47 -04:00
BR.EmitBasicReport(isNSErrorWarning
? "Bad return type when passing NSError**"
: "Bad return type when passing CFError*",
2009-12-01 06:08:04 -05:00
getCategory(), os.str(),
2009-06-02 13:58:47 -04:00
CodeDecl.getLocation());
}
void
2009-11-04 10:04:32 -05:00
NSErrorChecker::CheckSignature(const ObjCMethodDecl& M, QualType& ResultTy,
2009-06-02 13:58:47 -04:00
llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
ResultTy = M.getResultType();
2009-10-14 14:03:49 -04:00
for (ObjCMethodDecl::param_iterator I=M.param_begin(),
2009-06-02 13:58:47 -04:00
E=M.param_end(); I!=E; ++I) {
2009-10-14 14:03:49 -04:00
QualType T = (*I)->getType();
2009-06-02 13:58:47 -04:00
if (isNSErrorWarning) {
if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
}
else if (CheckCFErrorArgument(T))
ErrorParams.push_back(*I);
}
}
void
2009-11-04 10:04:32 -05:00
NSErrorChecker::CheckSignature(const FunctionDecl& F, QualType& ResultTy,
2009-06-02 13:58:47 -04:00
llvm::SmallVectorImpl<VarDecl*>& ErrorParams) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
ResultTy = F.getResultType();
2009-10-14 14:03:49 -04:00
for (FunctionDecl::param_const_iterator I = F.param_begin(),
E = F.param_end(); I != E; ++I) {
QualType T = (*I)->getType();
2009-06-02 13:58:47 -04:00
if (isNSErrorWarning) {
if (CheckNSErrorArgument(T)) ErrorParams.push_back(*I);
}
else if (CheckCFErrorArgument(T))
ErrorParams.push_back(*I);
}
}
2009-11-04 10:04:32 -05:00
bool NSErrorChecker::CheckNSErrorArgument(QualType ArgTy) {
2009-10-14 14:03:49 -04:00
const PointerType* PPT = ArgTy->getAs<PointerType>();
if (!PPT)
return false;
const ObjCObjectPointerType* PT =
PPT->getPointeeType()->getAs<ObjCObjectPointerType>();
if (!PT)
return false;
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
// FIXME: Can ID ever be NULL?
if (ID)
return II == ID->getIdentifier();
return false;
2009-06-02 13:58:47 -04:00
}
2009-11-04 10:04:32 -05:00
bool NSErrorChecker::CheckCFErrorArgument(QualType ArgTy) {
2009-10-14 14:03:49 -04:00
const PointerType* PPT = ArgTy->getAs<PointerType>();
2009-06-02 13:58:47 -04:00
if (!PPT) return false;
2009-10-14 14:03:49 -04:00
const TypedefType* TT = PPT->getPointeeType()->getAs<TypedefType>();
2009-06-02 13:58:47 -04:00
if (!TT) return false;
return TT->getDecl()->getIdentifier() == II;
}
2009-11-04 10:04:32 -05:00
void NSErrorChecker::CheckParamDeref(const VarDecl *Param,
2009-10-14 14:03:49 -04:00
const LocationContext *LC,
const GRState *rootState,
2009-06-02 13:58:47 -04:00
BugReporter& BR) {
2009-10-14 14:03:49 -04:00
SVal ParamL = rootState->getLValue(Param, LC);
2009-06-02 13:58:47 -04:00
const MemRegion* ParamR = cast<loc::MemRegionVal>(ParamL).getRegionAs<VarRegion>();
assert (ParamR && "Parameters always have VarRegions.");
2009-06-22 04:08:35 -04:00
SVal ParamSVal = rootState->getSVal(ParamR);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// FIXME: For now assume that ParamSVal is symbolic. We need to generalize
// this later.
SymbolRef ParamSym = ParamSVal.getAsLocSymbol();
if (!ParamSym)
return;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Iterate over the implicit-null dereferences.
2009-11-18 09:59:57 -05:00
ExplodedNode *const* I, *const* E;
llvm::tie(I, E) = GetImplicitNullDereferences(Eng);
for ( ; I != E; ++I) {
2009-06-22 04:08:35 -04:00
const GRState *state = (*I)->getState();
2009-11-18 09:59:57 -05:00
SVal location = state->getSVal((*I)->getLocationAs<StmtPoint>()->getStmt());
if (location.getAsSymbol() != ParamSym)
2009-06-02 13:58:47 -04:00
continue;
// Emit an error.
std::string sbuf;
llvm::raw_string_ostream os(sbuf);
os << "Potential null dereference. According to coding standards ";
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (isNSErrorWarning)
os << "in 'Creating and Returning NSError Objects' the parameter '";
else
os << "documented in CoreFoundation/CFError.h the parameter '";
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
os << Param->getNameAsString() << "' may be null.";
2009-10-14 14:03:49 -04:00
2009-12-01 06:08:04 -05:00
BugReport *report = new BugReport(*this, os.str(), *I);
2009-06-02 13:58:47 -04:00
// FIXME: Notable symbols are now part of the report. We should
// add support for notable symbols in BugReport.
// BR.addNotableSymbol(SV->getSymbol());
BR.EmitReport(report);
}
}