ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept. It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend, whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can imagine, used to circumvent the problem that the registry functions of the checkers are located in the clangStaticAnalyzerCheckers library, but that library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend depends on both of those libraries. One can make the observation however, that CheckerRegistry has no place in Core, it isn't used there at all! The only place where it is used is Frontend, which is where it ultimately belongs. This move implies that since include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function: class CheckerRegistry; void registerBuiltinCheckers(CheckerRegistry ®istry); it had to re purposed, as CheckerRegistry is no longer available to clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h, which actually describes it a lot better -- it does not contain the registration functions for checkers, but only those generated by the tblgen files. Differential Revision: https://reviews.llvm.org/D54436 llvm-svn: 349275
141 lines
4.6 KiB
C++
141 lines
4.6 KiB
C++
//==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This checker adds an assumption that constant globals of certain types* are
|
|
// non-null, as otherwise they generally do not convey any useful information.
|
|
// The assumption is useful, as many framework use e. g. global const strings,
|
|
// and the analyzer might not be able to infer the global value if the
|
|
// definition is in a separate translation unit.
|
|
// The following types (and their typedef aliases) are considered to be
|
|
// non-null:
|
|
// - `char* const`
|
|
// - `const CFStringRef` from CoreFoundation
|
|
// - `NSString* const` from Foundation
|
|
// - `CFBooleanRef` from Foundation
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
|
|
|
using namespace clang;
|
|
using namespace ento;
|
|
|
|
namespace {
|
|
|
|
class NonnullGlobalConstantsChecker : public Checker<check::Location> {
|
|
mutable IdentifierInfo *NSStringII = nullptr;
|
|
mutable IdentifierInfo *CFStringRefII = nullptr;
|
|
mutable IdentifierInfo *CFBooleanRefII = nullptr;
|
|
|
|
public:
|
|
NonnullGlobalConstantsChecker() {}
|
|
|
|
void checkLocation(SVal l, bool isLoad, const Stmt *S,
|
|
CheckerContext &C) const;
|
|
|
|
private:
|
|
void initIdentifierInfo(ASTContext &Ctx) const;
|
|
|
|
bool isGlobalConstString(SVal V) const;
|
|
|
|
bool isNonnullType(QualType Ty) const;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
/// Lazily initialize cache for required identifier information.
|
|
void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
|
if (NSStringII)
|
|
return;
|
|
|
|
NSStringII = &Ctx.Idents.get("NSString");
|
|
CFStringRefII = &Ctx.Idents.get("CFStringRef");
|
|
CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
|
|
}
|
|
|
|
/// Add an assumption that const string-like globals are non-null.
|
|
void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
|
|
const Stmt *S,
|
|
CheckerContext &C) const {
|
|
initIdentifierInfo(C.getASTContext());
|
|
if (!isLoad || !location.isValid())
|
|
return;
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
if (isGlobalConstString(location)) {
|
|
SVal V = State->getSVal(location.castAs<Loc>());
|
|
Optional<DefinedOrUnknownSVal> Constr = V.getAs<DefinedOrUnknownSVal>();
|
|
|
|
if (Constr) {
|
|
|
|
// Assume that the variable is non-null.
|
|
ProgramStateRef OutputState = State->assume(*Constr, true);
|
|
C.addTransition(OutputState);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// \param V loaded lvalue.
|
|
/// \return whether {@code val} is a string-like const global.
|
|
bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
|
|
Optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
|
|
if (!RegionVal)
|
|
return false;
|
|
auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
|
|
if (!Region)
|
|
return false;
|
|
const VarDecl *Decl = Region->getDecl();
|
|
|
|
if (!Decl->hasGlobalStorage())
|
|
return false;
|
|
|
|
QualType Ty = Decl->getType();
|
|
bool HasConst = Ty.isConstQualified();
|
|
if (isNonnullType(Ty) && HasConst)
|
|
return true;
|
|
|
|
// Look through the typedefs.
|
|
while (auto *T = dyn_cast<TypedefType>(Ty)) {
|
|
Ty = T->getDecl()->getUnderlyingType();
|
|
|
|
// It is sufficient for any intermediate typedef
|
|
// to be classified const.
|
|
HasConst = HasConst || Ty.isConstQualified();
|
|
if (isNonnullType(Ty) && HasConst)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// \return whether {@code type} is extremely unlikely to be null
|
|
bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
|
|
|
|
if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
|
|
return true;
|
|
|
|
if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
|
|
return T->getInterfaceDecl() &&
|
|
T->getInterfaceDecl()->getIdentifier() == NSStringII;
|
|
} else if (auto *T = dyn_cast<TypedefType>(Ty)) {
|
|
IdentifierInfo* II = T->getDecl()->getIdentifier();
|
|
return II == CFStringRefII || II == CFBooleanRefII;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
|
|
Mgr.registerChecker<NonnullGlobalConstantsChecker>();
|
|
}
|