clang 22.0.0git
AnalysisOrderChecker.cpp
Go to the documentation of this file.
1//===- AnalysisOrderChecker - Print callbacks called ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This checker prints callbacks that are called during analysis.
10// This is required to ensure that callbacks are fired in order
11// and do not duplicate or get lost.
12// Feel free to extend this checker with any callback you need to check.
13//
14//===----------------------------------------------------------------------===//
15
16#include "clang/AST/ExprCXX.h"
23
24using namespace clang;
25using namespace ento;
26
27namespace {
28
29class AnalysisOrderChecker
30 : public Checker<
31 check::PreStmt<CastExpr>, check::PostStmt<CastExpr>,
32 check::PreStmt<ArraySubscriptExpr>,
33 check::PostStmt<ArraySubscriptExpr>, check::PreStmt<CXXNewExpr>,
34 check::PostStmt<CXXNewExpr>, check::PreStmt<CXXDeleteExpr>,
35 check::PostStmt<CXXDeleteExpr>, check::PreStmt<CXXConstructExpr>,
36 check::PostStmt<CXXConstructExpr>, check::PreStmt<OffsetOfExpr>,
37 check::PostStmt<OffsetOfExpr>, check::PreCall, check::PostCall,
38 check::EndFunction, check::EndAnalysis, check::NewAllocator,
39 check::Bind, check::PointerEscape, check::RegionChanges,
40 check::LiveSymbols, eval::Call> {
41
42 bool isCallbackEnabled(const AnalyzerOptions &Opts,
43 StringRef CallbackName) const {
44 return Opts.getCheckerBooleanOption(this, "*") ||
45 Opts.getCheckerBooleanOption(this, CallbackName);
46 }
47
48 bool isCallbackEnabled(CheckerContext &C, StringRef CallbackName) const {
49 AnalyzerOptions &Opts = C.getAnalysisManager().getAnalyzerOptions();
50 return isCallbackEnabled(Opts, CallbackName);
51 }
52
53 bool isCallbackEnabled(ProgramStateRef State, StringRef CallbackName) const {
54 AnalyzerOptions &Opts = State->getStateManager().getOwningEngine()
55 .getAnalysisManager().getAnalyzerOptions();
56 return isCallbackEnabled(Opts, CallbackName);
57 }
58
59public:
60 void checkPreStmt(const CastExpr *CE, CheckerContext &C) const {
61 if (isCallbackEnabled(C, "PreStmtCastExpr"))
62 llvm::errs() << "PreStmt<CastExpr> (Kind : " << CE->getCastKindName()
63 << ")\n";
64 }
65
66 void checkPostStmt(const CastExpr *CE, CheckerContext &C) const {
67 if (isCallbackEnabled(C, "PostStmtCastExpr"))
68 llvm::errs() << "PostStmt<CastExpr> (Kind : " << CE->getCastKindName()
69 << ")\n";
70 }
71
72 void checkPreStmt(const ArraySubscriptExpr *SubExpr,
73 CheckerContext &C) const {
74 if (isCallbackEnabled(C, "PreStmtArraySubscriptExpr"))
75 llvm::errs() << "PreStmt<ArraySubscriptExpr>\n";
76 }
77
78 void checkPostStmt(const ArraySubscriptExpr *SubExpr,
79 CheckerContext &C) const {
80 if (isCallbackEnabled(C, "PostStmtArraySubscriptExpr"))
81 llvm::errs() << "PostStmt<ArraySubscriptExpr>\n";
82 }
83
84 void checkPreStmt(const CXXNewExpr *NE, CheckerContext &C) const {
85 if (isCallbackEnabled(C, "PreStmtCXXNewExpr"))
86 llvm::errs() << "PreStmt<CXXNewExpr>\n";
87 }
88
89 void checkPostStmt(const CXXNewExpr *NE, CheckerContext &C) const {
90 if (isCallbackEnabled(C, "PostStmtCXXNewExpr"))
91 llvm::errs() << "PostStmt<CXXNewExpr>\n";
92 }
93
94 void checkPreStmt(const CXXDeleteExpr *NE, CheckerContext &C) const {
95 if (isCallbackEnabled(C, "PreStmtCXXDeleteExpr"))
96 llvm::errs() << "PreStmt<CXXDeleteExpr>\n";
97 }
98
99 void checkPostStmt(const CXXDeleteExpr *NE, CheckerContext &C) const {
100 if (isCallbackEnabled(C, "PostStmtCXXDeleteExpr"))
101 llvm::errs() << "PostStmt<CXXDeleteExpr>\n";
102 }
103
104 void checkPreStmt(const CXXConstructExpr *NE, CheckerContext &C) const {
105 if (isCallbackEnabled(C, "PreStmtCXXConstructExpr"))
106 llvm::errs() << "PreStmt<CXXConstructExpr>\n";
107 }
108
109 void checkPostStmt(const CXXConstructExpr *NE, CheckerContext &C) const {
110 if (isCallbackEnabled(C, "PostStmtCXXConstructExpr"))
111 llvm::errs() << "PostStmt<CXXConstructExpr>\n";
112 }
113
114 void checkPreStmt(const OffsetOfExpr *OOE, CheckerContext &C) const {
115 if (isCallbackEnabled(C, "PreStmtOffsetOfExpr"))
116 llvm::errs() << "PreStmt<OffsetOfExpr>\n";
117 }
118
119 void checkPostStmt(const OffsetOfExpr *OOE, CheckerContext &C) const {
120 if (isCallbackEnabled(C, "PostStmtOffsetOfExpr"))
121 llvm::errs() << "PostStmt<OffsetOfExpr>\n";
122 }
123
124 bool evalCall(const CallEvent &Call, CheckerContext &C) const {
125 if (isCallbackEnabled(C, "EvalCall")) {
126 llvm::errs() << "EvalCall";
127 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
128 llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
129 llvm::errs() << " {argno: " << Call.getNumArgs() << '}';
130 llvm::errs() << " [" << Call.getKindAsString() << ']';
131 llvm::errs() << '\n';
132 // We can't return `true` from this callback without binding the return
133 // value. Let's just fallthrough here and return `false`.
134 }
135 return false;
136 }
137
138 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {
139 if (isCallbackEnabled(C, "PreCall")) {
140 llvm::errs() << "PreCall";
141 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
142 llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
143 llvm::errs() << " [" << Call.getKindAsString() << ']';
144 llvm::errs() << '\n';
145 }
146 }
147
148 void checkPostCall(const CallEvent &Call, CheckerContext &C) const {
149 if (isCallbackEnabled(C, "PostCall")) {
150 llvm::errs() << "PostCall";
151 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Call.getDecl()))
152 llvm::errs() << " (" << ND->getQualifiedNameAsString() << ')';
153 llvm::errs() << " [" << Call.getKindAsString() << ']';
154 llvm::errs() << '\n';
155 }
156 }
157
158 void checkEndFunction(const ReturnStmt *S, CheckerContext &C) const {
159 if (isCallbackEnabled(C, "EndFunction")) {
160 llvm::errs() << "EndFunction\nReturnStmt: " << (S ? "yes" : "no") << "\n";
161 if (!S)
162 return;
163
164 llvm::errs() << "CFGElement: ";
165 const CFGStmtMap *Map =
166 C.getCurrentAnalysisDeclContext()->getCFGStmtMap();
167 CFGElement LastElement = Map->getBlock(S)->back();
168
169 if (LastElement.getAs<CFGStmt>())
170 llvm::errs() << "CFGStmt\n";
171 else if (LastElement.getAs<CFGAutomaticObjDtor>())
172 llvm::errs() << "CFGAutomaticObjDtor\n";
173 }
174 }
175
176 void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
177 ExprEngine &Eng) const {
178 if (isCallbackEnabled(BR.getAnalyzerOptions(), "EndAnalysis"))
179 llvm::errs() << "EndAnalysis\n";
180 }
181
182 void checkNewAllocator(const CXXAllocatorCall &Call,
183 CheckerContext &C) const {
184 if (isCallbackEnabled(C, "NewAllocator"))
185 llvm::errs() << "NewAllocator\n";
186 }
187
188 void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit,
189 CheckerContext &C) const {
190 if (isCallbackEnabled(C, "Bind"))
191 llvm::errs() << "Bind\n";
192 }
193
194 void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SymReaper) const {
195 if (isCallbackEnabled(State, "LiveSymbols"))
196 llvm::errs() << "LiveSymbols\n";
197 }
198
200 checkRegionChanges(ProgramStateRef State,
201 const InvalidatedSymbols *Invalidated,
202 ArrayRef<const MemRegion *> ExplicitRegions,
203 ArrayRef<const MemRegion *> Regions,
204 const LocationContext *LCtx, const CallEvent *Call) const {
205 if (isCallbackEnabled(State, "RegionChanges"))
206 llvm::errs() << "RegionChanges\n";
207 return State;
208 }
209
210 ProgramStateRef checkPointerEscape(ProgramStateRef State,
211 const InvalidatedSymbols &Escaped,
212 const CallEvent *Call,
213 PointerEscapeKind Kind) const {
214 if (isCallbackEnabled(State, "PointerEscape"))
215 llvm::errs() << "PointerEscape\n";
216 return State;
217 }
218};
219} // end anonymous namespace
220
221//===----------------------------------------------------------------------===//
222// Registration.
223//===----------------------------------------------------------------------===//
224
225void ento::registerAnalysisOrderChecker(CheckerManager &mgr) {
226 mgr.registerChecker<AnalysisOrderChecker>();
227}
228
229bool ento::shouldRegisterAnalysisOrderChecker(const CheckerManager &mgr) {
230 return true;
231}
Defines the clang::Expr interface and subclasses for C++ expressions.
CFGElement back() const
Definition CFG.h:908
std::optional< T > getAs() const
Convert to the specified CFGElement type, returning std::nullopt if this CFGElement is not of the des...
Definition CFG.h:109
const CFGBlock * getBlock(const Stmt *S) const
Returns the CFGBlock the specified Stmt* appears in.
static const char * getCastKindName(CastKind CK)
Definition Expr.cpp:1950
const AnalyzerOptions & getAnalyzerOptions()
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
PointerEscapeKind
Describes the different reasons a pointer escapes during analysis.
llvm::DenseSet< SymbolRef > InvalidatedSymbols
Definition Store.h:51
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The JSON file list parser is used to communicate input to InstallAPI.