10b57cec5SDimitry Andric //===- AnalysisOrderChecker - Print callbacks called ------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This checker prints callbacks that are called during analysis.
100b57cec5SDimitry Andric // This is required to ensure that callbacks are fired in order
110b57cec5SDimitry Andric // and do not duplicate or get lost.
120b57cec5SDimitry Andric // Feel free to extend this checker with any callback you need to check.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h"
170b57cec5SDimitry Andric #include "clang/Analysis/CFGStmtMap.h"
18*5ffd83dbSDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
200b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23*5ffd83dbSDimitry Andric #include "llvm/Support/ErrorHandling.h"
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric using namespace clang;
260b57cec5SDimitry Andric using namespace ento;
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric namespace {
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric class AnalysisOrderChecker
31*5ffd83dbSDimitry Andric : public Checker<
32*5ffd83dbSDimitry Andric check::PreStmt<CastExpr>, check::PostStmt<CastExpr>,
330b57cec5SDimitry Andric check::PreStmt<ArraySubscriptExpr>,
34*5ffd83dbSDimitry Andric check::PostStmt<ArraySubscriptExpr>, check::PreStmt<CXXNewExpr>,
35*5ffd83dbSDimitry Andric check::PostStmt<CXXNewExpr>, check::PreStmt<CXXDeleteExpr>,
36*5ffd83dbSDimitry Andric check::PostStmt<CXXDeleteExpr>, check::PreStmt<CXXConstructExpr>,
37*5ffd83dbSDimitry Andric check::PostStmt<CXXConstructExpr>, check::PreStmt<OffsetOfExpr>,
38*5ffd83dbSDimitry Andric check::PostStmt<OffsetOfExpr>, check::PreCall, check::PostCall,
39*5ffd83dbSDimitry Andric check::EndFunction, check::EndAnalysis, check::NewAllocator,
40*5ffd83dbSDimitry Andric check::Bind, check::PointerEscape, check::RegionChanges,
41*5ffd83dbSDimitry Andric check::LiveSymbols, eval::Call> {
420b57cec5SDimitry Andric
isCallbackEnabled(const AnalyzerOptions & Opts,StringRef CallbackName) const43*5ffd83dbSDimitry Andric bool isCallbackEnabled(const AnalyzerOptions &Opts,
44*5ffd83dbSDimitry Andric StringRef CallbackName) const {
450b57cec5SDimitry Andric return Opts.getCheckerBooleanOption(this, "*") ||
460b57cec5SDimitry Andric Opts.getCheckerBooleanOption(this, CallbackName);
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric
isCallbackEnabled(CheckerContext & C,StringRef CallbackName) const490b57cec5SDimitry Andric bool isCallbackEnabled(CheckerContext &C, StringRef CallbackName) const {
500b57cec5SDimitry Andric AnalyzerOptions &Opts = C.getAnalysisManager().getAnalyzerOptions();
510b57cec5SDimitry Andric return isCallbackEnabled(Opts, CallbackName);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric
isCallbackEnabled(ProgramStateRef State,StringRef CallbackName) const540b57cec5SDimitry Andric bool isCallbackEnabled(ProgramStateRef State, StringRef CallbackName) const {
550b57cec5SDimitry Andric AnalyzerOptions &Opts = State->getStateManager().getOwningEngine()
560b57cec5SDimitry Andric .getAnalysisManager().getAnalyzerOptions();
570b57cec5SDimitry Andric return isCallbackEnabled(Opts, CallbackName);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric public:
checkPreStmt(const CastExpr * CE,CheckerContext & C) const610b57cec5SDimitry Andric void checkPreStmt(const CastExpr *CE, CheckerContext &C) const {
620b57cec5SDimitry Andric if (isCallbackEnabled(C, "PreStmtCastExpr"))
630b57cec5SDimitry Andric llvm::errs() << "PreStmt<CastExpr> (Kind : " << CE->getCastKindName()
640b57cec5SDimitry Andric << ")\n";
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric
checkPostStmt(const CastExpr * CE,CheckerContext & C) const670b57cec5SDimitry Andric void checkPostStmt(const CastExpr *CE, CheckerContext &C) const {
680b57cec5SDimitry Andric if (isCallbackEnabled(C, "PostStmtCastExpr"))
690b57cec5SDimitry Andric llvm::errs() << "PostStmt<CastExpr> (Kind : " << CE->getCastKindName()
700b57cec5SDimitry Andric << ")\n";
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric
checkPreStmt(const ArraySubscriptExpr * SubExpr,CheckerContext & C) const730b57cec5SDimitry Andric void checkPreStmt(const ArraySubscriptExpr *SubExpr,
740b57cec5SDimitry Andric CheckerContext &C) const {
750b57cec5SDimitry Andric if (isCallbackEnabled(C, "PreStmtArraySubscriptExpr"))
760b57cec5SDimitry Andric llvm::errs() << "PreStmt<ArraySubscriptExpr>\n";
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
checkPostStmt(const ArraySubscriptExpr * SubExpr,CheckerContext & C) const790b57cec5SDimitry Andric void checkPostStmt(const ArraySubscriptExpr *SubExpr,
800b57cec5SDimitry Andric CheckerContext &C) const {
810b57cec5SDimitry Andric if (isCallbackEnabled(C, "PostStmtArraySubscriptExpr"))
820b57cec5SDimitry Andric llvm::errs() << "PostStmt<ArraySubscriptExpr>\n";
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
checkPreStmt(const CXXNewExpr * NE,CheckerContext & C) const850b57cec5SDimitry Andric void checkPreStmt(const CXXNewExpr *NE, CheckerContext &C) const {
860b57cec5SDimitry Andric if (isCallbackEnabled(C, "PreStmtCXXNewExpr"))
870b57cec5SDimitry Andric llvm::errs() << "PreStmt<CXXNewExpr>\n";
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
checkPostStmt(const CXXNewExpr * NE,CheckerContext & C) const900b57cec5SDimitry Andric void checkPostStmt(const CXXNewExpr *NE, CheckerContext &C) const {
910b57cec5SDimitry Andric if (isCallbackEnabled(C, "PostStmtCXXNewExpr"))
920b57cec5SDimitry Andric llvm::errs() << "PostStmt<CXXNewExpr>\n";
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
checkPreStmt(const CXXDeleteExpr * NE,CheckerContext & C) const95*5ffd83dbSDimitry Andric void checkPreStmt(const CXXDeleteExpr *NE, CheckerContext &C) const {
96*5ffd83dbSDimitry Andric if (isCallbackEnabled(C, "PreStmtCXXDeleteExpr"))
97*5ffd83dbSDimitry Andric llvm::errs() << "PreStmt<CXXDeleteExpr>\n";
98*5ffd83dbSDimitry Andric }
99*5ffd83dbSDimitry Andric
checkPostStmt(const CXXDeleteExpr * NE,CheckerContext & C) const100*5ffd83dbSDimitry Andric void checkPostStmt(const CXXDeleteExpr *NE, CheckerContext &C) const {
101*5ffd83dbSDimitry Andric if (isCallbackEnabled(C, "PostStmtCXXDeleteExpr"))
102*5ffd83dbSDimitry Andric llvm::errs() << "PostStmt<CXXDeleteExpr>\n";
103*5ffd83dbSDimitry Andric }
104*5ffd83dbSDimitry Andric
checkPreStmt(const CXXConstructExpr * NE,CheckerContext & C) const105*5ffd83dbSDimitry Andric void checkPreStmt(const CXXConstructExpr *NE, CheckerContext &C) const {
106*5ffd83dbSDimitry Andric if (isCallbackEnabled(C, "PreStmtCXXConstructExpr"))
107*5ffd83dbSDimitry Andric llvm::errs() << "PreStmt<CXXConstructExpr>\n";
108*5ffd83dbSDimitry Andric }
109*5ffd83dbSDimitry Andric
checkPostStmt(const CXXConstructExpr * NE,CheckerContext & C) const110*5ffd83dbSDimitry Andric void checkPostStmt(const CXXConstructExpr *NE, CheckerContext &C) const {
111*5ffd83dbSDimitry Andric if (isCallbackEnabled(C, "PostStmtCXXConstructExpr"))
112*5ffd83dbSDimitry Andric llvm::errs() << "PostStmt<CXXConstructExpr>\n";
113*5ffd83dbSDimitry Andric }
114*5ffd83dbSDimitry Andric
checkPreStmt(const OffsetOfExpr * OOE,CheckerContext & C) const1150b57cec5SDimitry Andric void checkPreStmt(const OffsetOfExpr *OOE, CheckerContext &C) const {
1160b57cec5SDimitry Andric if (isCallbackEnabled(C, "PreStmtOffsetOfExpr"))
1170b57cec5SDimitry Andric llvm::errs() << "PreStmt<OffsetOfExpr>\n";
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric
checkPostStmt(const OffsetOfExpr * OOE,CheckerContext & C) const1200b57cec5SDimitry Andric void checkPostStmt(const OffsetOfExpr *OOE, CheckerContext &C) const {
1210b57cec5SDimitry Andric if (isCallbackEnabled(C, "PostStmtOffsetOfExpr"))
1220b57cec5SDimitry Andric llvm::errs() << "PostStmt<OffsetOfExpr>\n";
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric
evalCall(const CallEvent & Call,CheckerContext & C) const125*5ffd83dbSDimitry Andric bool evalCall(const CallEvent &Call, CheckerContext &C) const {
126*5ffd83dbSDimitry Andric if (isCallbackEnabled(C, "EvalCall")) {
127*5ffd83dbSDimitry Andric llvm::errs() << "EvalCall";
128*5ffd83dbSDimitry Andric if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
129*5ffd83dbSDimitry Andric llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
130*5ffd83dbSDimitry Andric llvm::errs() << " {argno: " << Call.getNumArgs() << '}';
131*5ffd83dbSDimitry Andric llvm::errs() << " [" << Call.getKindAsString() << ']';
132*5ffd83dbSDimitry Andric llvm::errs() << '\n';
133*5ffd83dbSDimitry Andric return true;
134*5ffd83dbSDimitry Andric }
135*5ffd83dbSDimitry Andric return false;
136*5ffd83dbSDimitry Andric }
137*5ffd83dbSDimitry Andric
checkPreCall(const CallEvent & Call,CheckerContext & C) const1380b57cec5SDimitry Andric void checkPreCall(const CallEvent &Call, CheckerContext &C) const {
1390b57cec5SDimitry Andric if (isCallbackEnabled(C, "PreCall")) {
1400b57cec5SDimitry Andric llvm::errs() << "PreCall";
1410b57cec5SDimitry Andric if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
1420b57cec5SDimitry Andric llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
143*5ffd83dbSDimitry Andric llvm::errs() << " [" << Call.getKindAsString() << ']';
1440b57cec5SDimitry Andric llvm::errs() << '\n';
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric
checkPostCall(const CallEvent & Call,CheckerContext & C) const1480b57cec5SDimitry Andric void checkPostCall(const CallEvent &Call, CheckerContext &C) const {
1490b57cec5SDimitry Andric if (isCallbackEnabled(C, "PostCall")) {
1500b57cec5SDimitry Andric llvm::errs() << "PostCall";
1510b57cec5SDimitry Andric if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
1520b57cec5SDimitry Andric llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
153*5ffd83dbSDimitry Andric llvm::errs() << " [" << Call.getKindAsString() << ']';
1540b57cec5SDimitry Andric llvm::errs() << '\n';
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
checkEndFunction(const ReturnStmt * S,CheckerContext & C) const1580b57cec5SDimitry Andric void checkEndFunction(const ReturnStmt *S, CheckerContext &C) const {
1590b57cec5SDimitry Andric if (isCallbackEnabled(C, "EndFunction")) {
1600b57cec5SDimitry Andric llvm::errs() << "EndFunction\nReturnStmt: " << (S ? "yes" : "no") << "\n";
1610b57cec5SDimitry Andric if (!S)
1620b57cec5SDimitry Andric return;
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric llvm::errs() << "CFGElement: ";
1650b57cec5SDimitry Andric CFGStmtMap *Map = C.getCurrentAnalysisDeclContext()->getCFGStmtMap();
1660b57cec5SDimitry Andric CFGElement LastElement = Map->getBlock(S)->back();
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric if (LastElement.getAs<CFGStmt>())
1690b57cec5SDimitry Andric llvm::errs() << "CFGStmt\n";
1700b57cec5SDimitry Andric else if (LastElement.getAs<CFGAutomaticObjDtor>())
1710b57cec5SDimitry Andric llvm::errs() << "CFGAutomaticObjDtor\n";
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric
checkEndAnalysis(ExplodedGraph & G,BugReporter & BR,ExprEngine & Eng) const175*5ffd83dbSDimitry Andric void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
176*5ffd83dbSDimitry Andric ExprEngine &Eng) const {
177*5ffd83dbSDimitry Andric if (isCallbackEnabled(BR.getAnalyzerOptions(), "EndAnalysis"))
178*5ffd83dbSDimitry Andric llvm::errs() << "EndAnalysis\n";
179*5ffd83dbSDimitry Andric }
180*5ffd83dbSDimitry Andric
checkNewAllocator(const CXXAllocatorCall & Call,CheckerContext & C) const181*5ffd83dbSDimitry Andric void checkNewAllocator(const CXXAllocatorCall &Call,
1820b57cec5SDimitry Andric CheckerContext &C) const {
1830b57cec5SDimitry Andric if (isCallbackEnabled(C, "NewAllocator"))
1840b57cec5SDimitry Andric llvm::errs() << "NewAllocator\n";
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
checkBind(SVal Loc,SVal Val,const Stmt * S,CheckerContext & C) const1870b57cec5SDimitry Andric void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const {
1880b57cec5SDimitry Andric if (isCallbackEnabled(C, "Bind"))
1890b57cec5SDimitry Andric llvm::errs() << "Bind\n";
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric
checkLiveSymbols(ProgramStateRef State,SymbolReaper & SymReaper) const1920b57cec5SDimitry Andric void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SymReaper) const {
1930b57cec5SDimitry Andric if (isCallbackEnabled(State, "LiveSymbols"))
1940b57cec5SDimitry Andric llvm::errs() << "LiveSymbols\n";
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric ProgramStateRef
checkRegionChanges(ProgramStateRef State,const InvalidatedSymbols * Invalidated,ArrayRef<const MemRegion * > ExplicitRegions,ArrayRef<const MemRegion * > Regions,const LocationContext * LCtx,const CallEvent * Call) const1980b57cec5SDimitry Andric checkRegionChanges(ProgramStateRef State,
1990b57cec5SDimitry Andric const InvalidatedSymbols *Invalidated,
2000b57cec5SDimitry Andric ArrayRef<const MemRegion *> ExplicitRegions,
2010b57cec5SDimitry Andric ArrayRef<const MemRegion *> Regions,
2020b57cec5SDimitry Andric const LocationContext *LCtx, const CallEvent *Call) const {
2030b57cec5SDimitry Andric if (isCallbackEnabled(State, "RegionChanges"))
2040b57cec5SDimitry Andric llvm::errs() << "RegionChanges\n";
2050b57cec5SDimitry Andric return State;
2060b57cec5SDimitry Andric }
207480093f4SDimitry Andric
checkPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const208480093f4SDimitry Andric ProgramStateRef checkPointerEscape(ProgramStateRef State,
209480093f4SDimitry Andric const InvalidatedSymbols &Escaped,
210480093f4SDimitry Andric const CallEvent *Call,
211480093f4SDimitry Andric PointerEscapeKind Kind) const {
212480093f4SDimitry Andric if (isCallbackEnabled(State, "PointerEscape"))
213480093f4SDimitry Andric llvm::errs() << "PointerEscape\n";
214480093f4SDimitry Andric return State;
215480093f4SDimitry Andric }
2160b57cec5SDimitry Andric };
2170b57cec5SDimitry Andric } // end anonymous namespace
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2200b57cec5SDimitry Andric // Registration.
2210b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2220b57cec5SDimitry Andric
registerAnalysisOrderChecker(CheckerManager & mgr)2230b57cec5SDimitry Andric void ento::registerAnalysisOrderChecker(CheckerManager &mgr) {
2240b57cec5SDimitry Andric mgr.registerChecker<AnalysisOrderChecker>();
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric
shouldRegisterAnalysisOrderChecker(const CheckerManager & mgr)227*5ffd83dbSDimitry Andric bool ento::shouldRegisterAnalysisOrderChecker(const CheckerManager &mgr) {
2280b57cec5SDimitry Andric return true;
2290b57cec5SDimitry Andric }
230