blob: ab1e2ec1f54e0f60cea0aba393c6f74d0c39d898 [file] [log] [blame]
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001//===- CheckerDocumentation.cpp - Documentation checker ---------*- C++ -*-===//
Anna Zaks92297f92011-11-30 17:12:52 +00002//
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,
Artem Dergachev13b20262018-01-17 23:46:13 +000045 check::NewAllocator,
Anna Zaks92297f92011-11-30 17:12:52 +000046 check::Location,
47 check::Bind,
48 check::DeadSymbols,
Devin Coughline434fc42016-07-28 00:52:10 +000049 check::BeginFunction,
Anna Zaks3fdcc0b2013-01-03 00:25:29 +000050 check::EndFunction,
Anna Zaks92297f92011-11-30 17:12:52 +000051 check::EndAnalysis,
52 check::EndOfTranslationUnit,
53 eval::Call,
54 eval::Assume,
55 check::LiveSymbols,
56 check::RegionChanges,
Anna Zaksdc154152012-12-20 00:38:25 +000057 check::PointerEscape,
Anna Zaks4b04e662013-03-28 23:15:32 +000058 check::ConstPointerEscape,
Anna Zaks92297f92011-11-30 17:12:52 +000059 check::Event<ImplicitNullDerefEvent>,
60 check::ASTDecl<FunctionDecl> > {
61public:
Anna Zaks92297f92011-11-30 17:12:52 +000062 /// \brief Pre-visit the Statement.
63 ///
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.
68 ///
69 /// See checkBranchCondition() callback for performing custom processing of
70 /// the branching statements.
71 ///
Jordan Rose4080b0c2012-11-02 23:49:33 +000072 /// check::PreStmt<ReturnStmt>
73 void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}
Anna Zaks92297f92011-11-30 17:12:52 +000074
75 /// \brief Post-visit the Statement.
76 ///
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.
81 ///
Jordan Rose4080b0c2012-11-02 23:49:33 +000082 /// check::PostStmt<DeclStmt>
83 void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
Anna Zaks92297f92011-11-30 17:12:52 +000084
Jordan Roseafe7c2c2012-07-02 19:28:16 +000085 /// \brief Pre-visit the Objective C message.
86 ///
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,
Jordan Rose627b0462012-07-18 21:59:51 +000089 /// including explicit message syntax and property access.
Jordan Roseafe7c2c2012-07-02 19:28:16 +000090 ///
91 /// check::PreObjCMessage
Jordan Rose547060b2012-07-02 19:28:04 +000092 void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
Anna Zaks92297f92011-11-30 17:12:52 +000093
Jordan Roseafe7c2c2012-07-02 19:28:16 +000094 /// \brief Post-visit the Objective C message.
95 /// \sa checkPreObjCMessage()
96 ///
97 /// check::PostObjCMessage
Jordan Rose547060b2012-07-02 19:28:04 +000098 void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
Anna Zaks92297f92011-11-30 17:12:52 +000099
Devin Coughlinca5ab2b2015-09-15 01:13:53 +0000100 /// \brief Visit an Objective-C message whose receiver is nil.
101 ///
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.
105 ///
106 /// check::ObjCMessageNil
107 void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}
108
Jordan Roseafe7c2c2012-07-02 19:28:16 +0000109 /// \brief Pre-visit an abstract "call" event.
110 ///
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.
113 ///
114 /// Note that this includes ALL cross-body invocations, so if you want to
Jordan Rose4080b0c2012-11-02 23:49:33 +0000115 /// limit your checks to, say, function calls, you should test for that at the
116 /// beginning of your callback function.
Jordan Roseafe7c2c2012-07-02 19:28:16 +0000117 ///
118 /// check::PreCall
119 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}
120
121 /// \brief Post-visit an abstract "call" event.
122 /// \sa checkPreObjCMessage()
123 ///
124 /// check::PostCall
125 void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}
126
Anna Zaks92297f92011-11-30 17:12:52 +0000127 /// \brief Pre-visit of the condition statement of a branch (such as IfStmt).
128 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}
129
Artem Dergachev13b20262018-01-17 23:46:13 +0000130 /// \brief Post-visit the C++ operator new's allocation call.
131 ///
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 {}
145
Anna Zaks92297f92011-11-30 17:12:52 +0000146 /// \brief Called on a load from and a store to a location.
147 ///
148 /// The method will be called each time a location (pointer) value is
149 /// accessed.
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.
153 ///
154 /// check::Location
155 void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
James Dennett845619a2012-06-15 07:41:35 +0000156 CheckerContext &) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000157
158 /// \brief Called on binding of a value to a location.
159 ///
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.
163 ///
164 /// check::Bind
James Dennett845619a2012-06-15 07:41:35 +0000165 void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000166
Anna Zaks92297f92011-11-30 17:12:52 +0000167 /// \brief Called whenever a symbol becomes dead.
168 ///
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.)
176 ///
177 /// \param SR The SymbolReaper object can be queried to determine which
178 /// symbols are dead.
179 ///
180 /// check::DeadSymbols
181 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}
182
Devin Coughlin8d922aa2016-02-19 01:35:10 +0000183
184 /// \brief Called when the analyzer core starts analyzing a function,
185 /// regardless of whether it is analyzed at the top level or is inlined.
186 ///
187 /// check::BeginFunction
188 void checkBeginFunction(CheckerContext &Ctx) const {}
189
Anna Zaks3fdcc0b2013-01-03 00:25:29 +0000190 /// \brief Called when the analyzer core reaches the end of a
Devin Coughlin8d922aa2016-02-19 01:35:10 +0000191 /// function being analyzed regardless of whether it is analyzed at the top
192 /// level or is inlined.
Anna Zaks92297f92011-11-30 17:12:52 +0000193 ///
Anna Zaks3fdcc0b2013-01-03 00:25:29 +0000194 /// check::EndFunction
195 void checkEndFunction(CheckerContext &Ctx) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000196
197 /// \brief Called after all the paths in the ExplodedGraph reach end of path
198 /// - the symbolic execution graph is fully explored.
199 ///
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.
204 ///
205 /// check::EndAnalysis
206 void checkEndAnalysis(ExplodedGraph &G,
207 BugReporter &BR,
208 ExprEngine &Eng) const {}
209
210 /// \brief Called after analysis of a TranslationUnit is complete.
211 ///
212 /// check::EndOfTranslationUnit
Anton Yartsevedb06282012-03-23 02:43:24 +0000213 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
214 AnalysisManager &Mgr,
215 BugReporter &BR) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000216
Anna Zaks92297f92011-11-30 17:12:52 +0000217 /// \brief Evaluates function call.
218 ///
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.
223 ///
224 /// \returns true if the call has been successfully evaluated
225 /// and false otherwise. Note, that only one checker can evaluate a call. If
Duncan Sandsf3dcb682013-05-25 02:22:10 +0000226 /// more than one checker claims that they can evaluate the same call the
Anna Zaks92297f92011-11-30 17:12:52 +0000227 /// first one wins.
228 ///
229 /// eval::Call
230 bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; }
231
232 /// \brief Handles assumptions on symbolic values.
233 ///
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.
238 ///
239 /// eval::Assume
Ted Kremenek49b1e382012-01-26 21:29:00 +0000240 ProgramStateRef evalAssume(ProgramStateRef State,
Anna Zaks92297f92011-11-30 17:12:52 +0000241 SVal Cond,
242 bool Assumption) const { return State; }
243
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.
247 ///
248 /// check::LiveSymbols
Ted Kremenek49b1e382012-01-26 21:29:00 +0000249 void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000250
Jordan Rosef684db62012-11-07 02:35:02 +0000251 /// \brief Called when the contents of one or more regions change.
252 ///
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.
James Dennett845619a2012-06-15 07:41:35 +0000256 ///
257 /// \param State The current program state.
258 /// \param Invalidated A set of all symbols potentially touched by the change.
Anna Zaks3d348342012-02-14 21:55:24 +0000259 /// \param ExplicitRegions The regions explicitly requested for invalidation.
Jordan Rosef684db62012-11-07 02:35:02 +0000260 /// 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.
Anna Zaksb5701952017-01-13 00:50:57 +0000267 /// \param LCtx LocationContext that is useful for getting various contextual
268 /// info, like callstack, CFG etc.
Jordan Rosef684db62012-11-07 02:35:02 +0000269 /// \param Call The opaque call triggering this invalidation. Will be 0 if the
270 /// change was not triggered by a call.
271 ///
James Dennett845619a2012-06-15 07:41:35 +0000272 /// check::RegionChanges
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000273 ProgramStateRef
Ted Kremenek49b1e382012-01-26 21:29:00 +0000274 checkRegionChanges(ProgramStateRef State,
Anna Zaksdc154152012-12-20 00:38:25 +0000275 const InvalidatedSymbols *Invalidated,
Anna Zaks92297f92011-11-30 17:12:52 +0000276 ArrayRef<const MemRegion *> ExplicitRegions,
Anna Zaks3d348342012-02-14 21:55:24 +0000277 ArrayRef<const MemRegion *> Regions,
Anna Zaksb5701952017-01-13 00:50:57 +0000278 const LocationContext *LCtx,
Jordan Rose742920c2012-07-02 19:27:35 +0000279 const CallEvent *Call) const {
Anna Zaks92297f92011-11-30 17:12:52 +0000280 return State;
281 }
282
Anna Zaksdc154152012-12-20 00:38:25 +0000283 /// \brief Called when pointers escape.
284 ///
285 /// This notifies the checkers about pointer escape, which occurs whenever
Anna Zaks9747feb2012-12-21 01:50:14 +0000286 /// the analyzer cannot track the symbol any more. For example, as a
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000287 /// result of assigning a pointer into a global or when it's passed to a
Anna Zaksdc154152012-12-20 00:38:25 +0000288 /// function call the analyzer cannot model.
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000289 ///
Anna Zaksdc154152012-12-20 00:38:25 +0000290 /// \param State The state at the point of escape.
291 /// \param Escaped The list of escaped symbols.
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000292 /// \param Call The corresponding CallEvent, if the symbols escape as
Anna Zaksdc154152012-12-20 00:38:25 +0000293 /// parameters to the given call.
Anna Zaksacdc13c2013-02-07 23:05:43 +0000294 /// \param Kind How the symbols have escaped.
Anna Zaksdc154152012-12-20 00:38:25 +0000295 /// \returns Checkers can modify the state by returning a new state.
296 ProgramStateRef checkPointerEscape(ProgramStateRef State,
297 const InvalidatedSymbols &Escaped,
Anna Zaksacdc13c2013-02-07 23:05:43 +0000298 const CallEvent *Call,
299 PointerEscapeKind Kind) const {
Anna Zaksdc154152012-12-20 00:38:25 +0000300 return State;
301 }
302
Anna Zaks4b04e662013-03-28 23:15:32 +0000303 /// \brief Called when const pointers escape.
304 ///
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 {
311 return State;
312 }
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000313
Anna Zaks92297f92011-11-30 17:12:52 +0000314 /// check::Event<ImplicitNullDerefEvent>
315 void checkEvent(ImplicitNullDerefEvent Event) const {}
316
317 /// \brief Check every declaration in the AST.
318 ///
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,
322 /// FunctionDecl.
323 ///
324 /// check::ASTDecl<FunctionDecl>
325 void checkASTDecl(const FunctionDecl *D,
326 AnalysisManager &Mgr,
327 BugReporter &BR) const {}
Anna Zaks92297f92011-11-30 17:12:52 +0000328};
329
Jordan Rose4080b0c2012-11-02 23:49:33 +0000330void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,
Anna Zaks92297f92011-11-30 17:12:52 +0000331 CheckerContext &C) const {
Anna Zaks92297f92011-11-30 17:12:52 +0000332}
333
Jordan Rosef684db62012-11-07 02:35:02 +0000334} // end namespace ento
335} // end namespace clang