blob: 37b84480f892917624c24e74bccf2558f2f7ce4f [file] [log] [blame]
Anna Zaks92297f92011-11-30 17:12:52 +00001//= CheckerDocumentation.cpp - Documentation checker ---------------*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This checker lists all the checker callbacks and provides documentation for
11// checker writers.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Anna Zaks92297f92011-11-30 17:12:52 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
18#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Anna Zaks92297f92011-11-30 17:12:52 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21
22using namespace clang;
23using namespace ento;
24
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.
Jordan Rosef684db62012-11-07 02:35:02 +000028namespace clang {
Anna Zaks92297f92011-11-30 17:12:52 +000029namespace ento {
30
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
34/// checking.
35///
36/// \sa CheckerContext
Jordan Rose4080b0c2012-11-02 23:49:33 +000037class CheckerDocumentation : public Checker< check::PreStmt<ReturnStmt>,
38 check::PostStmt<DeclStmt>,
Anna Zaks92297f92011-11-30 17:12:52 +000039 check::PreObjCMessage,
40 check::PostObjCMessage,
Devin Coughlinca5ab2b2015-09-15 01:13:53 +000041 check::ObjCMessageNil,
Jordan Roseafe7c2c2012-07-02 19:28:16 +000042 check::PreCall,
43 check::PostCall,
Anna Zaks92297f92011-11-30 17:12:52 +000044 check::BranchCondition,
45 check::Location,
46 check::Bind,
47 check::DeadSymbols,
Anna Zaks3fdcc0b2013-01-03 00:25:29 +000048 check::EndFunction,
Anna Zaks92297f92011-11-30 17:12:52 +000049 check::EndAnalysis,
50 check::EndOfTranslationUnit,
51 eval::Call,
52 eval::Assume,
53 check::LiveSymbols,
54 check::RegionChanges,
Anna Zaksdc154152012-12-20 00:38:25 +000055 check::PointerEscape,
Anna Zaks4b04e662013-03-28 23:15:32 +000056 check::ConstPointerEscape,
Anna Zaks92297f92011-11-30 17:12:52 +000057 check::Event<ImplicitNullDerefEvent>,
58 check::ASTDecl<FunctionDecl> > {
59public:
60
61 /// \brief Pre-visit the Statement.
62 ///
63 /// The method will be called before the analyzer core processes the
64 /// statement. The notification is performed for every explored CFGElement,
65 /// which does not include the control flow statements such as IfStmt. The
66 /// callback can be specialized to be called with any subclass of Stmt.
67 ///
68 /// See checkBranchCondition() callback for performing custom processing of
69 /// the branching statements.
70 ///
Jordan Rose4080b0c2012-11-02 23:49:33 +000071 /// check::PreStmt<ReturnStmt>
72 void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}
Anna Zaks92297f92011-11-30 17:12:52 +000073
74 /// \brief Post-visit the Statement.
75 ///
76 /// The method will be called after the analyzer core processes the
77 /// statement. The notification is performed for every explored CFGElement,
78 /// which does not include the control flow statements such as IfStmt. The
79 /// callback can be specialized to be called with any subclass of Stmt.
80 ///
Jordan Rose4080b0c2012-11-02 23:49:33 +000081 /// check::PostStmt<DeclStmt>
82 void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
Anna Zaks92297f92011-11-30 17:12:52 +000083
Jordan Roseafe7c2c2012-07-02 19:28:16 +000084 /// \brief Pre-visit the Objective C message.
85 ///
86 /// This will be called before the analyzer core processes the method call.
87 /// This is called for any action which produces an Objective-C message send,
Jordan Rose627b0462012-07-18 21:59:51 +000088 /// including explicit message syntax and property access.
Jordan Roseafe7c2c2012-07-02 19:28:16 +000089 ///
90 /// check::PreObjCMessage
Jordan Rose547060b2012-07-02 19:28:04 +000091 void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
Anna Zaks92297f92011-11-30 17:12:52 +000092
Jordan Roseafe7c2c2012-07-02 19:28:16 +000093 /// \brief Post-visit the Objective C message.
94 /// \sa checkPreObjCMessage()
95 ///
96 /// check::PostObjCMessage
Jordan Rose547060b2012-07-02 19:28:04 +000097 void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
Anna Zaks92297f92011-11-30 17:12:52 +000098
Devin Coughlinca5ab2b2015-09-15 01:13:53 +000099 /// \brief Visit an Objective-C message whose receiver is nil.
100 ///
101 /// This will be called when the analyzer core processes a method call whose
102 /// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and
103 /// check{Pre/Post}Call will not be called.
104 ///
105 /// check::ObjCMessageNil
106 void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}
107
Jordan Roseafe7c2c2012-07-02 19:28:16 +0000108 /// \brief Pre-visit an abstract "call" event.
109 ///
110 /// This is used for checkers that want to check arguments or attributed
111 /// behavior for functions and methods no matter how they are being invoked.
112 ///
113 /// Note that this includes ALL cross-body invocations, so if you want to
Jordan Rose4080b0c2012-11-02 23:49:33 +0000114 /// limit your checks to, say, function calls, you should test for that at the
115 /// beginning of your callback function.
Jordan Roseafe7c2c2012-07-02 19:28:16 +0000116 ///
117 /// check::PreCall
118 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}
119
120 /// \brief Post-visit an abstract "call" event.
121 /// \sa checkPreObjCMessage()
122 ///
123 /// check::PostCall
124 void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}
125
Anna Zaks92297f92011-11-30 17:12:52 +0000126 /// \brief Pre-visit of the condition statement of a branch (such as IfStmt).
127 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}
128
129 /// \brief Called on a load from and a store to a location.
130 ///
131 /// The method will be called each time a location (pointer) value is
132 /// accessed.
133 /// \param Loc The value of the location (pointer).
134 /// \param IsLoad The flag specifying if the location is a store or a load.
135 /// \param S The load is performed while processing the statement.
136 ///
137 /// check::Location
138 void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
James Dennett845619a2012-06-15 07:41:35 +0000139 CheckerContext &) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000140
141 /// \brief Called on binding of a value to a location.
142 ///
143 /// \param Loc The value of the location (pointer).
144 /// \param Val The value which will be stored at the location Loc.
145 /// \param S The bind is performed while processing the statement S.
146 ///
147 /// check::Bind
James Dennett845619a2012-06-15 07:41:35 +0000148 void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000149
150
151 /// \brief Called whenever a symbol becomes dead.
152 ///
153 /// This callback should be used by the checkers to aggressively clean
154 /// up/reduce the checker state, which is important for reducing the overall
155 /// memory usage. Specifically, if a checker keeps symbol specific information
156 /// in the sate, it can and should be dropped after the symbol becomes dead.
157 /// In addition, reporting a bug as soon as the checker becomes dead leads to
158 /// more precise diagnostics. (For example, one should report that a malloced
159 /// variable is not freed right after it goes out of scope.)
160 ///
161 /// \param SR The SymbolReaper object can be queried to determine which
162 /// symbols are dead.
163 ///
164 /// check::DeadSymbols
165 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}
166
Anna Zaks3fdcc0b2013-01-03 00:25:29 +0000167 /// \brief Called when the analyzer core reaches the end of a
Jordan Rosef684db62012-11-07 02:35:02 +0000168 /// function being analyzed.
Anna Zaks92297f92011-11-30 17:12:52 +0000169 ///
Anna Zaks3fdcc0b2013-01-03 00:25:29 +0000170 /// check::EndFunction
171 void checkEndFunction(CheckerContext &Ctx) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000172
173 /// \brief Called after all the paths in the ExplodedGraph reach end of path
174 /// - the symbolic execution graph is fully explored.
175 ///
176 /// This callback should be used in cases when a checker needs to have a
177 /// global view of the information generated on all paths. For example, to
178 /// compare execution summary/result several paths.
179 /// See IdempotentOperationChecker for a usage example.
180 ///
181 /// check::EndAnalysis
182 void checkEndAnalysis(ExplodedGraph &G,
183 BugReporter &BR,
184 ExprEngine &Eng) const {}
185
186 /// \brief Called after analysis of a TranslationUnit is complete.
187 ///
188 /// check::EndOfTranslationUnit
Anton Yartsevedb06282012-03-23 02:43:24 +0000189 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
190 AnalysisManager &Mgr,
191 BugReporter &BR) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000192
193
194 /// \brief Evaluates function call.
195 ///
196 /// The analysis core threats all function calls in the same way. However, some
197 /// functions have special meaning, which should be reflected in the program
198 /// state. This callback allows a checker to provide domain specific knowledge
199 /// about the particular functions it knows about.
200 ///
201 /// \returns true if the call has been successfully evaluated
202 /// and false otherwise. Note, that only one checker can evaluate a call. If
Duncan Sandsf3dcb682013-05-25 02:22:10 +0000203 /// more than one checker claims that they can evaluate the same call the
Anna Zaks92297f92011-11-30 17:12:52 +0000204 /// first one wins.
205 ///
206 /// eval::Call
207 bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; }
208
209 /// \brief Handles assumptions on symbolic values.
210 ///
211 /// This method is called when a symbolic expression is assumed to be true or
212 /// false. For example, the assumptions are performed when evaluating a
213 /// condition at a branch. The callback allows checkers track the assumptions
214 /// performed on the symbols of interest and change the state accordingly.
215 ///
216 /// eval::Assume
Ted Kremenek49b1e382012-01-26 21:29:00 +0000217 ProgramStateRef evalAssume(ProgramStateRef State,
Anna Zaks92297f92011-11-30 17:12:52 +0000218 SVal Cond,
219 bool Assumption) const { return State; }
220
221 /// Allows modifying SymbolReaper object. For example, checkers can explicitly
222 /// register symbols of interest as live. These symbols will not be marked
223 /// dead and removed.
224 ///
225 /// check::LiveSymbols
Ted Kremenek49b1e382012-01-26 21:29:00 +0000226 void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000227
Jordan Rosef684db62012-11-07 02:35:02 +0000228 /// \brief Called to determine if the checker currently needs to know if when
229 /// contents of any regions change.
230 ///
231 /// Since it is not necessarily cheap to compute which regions are being
232 /// changed, this allows the analyzer core to skip the more expensive
233 /// #checkRegionChanges when no checkers are tracking any state.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000234 bool wantsRegionChangeUpdate(ProgramStateRef St) const { return true; }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000235
Jordan Rosef684db62012-11-07 02:35:02 +0000236 /// \brief Called when the contents of one or more regions change.
237 ///
238 /// This can occur in many different ways: an explicit bind, a blanket
239 /// invalidation of the region contents, or by passing a region to a function
240 /// call whose behavior the analyzer cannot model perfectly.
James Dennett845619a2012-06-15 07:41:35 +0000241 ///
242 /// \param State The current program state.
243 /// \param Invalidated A set of all symbols potentially touched by the change.
Anna Zaks3d348342012-02-14 21:55:24 +0000244 /// \param ExplicitRegions The regions explicitly requested for invalidation.
Jordan Rosef684db62012-11-07 02:35:02 +0000245 /// For a function call, this would be the arguments. For a bind, this
246 /// would be the region being bound to.
247 /// \param Regions The transitive closure of regions accessible from,
248 /// \p ExplicitRegions, i.e. all regions that may have been touched
249 /// by this change. For a simple bind, this list will be the same as
250 /// \p ExplicitRegions, since a bind does not affect the contents of
251 /// anything accessible through the base region.
252 /// \param Call The opaque call triggering this invalidation. Will be 0 if the
253 /// change was not triggered by a call.
254 ///
255 /// Note that this callback will not be invoked unless
256 /// #wantsRegionChangeUpdate returns \c true.
James Dennett845619a2012-06-15 07:41:35 +0000257 ///
258 /// check::RegionChanges
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000259 ProgramStateRef
Ted Kremenek49b1e382012-01-26 21:29:00 +0000260 checkRegionChanges(ProgramStateRef State,
Anna Zaksdc154152012-12-20 00:38:25 +0000261 const InvalidatedSymbols *Invalidated,
Anna Zaks92297f92011-11-30 17:12:52 +0000262 ArrayRef<const MemRegion *> ExplicitRegions,
Anna Zaks3d348342012-02-14 21:55:24 +0000263 ArrayRef<const MemRegion *> Regions,
Jordan Rose742920c2012-07-02 19:27:35 +0000264 const CallEvent *Call) const {
Anna Zaks92297f92011-11-30 17:12:52 +0000265 return State;
266 }
267
Anna Zaksdc154152012-12-20 00:38:25 +0000268 /// \brief Called when pointers escape.
269 ///
270 /// This notifies the checkers about pointer escape, which occurs whenever
Anna Zaks9747feb2012-12-21 01:50:14 +0000271 /// the analyzer cannot track the symbol any more. For example, as a
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000272 /// result of assigning a pointer into a global or when it's passed to a
Anna Zaksdc154152012-12-20 00:38:25 +0000273 /// function call the analyzer cannot model.
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000274 ///
Anna Zaksdc154152012-12-20 00:38:25 +0000275 /// \param State The state at the point of escape.
276 /// \param Escaped The list of escaped symbols.
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000277 /// \param Call The corresponding CallEvent, if the symbols escape as
Anna Zaksdc154152012-12-20 00:38:25 +0000278 /// parameters to the given call.
Anna Zaksacdc13c2013-02-07 23:05:43 +0000279 /// \param Kind How the symbols have escaped.
Anna Zaksdc154152012-12-20 00:38:25 +0000280 /// \returns Checkers can modify the state by returning a new state.
281 ProgramStateRef checkPointerEscape(ProgramStateRef State,
282 const InvalidatedSymbols &Escaped,
Anna Zaksacdc13c2013-02-07 23:05:43 +0000283 const CallEvent *Call,
284 PointerEscapeKind Kind) const {
Anna Zaksdc154152012-12-20 00:38:25 +0000285 return State;
286 }
287
Anna Zaks4b04e662013-03-28 23:15:32 +0000288 /// \brief Called when const pointers escape.
289 ///
290 /// Note: in most cases checkPointerEscape callback is sufficient.
291 /// \sa checkPointerEscape
292 ProgramStateRef checkConstPointerEscape(ProgramStateRef State,
293 const InvalidatedSymbols &Escaped,
294 const CallEvent *Call,
295 PointerEscapeKind Kind) const {
296 return State;
297 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000298
Anna Zaks92297f92011-11-30 17:12:52 +0000299 /// check::Event<ImplicitNullDerefEvent>
300 void checkEvent(ImplicitNullDerefEvent Event) const {}
301
302 /// \brief Check every declaration in the AST.
303 ///
304 /// An AST traversal callback, which should only be used when the checker is
305 /// not path sensitive. It will be called for every Declaration in the AST and
306 /// can be specialized to only be called on subclasses of Decl, for example,
307 /// FunctionDecl.
308 ///
309 /// check::ASTDecl<FunctionDecl>
310 void checkASTDecl(const FunctionDecl *D,
311 AnalysisManager &Mgr,
312 BugReporter &BR) const {}
313
314};
315
Jordan Rose4080b0c2012-11-02 23:49:33 +0000316void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,
Anna Zaks92297f92011-11-30 17:12:52 +0000317 CheckerContext &C) const {
318 return;
319}
320
Jordan Rosef684db62012-11-07 02:35:02 +0000321} // end namespace ento
322} // end namespace clang