CheckerDocumentation.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //===- CheckerDocumentation.cpp - Documentation checker ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This checker lists all the checker callbacks and provides documentation for
  10. // checker writers.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  19. using namespace clang;
  20. using namespace ento;
  21. // All checkers should be placed into anonymous namespace.
  22. // We place the CheckerDocumentation inside ento namespace to make the
  23. // it visible in doxygen.
  24. namespace clang {
  25. namespace ento {
  26. /// This checker documents the callback functions checkers can use to implement
  27. /// the custom handling of the specific events during path exploration as well
  28. /// as reporting bugs. Most of the callbacks are targeted at path-sensitive
  29. /// checking.
  30. ///
  31. /// \sa CheckerContext
  32. class CheckerDocumentation : public Checker< check::PreStmt<ReturnStmt>,
  33. check::PostStmt<DeclStmt>,
  34. check::PreObjCMessage,
  35. check::PostObjCMessage,
  36. check::ObjCMessageNil,
  37. check::PreCall,
  38. check::PostCall,
  39. check::BranchCondition,
  40. check::NewAllocator,
  41. check::Location,
  42. check::Bind,
  43. check::DeadSymbols,
  44. check::BeginFunction,
  45. check::EndFunction,
  46. check::EndAnalysis,
  47. check::EndOfTranslationUnit,
  48. eval::Call,
  49. eval::Assume,
  50. check::LiveSymbols,
  51. check::RegionChanges,
  52. check::PointerEscape,
  53. check::ConstPointerEscape,
  54. check::Event<ImplicitNullDerefEvent>,
  55. check::ASTDecl<FunctionDecl> > {
  56. public:
  57. /// Pre-visit the Statement.
  58. ///
  59. /// The method will be called before the analyzer core processes the
  60. /// statement. The notification is performed for every explored CFGElement,
  61. /// which does not include the control flow statements such as IfStmt. The
  62. /// callback can be specialized to be called with any subclass of Stmt.
  63. ///
  64. /// See checkBranchCondition() callback for performing custom processing of
  65. /// the branching statements.
  66. ///
  67. /// check::PreStmt<ReturnStmt>
  68. void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}
  69. /// Post-visit the Statement.
  70. ///
  71. /// The method will be called after the analyzer core processes the
  72. /// statement. The notification is performed for every explored CFGElement,
  73. /// which does not include the control flow statements such as IfStmt. The
  74. /// callback can be specialized to be called with any subclass of Stmt.
  75. ///
  76. /// check::PostStmt<DeclStmt>
  77. void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;
  78. /// Pre-visit the Objective C message.
  79. ///
  80. /// This will be called before the analyzer core processes the method call.
  81. /// This is called for any action which produces an Objective-C message send,
  82. /// including explicit message syntax and property access.
  83. ///
  84. /// check::PreObjCMessage
  85. void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
  86. /// Post-visit the Objective C message.
  87. /// \sa checkPreObjCMessage()
  88. ///
  89. /// check::PostObjCMessage
  90. void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}
  91. /// Visit an Objective-C message whose receiver is nil.
  92. ///
  93. /// This will be called when the analyzer core processes a method call whose
  94. /// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and
  95. /// check{Pre/Post}Call will not be called.
  96. ///
  97. /// check::ObjCMessageNil
  98. void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}
  99. /// Pre-visit an abstract "call" event.
  100. ///
  101. /// This is used for checkers that want to check arguments or attributed
  102. /// behavior for functions and methods no matter how they are being invoked.
  103. ///
  104. /// Note that this includes ALL cross-body invocations, so if you want to
  105. /// limit your checks to, say, function calls, you should test for that at the
  106. /// beginning of your callback function.
  107. ///
  108. /// check::PreCall
  109. void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}
  110. /// Post-visit an abstract "call" event.
  111. /// \sa checkPreObjCMessage()
  112. ///
  113. /// check::PostCall
  114. void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}
  115. /// Pre-visit of the condition statement of a branch (such as IfStmt).
  116. void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}
  117. /// Post-visit the C++ operator new's allocation call.
  118. ///
  119. /// Execution of C++ operator new consists of the following phases: (1) call
  120. /// default or overridden operator new() to allocate memory (2) cast the
  121. /// return value of operator new() from void pointer type to class pointer
  122. /// type, (3) assuming that the value is non-null, call the object's
  123. /// constructor over this pointer, (4) declare that the value of the
  124. /// new-expression is this pointer. This callback is called between steps
  125. /// (2) and (3). Post-call for the allocator is called after step (1).
  126. /// Pre-statement for the new-expression is called on step (4) when the value
  127. /// of the expression is evaluated.
  128. /// \param NE The C++ new-expression that triggered the allocation.
  129. /// \param Target The allocated region, casted to the class type.
  130. void checkNewAllocator(const CXXNewExpr *NE, SVal Target,
  131. CheckerContext &) const {}
  132. /// Called on a load from and a store to a location.
  133. ///
  134. /// The method will be called each time a location (pointer) value is
  135. /// accessed.
  136. /// \param Loc The value of the location (pointer).
  137. /// \param IsLoad The flag specifying if the location is a store or a load.
  138. /// \param S The load is performed while processing the statement.
  139. ///
  140. /// check::Location
  141. void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
  142. CheckerContext &) const {}
  143. /// Called on binding of a value to a location.
  144. ///
  145. /// \param Loc The value of the location (pointer).
  146. /// \param Val The value which will be stored at the location Loc.
  147. /// \param S The bind is performed while processing the statement S.
  148. ///
  149. /// check::Bind
  150. void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}
  151. /// 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 state, 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. /// Called when the analyzer core starts analyzing a function,
  167. /// regardless of whether it is analyzed at the top level or is inlined.
  168. ///
  169. /// check::BeginFunction
  170. void checkBeginFunction(CheckerContext &Ctx) const {}
  171. /// Called when the analyzer core reaches the end of a
  172. /// function being analyzed regardless of whether it is analyzed at the top
  173. /// level or is inlined.
  174. ///
  175. /// check::EndFunction
  176. void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const {}
  177. /// Called after all the paths in the ExplodedGraph reach end of path
  178. /// - the symbolic execution graph is fully explored.
  179. ///
  180. /// This callback should be used in cases when a checker needs to have a
  181. /// global view of the information generated on all paths. For example, to
  182. /// compare execution summary/result several paths.
  183. /// See IdempotentOperationChecker for a usage example.
  184. ///
  185. /// check::EndAnalysis
  186. void checkEndAnalysis(ExplodedGraph &G,
  187. BugReporter &BR,
  188. ExprEngine &Eng) const {}
  189. /// Called after analysis of a TranslationUnit is complete.
  190. ///
  191. /// check::EndOfTranslationUnit
  192. void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
  193. AnalysisManager &Mgr,
  194. BugReporter &BR) const {}
  195. /// Evaluates function call.
  196. ///
  197. /// The analysis core treats all function calls in the same way. However, some
  198. /// functions have special meaning, which should be reflected in the program
  199. /// state. This callback allows a checker to provide domain specific knowledge
  200. /// about the particular functions it knows about.
  201. ///
  202. /// \returns true if the call has been successfully evaluated
  203. /// and false otherwise. Note, that only one checker can evaluate a call. If
  204. /// more than one checker claims that they can evaluate the same call the
  205. /// first one wins.
  206. ///
  207. /// eval::Call
  208. bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; }
  209. /// 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
  217. ProgramStateRef evalAssume(ProgramStateRef State,
  218. SVal Cond,
  219. bool Assumption) const { return State; }
  220. /// Allows modifying SymbolReaper object. For example, checkers can explicitly
  221. /// register symbols of interest as live. These symbols will not be marked
  222. /// dead and removed.
  223. ///
  224. /// check::LiveSymbols
  225. void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}
  226. /// Called when the contents of one or more regions change.
  227. ///
  228. /// This can occur in many different ways: an explicit bind, a blanket
  229. /// invalidation of the region contents, or by passing a region to a function
  230. /// call whose behavior the analyzer cannot model perfectly.
  231. ///
  232. /// \param State The current program state.
  233. /// \param Invalidated A set of all symbols potentially touched by the change.
  234. /// \param ExplicitRegions The regions explicitly requested for invalidation.
  235. /// For a function call, this would be the arguments. For a bind, this
  236. /// would be the region being bound to.
  237. /// \param Regions The transitive closure of regions accessible from,
  238. /// \p ExplicitRegions, i.e. all regions that may have been touched
  239. /// by this change. For a simple bind, this list will be the same as
  240. /// \p ExplicitRegions, since a bind does not affect the contents of
  241. /// anything accessible through the base region.
  242. /// \param LCtx LocationContext that is useful for getting various contextual
  243. /// info, like callstack, CFG etc.
  244. /// \param Call The opaque call triggering this invalidation. Will be 0 if the
  245. /// change was not triggered by a call.
  246. ///
  247. /// check::RegionChanges
  248. ProgramStateRef
  249. checkRegionChanges(ProgramStateRef State,
  250. const InvalidatedSymbols *Invalidated,
  251. ArrayRef<const MemRegion *> ExplicitRegions,
  252. ArrayRef<const MemRegion *> Regions,
  253. const LocationContext *LCtx,
  254. const CallEvent *Call) const {
  255. return State;
  256. }
  257. /// Called when pointers escape.
  258. ///
  259. /// This notifies the checkers about pointer escape, which occurs whenever
  260. /// the analyzer cannot track the symbol any more. For example, as a
  261. /// result of assigning a pointer into a global or when it's passed to a
  262. /// function call the analyzer cannot model.
  263. ///
  264. /// \param State The state at the point of escape.
  265. /// \param Escaped The list of escaped symbols.
  266. /// \param Call The corresponding CallEvent, if the symbols escape as
  267. /// parameters to the given call.
  268. /// \param Kind How the symbols have escaped.
  269. /// \returns Checkers can modify the state by returning a new state.
  270. ProgramStateRef checkPointerEscape(ProgramStateRef State,
  271. const InvalidatedSymbols &Escaped,
  272. const CallEvent *Call,
  273. PointerEscapeKind Kind) const {
  274. return State;
  275. }
  276. /// Called when const pointers escape.
  277. ///
  278. /// Note: in most cases checkPointerEscape callback is sufficient.
  279. /// \sa checkPointerEscape
  280. ProgramStateRef checkConstPointerEscape(ProgramStateRef State,
  281. const InvalidatedSymbols &Escaped,
  282. const CallEvent *Call,
  283. PointerEscapeKind Kind) const {
  284. return State;
  285. }
  286. /// check::Event<ImplicitNullDerefEvent>
  287. void checkEvent(ImplicitNullDerefEvent Event) const {}
  288. /// Check every declaration in the AST.
  289. ///
  290. /// An AST traversal callback, which should only be used when the checker is
  291. /// not path sensitive. It will be called for every Declaration in the AST and
  292. /// can be specialized to only be called on subclasses of Decl, for example,
  293. /// FunctionDecl.
  294. ///
  295. /// check::ASTDecl<FunctionDecl>
  296. void checkASTDecl(const FunctionDecl *D,
  297. AnalysisManager &Mgr,
  298. BugReporter &BR) const {}
  299. };
  300. void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,
  301. CheckerContext &C) const {
  302. }
  303. } // end namespace ento
  304. } // end namespace clang