clang API Documentation

CheckerDocumentation.cpp
Go to the documentation of this file.
00001 //= CheckerDocumentation.cpp - Documentation checker ---------------*- C++ -*-//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This checker lists all the checker callbacks and provides documentation for
00011 // checker writers.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "ClangSACheckers.h"
00016 #include "clang/StaticAnalyzer/Core/Checker.h"
00017 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00019 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00020 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
00021 
00022 using namespace clang;
00023 using namespace ento;
00024 
00025 // All checkers should be placed into anonymous namespace.
00026 // We place the CheckerDocumentation inside ento namespace to make the
00027 // it visible in doxygen.
00028 namespace ento {
00029 
00030 /// This checker documents the callback functions checkers can use to implement
00031 /// the custom handling of the specific events during path exploration as well
00032 /// as reporting bugs. Most of the callbacks are targeted at path-sensitive
00033 /// checking.
00034 ///
00035 /// \sa CheckerContext
00036 class CheckerDocumentation : public Checker< check::PreStmt<DeclStmt>,
00037                                        check::PostStmt<CallExpr>,
00038                                        check::PreObjCMessage,
00039                                        check::PostObjCMessage,
00040                                        check::BranchCondition,
00041                                        check::Location,
00042                                        check::Bind,
00043                                        check::DeadSymbols,
00044                                        check::EndPath,
00045                                        check::EndAnalysis,
00046                                        check::EndOfTranslationUnit,
00047                                        eval::Call,
00048                                        eval::Assume,
00049                                        check::LiveSymbols,
00050                                        check::RegionChanges,
00051                                        check::Event<ImplicitNullDerefEvent>,
00052                                        check::ASTDecl<FunctionDecl> > {
00053 public:
00054 
00055   /// \brief Pre-visit the Statement.
00056   ///
00057   /// The method will be called before the analyzer core processes the
00058   /// statement. The notification is performed for every explored CFGElement,
00059   /// which does not include the control flow statements such as IfStmt. The
00060   /// callback can be specialized to be called with any subclass of Stmt.
00061   ///
00062   /// See checkBranchCondition() callback for performing custom processing of
00063   /// the branching statements.
00064   ///
00065   /// check::PreStmt<DeclStmt>
00066   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {}
00067 
00068   /// \brief Post-visit the Statement.
00069   ///
00070   /// The method will be called after the analyzer core processes the
00071   /// statement. The notification is performed for every explored CFGElement,
00072   /// which does not include the control flow statements such as IfStmt. The
00073   /// callback can be specialized to be called with any subclass of Stmt.
00074   ///
00075   /// check::PostStmt<DeclStmt>
00076   void checkPostStmt(const CallExpr *DS, CheckerContext &C) const;
00077 
00078   /// \brief Pre-visit the Objective C messages.
00079   void checkPreObjCMessage(const ObjCMessage &Msg, CheckerContext &C) const {}
00080 
00081   /// \brief Post-visit the Objective C messages.
00082   void checkPostObjCMessage(const ObjCMessage &Msg, CheckerContext &C) const {}
00083 
00084   /// \brief Pre-visit of the condition statement of a branch (such as IfStmt).
00085   void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {}
00086 
00087   /// \brief Called on a load from and a store to a location.
00088   ///
00089   /// The method will be called each time a location (pointer) value is
00090   /// accessed.
00091   /// \param Loc    The value of the location (pointer).
00092   /// \param IsLoad The flag specifying if the location is a store or a load.
00093   /// \param S      The load is performed while processing the statement.
00094   ///
00095   /// check::Location
00096   void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
00097                      CheckerContext &C) const {}
00098 
00099   /// \brief Called on binding of a value to a location.
00100   ///
00101   /// \param Loc The value of the location (pointer).
00102   /// \param Val The value which will be stored at the location Loc.
00103   /// \param S   The bind is performed while processing the statement S.
00104   ///
00105   /// check::Bind
00106   void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const {}
00107 
00108 
00109   /// \brief Called whenever a symbol becomes dead.
00110   ///
00111   /// This callback should be used by the checkers to aggressively clean
00112   /// up/reduce the checker state, which is important for reducing the overall
00113   /// memory usage. Specifically, if a checker keeps symbol specific information
00114   /// in the sate, it can and should be dropped after the symbol becomes dead.
00115   /// In addition, reporting a bug as soon as the checker becomes dead leads to
00116   /// more precise diagnostics. (For example, one should report that a malloced
00117   /// variable is not freed right after it goes out of scope.)
00118   ///
00119   /// \param SR The SymbolReaper object can be queried to determine which
00120   ///           symbols are dead.
00121   ///
00122   /// check::DeadSymbols
00123   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {}
00124 
00125   /// \brief Called when an end of path is reached in the ExplodedGraph.
00126   ///
00127   /// This callback should be used to check if the allocated resources are freed.
00128   ///
00129   /// check::EndPath
00130   void checkEndPath(CheckerContext &Ctx) const {}
00131 
00132   /// \brief Called after all the paths in the ExplodedGraph reach end of path
00133   /// - the symbolic execution graph is fully explored.
00134   ///
00135   /// This callback should be used in cases when a checker needs to have a
00136   /// global view of the information generated on all paths. For example, to
00137   /// compare execution summary/result several paths.
00138   /// See IdempotentOperationChecker for a usage example.
00139   ///
00140   /// check::EndAnalysis
00141   void checkEndAnalysis(ExplodedGraph &G,
00142                         BugReporter &BR,
00143                         ExprEngine &Eng) const {}
00144 
00145   /// \brief Called after analysis of a TranslationUnit is complete.
00146   ///
00147   /// check::EndOfTranslationUnit
00148   void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
00149                                  AnalysisManager &Mgr,
00150                                  BugReporter &BR) const {}
00151 
00152 
00153   /// \brief Evaluates function call.
00154   ///
00155   /// The analysis core threats all function calls in the same way. However, some
00156   /// functions have special meaning, which should be reflected in the program
00157   /// state. This callback allows a checker to provide domain specific knowledge
00158   /// about the particular functions it knows about.
00159   ///
00160   /// \returns true if the call has been successfully evaluated
00161   /// and false otherwise. Note, that only one checker can evaluate a call. If
00162   /// more then one checker claim that they can evaluate the same call the
00163   /// first one wins.
00164   ///
00165   /// eval::Call
00166   bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; }
00167 
00168   /// \brief Handles assumptions on symbolic values.
00169   ///
00170   /// This method is called when a symbolic expression is assumed to be true or
00171   /// false. For example, the assumptions are performed when evaluating a
00172   /// condition at a branch. The callback allows checkers track the assumptions
00173   /// performed on the symbols of interest and change the state accordingly.
00174   ///
00175   /// eval::Assume
00176   ProgramStateRef evalAssume(ProgramStateRef State,
00177                                  SVal Cond,
00178                                  bool Assumption) const { return State; }
00179 
00180   /// Allows modifying SymbolReaper object. For example, checkers can explicitly
00181   /// register symbols of interest as live. These symbols will not be marked
00182   /// dead and removed.
00183   ///
00184   /// check::LiveSymbols
00185   void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {}
00186 
00187 
00188   bool wantsRegionChangeUpdate(ProgramStateRef St) const { return true; }
00189   
00190   /// check::RegionChanges
00191   /// Allows tracking regions which get invalidated.
00192   /// \param state The current program state.
00193   /// \param invalidated A set of all symbols potentially touched by the change.
00194   /// \param ExplicitRegions The regions explicitly requested for invalidation.
00195   ///   For example, in the case of a function call, these would be arguments.
00196   /// \param Regions The transitive closure of accessible regions,
00197   ///   i.e. all regions that may have been touched by this change.
00198   /// \param The call expression wrapper if the regions are invalidated by a
00199   ///   call, 0 otherwise.
00200   /// Note, in order to be notified, the checker should also implement 
00201   /// wantsRegionChangeUpdate callback.
00202   ProgramStateRef 
00203     checkRegionChanges(ProgramStateRef State,
00204                        const StoreManager::InvalidatedSymbols *,
00205                        ArrayRef<const MemRegion *> ExplicitRegions,
00206                        ArrayRef<const MemRegion *> Regions,
00207                        const CallOrObjCMessage *Call) const {
00208     return State;
00209   }
00210 
00211   /// check::Event<ImplicitNullDerefEvent>
00212   void checkEvent(ImplicitNullDerefEvent Event) const {}
00213 
00214   /// \brief Check every declaration in the AST.
00215   ///
00216   /// An AST traversal callback, which should only be used when the checker is
00217   /// not path sensitive. It will be called for every Declaration in the AST and
00218   /// can be specialized to only be called on subclasses of Decl, for example,
00219   /// FunctionDecl.
00220   ///
00221   /// check::ASTDecl<FunctionDecl>
00222   void checkASTDecl(const FunctionDecl *D,
00223                     AnalysisManager &Mgr,
00224                     BugReporter &BR) const {}
00225 
00226 };
00227 
00228 void CheckerDocumentation::checkPostStmt(const CallExpr *DS,
00229                                          CheckerContext &C) const {
00230   return;
00231 }
00232 
00233 } // end namespace