401 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This checker lists all the checker callbacks and provides documentation for10// 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 20using namespace clang;21using namespace ento;22 23// All checkers should be placed into anonymous namespace.24// We place the CheckerDocumentation inside ento namespace to make the25// it visible in doxygen.26namespace clang {27namespace ento {28 29/// This checker documents the callback functions checkers can use to implement30/// the custom handling of the specific events during path exploration as well31/// as reporting bugs. Most of the callbacks are targeted at path-sensitive32/// checking.33///34/// \sa CheckerContext35class CheckerDocumentation36 : public Checker<37 // clang-format off38 check::ASTCodeBody,39 check::ASTDecl<FunctionDecl>,40 check::BeginFunction,41 check::Bind,42 check::BlockEntrance,43 check::BranchCondition,44 check::ConstPointerEscape,45 check::DeadSymbols,46 check::EndAnalysis,47 check::EndFunction,48 check::EndOfTranslationUnit,49 check::Event<ImplicitNullDerefEvent>,50 check::LiveSymbols,51 check::Location,52 check::NewAllocator,53 check::ObjCMessageNil,54 check::PointerEscape,55 check::PostCall,56 check::PostObjCMessage,57 check::PostStmt<DeclStmt>,58 check::PreCall,59 check::PreObjCMessage,60 check::PreStmt<ReturnStmt>,61 check::RegionChanges,62 eval::Assume,63 eval::Call64 // clang-format on65 > {66public:67 /// Pre-visit the Statement.68 ///69 /// The method will be called before the analyzer core processes the70 /// statement. The notification is performed for every explored CFGElement,71 /// which does not include the control flow statements such as IfStmt. The72 /// callback can be specialized to be called with any subclass of Stmt.73 ///74 /// See checkBranchCondition() callback for performing custom processing of75 /// the branching statements.76 ///77 /// check::PreStmt<ReturnStmt>78 void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}79 80 /// Post-visit the Statement.81 ///82 /// The method will be called after the analyzer core processes the83 /// statement. The notification is performed for every explored CFGElement,84 /// which does not include the control flow statements such as IfStmt. The85 /// callback can be specialized to be called with any subclass of Stmt.86 ///87 /// check::PostStmt<DeclStmt>88 void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;89 90 /// Pre-visit the Objective C message.91 ///92 /// This will be called before the analyzer core processes the method call.93 /// This is called for any action which produces an Objective-C message send,94 /// including explicit message syntax and property access.95 ///96 /// check::PreObjCMessage97 void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}98 99 /// Post-visit the Objective C message.100 /// \sa checkPreObjCMessage()101 ///102 /// check::PostObjCMessage103 void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}104 105 /// Visit an Objective-C message whose receiver is nil.106 ///107 /// This will be called when the analyzer core processes a method call whose108 /// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and109 /// check{Pre/Post}Call will not be called.110 ///111 /// check::ObjCMessageNil112 void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}113 114 /// Pre-visit an abstract "call" event.115 ///116 /// This is used for checkers that want to check arguments or attributed117 /// behavior for functions and methods no matter how they are being invoked.118 ///119 /// Note that this includes ALL cross-body invocations, so if you want to120 /// limit your checks to, say, function calls, you should test for that at the121 /// beginning of your callback function.122 ///123 /// check::PreCall124 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}125 126 /// Post-visit an abstract "call" event.127 /// \sa checkPreObjCMessage()128 ///129 /// check::PostCall130 void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}131 132 /// Pre-visit of the condition statement of a branch.133 /// For example:134 /// - logical operators (&&, ||)135 /// - if, do, while, for, ranged-for statements136 /// - ternary operators (?:), gnu conditionals, gnu choose expressions137 /// Interestingly, switch statements don't seem to trigger BranchCondition.138 ///139 /// check::BlockEntrance is a similar callback, which is strictly more140 /// generic. Prefer check::BranchCondition to check::BlockEntrance if141 /// pre-visiting conditional statements is enough for the checker.142 /// Note that check::BlockEntrance is also invoked for leaving basic blocks143 /// while entering the next.144 ///145 /// check::BranchCondition146 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}147 148 /// Post-visit the C++ operator new's allocation call.149 ///150 /// Execution of C++ operator new consists of the following phases: (1) call151 /// default or overridden operator new() to allocate memory (2) cast the152 /// return value of operator new() from void pointer type to class pointer153 /// type, (3) assuming that the value is non-null, call the object's154 /// constructor over this pointer, (4) declare that the value of the155 /// new-expression is this pointer. This callback is called between steps156 /// (2) and (3). Post-call for the allocator is called after step (1).157 /// Pre-statement for the new-expression is called on step (4) when the value158 /// of the expression is evaluated.159 void checkNewAllocator(const CXXAllocatorCall &, CheckerContext &) const {}160 161 /// Called on a load from and a store to a location.162 ///163 /// The method will be called each time a location (pointer) value is164 /// accessed.165 /// \param Loc The value of the location (pointer).166 /// \param IsLoad The flag specifying if the location is a store or a load.167 /// \param S The load is performed while processing the statement.168 ///169 /// check::Location170 void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,171 CheckerContext &) const {}172 173 /// Called on binding of a value to a location.174 ///175 /// \param Loc The value of the location (pointer).176 /// \param Val The value which will be stored at the location Loc.177 /// \param S The bind is performed while processing the statement S.178 /// \param AtDeclInit Whether the bind is performed during declaration179 /// initialization.180 ///181 /// check::Bind182 void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit,183 CheckerContext &) const {}184 185 /// Called after a CFG edge is taken within a function.186 ///187 /// This callback can be used to obtain information about potential branching188 /// points or any other constructs that involve traversing a CFG edge.189 ///190 /// check::BranchCondition is a similar callback, which is only invoked for191 /// pre-visiting the condition statement of a branch. Prefer that callback if192 /// possible.193 ///194 /// \remark There is no CFG edge from the caller to a callee, consequently195 /// this callback is not invoked for "inlining" a function call.196 /// \remark Once a function call is inlined, we will start from the imaginary197 /// "entry" basic block of that CFG. This callback will be invoked for198 /// entering the real first basic block of the "inlined" function body from199 /// that "entry" basic block.200 /// \remark This callback is also invoked for entering the imaginary "exit"201 /// basic block of the CFG when returning from a function.202 ///203 /// \param E The ProgramPoint that describes the transition.204 ///205 /// check::BlockEntrance206 void checkBlockEntrance(const BlockEntrance &E, CheckerContext &) const {}207 208 /// Called whenever a symbol becomes dead.209 ///210 /// This callback should be used by the checkers to aggressively clean211 /// up/reduce the checker state, which is important for reducing the overall212 /// memory usage. Specifically, if a checker keeps symbol specific information213 /// in the state, it can and should be dropped after the symbol becomes dead.214 /// In addition, reporting a bug as soon as the checker becomes dead leads to215 /// more precise diagnostics. (For example, one should report that a malloced216 /// variable is not freed right after it goes out of scope.)217 ///218 /// \param SR The SymbolReaper object can be queried to determine which219 /// symbols are dead.220 ///221 /// check::DeadSymbols222 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}223 224 225 /// Called when the analyzer core starts analyzing a function,226 /// regardless of whether it is analyzed at the top level or is inlined.227 ///228 /// check::BeginFunction229 void checkBeginFunction(CheckerContext &Ctx) const {}230 231 /// Called when the analyzer core reaches the end of a232 /// function being analyzed regardless of whether it is analyzed at the top233 /// level or is inlined.234 ///235 /// check::EndFunction236 void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const {}237 238 /// Called after all the paths in the ExplodedGraph reach end of path239 /// - the symbolic execution graph is fully explored.240 ///241 /// This callback should be used in cases when a checker needs to have a242 /// global view of the information generated on all paths. For example, to243 /// compare execution summary/result several paths.244 /// See IdempotentOperationChecker for a usage example.245 ///246 /// check::EndAnalysis247 void checkEndAnalysis(ExplodedGraph &G,248 BugReporter &BR,249 ExprEngine &Eng) const {}250 251 /// Called after analysis of a TranslationUnit is complete.252 ///253 /// check::EndOfTranslationUnit254 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,255 AnalysisManager &Mgr,256 BugReporter &BR) const {}257 258 /// Evaluates function call.259 ///260 /// The analysis core treats all function calls in the same way. However, some261 /// functions have special meaning, which should be reflected in the program262 /// state. This callback allows a checker to provide domain specific knowledge263 /// about the particular functions it knows about.264 ///265 /// Note that to evaluate a call, the handler MUST bind the return value if266 /// its a non-void function. Invalidate the arguments if necessary.267 ///268 /// Note that in general, user-provided functions should not be eval-called269 /// because the checker can't predict the exact semantics/contract of the270 /// callee, and by having the eval::Call callback, we also prevent it from271 /// getting inlined, potentially regressing analysis quality.272 /// Consider using check::PreCall or check::PostCall to allow inlining.273 ///274 /// \returns true if the call has been successfully evaluated275 /// and false otherwise. Note, that only one checker can evaluate a call. If276 /// more than one checker claims that they can evaluate the same call the277 /// first one wins.278 ///279 /// eval::Call280 bool evalCall(const CallEvent &Call, CheckerContext &C) const { return true; }281 282 /// Handles assumptions on symbolic values.283 ///284 /// This method is called when a symbolic expression is assumed to be true or285 /// false. For example, the assumptions are performed when evaluating a286 /// condition at a branch. The callback allows checkers track the assumptions287 /// performed on the symbols of interest and change the state accordingly.288 ///289 /// eval::Assume290 ProgramStateRef evalAssume(ProgramStateRef State,291 SVal Cond,292 bool Assumption) const { return State; }293 294 /// Allows modifying SymbolReaper object. For example, checkers can explicitly295 /// register symbols of interest as live. These symbols will not be marked296 /// dead and removed.297 ///298 /// check::LiveSymbols299 void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}300 301 /// Called when the contents of one or more regions change.302 ///303 /// This can occur in many different ways: an explicit bind, a blanket304 /// invalidation of the region contents, or by passing a region to a function305 /// call whose behavior the analyzer cannot model perfectly.306 ///307 /// \param State The current program state.308 /// \param Invalidated A set of all symbols potentially touched by the change.309 /// \param ExplicitRegions The regions explicitly requested for invalidation.310 /// For a function call, this would be the arguments. For a bind, this311 /// would be the region being bound to.312 /// \param Regions The transitive closure of regions accessible from,313 /// \p ExplicitRegions, i.e. all regions that may have been touched314 /// by this change. For a simple bind, this list will be the same as315 /// \p ExplicitRegions, since a bind does not affect the contents of316 /// anything accessible through the base region.317 /// \param LCtx LocationContext that is useful for getting various contextual318 /// info, like callstack, CFG etc.319 /// \param Call The opaque call triggering this invalidation. Will be 0 if the320 /// change was not triggered by a call.321 ///322 /// check::RegionChanges323 ProgramStateRef324 checkRegionChanges(ProgramStateRef State,325 const InvalidatedSymbols *Invalidated,326 ArrayRef<const MemRegion *> ExplicitRegions,327 ArrayRef<const MemRegion *> Regions,328 const LocationContext *LCtx,329 const CallEvent *Call) const {330 return State;331 }332 333 /// Called when pointers escape.334 ///335 /// This notifies the checkers about pointer escape, which occurs whenever336 /// the analyzer cannot track the symbol any more. For example, as a337 /// result of assigning a pointer into a global or when it's passed to a338 /// function call the analyzer cannot model.339 ///340 /// \param State The state at the point of escape.341 /// \param Escaped The list of escaped symbols.342 /// \param Call The corresponding CallEvent, if the symbols escape as343 /// parameters to the given call.344 /// \param Kind How the symbols have escaped.345 /// \returns Checkers can modify the state by returning a new state.346 ProgramStateRef checkPointerEscape(ProgramStateRef State,347 const InvalidatedSymbols &Escaped,348 const CallEvent *Call,349 PointerEscapeKind Kind) const {350 return State;351 }352 353 /// Called when const pointers escape.354 ///355 /// Note: in most cases checkPointerEscape callback is sufficient.356 /// \sa checkPointerEscape357 ProgramStateRef checkConstPointerEscape(ProgramStateRef State,358 const InvalidatedSymbols &Escaped,359 const CallEvent *Call,360 PointerEscapeKind Kind) const {361 return State;362 }363 364 /// check::Event<ImplicitNullDerefEvent>365 void checkEvent(ImplicitNullDerefEvent Event) const {}366 367 /// Check every declaration in the AST.368 ///369 /// An AST traversal callback, which should only be used when the checker is370 /// not path sensitive. It will be called for every Declaration in the AST and371 /// can be specialized to only be called on subclasses of Decl, for example,372 /// FunctionDecl.373 ///374 /// check::ASTDecl<FunctionDecl>375 void checkASTDecl(const FunctionDecl *D,376 AnalysisManager &Mgr,377 BugReporter &BR) const {}378 379 /// Check every declaration that has a statement body in the AST.380 ///381 /// As AST traversal callback, which should only be used when the checker is382 /// not path sensitive. It will be called for every Declaration in the AST.383 void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,384 BugReporter &BR) const {}385};386 387void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,388 CheckerContext &C) const {389}390 391void registerCheckerDocumentationChecker(CheckerManager &Mgr) {392 Mgr.registerChecker<CheckerDocumentation>();393}394 395bool shouldRegisterCheckerDocumentationChecker(const CheckerManager &) {396 return false;397}398 399} // end namespace ento400} // end namespace clang401