CheckerDocumentation.cpp 14 KB

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