CheckerDocumentation.cpp 13 KB

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