1 //===- CheckerDocumentation.cpp - Documentation checker ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This checker lists all the checker callbacks and provides documentation for
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
22 using namespace clang;
25 // All checkers should be placed into anonymous namespace.
26 // We place the CheckerDocumentation inside ento namespace to make the
27 // it visible in doxygen.
31 /// This checker documents the callback functions checkers can use to implement
32 /// the custom handling of the specific events during path exploration as well
33 /// as reporting bugs. Most of the callbacks are targeted at path-sensitive
36 /// \sa CheckerContext
37 class CheckerDocumentation : public Checker< check::PreStmt<ReturnStmt>,
38 check::PostStmt<DeclStmt>,
39 check::PreObjCMessage,
40 check::PostObjCMessage,
41 check::ObjCMessageNil,
44 check::BranchCondition,
52 check::EndOfTranslationUnit,
58 check::ConstPointerEscape,
59 check::Event<ImplicitNullDerefEvent>,
60 check::ASTDecl<FunctionDecl> > {
62 /// Pre-visit the Statement.
64 /// The method will be called before the analyzer core processes the
65 /// statement. The notification is performed for every explored CFGElement,
66 /// which does not include the control flow statements such as IfStmt. The
67 /// callback can be specialized to be called with any subclass of Stmt.
69 /// See checkBranchCondition() callback for performing custom processing of
70 /// the branching statements.
72 /// check::PreStmt<ReturnStmt>
73 void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}
75 /// Post-visit the Statement.
77 /// The method will be called after the analyzer core processes the
78 /// statement. The notification is performed for every explored CFGElement,
79 /// which does not include the control flow statements such as IfStmt. The
80 /// callback can be specialized to be called with any subclass of Stmt.
82 /// check::PostStmt<DeclStmt>
83 void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
85 /// Pre-visit the Objective C message.
87 /// This will be called before the analyzer core processes the method call.
88 /// This is called for any action which produces an Objective-C message send,
89 /// including explicit message syntax and property access.
91 /// check::PreObjCMessage
92 void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
94 /// Post-visit the Objective C message.
95 /// \sa checkPreObjCMessage()
97 /// check::PostObjCMessage
98 void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
100 /// Visit an Objective-C message whose receiver is nil.
102 /// This will be called when the analyzer core processes a method call whose
103 /// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and
104 /// check{Pre/Post}Call will not be called.
106 /// check::ObjCMessageNil
107 void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}
109 /// Pre-visit an abstract "call" event.
111 /// This is used for checkers that want to check arguments or attributed
112 /// behavior for functions and methods no matter how they are being invoked.
114 /// Note that this includes ALL cross-body invocations, so if you want to
115 /// limit your checks to, say, function calls, you should test for that at the
116 /// beginning of your callback function.
119 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}
121 /// Post-visit an abstract "call" event.
122 /// \sa checkPreObjCMessage()
125 void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}
127 /// Pre-visit of the condition statement of a branch (such as IfStmt).
128 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}
130 /// Post-visit the C++ operator new's allocation call.
132 /// Execution of C++ operator new consists of the following phases: (1) call
133 /// default or overridden operator new() to allocate memory (2) cast the
134 /// return value of operator new() from void pointer type to class pointer
135 /// type, (3) assuming that the value is non-null, call the object's
136 /// constructor over this pointer, (4) declare that the value of the
137 /// new-expression is this pointer. This callback is called between steps
138 /// (2) and (3). Post-call for the allocator is called after step (1).
139 /// Pre-statement for the new-expression is called on step (4) when the value
140 /// of the expression is evaluated.
141 /// \param NE The C++ new-expression that triggered the allocation.
142 /// \param Target The allocated region, casted to the class type.
143 void checkNewAllocator(const CXXNewExpr *NE, SVal Target,
144 CheckerContext &) const {}
146 /// Called on a load from and a store to a location.
148 /// The method will be called each time a location (pointer) value is
150 /// \param Loc The value of the location (pointer).
151 /// \param IsLoad The flag specifying if the location is a store or a load.
152 /// \param S The load is performed while processing the statement.
155 void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
156 CheckerContext &) const {}
158 /// Called on binding of a value to a location.
160 /// \param Loc The value of the location (pointer).
161 /// \param Val The value which will be stored at the location Loc.
162 /// \param S The bind is performed while processing the statement S.
165 void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}
167 /// Called whenever a symbol becomes dead.
169 /// This callback should be used by the checkers to aggressively clean
170 /// up/reduce the checker state, which is important for reducing the overall
171 /// memory usage. Specifically, if a checker keeps symbol specific information
172 /// in the sate, it can and should be dropped after the symbol becomes dead.
173 /// In addition, reporting a bug as soon as the checker becomes dead leads to
174 /// more precise diagnostics. (For example, one should report that a malloced
175 /// variable is not freed right after it goes out of scope.)
177 /// \param SR The SymbolReaper object can be queried to determine which
178 /// symbols are dead.
180 /// check::DeadSymbols
181 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}
184 /// Called when the analyzer core starts analyzing a function,
185 /// regardless of whether it is analyzed at the top level or is inlined.
187 /// check::BeginFunction
188 void checkBeginFunction(CheckerContext &Ctx) const {}
190 /// Called when the analyzer core reaches the end of a
191 /// function being analyzed regardless of whether it is analyzed at the top
192 /// level or is inlined.
194 /// check::EndFunction
195 void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const {}
197 /// Called after all the paths in the ExplodedGraph reach end of path
198 /// - the symbolic execution graph is fully explored.
200 /// This callback should be used in cases when a checker needs to have a
201 /// global view of the information generated on all paths. For example, to
202 /// compare execution summary/result several paths.
203 /// See IdempotentOperationChecker for a usage example.
205 /// check::EndAnalysis
206 void checkEndAnalysis(ExplodedGraph &G,
208 ExprEngine &Eng) const {}
210 /// Called after analysis of a TranslationUnit is complete.
212 /// check::EndOfTranslationUnit
213 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
214 AnalysisManager &Mgr,
215 BugReporter &BR) const {}
217 /// Evaluates function call.
219 /// The analysis core threats all function calls in the same way. However, some
220 /// functions have special meaning, which should be reflected in the program
221 /// state. This callback allows a checker to provide domain specific knowledge
222 /// about the particular functions it knows about.
224 /// \returns true if the call has been successfully evaluated
225 /// and false otherwise. Note, that only one checker can evaluate a call. If
226 /// more than one checker claims that they can evaluate the same call the
230 bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; }
232 /// Handles assumptions on symbolic values.
234 /// This method is called when a symbolic expression is assumed to be true or
235 /// false. For example, the assumptions are performed when evaluating a
236 /// condition at a branch. The callback allows checkers track the assumptions
237 /// performed on the symbols of interest and change the state accordingly.
240 ProgramStateRef evalAssume(ProgramStateRef State,
242 bool Assumption) const { return State; }
244 /// Allows modifying SymbolReaper object. For example, checkers can explicitly
245 /// register symbols of interest as live. These symbols will not be marked
246 /// dead and removed.
248 /// check::LiveSymbols
249 void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}
251 /// Called when the contents of one or more regions change.
253 /// This can occur in many different ways: an explicit bind, a blanket
254 /// invalidation of the region contents, or by passing a region to a function
255 /// call whose behavior the analyzer cannot model perfectly.
257 /// \param State The current program state.
258 /// \param Invalidated A set of all symbols potentially touched by the change.
259 /// \param ExplicitRegions The regions explicitly requested for invalidation.
260 /// For a function call, this would be the arguments. For a bind, this
261 /// would be the region being bound to.
262 /// \param Regions The transitive closure of regions accessible from,
263 /// \p ExplicitRegions, i.e. all regions that may have been touched
264 /// by this change. For a simple bind, this list will be the same as
265 /// \p ExplicitRegions, since a bind does not affect the contents of
266 /// anything accessible through the base region.
267 /// \param LCtx LocationContext that is useful for getting various contextual
268 /// info, like callstack, CFG etc.
269 /// \param Call The opaque call triggering this invalidation. Will be 0 if the
270 /// change was not triggered by a call.
272 /// check::RegionChanges
274 checkRegionChanges(ProgramStateRef State,
275 const InvalidatedSymbols *Invalidated,
276 ArrayRef<const MemRegion *> ExplicitRegions,
277 ArrayRef<const MemRegion *> Regions,
278 const LocationContext *LCtx,
279 const CallEvent *Call) const {
283 /// Called when pointers escape.
285 /// This notifies the checkers about pointer escape, which occurs whenever
286 /// the analyzer cannot track the symbol any more. For example, as a
287 /// result of assigning a pointer into a global or when it's passed to a
288 /// function call the analyzer cannot model.
290 /// \param State The state at the point of escape.
291 /// \param Escaped The list of escaped symbols.
292 /// \param Call The corresponding CallEvent, if the symbols escape as
293 /// parameters to the given call.
294 /// \param Kind How the symbols have escaped.
295 /// \returns Checkers can modify the state by returning a new state.
296 ProgramStateRef checkPointerEscape(ProgramStateRef State,
297 const InvalidatedSymbols &Escaped,
298 const CallEvent *Call,
299 PointerEscapeKind Kind) const {
303 /// Called when const pointers escape.
305 /// Note: in most cases checkPointerEscape callback is sufficient.
306 /// \sa checkPointerEscape
307 ProgramStateRef checkConstPointerEscape(ProgramStateRef State,
308 const InvalidatedSymbols &Escaped,
309 const CallEvent *Call,
310 PointerEscapeKind Kind) const {
314 /// check::Event<ImplicitNullDerefEvent>
315 void checkEvent(ImplicitNullDerefEvent Event) const {}
317 /// Check every declaration in the AST.
319 /// An AST traversal callback, which should only be used when the checker is
320 /// not path sensitive. It will be called for every Declaration in the AST and
321 /// can be specialized to only be called on subclasses of Decl, for example,
324 /// check::ASTDecl<FunctionDecl>
325 void checkASTDecl(const FunctionDecl *D,
326 AnalysisManager &Mgr,
327 BugReporter &BR) const {}
330 void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,
331 CheckerContext &C) const {
334 } // end namespace ento
335 } // end namespace clang