Path: blob/main/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/CheckerDocumentation.cpp
35266 views
//===- CheckerDocumentation.cpp - Documentation checker ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This checker lists all the checker callbacks and provides documentation for9// checker writers.10//11//===----------------------------------------------------------------------===//1213#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"1920using namespace clang;21using namespace ento;2223// 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 {2829/// 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 off38check::ASTCodeBody,39check::ASTDecl<FunctionDecl>,40check::BeginFunction,41check::Bind,42check::BranchCondition,43check::ConstPointerEscape,44check::DeadSymbols,45check::EndAnalysis,46check::EndFunction,47check::EndOfTranslationUnit,48check::Event<ImplicitNullDerefEvent>,49check::LiveSymbols,50check::Location,51check::NewAllocator,52check::ObjCMessageNil,53check::PointerEscape,54check::PostCall,55check::PostObjCMessage,56check::PostStmt<DeclStmt>,57check::PreCall,58check::PreObjCMessage,59check::PreStmt<ReturnStmt>,60check::RegionChanges,61eval::Assume,62eval::Call63// clang-format on64> {65public:66/// Pre-visit the Statement.67///68/// The method will be called before the analyzer core processes the69/// statement. The notification is performed for every explored CFGElement,70/// which does not include the control flow statements such as IfStmt. The71/// callback can be specialized to be called with any subclass of Stmt.72///73/// See checkBranchCondition() callback for performing custom processing of74/// the branching statements.75///76/// check::PreStmt<ReturnStmt>77void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const {}7879/// Post-visit the Statement.80///81/// The method will be called after the analyzer core processes the82/// statement. The notification is performed for every explored CFGElement,83/// which does not include the control flow statements such as IfStmt. The84/// callback can be specialized to be called with any subclass of Stmt.85///86/// check::PostStmt<DeclStmt>87void checkPostStmt(const DeclStmt *DS, CheckerContext &C) const;8889/// Pre-visit the Objective C message.90///91/// This will be called before the analyzer core processes the method call.92/// This is called for any action which produces an Objective-C message send,93/// including explicit message syntax and property access.94///95/// check::PreObjCMessage96void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}9798/// Post-visit the Objective C message.99/// \sa checkPreObjCMessage()100///101/// check::PostObjCMessage102void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {}103104/// Visit an Objective-C message whose receiver is nil.105///106/// This will be called when the analyzer core processes a method call whose107/// receiver is definitely nil. In this case, check{Pre/Post}ObjCMessage and108/// check{Pre/Post}Call will not be called.109///110/// check::ObjCMessageNil111void checkObjCMessageNil(const ObjCMethodCall &M, CheckerContext &C) const {}112113/// Pre-visit an abstract "call" event.114///115/// This is used for checkers that want to check arguments or attributed116/// behavior for functions and methods no matter how they are being invoked.117///118/// Note that this includes ALL cross-body invocations, so if you want to119/// limit your checks to, say, function calls, you should test for that at the120/// beginning of your callback function.121///122/// check::PreCall123void checkPreCall(const CallEvent &Call, CheckerContext &C) const {}124125/// Post-visit an abstract "call" event.126/// \sa checkPreObjCMessage()127///128/// check::PostCall129void checkPostCall(const CallEvent &Call, CheckerContext &C) const {}130131/// Pre-visit of the condition statement of a branch (such as IfStmt).132void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}133134/// Post-visit the C++ operator new's allocation call.135///136/// Execution of C++ operator new consists of the following phases: (1) call137/// default or overridden operator new() to allocate memory (2) cast the138/// return value of operator new() from void pointer type to class pointer139/// type, (3) assuming that the value is non-null, call the object's140/// constructor over this pointer, (4) declare that the value of the141/// new-expression is this pointer. This callback is called between steps142/// (2) and (3). Post-call for the allocator is called after step (1).143/// Pre-statement for the new-expression is called on step (4) when the value144/// of the expression is evaluated.145void checkNewAllocator(const CXXAllocatorCall &, CheckerContext &) const {}146147/// Called on a load from and a store to a location.148///149/// The method will be called each time a location (pointer) value is150/// accessed.151/// \param Loc The value of the location (pointer).152/// \param IsLoad The flag specifying if the location is a store or a load.153/// \param S The load is performed while processing the statement.154///155/// check::Location156void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,157CheckerContext &) const {}158159/// Called on binding of a value to a location.160///161/// \param Loc The value of the location (pointer).162/// \param Val The value which will be stored at the location Loc.163/// \param S The bind is performed while processing the statement S.164///165/// check::Bind166void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}167168/// Called whenever a symbol becomes dead.169///170/// This callback should be used by the checkers to aggressively clean171/// up/reduce the checker state, which is important for reducing the overall172/// memory usage. Specifically, if a checker keeps symbol specific information173/// in the state, it can and should be dropped after the symbol becomes dead.174/// In addition, reporting a bug as soon as the checker becomes dead leads to175/// more precise diagnostics. (For example, one should report that a malloced176/// variable is not freed right after it goes out of scope.)177///178/// \param SR The SymbolReaper object can be queried to determine which179/// symbols are dead.180///181/// check::DeadSymbols182void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}183184185/// Called when the analyzer core starts analyzing a function,186/// regardless of whether it is analyzed at the top level or is inlined.187///188/// check::BeginFunction189void checkBeginFunction(CheckerContext &Ctx) const {}190191/// Called when the analyzer core reaches the end of a192/// function being analyzed regardless of whether it is analyzed at the top193/// level or is inlined.194///195/// check::EndFunction196void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const {}197198/// Called after all the paths in the ExplodedGraph reach end of path199/// - the symbolic execution graph is fully explored.200///201/// This callback should be used in cases when a checker needs to have a202/// global view of the information generated on all paths. For example, to203/// compare execution summary/result several paths.204/// See IdempotentOperationChecker for a usage example.205///206/// check::EndAnalysis207void checkEndAnalysis(ExplodedGraph &G,208BugReporter &BR,209ExprEngine &Eng) const {}210211/// Called after analysis of a TranslationUnit is complete.212///213/// check::EndOfTranslationUnit214void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,215AnalysisManager &Mgr,216BugReporter &BR) const {}217218/// Evaluates function call.219///220/// The analysis core treats all function calls in the same way. However, some221/// functions have special meaning, which should be reflected in the program222/// state. This callback allows a checker to provide domain specific knowledge223/// about the particular functions it knows about.224///225/// \returns true if the call has been successfully evaluated226/// and false otherwise. Note, that only one checker can evaluate a call. If227/// more than one checker claims that they can evaluate the same call the228/// first one wins.229///230/// eval::Call231bool evalCall(const CallEvent &Call, CheckerContext &C) const { return true; }232233/// Handles assumptions on symbolic values.234///235/// This method is called when a symbolic expression is assumed to be true or236/// false. For example, the assumptions are performed when evaluating a237/// condition at a branch. The callback allows checkers track the assumptions238/// performed on the symbols of interest and change the state accordingly.239///240/// eval::Assume241ProgramStateRef evalAssume(ProgramStateRef State,242SVal Cond,243bool Assumption) const { return State; }244245/// Allows modifying SymbolReaper object. For example, checkers can explicitly246/// register symbols of interest as live. These symbols will not be marked247/// dead and removed.248///249/// check::LiveSymbols250void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}251252/// Called when the contents of one or more regions change.253///254/// This can occur in many different ways: an explicit bind, a blanket255/// invalidation of the region contents, or by passing a region to a function256/// call whose behavior the analyzer cannot model perfectly.257///258/// \param State The current program state.259/// \param Invalidated A set of all symbols potentially touched by the change.260/// \param ExplicitRegions The regions explicitly requested for invalidation.261/// For a function call, this would be the arguments. For a bind, this262/// would be the region being bound to.263/// \param Regions The transitive closure of regions accessible from,264/// \p ExplicitRegions, i.e. all regions that may have been touched265/// by this change. For a simple bind, this list will be the same as266/// \p ExplicitRegions, since a bind does not affect the contents of267/// anything accessible through the base region.268/// \param LCtx LocationContext that is useful for getting various contextual269/// info, like callstack, CFG etc.270/// \param Call The opaque call triggering this invalidation. Will be 0 if the271/// change was not triggered by a call.272///273/// check::RegionChanges274ProgramStateRef275checkRegionChanges(ProgramStateRef State,276const InvalidatedSymbols *Invalidated,277ArrayRef<const MemRegion *> ExplicitRegions,278ArrayRef<const MemRegion *> Regions,279const LocationContext *LCtx,280const CallEvent *Call) const {281return State;282}283284/// Called when pointers escape.285///286/// This notifies the checkers about pointer escape, which occurs whenever287/// the analyzer cannot track the symbol any more. For example, as a288/// result of assigning a pointer into a global or when it's passed to a289/// function call the analyzer cannot model.290///291/// \param State The state at the point of escape.292/// \param Escaped The list of escaped symbols.293/// \param Call The corresponding CallEvent, if the symbols escape as294/// parameters to the given call.295/// \param Kind How the symbols have escaped.296/// \returns Checkers can modify the state by returning a new state.297ProgramStateRef checkPointerEscape(ProgramStateRef State,298const InvalidatedSymbols &Escaped,299const CallEvent *Call,300PointerEscapeKind Kind) const {301return State;302}303304/// Called when const pointers escape.305///306/// Note: in most cases checkPointerEscape callback is sufficient.307/// \sa checkPointerEscape308ProgramStateRef checkConstPointerEscape(ProgramStateRef State,309const InvalidatedSymbols &Escaped,310const CallEvent *Call,311PointerEscapeKind Kind) const {312return State;313}314315/// check::Event<ImplicitNullDerefEvent>316void checkEvent(ImplicitNullDerefEvent Event) const {}317318/// Check every declaration in the AST.319///320/// An AST traversal callback, which should only be used when the checker is321/// not path sensitive. It will be called for every Declaration in the AST and322/// can be specialized to only be called on subclasses of Decl, for example,323/// FunctionDecl.324///325/// check::ASTDecl<FunctionDecl>326void checkASTDecl(const FunctionDecl *D,327AnalysisManager &Mgr,328BugReporter &BR) const {}329330/// Check every declaration that has a statement body in the AST.331///332/// As AST traversal callback, which should only be used when the checker is333/// not path sensitive. It will be called for every Declaration in the AST.334void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,335BugReporter &BR) const {}336};337338void CheckerDocumentation::checkPostStmt(const DeclStmt *DS,339CheckerContext &C) const {340}341342void registerCheckerDocumentationChecker(CheckerManager &Mgr) {343Mgr.registerChecker<CheckerDocumentation>();344}345346bool shouldRegisterCheckerDocumentationChecker(const CheckerManager &) {347return false;348}349350} // end namespace ento351} // end namespace clang352353354