
One of the reasons why AnalyzerOptions is so chaotic is that options can be retrieved from the command line whenever and wherever. This allowed for some options to be forgotten for a looooooong time. Have you ever heard of "region-store-small-struct-limit"? In order to prevent this in the future, I'm proposing to restrict AnalyzerOptions' interface so that only checker options can be retrieved without special getters. I would like to make every option be accessible only through a getter, but checkers from plugins are a thing, so I'll have to figure something out for that. This also forces developers who'd like to add a new option to register it properly in the .def file. This is done by * making the third checker pointer parameter non-optional, and checked by an assert to be non-null. * I added new, but private non-checkers option initializers, meant only for internal use, * Renamed these methods accordingly (mind the consistent name for once with getBooleanOption!): - getOptionAsString -> getCheckerStringOption, - getOptionAsInteger -> getCheckerIntegerOption * The 3 functions meant for initializing data members (with the not very descriptive getBooleanOption, getOptionAsString and getOptionAsUInt names) were renamed to be overloads of the getAndInitOption function name. * All options were in some way retrieved via getCheckerOption. I removed it, and moved the logic to getStringOption and getCheckerStringOption. This did cause some code duplication, but that's the only way I could do it, now that checker and non-checker options are separated. Note that the non-checker version inserts the new option to the ConfigTable with the default value, but the checker version only attempts to find already existing entries. This is how it always worked, but this is clunky and I might end reworking that too, so we can eventually get a ConfigTable that contains the entire configuration of the analyzer. Differential Revision: https://reviews.llvm.org/D53483 llvm-svn: 346113
179 lines
6.5 KiB
C++
179 lines
6.5 KiB
C++
//===- AnalysisOrderChecker - Print callbacks called ------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This checker prints callbacks that are called during analysis.
|
|
// This is required to ensure that callbacks are fired in order
|
|
// and do not duplicate or get lost.
|
|
// Feel free to extend this checker with any callback you need to check.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "ClangSACheckers.h"
|
|
#include "clang/AST/ExprCXX.h"
|
|
#include "clang/Analysis/CFGStmtMap.h"
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
using namespace clang;
|
|
using namespace ento;
|
|
|
|
namespace {
|
|
|
|
class AnalysisOrderChecker
|
|
: public Checker<check::PreStmt<CastExpr>,
|
|
check::PostStmt<CastExpr>,
|
|
check::PreStmt<ArraySubscriptExpr>,
|
|
check::PostStmt<ArraySubscriptExpr>,
|
|
check::PreStmt<CXXNewExpr>,
|
|
check::PostStmt<CXXNewExpr>,
|
|
check::PreStmt<OffsetOfExpr>,
|
|
check::PostStmt<OffsetOfExpr>,
|
|
check::PreCall,
|
|
check::PostCall,
|
|
check::EndFunction,
|
|
check::NewAllocator,
|
|
check::Bind,
|
|
check::RegionChanges,
|
|
check::LiveSymbols> {
|
|
|
|
bool isCallbackEnabled(AnalyzerOptions &Opts, StringRef CallbackName) const {
|
|
return Opts.getCheckerBooleanOption("*", false, this) ||
|
|
Opts.getCheckerBooleanOption(CallbackName, false, this);
|
|
}
|
|
|
|
bool isCallbackEnabled(CheckerContext &C, StringRef CallbackName) const {
|
|
AnalyzerOptions &Opts = C.getAnalysisManager().getAnalyzerOptions();
|
|
return isCallbackEnabled(Opts, CallbackName);
|
|
}
|
|
|
|
bool isCallbackEnabled(ProgramStateRef State, StringRef CallbackName) const {
|
|
AnalyzerOptions &Opts = State->getStateManager().getOwningEngine()
|
|
->getAnalysisManager().getAnalyzerOptions();
|
|
return isCallbackEnabled(Opts, CallbackName);
|
|
}
|
|
|
|
public:
|
|
void checkPreStmt(const CastExpr *CE, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PreStmtCastExpr"))
|
|
llvm::errs() << "PreStmt<CastExpr> (Kind : " << CE->getCastKindName()
|
|
<< ")\n";
|
|
}
|
|
|
|
void checkPostStmt(const CastExpr *CE, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PostStmtCastExpr"))
|
|
llvm::errs() << "PostStmt<CastExpr> (Kind : " << CE->getCastKindName()
|
|
<< ")\n";
|
|
}
|
|
|
|
void checkPreStmt(const ArraySubscriptExpr *SubExpr,
|
|
CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PreStmtArraySubscriptExpr"))
|
|
llvm::errs() << "PreStmt<ArraySubscriptExpr>\n";
|
|
}
|
|
|
|
void checkPostStmt(const ArraySubscriptExpr *SubExpr,
|
|
CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PostStmtArraySubscriptExpr"))
|
|
llvm::errs() << "PostStmt<ArraySubscriptExpr>\n";
|
|
}
|
|
|
|
void checkPreStmt(const CXXNewExpr *NE, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PreStmtCXXNewExpr"))
|
|
llvm::errs() << "PreStmt<CXXNewExpr>\n";
|
|
}
|
|
|
|
void checkPostStmt(const CXXNewExpr *NE, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PostStmtCXXNewExpr"))
|
|
llvm::errs() << "PostStmt<CXXNewExpr>\n";
|
|
}
|
|
|
|
void checkPreStmt(const OffsetOfExpr *OOE, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PreStmtOffsetOfExpr"))
|
|
llvm::errs() << "PreStmt<OffsetOfExpr>\n";
|
|
}
|
|
|
|
void checkPostStmt(const OffsetOfExpr *OOE, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PostStmtOffsetOfExpr"))
|
|
llvm::errs() << "PostStmt<OffsetOfExpr>\n";
|
|
}
|
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PreCall")) {
|
|
llvm::errs() << "PreCall";
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
|
|
llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
|
|
llvm::errs() << '\n';
|
|
}
|
|
}
|
|
|
|
void checkPostCall(const CallEvent &Call, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "PostCall")) {
|
|
llvm::errs() << "PostCall";
|
|
if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
|
|
llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
|
|
llvm::errs() << '\n';
|
|
}
|
|
}
|
|
|
|
void checkEndFunction(const ReturnStmt *S, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "EndFunction")) {
|
|
llvm::errs() << "EndFunction\nReturnStmt: " << (S ? "yes" : "no") << "\n";
|
|
if (!S)
|
|
return;
|
|
|
|
llvm::errs() << "CFGElement: ";
|
|
CFGStmtMap *Map = C.getCurrentAnalysisDeclContext()->getCFGStmtMap();
|
|
CFGElement LastElement = Map->getBlock(S)->back();
|
|
|
|
if (LastElement.getAs<CFGStmt>())
|
|
llvm::errs() << "CFGStmt\n";
|
|
else if (LastElement.getAs<CFGAutomaticObjDtor>())
|
|
llvm::errs() << "CFGAutomaticObjDtor\n";
|
|
}
|
|
}
|
|
|
|
void checkNewAllocator(const CXXNewExpr *CNE, SVal Target,
|
|
CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "NewAllocator"))
|
|
llvm::errs() << "NewAllocator\n";
|
|
}
|
|
|
|
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const {
|
|
if (isCallbackEnabled(C, "Bind"))
|
|
llvm::errs() << "Bind\n";
|
|
}
|
|
|
|
void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SymReaper) const {
|
|
if (isCallbackEnabled(State, "LiveSymbols"))
|
|
llvm::errs() << "LiveSymbols\n";
|
|
}
|
|
|
|
ProgramStateRef
|
|
checkRegionChanges(ProgramStateRef State,
|
|
const InvalidatedSymbols *Invalidated,
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
|
ArrayRef<const MemRegion *> Regions,
|
|
const LocationContext *LCtx, const CallEvent *Call) const {
|
|
if (isCallbackEnabled(State, "RegionChanges"))
|
|
llvm::errs() << "RegionChanges\n";
|
|
return State;
|
|
}
|
|
};
|
|
} // end anonymous namespace
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Registration.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void ento::registerAnalysisOrderChecker(CheckerManager &mgr) {
|
|
mgr.registerChecker<AnalysisOrderChecker>();
|
|
}
|