Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 1 | //= CheckerDocumentation.cpp - Documentation checker ---------------*- C++ -*-// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This checker lists all the checker callbacks and provides documentation for |
| 11 | // checker writers. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ClangSACheckers.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/BugReporter/BugType.h" |
| 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace ento; |
| 24 | |
| 25 | // All checkers should be placed into anonymous namespace. |
| 26 | // We place the CheckerDocumentation inside ento namespace to make the |
| 27 | // it visible in doxygen. |
| 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<DeclStmt>, |
| 37 | check::PostStmt<CallExpr>, |
| 38 | check::PreObjCMessage, |
| 39 | check::PostObjCMessage, |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 40 | check::PreCall, |
| 41 | check::PostCall, |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 42 | check::BranchCondition, |
| 43 | check::Location, |
| 44 | check::Bind, |
| 45 | check::DeadSymbols, |
| 46 | check::EndPath, |
| 47 | check::EndAnalysis, |
| 48 | check::EndOfTranslationUnit, |
| 49 | eval::Call, |
| 50 | eval::Assume, |
| 51 | check::LiveSymbols, |
| 52 | check::RegionChanges, |
| 53 | check::Event<ImplicitNullDerefEvent>, |
| 54 | check::ASTDecl<FunctionDecl> > { |
| 55 | public: |
| 56 | |
| 57 | /// \brief 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<DeclStmt> |
| 68 | void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {} |
| 69 | |
| 70 | /// \brief Post-visit the Statement. |
| 71 | /// |
| 72 | /// The method will be called after the analyzer core processes the |
| 73 | /// statement. The notification is performed for every explored CFGElement, |
| 74 | /// which does not include the control flow statements such as IfStmt. The |
| 75 | /// callback can be specialized to be called with any subclass of Stmt. |
| 76 | /// |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 77 | /// check::PostStmt<CallExpr> |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 78 | void checkPostStmt(const CallExpr *DS, CheckerContext &C) const; |
| 79 | |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 80 | /// \brief Pre-visit the Objective C message. |
| 81 | /// |
| 82 | /// This will be called before the analyzer core processes the method call. |
| 83 | /// This is called for any action which produces an Objective-C message send, |
| 84 | /// including explicit message syntax and property access. See the subclasses |
| 85 | /// of ObjCMethodCall for more details. |
| 86 | /// |
| 87 | /// check::PreObjCMessage |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 88 | void checkPreObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {} |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 89 | |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 90 | /// \brief Post-visit the Objective C message. |
| 91 | /// \sa checkPreObjCMessage() |
| 92 | /// |
| 93 | /// check::PostObjCMessage |
Jordan Rose | de507ea | 2012-07-02 19:28:04 +0000 | [diff] [blame] | 94 | void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const {} |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 95 | |
Jordan Rose | 96479da | 2012-07-02 19:28:16 +0000 | [diff] [blame] | 96 | /// \brief Pre-visit an abstract "call" event. |
| 97 | /// |
| 98 | /// This is used for checkers that want to check arguments or attributed |
| 99 | /// behavior for functions and methods no matter how they are being invoked. |
| 100 | /// |
| 101 | /// Note that this includes ALL cross-body invocations, so if you want to |
| 102 | /// limit your checks to, say, function calls, you can either test for that |
| 103 | /// or fall back to the explicit callback (i.e. check::PreStmt). |
| 104 | /// |
| 105 | /// check::PreCall |
| 106 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const {} |
| 107 | |
| 108 | /// \brief Post-visit an abstract "call" event. |
| 109 | /// \sa checkPreObjCMessage() |
| 110 | /// |
| 111 | /// check::PostCall |
| 112 | void checkPostCall(const CallEvent &Call, CheckerContext &C) const {} |
| 113 | |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 114 | /// \brief Pre-visit of the condition statement of a branch (such as IfStmt). |
| 115 | void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const {} |
| 116 | |
| 117 | /// \brief Called on a load from and a store to a location. |
| 118 | /// |
| 119 | /// The method will be called each time a location (pointer) value is |
| 120 | /// accessed. |
| 121 | /// \param Loc The value of the location (pointer). |
| 122 | /// \param IsLoad The flag specifying if the location is a store or a load. |
| 123 | /// \param S The load is performed while processing the statement. |
| 124 | /// |
| 125 | /// check::Location |
| 126 | void checkLocation(SVal Loc, bool IsLoad, const Stmt *S, |
James Dennett | 81c16fc | 2012-06-15 07:41:35 +0000 | [diff] [blame] | 127 | CheckerContext &) const {} |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 128 | |
| 129 | /// \brief Called on binding of a value to a location. |
| 130 | /// |
| 131 | /// \param Loc The value of the location (pointer). |
| 132 | /// \param Val The value which will be stored at the location Loc. |
| 133 | /// \param S The bind is performed while processing the statement S. |
| 134 | /// |
| 135 | /// check::Bind |
James Dennett | 81c16fc | 2012-06-15 07:41:35 +0000 | [diff] [blame] | 136 | void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {} |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | /// \brief Called whenever a symbol becomes dead. |
| 140 | /// |
| 141 | /// This callback should be used by the checkers to aggressively clean |
| 142 | /// up/reduce the checker state, which is important for reducing the overall |
| 143 | /// memory usage. Specifically, if a checker keeps symbol specific information |
| 144 | /// in the sate, it can and should be dropped after the symbol becomes dead. |
| 145 | /// In addition, reporting a bug as soon as the checker becomes dead leads to |
| 146 | /// more precise diagnostics. (For example, one should report that a malloced |
| 147 | /// variable is not freed right after it goes out of scope.) |
| 148 | /// |
| 149 | /// \param SR The SymbolReaper object can be queried to determine which |
| 150 | /// symbols are dead. |
| 151 | /// |
| 152 | /// check::DeadSymbols |
| 153 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const {} |
| 154 | |
| 155 | /// \brief Called when an end of path is reached in the ExplodedGraph. |
| 156 | /// |
| 157 | /// This callback should be used to check if the allocated resources are freed. |
| 158 | /// |
| 159 | /// check::EndPath |
| 160 | void checkEndPath(CheckerContext &Ctx) const {} |
| 161 | |
| 162 | /// \brief Called after all the paths in the ExplodedGraph reach end of path |
| 163 | /// - the symbolic execution graph is fully explored. |
| 164 | /// |
| 165 | /// This callback should be used in cases when a checker needs to have a |
| 166 | /// global view of the information generated on all paths. For example, to |
| 167 | /// compare execution summary/result several paths. |
| 168 | /// See IdempotentOperationChecker for a usage example. |
| 169 | /// |
| 170 | /// check::EndAnalysis |
| 171 | void checkEndAnalysis(ExplodedGraph &G, |
| 172 | BugReporter &BR, |
| 173 | ExprEngine &Eng) const {} |
| 174 | |
| 175 | /// \brief Called after analysis of a TranslationUnit is complete. |
| 176 | /// |
| 177 | /// check::EndOfTranslationUnit |
Anton Yartsev | e8018f2 | 2012-03-23 02:43:24 +0000 | [diff] [blame] | 178 | void checkEndOfTranslationUnit(const TranslationUnitDecl *TU, |
| 179 | AnalysisManager &Mgr, |
| 180 | BugReporter &BR) const {} |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | /// \brief Evaluates function call. |
| 184 | /// |
| 185 | /// The analysis core threats all function calls in the same way. However, some |
| 186 | /// functions have special meaning, which should be reflected in the program |
| 187 | /// state. This callback allows a checker to provide domain specific knowledge |
| 188 | /// about the particular functions it knows about. |
| 189 | /// |
| 190 | /// \returns true if the call has been successfully evaluated |
| 191 | /// and false otherwise. Note, that only one checker can evaluate a call. If |
| 192 | /// more then one checker claim that they can evaluate the same call the |
| 193 | /// first one wins. |
| 194 | /// |
| 195 | /// eval::Call |
| 196 | bool evalCall(const CallExpr *CE, CheckerContext &C) const { return true; } |
| 197 | |
| 198 | /// \brief Handles assumptions on symbolic values. |
| 199 | /// |
| 200 | /// This method is called when a symbolic expression is assumed to be true or |
| 201 | /// false. For example, the assumptions are performed when evaluating a |
| 202 | /// condition at a branch. The callback allows checkers track the assumptions |
| 203 | /// performed on the symbols of interest and change the state accordingly. |
| 204 | /// |
| 205 | /// eval::Assume |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 206 | ProgramStateRef evalAssume(ProgramStateRef State, |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 207 | SVal Cond, |
| 208 | bool Assumption) const { return State; } |
| 209 | |
| 210 | /// Allows modifying SymbolReaper object. For example, checkers can explicitly |
| 211 | /// register symbols of interest as live. These symbols will not be marked |
| 212 | /// dead and removed. |
| 213 | /// |
| 214 | /// check::LiveSymbols |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 215 | void checkLiveSymbols(ProgramStateRef State, SymbolReaper &SR) const {} |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 216 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 218 | bool wantsRegionChangeUpdate(ProgramStateRef St) const { return true; } |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 219 | |
James Dennett | 81c16fc | 2012-06-15 07:41:35 +0000 | [diff] [blame] | 220 | /// \brief Allows tracking regions which get invalidated. |
| 221 | /// |
| 222 | /// \param State The current program state. |
| 223 | /// \param Invalidated A set of all symbols potentially touched by the change. |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 224 | /// \param ExplicitRegions The regions explicitly requested for invalidation. |
| 225 | /// For example, in the case of a function call, these would be arguments. |
| 226 | /// \param Regions The transitive closure of accessible regions, |
| 227 | /// i.e. all regions that may have been touched by this change. |
James Dennett | 81c16fc | 2012-06-15 07:41:35 +0000 | [diff] [blame] | 228 | /// \param Call The call expression wrapper if the regions are invalidated |
| 229 | /// by a call, 0 otherwise. |
| 230 | /// Note, in order to be notified, the checker should also implement the |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 231 | /// wantsRegionChangeUpdate callback. |
James Dennett | 81c16fc | 2012-06-15 07:41:35 +0000 | [diff] [blame] | 232 | /// |
| 233 | /// check::RegionChanges |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 234 | ProgramStateRef |
| 235 | checkRegionChanges(ProgramStateRef State, |
James Dennett | 81c16fc | 2012-06-15 07:41:35 +0000 | [diff] [blame] | 236 | const StoreManager::InvalidatedSymbols *Invalidated, |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 237 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 238 | ArrayRef<const MemRegion *> Regions, |
Jordan Rose | 740d490 | 2012-07-02 19:27:35 +0000 | [diff] [blame] | 239 | const CallEvent *Call) const { |
Anna Zaks | d699ade | 2011-11-30 17:12:52 +0000 | [diff] [blame] | 240 | return State; |
| 241 | } |
| 242 | |
| 243 | /// check::Event<ImplicitNullDerefEvent> |
| 244 | void checkEvent(ImplicitNullDerefEvent Event) const {} |
| 245 | |
| 246 | /// \brief Check every declaration in the AST. |
| 247 | /// |
| 248 | /// An AST traversal callback, which should only be used when the checker is |
| 249 | /// not path sensitive. It will be called for every Declaration in the AST and |
| 250 | /// can be specialized to only be called on subclasses of Decl, for example, |
| 251 | /// FunctionDecl. |
| 252 | /// |
| 253 | /// check::ASTDecl<FunctionDecl> |
| 254 | void checkASTDecl(const FunctionDecl *D, |
| 255 | AnalysisManager &Mgr, |
| 256 | BugReporter &BR) const {} |
| 257 | |
| 258 | }; |
| 259 | |
| 260 | void CheckerDocumentation::checkPostStmt(const CallExpr *DS, |
| 261 | CheckerContext &C) const { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | } // end namespace |