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 14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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 21 using namespace clang; 22 using namespace ento; 23 24 // All checkers should be placed into anonymous namespace. 25 // We place the CheckerDocumentation inside ento namespace to make the 26 // it visible in doxygen. 27 namespace clang { 28 namespace ento { 29 30 /// This checker documents the callback functions checkers can use to implement 31 /// the custom handling of the specific events during path exploration as well 32 /// as reporting bugs. Most of the callbacks are targeted at path-sensitive 33 /// checking. 34 /// 35 /// \sa CheckerContext 36 class CheckerDocumentation : public Checker< check::PreStmt<ReturnStmt>, 37 check::PostStmt<DeclStmt>, 38 check::PreObjCMessage, 39 check::PostObjCMessage, 40 check::ObjCMessageNil, 41 check::PreCall, 42 check::PostCall, 43 check::BranchCondition, 44 check::NewAllocator, 45 check::Location, 46 check::Bind, 47 check::DeadSymbols, 48 check::BeginFunction, 49 check::EndFunction, 50 check::EndAnalysis, 51 check::EndOfTranslationUnit, 52 eval::Call, 53 eval::Assume, 54 check::LiveSymbols, 55 check::RegionChanges, 56 check::PointerEscape, 57 check::ConstPointerEscape, 58 check::Event<ImplicitNullDerefEvent>, 59 check::ASTDecl<FunctionDecl> > { 60 public: 61 /// 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 /// 71 /// check::PreStmt<ReturnStmt> 72 void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {} 73 74 /// 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 /// 81 /// check::PostStmt<DeclStmt> 82 void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const; 83 84 /// 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, 88 /// including explicit message syntax and property access. 89 /// 90 /// check::PreObjCMessage 91 void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {} 92 93 /// Post-visit the Objective C message. 94 /// \sa checkPreObjCMessage() 95 /// 96 /// check::PostObjCMessage 97 void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {} 98 99 /// 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 108 /// 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 114 /// limit your checks to, say, function calls, you should test for that at the 115 /// beginning of your callback function. 116 /// 117 /// check::PreCall 118 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {} 119 120 /// Post-visit an abstract "call" event. 121 /// \sa checkPreObjCMessage() 122 /// 123 /// check::PostCall 124 void checkPostCall(const CallEvent &Call, CheckerContext &C) const {} 125 126 /// Pre-visit of the condition statement of a branch (such as IfStmt). 127 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {} 128 129 /// Post-visit the C++ operator new's allocation call. 130 /// 131 /// Execution of C++ operator new consists of the following phases: (1) call 132 /// default or overridden operator new() to allocate memory (2) cast the 133 /// return value of operator new() from void pointer type to class pointer 134 /// type, (3) assuming that the value is non-null, call the object's 135 /// constructor over this pointer, (4) declare that the value of the 136 /// new-expression is this pointer. This callback is called between steps 137 /// (2) and (3). Post-call for the allocator is called after step (1). 138 /// Pre-statement for the new-expression is called on step (4) when the value 139 /// of the expression is evaluated. 140 void checkNewAllocator(const CXXAllocatorCall &, CheckerContext &) const {} 141 142 /// Called on a load from and a store to a location. 143 /// 144 /// The method will be called each time a location (pointer) value is 145 /// accessed. 146 /// \param Loc The value of the location (pointer). 147 /// \param IsLoad The flag specifying if the location is a store or a load. 148 /// \param S The load is performed while processing the statement. 149 /// 150 /// check::Location 151 void checkLocation(SVal Loc, bool IsLoad, const Stmt *S, 152 CheckerContext &) const {} 153 154 /// Called on binding of a value to a location. 155 /// 156 /// \param Loc The value of the location (pointer). 157 /// \param Val The value which will be stored at the location Loc. 158 /// \param S The bind is performed while processing the statement S. 159 /// 160 /// check::Bind 161 void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {} 162 163 /// Called whenever a symbol becomes dead. 164 /// 165 /// This callback should be used by the checkers to aggressively clean 166 /// up/reduce the checker state, which is important for reducing the overall 167 /// memory usage. Specifically, if a checker keeps symbol specific information 168 /// in the state, it can and should be dropped after the symbol becomes dead. 169 /// In addition, reporting a bug as soon as the checker becomes dead leads to 170 /// more precise diagnostics. (For example, one should report that a malloced 171 /// variable is not freed right after it goes out of scope.) 172 /// 173 /// \param SR The SymbolReaper object can be queried to determine which 174 /// symbols are dead. 175 /// 176 /// check::DeadSymbols 177 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {} 178 179 180 /// Called when the analyzer core starts analyzing a function, 181 /// regardless of whether it is analyzed at the top level or is inlined. 182 /// 183 /// check::BeginFunction 184 void checkBeginFunction(CheckerContext &Ctx) const {} 185 186 /// Called when the analyzer core reaches the end of a 187 /// function being analyzed regardless of whether it is analyzed at the top 188 /// level or is inlined. 189 /// 190 /// check::EndFunction 191 void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const {} 192 193 /// Called after all the paths in the ExplodedGraph reach end of path 194 /// - the symbolic execution graph is fully explored. 195 /// 196 /// This callback should be used in cases when a checker needs to have a 197 /// global view of the information generated on all paths. For example, to 198 /// compare execution summary/result several paths. 199 /// See IdempotentOperationChecker for a usage example. 200 /// 201 /// check::EndAnalysis 202 void checkEndAnalysis(ExplodedGraph &G, 203 BugReporter &BR, 204 ExprEngine &Eng) const {} 205 206 /// Called after analysis of a TranslationUnit is complete. 207 /// 208 /// check::EndOfTranslationUnit 209 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU, 210 AnalysisManager &Mgr, 211 BugReporter &BR) const {} 212 213 /// Evaluates function call. 214 /// 215 /// The analysis core treats all function calls in the same way. However, some 216 /// functions have special meaning, which should be reflected in the program 217 /// state. This callback allows a checker to provide domain specific knowledge 218 /// about the particular functions it knows about. 219 /// 220 /// \returns true if the call has been successfully evaluated 221 /// and false otherwise. Note, that only one checker can evaluate a call. If 222 /// more than one checker claims that they can evaluate the same call the 223 /// first one wins. 224 /// 225 /// eval::Call 226 bool evalCall(const CallEvent &Call, CheckerContext &C) const { return true; } 227 228 /// Handles assumptions on symbolic values. 229 /// 230 /// This method is called when a symbolic expression is assumed to be true or 231 /// false. For example, the assumptions are performed when evaluating a 232 /// condition at a branch. The callback allows checkers track the assumptions 233 /// performed on the symbols of interest and change the state accordingly. 234 /// 235 /// eval::Assume 236 ProgramStateRef evalAssume(ProgramStateRef State, 237 SVal Cond, 238 bool Assumption) const { return State; } 239 240 /// Allows modifying SymbolReaper object. For example, checkers can explicitly 241 /// register symbols of interest as live. These symbols will not be marked 242 /// dead and removed. 243 /// 244 /// check::LiveSymbols 245 void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {} 246 247 /// Called when the contents of one or more regions change. 248 /// 249 /// This can occur in many different ways: an explicit bind, a blanket 250 /// invalidation of the region contents, or by passing a region to a function 251 /// call whose behavior the analyzer cannot model perfectly. 252 /// 253 /// \param State The current program state. 254 /// \param Invalidated A set of all symbols potentially touched by the change. 255 /// \param ExplicitRegions The regions explicitly requested for invalidation. 256 /// For a function call, this would be the arguments. For a bind, this 257 /// would be the region being bound to. 258 /// \param Regions The transitive closure of regions accessible from, 259 /// \p ExplicitRegions, i.e. all regions that may have been touched 260 /// by this change. For a simple bind, this list will be the same as 261 /// \p ExplicitRegions, since a bind does not affect the contents of 262 /// anything accessible through the base region. 263 /// \param LCtx LocationContext that is useful for getting various contextual 264 /// info, like callstack, CFG etc. 265 /// \param Call The opaque call triggering this invalidation. Will be 0 if the 266 /// change was not triggered by a call. 267 /// 268 /// check::RegionChanges 269 ProgramStateRef 270 checkRegionChanges(ProgramStateRef State, 271 const InvalidatedSymbols *Invalidated, 272 ArrayRef<const MemRegion *> ExplicitRegions, 273 ArrayRef<const MemRegion *> Regions, 274 const LocationContext *LCtx, 275 const CallEvent *Call) const { 276 return State; 277 } 278 279 /// Called when pointers escape. 280 /// 281 /// This notifies the checkers about pointer escape, which occurs whenever 282 /// the analyzer cannot track the symbol any more. For example, as a 283 /// result of assigning a pointer into a global or when it's passed to a 284 /// function call the analyzer cannot model. 285 /// 286 /// \param State The state at the point of escape. 287 /// \param Escaped The list of escaped symbols. 288 /// \param Call The corresponding CallEvent, if the symbols escape as 289 /// parameters to the given call. 290 /// \param Kind How the symbols have escaped. 291 /// \returns Checkers can modify the state by returning a new state. 292 ProgramStateRef checkPointerEscape(ProgramStateRef State, 293 const InvalidatedSymbols &Escaped, 294 const CallEvent *Call, 295 PointerEscapeKind Kind) const { 296 return State; 297 } 298 299 /// Called when const pointers escape. 300 /// 301 /// Note: in most cases checkPointerEscape callback is sufficient. 302 /// \sa checkPointerEscape 303 ProgramStateRef checkConstPointerEscape(ProgramStateRef State, 304 const InvalidatedSymbols &Escaped, 305 const CallEvent *Call, 306 PointerEscapeKind Kind) const { 307 return State; 308 } 309 310 /// check::Event<ImplicitNullDerefEvent> 311 void checkEvent(ImplicitNullDerefEvent Event) const {} 312 313 /// Check every declaration in the AST. 314 /// 315 /// An AST traversal callback, which should only be used when the checker is 316 /// not path sensitive. It will be called for every Declaration in the AST and 317 /// can be specialized to only be called on subclasses of Decl, for example, 318 /// FunctionDecl. 319 /// 320 /// check::ASTDecl<FunctionDecl> 321 void checkASTDecl(const FunctionDecl *D, 322 AnalysisManager &Mgr, 323 BugReporter &BR) const {} 324 }; 325 326 void CheckerDocumentation::checkPostStmt(const DeclStmt *DS, 327 CheckerContext &C) const { 328 } 329 330 void registerCheckerDocumentationChecker(CheckerManager &Mgr) { 331 Mgr.registerChecker<CheckerDocumentation>(); 332 } 333 334 bool shouldRegisterCheckerDocumentationChecker(const CheckerManager &) { 335 return false; 336 } 337 338 } // end namespace ento 339 } // end namespace clang 340