1*67e74705SXin Li //===- CheckerDocumentation.cpp - Documentation checker ---------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This checker lists all the checker callbacks and provides documentation for
11*67e74705SXin Li // checker writers.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li
15*67e74705SXin Li #include "ClangSACheckers.h"
16*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
18*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21*67e74705SXin Li
22*67e74705SXin Li using namespace clang;
23*67e74705SXin Li using namespace ento;
24*67e74705SXin Li
25*67e74705SXin Li // All checkers should be placed into anonymous namespace.
26*67e74705SXin Li // We place the CheckerDocumentation inside ento namespace to make the
27*67e74705SXin Li // it visible in doxygen.
28*67e74705SXin Li namespace clang {
29*67e74705SXin Li namespace ento {
30*67e74705SXin Li
31*67e74705SXin Li /// This checker documents the callback functions checkers can use to implement
32*67e74705SXin Li /// the custom handling of the specific events during path exploration as well
33*67e74705SXin Li /// as reporting bugs. Most of the callbacks are targeted at path-sensitive
34*67e74705SXin Li /// checking.
35*67e74705SXin Li ///
36*67e74705SXin Li /// \sa CheckerContext
37*67e74705SXin Li class CheckerDocumentation : public Checker< check::PreStmt<ReturnStmt>,
38*67e74705SXin Li check::PostStmt<DeclStmt>,
39*67e74705SXin Li check::PreObjCMessage,
40*67e74705SXin Li check::PostObjCMessage,
41*67e74705SXin Li check::ObjCMessageNil,
42*67e74705SXin Li check::PreCall,
43*67e74705SXin Li check::PostCall,
44*67e74705SXin Li check::BranchCondition,
45*67e74705SXin Li check::Location,
46*67e74705SXin Li check::Bind,
47*67e74705SXin Li check::DeadSymbols,
48*67e74705SXin Li check::EndFunction,
49*67e74705SXin Li check::EndAnalysis,
50*67e74705SXin Li check::EndOfTranslationUnit,
51*67e74705SXin Li eval::Call,
52*67e74705SXin Li eval::Assume,
53*67e74705SXin Li check::LiveSymbols,
54*67e74705SXin Li check::RegionChanges,
55*67e74705SXin Li check::PointerEscape,
56*67e74705SXin Li check::ConstPointerEscape,
57*67e74705SXin Li check::Event<ImplicitNullDerefEvent>,
58*67e74705SXin Li check::ASTDecl<FunctionDecl> > {
59*67e74705SXin Li public:
60*67e74705SXin Li /// \brief Pre-visit the Statement.
61*67e74705SXin Li ///
62*67e74705SXin Li /// The method will be called before the analyzer core processes the
63*67e74705SXin Li /// statement. The notification is performed for every explored CFGElement,
64*67e74705SXin Li /// which does not include the control flow statements such as IfStmt. The
65*67e74705SXin Li /// callback can be specialized to be called with any subclass of Stmt.
66*67e74705SXin Li ///
67*67e74705SXin Li /// See checkBranchCondition() callback for performing custom processing of
68*67e74705SXin Li /// the branching statements.
69*67e74705SXin Li ///
70*67e74705SXin Li /// check::PreStmt<ReturnStmt>
checkPreStmt(const ReturnStmt * DS,CheckerContext & C) const71*67e74705SXin Li void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}
72*67e74705SXin Li
73*67e74705SXin Li /// \brief Post-visit the Statement.
74*67e74705SXin Li ///
75*67e74705SXin Li /// The method will be called after the analyzer core processes the
76*67e74705SXin Li /// statement. The notification is performed for every explored CFGElement,
77*67e74705SXin Li /// which does not include the control flow statements such as IfStmt. The
78*67e74705SXin Li /// callback can be specialized to be called with any subclass of Stmt.
79*67e74705SXin Li ///
80*67e74705SXin Li /// check::PostStmt<DeclStmt>
81*67e74705SXin Li void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
82*67e74705SXin Li
83*67e74705SXin Li /// \brief Pre-visit the Objective C message.
84*67e74705SXin Li ///
85*67e74705SXin Li /// This will be called before the analyzer core processes the method call.
86*67e74705SXin Li /// This is called for any action which produces an Objective-C message send,
87*67e74705SXin Li /// including explicit message syntax and property access.
88*67e74705SXin Li ///
89*67e74705SXin Li /// check::PreObjCMessage
checkPreObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const90*67e74705SXin Li void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
91*67e74705SXin Li
92*67e74705SXin Li /// \brief Post-visit the Objective C message.
93*67e74705SXin Li /// \sa checkPreObjCMessage()
94*67e74705SXin Li ///
95*67e74705SXin Li /// check::PostObjCMessage
checkPostObjCMessage(const ObjCMethodCall & M,CheckerContext & C) const96*67e74705SXin Li void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
97*67e74705SXin Li
98*67e74705SXin Li /// \brief Visit an Objective-C message whose receiver is nil.
99*67e74705SXin Li ///
100*67e74705SXin Li /// This will be called when the analyzer core processes a method call whose
101*67e74705SXin Li /// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and
102*67e74705SXin Li /// check{Pre/Post}Call will not be called.
103*67e74705SXin Li ///
104*67e74705SXin Li /// check::ObjCMessageNil
checkObjCMessageNil(const ObjCMethodCall & M,CheckerContext & C) const105*67e74705SXin Li void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}
106*67e74705SXin Li
107*67e74705SXin Li /// \brief Pre-visit an abstract "call" event.
108*67e74705SXin Li ///
109*67e74705SXin Li /// This is used for checkers that want to check arguments or attributed
110*67e74705SXin Li /// behavior for functions and methods no matter how they are being invoked.
111*67e74705SXin Li ///
112*67e74705SXin Li /// Note that this includes ALL cross-body invocations, so if you want to
113*67e74705SXin Li /// limit your checks to, say, function calls, you should test for that at the
114*67e74705SXin Li /// beginning of your callback function.
115*67e74705SXin Li ///
116*67e74705SXin Li /// check::PreCall
checkPreCall(const CallEvent & Call,CheckerContext & C) const117*67e74705SXin Li void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}
118*67e74705SXin Li
119*67e74705SXin Li /// \brief Post-visit an abstract "call" event.
120*67e74705SXin Li /// \sa checkPreObjCMessage()
121*67e74705SXin Li ///
122*67e74705SXin Li /// check::PostCall
checkPostCall(const CallEvent & Call,CheckerContext & C) const123*67e74705SXin Li void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}
124*67e74705SXin Li
125*67e74705SXin Li /// \brief Pre-visit of the condition statement of a branch (such as IfStmt).
checkBranchCondition(const Stmt * Condition,CheckerContext & Ctx) const126*67e74705SXin Li void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}
127*67e74705SXin Li
128*67e74705SXin Li /// \brief Called on a load from and a store to a location.
129*67e74705SXin Li ///
130*67e74705SXin Li /// The method will be called each time a location (pointer) value is
131*67e74705SXin Li /// accessed.
132*67e74705SXin Li /// \param Loc The value of the location (pointer).
133*67e74705SXin Li /// \param IsLoad The flag specifying if the location is a store or a load.
134*67e74705SXin Li /// \param S The load is performed while processing the statement.
135*67e74705SXin Li ///
136*67e74705SXin Li /// check::Location
checkLocation(SVal Loc,bool IsLoad,const Stmt * S,CheckerContext &) const137*67e74705SXin Li void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
138*67e74705SXin Li CheckerContext &) const {}
139*67e74705SXin Li
140*67e74705SXin Li /// \brief Called on binding of a value to a location.
141*67e74705SXin Li ///
142*67e74705SXin Li /// \param Loc The value of the location (pointer).
143*67e74705SXin Li /// \param Val The value which will be stored at the location Loc.
144*67e74705SXin Li /// \param S The bind is performed while processing the statement S.
145*67e74705SXin Li ///
146*67e74705SXin Li /// check::Bind
checkBind(SVal Loc,SVal Val,const Stmt * S,CheckerContext &) const147*67e74705SXin Li void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}
148*67e74705SXin Li
149*67e74705SXin Li /// \brief Called whenever a symbol becomes dead.
150*67e74705SXin Li ///
151*67e74705SXin Li /// This callback should be used by the checkers to aggressively clean
152*67e74705SXin Li /// up/reduce the checker state, which is important for reducing the overall
153*67e74705SXin Li /// memory usage. Specifically, if a checker keeps symbol specific information
154*67e74705SXin Li /// in the sate, it can and should be dropped after the symbol becomes dead.
155*67e74705SXin Li /// In addition, reporting a bug as soon as the checker becomes dead leads to
156*67e74705SXin Li /// more precise diagnostics. (For example, one should report that a malloced
157*67e74705SXin Li /// variable is not freed right after it goes out of scope.)
158*67e74705SXin Li ///
159*67e74705SXin Li /// \param SR The SymbolReaper object can be queried to determine which
160*67e74705SXin Li /// symbols are dead.
161*67e74705SXin Li ///
162*67e74705SXin Li /// check::DeadSymbols
checkDeadSymbols(SymbolReaper & SR,CheckerContext & C) const163*67e74705SXin Li void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}
164*67e74705SXin Li
165*67e74705SXin Li
166*67e74705SXin Li /// \brief Called when the analyzer core starts analyzing a function,
167*67e74705SXin Li /// regardless of whether it is analyzed at the top level or is inlined.
168*67e74705SXin Li ///
169*67e74705SXin Li /// check::BeginFunction
checkBeginFunction(CheckerContext & Ctx) const170*67e74705SXin Li void checkBeginFunction(CheckerContext &Ctx) const {}
171*67e74705SXin Li
172*67e74705SXin Li /// \brief Called when the analyzer core reaches the end of a
173*67e74705SXin Li /// function being analyzed regardless of whether it is analyzed at the top
174*67e74705SXin Li /// level or is inlined.
175*67e74705SXin Li ///
176*67e74705SXin Li /// check::EndFunction
checkEndFunction(CheckerContext & Ctx) const177*67e74705SXin Li void checkEndFunction(CheckerContext &Ctx) const {}
178*67e74705SXin Li
179*67e74705SXin Li /// \brief Called after all the paths in the ExplodedGraph reach end of path
180*67e74705SXin Li /// - the symbolic execution graph is fully explored.
181*67e74705SXin Li ///
182*67e74705SXin Li /// This callback should be used in cases when a checker needs to have a
183*67e74705SXin Li /// global view of the information generated on all paths. For example, to
184*67e74705SXin Li /// compare execution summary/result several paths.
185*67e74705SXin Li /// See IdempotentOperationChecker for a usage example.
186*67e74705SXin Li ///
187*67e74705SXin Li /// check::EndAnalysis
checkEndAnalysis(ExplodedGraph & G,BugReporter & BR,ExprEngine & Eng) const188*67e74705SXin Li void checkEndAnalysis(ExplodedGraph &G,
189*67e74705SXin Li BugReporter &BR,
190*67e74705SXin Li ExprEngine &Eng) const {}
191*67e74705SXin Li
192*67e74705SXin Li /// \brief Called after analysis of a TranslationUnit is complete.
193*67e74705SXin Li ///
194*67e74705SXin Li /// check::EndOfTranslationUnit
checkEndOfTranslationUnit(const TranslationUnitDecl * TU,AnalysisManager & Mgr,BugReporter & BR) const195*67e74705SXin Li void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
196*67e74705SXin Li AnalysisManager &Mgr,
197*67e74705SXin Li BugReporter &BR) const {}
198*67e74705SXin Li
199*67e74705SXin Li /// \brief Evaluates function call.
200*67e74705SXin Li ///
201*67e74705SXin Li /// The analysis core threats all function calls in the same way. However, some
202*67e74705SXin Li /// functions have special meaning, which should be reflected in the program
203*67e74705SXin Li /// state. This callback allows a checker to provide domain specific knowledge
204*67e74705SXin Li /// about the particular functions it knows about.
205*67e74705SXin Li ///
206*67e74705SXin Li /// \returns true if the call has been successfully evaluated
207*67e74705SXin Li /// and false otherwise. Note, that only one checker can evaluate a call. If
208*67e74705SXin Li /// more than one checker claims that they can evaluate the same call the
209*67e74705SXin Li /// first one wins.
210*67e74705SXin Li ///
211*67e74705SXin Li /// eval::Call
evalCall(const CallExpr * CE,CheckerContext & C) const212*67e74705SXin Li bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; }
213*67e74705SXin Li
214*67e74705SXin Li /// \brief Handles assumptions on symbolic values.
215*67e74705SXin Li ///
216*67e74705SXin Li /// This method is called when a symbolic expression is assumed to be true or
217*67e74705SXin Li /// false. For example, the assumptions are performed when evaluating a
218*67e74705SXin Li /// condition at a branch. The callback allows checkers track the assumptions
219*67e74705SXin Li /// performed on the symbols of interest and change the state accordingly.
220*67e74705SXin Li ///
221*67e74705SXin Li /// eval::Assume
evalAssume(ProgramStateRef State,SVal Cond,bool Assumption) const222*67e74705SXin Li ProgramStateRef evalAssume(ProgramStateRef State,
223*67e74705SXin Li SVal Cond,
224*67e74705SXin Li bool Assumption) const { return State; }
225*67e74705SXin Li
226*67e74705SXin Li /// Allows modifying SymbolReaper object. For example, checkers can explicitly
227*67e74705SXin Li /// register symbols of interest as live. These symbols will not be marked
228*67e74705SXin Li /// dead and removed.
229*67e74705SXin Li ///
230*67e74705SXin Li /// check::LiveSymbols
checkLiveSymbols(ProgramStateRef State,SymbolReaper & SR) const231*67e74705SXin Li void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}
232*67e74705SXin Li
233*67e74705SXin Li /// \brief Called to determine if the checker currently needs to know if when
234*67e74705SXin Li /// contents of any regions change.
235*67e74705SXin Li ///
236*67e74705SXin Li /// Since it is not necessarily cheap to compute which regions are being
237*67e74705SXin Li /// changed, this allows the analyzer core to skip the more expensive
238*67e74705SXin Li /// #checkRegionChanges when no checkers are tracking any state.
wantsRegionChangeUpdate(ProgramStateRef St) const239*67e74705SXin Li bool wantsRegionChangeUpdate(ProgramStateRef St) const { return true; }
240*67e74705SXin Li
241*67e74705SXin Li /// \brief Called when the contents of one or more regions change.
242*67e74705SXin Li ///
243*67e74705SXin Li /// This can occur in many different ways: an explicit bind, a blanket
244*67e74705SXin Li /// invalidation of the region contents, or by passing a region to a function
245*67e74705SXin Li /// call whose behavior the analyzer cannot model perfectly.
246*67e74705SXin Li ///
247*67e74705SXin Li /// \param State The current program state.
248*67e74705SXin Li /// \param Invalidated A set of all symbols potentially touched by the change.
249*67e74705SXin Li /// \param ExplicitRegions The regions explicitly requested for invalidation.
250*67e74705SXin Li /// For a function call, this would be the arguments. For a bind, this
251*67e74705SXin Li /// would be the region being bound to.
252*67e74705SXin Li /// \param Regions The transitive closure of regions accessible from,
253*67e74705SXin Li /// \p ExplicitRegions, i.e. all regions that may have been touched
254*67e74705SXin Li /// by this change. For a simple bind, this list will be the same as
255*67e74705SXin Li /// \p ExplicitRegions, since a bind does not affect the contents of
256*67e74705SXin Li /// anything accessible through the base region.
257*67e74705SXin Li /// \param Call The opaque call triggering this invalidation. Will be 0 if the
258*67e74705SXin Li /// change was not triggered by a call.
259*67e74705SXin Li ///
260*67e74705SXin Li /// Note that this callback will not be invoked unless
261*67e74705SXin Li /// #wantsRegionChangeUpdate returns \c true.
262*67e74705SXin Li ///
263*67e74705SXin Li /// check::RegionChanges
264*67e74705SXin Li ProgramStateRef
checkRegionChanges(ProgramStateRef State,const InvalidatedSymbols * Invalidated,ArrayRef<const MemRegion * > ExplicitRegions,ArrayRef<const MemRegion * > Regions,const CallEvent * Call) const265*67e74705SXin Li checkRegionChanges(ProgramStateRef State,
266*67e74705SXin Li const InvalidatedSymbols *Invalidated,
267*67e74705SXin Li ArrayRef<const MemRegion *> ExplicitRegions,
268*67e74705SXin Li ArrayRef<const MemRegion *> Regions,
269*67e74705SXin Li const CallEvent *Call) const {
270*67e74705SXin Li return State;
271*67e74705SXin Li }
272*67e74705SXin Li
273*67e74705SXin Li /// \brief Called when pointers escape.
274*67e74705SXin Li ///
275*67e74705SXin Li /// This notifies the checkers about pointer escape, which occurs whenever
276*67e74705SXin Li /// the analyzer cannot track the symbol any more. For example, as a
277*67e74705SXin Li /// result of assigning a pointer into a global or when it's passed to a
278*67e74705SXin Li /// function call the analyzer cannot model.
279*67e74705SXin Li ///
280*67e74705SXin Li /// \param State The state at the point of escape.
281*67e74705SXin Li /// \param Escaped The list of escaped symbols.
282*67e74705SXin Li /// \param Call The corresponding CallEvent, if the symbols escape as
283*67e74705SXin Li /// parameters to the given call.
284*67e74705SXin Li /// \param Kind How the symbols have escaped.
285*67e74705SXin Li /// \returns Checkers can modify the state by returning a new state.
checkPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const286*67e74705SXin Li ProgramStateRef checkPointerEscape(ProgramStateRef State,
287*67e74705SXin Li const InvalidatedSymbols &Escaped,
288*67e74705SXin Li const CallEvent *Call,
289*67e74705SXin Li PointerEscapeKind Kind) const {
290*67e74705SXin Li return State;
291*67e74705SXin Li }
292*67e74705SXin Li
293*67e74705SXin Li /// \brief Called when const pointers escape.
294*67e74705SXin Li ///
295*67e74705SXin Li /// Note: in most cases checkPointerEscape callback is sufficient.
296*67e74705SXin Li /// \sa checkPointerEscape
checkConstPointerEscape(ProgramStateRef State,const InvalidatedSymbols & Escaped,const CallEvent * Call,PointerEscapeKind Kind) const297*67e74705SXin Li ProgramStateRef checkConstPointerEscape(ProgramStateRef State,
298*67e74705SXin Li const InvalidatedSymbols &Escaped,
299*67e74705SXin Li const CallEvent *Call,
300*67e74705SXin Li PointerEscapeKind Kind) const {
301*67e74705SXin Li return State;
302*67e74705SXin Li }
303*67e74705SXin Li
304*67e74705SXin Li /// check::Event<ImplicitNullDerefEvent>
checkEvent(ImplicitNullDerefEvent Event) const305*67e74705SXin Li void checkEvent(ImplicitNullDerefEvent Event) const {}
306*67e74705SXin Li
307*67e74705SXin Li /// \brief Check every declaration in the AST.
308*67e74705SXin Li ///
309*67e74705SXin Li /// An AST traversal callback, which should only be used when the checker is
310*67e74705SXin Li /// not path sensitive. It will be called for every Declaration in the AST and
311*67e74705SXin Li /// can be specialized to only be called on subclasses of Decl, for example,
312*67e74705SXin Li /// FunctionDecl.
313*67e74705SXin Li ///
314*67e74705SXin Li /// check::ASTDecl<FunctionDecl>
checkASTDecl(const FunctionDecl * D,AnalysisManager & Mgr,BugReporter & BR) const315*67e74705SXin Li void checkASTDecl(const FunctionDecl *D,
316*67e74705SXin Li AnalysisManager &Mgr,
317*67e74705SXin Li BugReporter &BR) const {}
318*67e74705SXin Li };
319*67e74705SXin Li
checkPostStmt(const DeclStmt * DS,CheckerContext & C) const320*67e74705SXin Li void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,
321*67e74705SXin Li CheckerContext &C) const {
322*67e74705SXin Li }
323*67e74705SXin Li
324*67e74705SXin Li } // end namespace ento
325*67e74705SXin Li } // end namespace clang
326