1 //===- CheckerDocumentation.cpp - Documentation checker ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This checker lists all the checker callbacks and provides documentation for
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21 using namespace clang
;
24 // All checkers should be placed into anonymous namespace.
25 // We place the CheckerDocumentation inside ento namespace to make the
26 // it visible in doxygen.
30 /// This checker documents the callback functions checkers can use to implement
31 /// the custom handling of the specific events during path exploration as well
32 /// as reporting bugs. Most of the callbacks are targeted at path-sensitive
35 /// \sa CheckerContext
36 class CheckerDocumentation
40 check::ASTDecl
<FunctionDecl
>,
43 check::BranchCondition
,
44 check::ConstPointerEscape
,
48 check::EndOfTranslationUnit
,
49 check::Event
<ImplicitNullDerefEvent
>,
53 check::ObjCMessageNil
,
56 check::PostObjCMessage
,
57 check::PostStmt
<DeclStmt
>,
59 check::PreObjCMessage
,
60 check::PreStmt
<ReturnStmt
>,
67 /// Pre-visit the Statement.
69 /// The method will be called before the analyzer core processes the
70 /// statement. The notification is performed for every explored CFGElement,
71 /// which does not include the control flow statements such as IfStmt. The
72 /// callback can be specialized to be called with any subclass of Stmt.
74 /// See checkBranchCondition() callback for performing custom processing of
75 /// the branching statements.
77 /// check::PreStmt<ReturnStmt>
78 void checkPreStmt(const ReturnStmt
*DS
, CheckerContext
&C
) const {}
80 /// Post-visit the Statement.
82 /// The method will be called after the analyzer core processes the
83 /// statement. The notification is performed for every explored CFGElement,
84 /// which does not include the control flow statements such as IfStmt. The
85 /// callback can be specialized to be called with any subclass of Stmt.
87 /// check::PostStmt<DeclStmt>
88 void checkPostStmt(const DeclStmt
*DS
, CheckerContext
&C
) const;
90 /// Pre-visit the Objective C message.
92 /// This will be called before the analyzer core processes the method call.
93 /// This is called for any action which produces an Objective-C message send,
94 /// including explicit message syntax and property access.
96 /// check::PreObjCMessage
97 void checkPreObjCMessage(const ObjCMethodCall
&M
, CheckerContext
&C
) const {}
99 /// Post-visit the Objective C message.
100 /// \sa checkPreObjCMessage()
102 /// check::PostObjCMessage
103 void checkPostObjCMessage(const ObjCMethodCall
&M
, CheckerContext
&C
) const {}
105 /// Visit an Objective-C message whose receiver is nil.
107 /// This will be called when the analyzer core processes a method call whose
108 /// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and
109 /// check{Pre/Post}Call will not be called.
111 /// check::ObjCMessageNil
112 void checkObjCMessageNil(const ObjCMethodCall
&M
, CheckerContext
&C
) const {}
114 /// Pre-visit an abstract "call" event.
116 /// This is used for checkers that want to check arguments or attributed
117 /// behavior for functions and methods no matter how they are being invoked.
119 /// Note that this includes ALL cross-body invocations, so if you want to
120 /// limit your checks to, say, function calls, you should test for that at the
121 /// beginning of your callback function.
124 void checkPreCall(const CallEvent
&Call
, CheckerContext
&C
) const {}
126 /// Post-visit an abstract "call" event.
127 /// \sa checkPreObjCMessage()
130 void checkPostCall(const CallEvent
&Call
, CheckerContext
&C
) const {}
132 /// Pre-visit of the condition statement of a branch (such as IfStmt).
133 void checkBranchCondition(const Stmt
*Condition
, CheckerContext
&Ctx
) const {}
135 /// Post-visit the C++ operator new's allocation call.
137 /// Execution of C++ operator new consists of the following phases: (1) call
138 /// default or overridden operator new() to allocate memory (2) cast the
139 /// return value of operator new() from void pointer type to class pointer
140 /// type, (3) assuming that the value is non-null, call the object's
141 /// constructor over this pointer, (4) declare that the value of the
142 /// new-expression is this pointer. This callback is called between steps
143 /// (2) and (3). Post-call for the allocator is called after step (1).
144 /// Pre-statement for the new-expression is called on step (4) when the value
145 /// of the expression is evaluated.
146 void checkNewAllocator(const CXXAllocatorCall
&, CheckerContext
&) const {}
148 /// Called on a load from and a store to a location.
150 /// The method will be called each time a location (pointer) value is
152 /// \param Loc The value of the location (pointer).
153 /// \param IsLoad The flag specifying if the location is a store or a load.
154 /// \param S The load is performed while processing the statement.
157 void checkLocation(SVal Loc
, bool IsLoad
, const Stmt
*S
,
158 CheckerContext
&) const {}
160 /// Called on binding of a value to a location.
162 /// \param Loc The value of the location (pointer).
163 /// \param Val The value which will be stored at the location Loc.
164 /// \param S The bind is performed while processing the statement S.
167 void checkBind(SVal Loc
, SVal Val
, const Stmt
*S
, CheckerContext
&) const {}
169 /// Called whenever a symbol becomes dead.
171 /// This callback should be used by the checkers to aggressively clean
172 /// up/reduce the checker state, which is important for reducing the overall
173 /// memory usage. Specifically, if a checker keeps symbol specific information
174 /// in the state, it can and should be dropped after the symbol becomes dead.
175 /// In addition, reporting a bug as soon as the checker becomes dead leads to
176 /// more precise diagnostics. (For example, one should report that a malloced
177 /// variable is not freed right after it goes out of scope.)
179 /// \param SR The SymbolReaper object can be queried to determine which
180 /// symbols are dead.
182 /// check::DeadSymbols
183 void checkDeadSymbols(SymbolReaper
&SR
, CheckerContext
&C
) const {}
186 /// Called when the analyzer core starts analyzing a function,
187 /// regardless of whether it is analyzed at the top level or is inlined.
189 /// check::BeginFunction
190 void checkBeginFunction(CheckerContext
&Ctx
) const {}
192 /// Called when the analyzer core reaches the end of a
193 /// function being analyzed regardless of whether it is analyzed at the top
194 /// level or is inlined.
196 /// check::EndFunction
197 void checkEndFunction(const ReturnStmt
*RS
, CheckerContext
&Ctx
) const {}
199 /// Called after all the paths in the ExplodedGraph reach end of path
200 /// - the symbolic execution graph is fully explored.
202 /// This callback should be used in cases when a checker needs to have a
203 /// global view of the information generated on all paths. For example, to
204 /// compare execution summary/result several paths.
205 /// See IdempotentOperationChecker for a usage example.
207 /// check::EndAnalysis
208 void checkEndAnalysis(ExplodedGraph
&G
,
210 ExprEngine
&Eng
) const {}
212 /// Called after analysis of a TranslationUnit is complete.
214 /// check::EndOfTranslationUnit
215 void checkEndOfTranslationUnit(const TranslationUnitDecl
*TU
,
216 AnalysisManager
&Mgr
,
217 BugReporter
&BR
) const {}
219 /// Evaluates function call.
221 /// The analysis core treats all function calls in the same way. However, some
222 /// functions have special meaning, which should be reflected in the program
223 /// state. This callback allows a checker to provide domain specific knowledge
224 /// about the particular functions it knows about.
226 /// \returns true if the call has been successfully evaluated
227 /// and false otherwise. Note, that only one checker can evaluate a call. If
228 /// more than one checker claims that they can evaluate the same call the
232 bool evalCall(const CallEvent
&Call
, CheckerContext
&C
) const { return true; }
234 /// Handles assumptions on symbolic values.
236 /// This method is called when a symbolic expression is assumed to be true or
237 /// false. For example, the assumptions are performed when evaluating a
238 /// condition at a branch. The callback allows checkers track the assumptions
239 /// performed on the symbols of interest and change the state accordingly.
242 ProgramStateRef
evalAssume(ProgramStateRef State
,
244 bool Assumption
) const { return State
; }
246 /// Allows modifying SymbolReaper object. For example, checkers can explicitly
247 /// register symbols of interest as live. These symbols will not be marked
248 /// dead and removed.
250 /// check::LiveSymbols
251 void checkLiveSymbols(ProgramStateRef State
, SymbolReaper
&SR
) const {}
253 /// Called when the contents of one or more regions change.
255 /// This can occur in many different ways: an explicit bind, a blanket
256 /// invalidation of the region contents, or by passing a region to a function
257 /// call whose behavior the analyzer cannot model perfectly.
259 /// \param State The current program state.
260 /// \param Invalidated A set of all symbols potentially touched by the change.
261 /// \param ExplicitRegions The regions explicitly requested for invalidation.
262 /// For a function call, this would be the arguments. For a bind, this
263 /// would be the region being bound to.
264 /// \param Regions The transitive closure of regions accessible from,
265 /// \p ExplicitRegions, i.e. all regions that may have been touched
266 /// by this change. For a simple bind, this list will be the same as
267 /// \p ExplicitRegions, since a bind does not affect the contents of
268 /// anything accessible through the base region.
269 /// \param LCtx LocationContext that is useful for getting various contextual
270 /// info, like callstack, CFG etc.
271 /// \param Call The opaque call triggering this invalidation. Will be 0 if the
272 /// change was not triggered by a call.
274 /// check::RegionChanges
276 checkRegionChanges(ProgramStateRef State
,
277 const InvalidatedSymbols
*Invalidated
,
278 ArrayRef
<const MemRegion
*> ExplicitRegions
,
279 ArrayRef
<const MemRegion
*> Regions
,
280 const LocationContext
*LCtx
,
281 const CallEvent
*Call
) const {
285 /// Called when pointers escape.
287 /// This notifies the checkers about pointer escape, which occurs whenever
288 /// the analyzer cannot track the symbol any more. For example, as a
289 /// result of assigning a pointer into a global or when it's passed to a
290 /// function call the analyzer cannot model.
292 /// \param State The state at the point of escape.
293 /// \param Escaped The list of escaped symbols.
294 /// \param Call The corresponding CallEvent, if the symbols escape as
295 /// parameters to the given call.
296 /// \param Kind How the symbols have escaped.
297 /// \returns Checkers can modify the state by returning a new state.
298 ProgramStateRef
checkPointerEscape(ProgramStateRef State
,
299 const InvalidatedSymbols
&Escaped
,
300 const CallEvent
*Call
,
301 PointerEscapeKind Kind
) const {
305 /// Called when const pointers escape.
307 /// Note: in most cases checkPointerEscape callback is sufficient.
308 /// \sa checkPointerEscape
309 ProgramStateRef
checkConstPointerEscape(ProgramStateRef State
,
310 const InvalidatedSymbols
&Escaped
,
311 const CallEvent
*Call
,
312 PointerEscapeKind Kind
) const {
316 /// check::Event<ImplicitNullDerefEvent>
317 void checkEvent(ImplicitNullDerefEvent Event
) const {}
319 /// Check every declaration in the AST.
321 /// An AST traversal callback, which should only be used when the checker is
322 /// not path sensitive. It will be called for every Declaration in the AST and
323 /// can be specialized to only be called on subclasses of Decl, for example,
326 /// check::ASTDecl<FunctionDecl>
327 void checkASTDecl(const FunctionDecl
*D
,
328 AnalysisManager
&Mgr
,
329 BugReporter
&BR
) const {}
331 /// Check every declaration that has a statement body in the AST.
333 /// As AST traversal callback, which should only be used when the checker is
334 /// not path sensitive. It will be called for every Declaration in the AST.
335 void checkASTCodeBody(const Decl
*D
, AnalysisManager
&Mgr
,
336 BugReporter
&BR
) const {}
339 void CheckerDocumentation::checkPostStmt(const DeclStmt
*DS
,
340 CheckerContext
&C
) const {
343 void registerCheckerDocumentationChecker(CheckerManager
&Mgr
) {
344 Mgr
.registerChecker
<CheckerDocumentation
>();
347 bool shouldRegisterCheckerDocumentationChecker(const CheckerManager
&) {
351 } // end namespace ento
352 } // end namespace clang