Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 1 | //=== AnalysisContext.h - Analysis context for Path Sens analysis --*- 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 | // |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 10 | // This file defines AnalysisDeclContext, a class that manages the analysis |
| 11 | // context data for path sensitive analysis. |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H |
| 16 | #define LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H |
| 17 | |
| 18 | #include "clang/AST/Decl.h" |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/CFG.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 20 | #include "clang/Analysis/CodeInjector.h" |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 30a2e16 | 2012-12-04 09:18:49 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/FoldingSet.h" |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Allocator.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 24 | #include <memory> |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 25 | |
| 26 | namespace clang { |
| 27 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 28 | class Stmt; |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 29 | class CFGReverseBlockReachabilityAnalysis; |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 30 | class CFGStmtMap; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 31 | class LiveVariables; |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 32 | class ManagedAnalysis; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 33 | class ParentMap; |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 34 | class PseudoConstantAnalysis; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 35 | class LocationContextManager; |
| 36 | class StackFrameContext; |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 37 | class BlockInvocationContext; |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 38 | class AnalysisDeclContextManager; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 39 | class LocationContext; |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 40 | |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 41 | namespace idx { class TranslationUnit; } |
| 42 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 43 | /// The base class of a hierarchy of objects representing analyses tied |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 44 | /// to AnalysisDeclContext. |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 45 | class ManagedAnalysis { |
| 46 | protected: |
| 47 | ManagedAnalysis() {} |
| 48 | public: |
| 49 | virtual ~ManagedAnalysis(); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 51 | // Subclasses need to implement: |
| 52 | // |
| 53 | // static const void *getTag(); |
| 54 | // |
| 55 | // Which returns a fixed pointer address to distinguish classes of |
| 56 | // analysis objects. They also need to implement: |
| 57 | // |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 58 | // static [Derived*] create(AnalysisDeclContext &Ctx); |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 59 | // |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 60 | // which creates the analysis object given an AnalysisDeclContext. |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 61 | }; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | /// AnalysisDeclContext contains the context data for the function or method |
| 65 | /// under analysis. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 66 | class AnalysisDeclContext { |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 67 | /// Backpoint to the AnalysisManager object that created this |
| 68 | /// AnalysisDeclContext. This may be null. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 69 | AnalysisDeclContextManager *Manager; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | cca300a | 2012-09-21 00:09:03 +0000 | [diff] [blame] | 71 | const Decl * const D; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 72 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 73 | std::unique_ptr<CFG> cfg, completeCFG; |
| 74 | std::unique_ptr<CFGStmtMap> cfgStmtMap; |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 75 | |
| 76 | CFG::BuildOptions cfgBuildOptions; |
| 77 | CFG::BuildOptions::ForcedBlkExprs *forcedBlkExprs; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 79 | bool builtCFG, builtCompleteCFG; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 80 | std::unique_ptr<ParentMap> PM; |
| 81 | std::unique_ptr<PseudoConstantAnalysis> PCA; |
| 82 | std::unique_ptr<CFGReverseBlockReachabilityAnalysis> CFA; |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 84 | llvm::BumpPtrAllocator A; |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 86 | llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars; |
| 87 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 88 | void *ManagedAnalyses; |
| 89 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 90 | public: |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 91 | AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
Jordy Rose | d200187 | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 92 | const Decl *D); |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 94 | AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 95 | const Decl *D, |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 96 | const CFG::BuildOptions &BuildOptions); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 98 | ~AnalysisDeclContext(); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 445895a | 2012-09-21 00:09:05 +0000 | [diff] [blame] | 100 | ASTContext &getASTContext() const { return D->getASTContext(); } |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 101 | const Decl *getDecl() const { return D; } |
| 102 | |
Ted Kremenek | ddc0c48 | 2012-09-21 06:13:13 +0000 | [diff] [blame] | 103 | /// Return the AnalysisDeclContextManager (if any) that created |
| 104 | /// this AnalysisDeclContext. |
| 105 | AnalysisDeclContextManager *getManager() const { |
| 106 | return Manager; |
| 107 | } |
| 108 | |
Ted Kremenek | 74fb1a4 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 109 | /// Return the build options used to construct the CFG. |
| 110 | CFG::BuildOptions &getCFGBuildOptions() { |
| 111 | return cfgBuildOptions; |
| 112 | } |
| 113 | |
| 114 | const CFG::BuildOptions &getCFGBuildOptions() const { |
| 115 | return cfgBuildOptions; |
| 116 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 117 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 118 | /// getAddEHEdges - Return true iff we are adding exceptional edges from |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 119 | /// callExprs. If this is false, then try/catch statements and blocks |
| 120 | /// reachable from them can appear to be dead in the CFG, analysis passes must |
| 121 | /// cope with that. |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 122 | bool getAddEHEdges() const { return cfgBuildOptions.AddEHEdges; } |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 123 | bool getUseUnoptimizedCFG() const { |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 124 | return !cfgBuildOptions.PruneTriviallyFalseEdges; |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 125 | } |
| 126 | bool getAddImplicitDtors() const { return cfgBuildOptions.AddImplicitDtors; } |
| 127 | bool getAddInitializers() const { return cfgBuildOptions.AddInitializers; } |
Ted Kremenek | 9b823e8 | 2010-08-03 00:09:51 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 129 | void registerForcedBlockExpression(const Stmt *stmt); |
| 130 | const CFGBlock *getBlockForRegisteredExpression(const Stmt *stmt); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 131 | |
Anna Zaks | 453cb85 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 132 | /// \brief Get the body of the Declaration. |
Anna Zaks | a2d7e65 | 2011-09-19 23:17:48 +0000 | [diff] [blame] | 133 | Stmt *getBody() const; |
Anna Zaks | 453cb85 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 134 | |
| 135 | /// \brief Get the body of the Declaration. |
Anna Zaks | 087f407 | 2013-02-05 19:52:24 +0000 | [diff] [blame] | 136 | /// \param[out] IsAutosynthesized Specifies if the body is auto-generated |
| 137 | /// by the BodyFarm. |
Anna Zaks | 453cb85 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 138 | Stmt *getBody(bool &IsAutosynthesized) const; |
| 139 | |
| 140 | /// \brief Checks if the body of the Decl is generated by the BodyFarm. |
| 141 | /// |
| 142 | /// Note, the lookup is not free. We are going to call getBody behind |
Anna Zaks | 087f407 | 2013-02-05 19:52:24 +0000 | [diff] [blame] | 143 | /// the scenes. |
Anna Zaks | 453cb85 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 144 | /// \sa getBody |
| 145 | bool isBodyAutosynthesized() const; |
| 146 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 147 | /// \brief Checks if the body of the Decl is generated by the BodyFarm from a |
| 148 | /// model file. |
| 149 | /// |
| 150 | /// Note, the lookup is not free. We are going to call getBody behind |
| 151 | /// the scenes. |
| 152 | /// \sa getBody |
| 153 | bool isBodyAutosynthesizedFromModelFile() const; |
| 154 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 155 | CFG *getCFG(); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 157 | CFGStmtMap *getCFGStmtMap(); |
Anders Carlsson | 04eeba4 | 2011-01-16 22:05:23 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 159 | CFGReverseBlockReachabilityAnalysis *getCFGReachablityAnalysis(); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 161 | /// Return a version of the CFG without any edges pruned. |
| 162 | CFG *getUnoptimizedCFG(); |
| 163 | |
Ted Kremenek | 682060c | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 164 | void dumpCFG(bool ShowColors); |
Anders Carlsson | 04eeba4 | 2011-01-16 22:05:23 +0000 | [diff] [blame] | 165 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 166 | /// \brief Returns true if we have built a CFG for this analysis context. |
| 167 | /// Note that this doesn't correspond to whether or not a valid CFG exists, it |
| 168 | /// corresponds to whether we *attempted* to build one. |
| 169 | bool isCFGBuilt() const { return builtCFG; } |
| 170 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 171 | ParentMap &getParentMap(); |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 172 | PseudoConstantAnalysis *getPseudoConstantAnalysis(); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 173 | |
| 174 | typedef const VarDecl * const * referenced_decls_iterator; |
| 175 | |
| 176 | std::pair<referenced_decls_iterator, referenced_decls_iterator> |
| 177 | getReferencedBlockVars(const BlockDecl *BD); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 179 | /// Return the ImplicitParamDecl* associated with 'self' if this |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 180 | /// AnalysisDeclContext wraps an ObjCMethodDecl. Returns NULL otherwise. |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 181 | const ImplicitParamDecl *getSelfDecl() const; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 183 | const StackFrameContext *getStackFrame(LocationContext const *Parent, |
| 184 | const Stmt *S, |
| 185 | const CFGBlock *Blk, |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 186 | unsigned Idx); |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 187 | |
| 188 | const BlockInvocationContext * |
| 189 | getBlockInvocationContext(const LocationContext *parent, |
| 190 | const BlockDecl *BD, |
| 191 | const void *ContextData); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 193 | /// Return the specified analysis object, lazily running the analysis if |
| 194 | /// necessary. Return NULL if the analysis could not run. |
| 195 | template <typename T> |
| 196 | T *getAnalysis() { |
| 197 | const void *tag = T::getTag(); |
| 198 | ManagedAnalysis *&data = getAnalysisImpl(tag); |
| 199 | if (!data) { |
| 200 | data = T::create(*this); |
| 201 | } |
| 202 | return static_cast<T*>(data); |
| 203 | } |
| 204 | private: |
| 205 | ManagedAnalysis *&getAnalysisImpl(const void* tag); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 207 | LocationContextManager &getLocationContextManager(); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | class LocationContext : public llvm::FoldingSetNode { |
| 211 | public: |
| 212 | enum ContextKind { StackFrame, Scope, Block }; |
| 213 | |
| 214 | private: |
| 215 | ContextKind Kind; |
Zhongxing Xu | a02d893 | 2010-07-20 02:14:22 +0000 | [diff] [blame] | 216 | |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 217 | // AnalysisDeclContext can't be const since some methods may modify its |
| 218 | // member. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 219 | AnalysisDeclContext *Ctx; |
Zhongxing Xu | a02d893 | 2010-07-20 02:14:22 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 221 | const LocationContext *Parent; |
| 222 | |
| 223 | protected: |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 224 | LocationContext(ContextKind k, AnalysisDeclContext *ctx, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 225 | const LocationContext *parent) |
| 226 | : Kind(k), Ctx(ctx), Parent(parent) {} |
| 227 | |
| 228 | public: |
| 229 | virtual ~LocationContext(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 231 | ContextKind getKind() const { return Kind; } |
| 232 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 233 | AnalysisDeclContext *getAnalysisDeclContext() const { return Ctx; } |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 234 | |
| 235 | const LocationContext *getParent() const { return Parent; } |
| 236 | |
Zhongxing Xu | 8ddf7ce | 2010-02-17 08:45:06 +0000 | [diff] [blame] | 237 | bool isParentOf(const LocationContext *LC) const; |
| 238 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 239 | const Decl *getDecl() const { return getAnalysisDeclContext()->getDecl(); } |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 241 | CFG *getCFG() const { return getAnalysisDeclContext()->getCFG(); } |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 243 | template <typename T> |
| 244 | T *getAnalysis() const { |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 245 | return getAnalysisDeclContext()->getAnalysis<T>(); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 248 | ParentMap &getParentMap() const { |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 249 | return getAnalysisDeclContext()->getParentMap(); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | const ImplicitParamDecl *getSelfDecl() const { |
| 253 | return Ctx->getSelfDecl(); |
| 254 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 256 | const StackFrameContext *getCurrentStackFrame() const; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 257 | |
Anna Zaks | fadcd5d | 2012-11-03 02:54:16 +0000 | [diff] [blame] | 258 | /// Return true if the current LocationContext has no caller context. |
| 259 | virtual bool inTopFrame() const; |
| 260 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 261 | virtual void Profile(llvm::FoldingSetNodeID &ID) = 0; |
| 262 | |
Jordan Rose | ac7cc2d | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 263 | void dumpStack(raw_ostream &OS, StringRef Indent = "") const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 264 | void dumpStack() const; |
Jordan Rose | 75f8bd0 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 266 | public: |
| 267 | static void ProfileCommon(llvm::FoldingSetNodeID &ID, |
| 268 | ContextKind ck, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 269 | AnalysisDeclContext *ctx, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 270 | const LocationContext *parent, |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 271 | const void *data); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | class StackFrameContext : public LocationContext { |
Zhongxing Xu | 26c9cb5 | 2011-01-10 11:28:29 +0000 | [diff] [blame] | 275 | // The callsite where this stack frame is established. |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 276 | const Stmt *CallSite; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 277 | |
| 278 | // The parent block of the callsite. |
| 279 | const CFGBlock *Block; |
| 280 | |
| 281 | // The index of the callsite in the CFGBlock. |
| 282 | unsigned Index; |
| 283 | |
| 284 | friend class LocationContextManager; |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 285 | StackFrameContext(AnalysisDeclContext *ctx, const LocationContext *parent, |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 286 | const Stmt *s, const CFGBlock *blk, |
Zhongxing Xu | d706434 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 287 | unsigned idx) |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 288 | : LocationContext(StackFrame, ctx, parent), CallSite(s), |
Zhongxing Xu | d706434 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 289 | Block(blk), Index(idx) {} |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 290 | |
| 291 | public: |
| 292 | ~StackFrameContext() {} |
| 293 | |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 294 | const Stmt *getCallSite() const { return CallSite; } |
Zhongxing Xu | d706434 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 296 | const CFGBlock *getCallSiteBlock() const { return Block; } |
| 297 | |
Anna Zaks | fadcd5d | 2012-11-03 02:54:16 +0000 | [diff] [blame] | 298 | /// Return true if the current LocationContext has no caller context. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 299 | bool inTopFrame() const override { return getParent() == nullptr; } |
Anna Zaks | fadcd5d | 2012-11-03 02:54:16 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 301 | unsigned getIndex() const { return Index; } |
| 302 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 303 | void Profile(llvm::FoldingSetNodeID &ID) override; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 305 | static void Profile(llvm::FoldingSetNodeID &ID, AnalysisDeclContext *ctx, |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 306 | const LocationContext *parent, const Stmt *s, |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 307 | const CFGBlock *blk, unsigned idx) { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 308 | ProfileCommon(ID, StackFrame, ctx, parent, s); |
| 309 | ID.AddPointer(blk); |
| 310 | ID.AddInteger(idx); |
| 311 | } |
| 312 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 313 | static bool classof(const LocationContext *Ctx) { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 314 | return Ctx->getKind() == StackFrame; |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | class ScopeContext : public LocationContext { |
| 319 | const Stmt *Enter; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 321 | friend class LocationContextManager; |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 322 | ScopeContext(AnalysisDeclContext *ctx, const LocationContext *parent, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 323 | const Stmt *s) |
| 324 | : LocationContext(Scope, ctx, parent), Enter(s) {} |
| 325 | |
| 326 | public: |
| 327 | ~ScopeContext() {} |
| 328 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 329 | void Profile(llvm::FoldingSetNodeID &ID) override; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 331 | static void Profile(llvm::FoldingSetNodeID &ID, AnalysisDeclContext *ctx, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 332 | const LocationContext *parent, const Stmt *s) { |
| 333 | ProfileCommon(ID, Scope, ctx, parent, s); |
| 334 | } |
| 335 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 336 | static bool classof(const LocationContext *Ctx) { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 337 | return Ctx->getKind() == Scope; |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | class BlockInvocationContext : public LocationContext { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 342 | const BlockDecl *BD; |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 343 | |
| 344 | // FIXME: Come up with a more type-safe way to model context-sensitivity. |
| 345 | const void *ContextData; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 346 | |
| 347 | friend class LocationContextManager; |
| 348 | |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 349 | BlockInvocationContext(AnalysisDeclContext *ctx, |
| 350 | const LocationContext *parent, |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 351 | const BlockDecl *bd, const void *contextData) |
| 352 | : LocationContext(Block, ctx, parent), BD(bd), ContextData(contextData) {} |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 353 | |
| 354 | public: |
| 355 | ~BlockInvocationContext() {} |
| 356 | |
| 357 | const BlockDecl *getBlockDecl() const { return BD; } |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 358 | |
| 359 | const void *getContextData() const { return ContextData; } |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 360 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 361 | void Profile(llvm::FoldingSetNodeID &ID) override; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 363 | static void Profile(llvm::FoldingSetNodeID &ID, AnalysisDeclContext *ctx, |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 364 | const LocationContext *parent, const BlockDecl *bd, |
| 365 | const void *contextData) { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 366 | ProfileCommon(ID, Block, ctx, parent, bd); |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 367 | ID.AddPointer(contextData); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 368 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 370 | static bool classof(const LocationContext *Ctx) { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 371 | return Ctx->getKind() == Block; |
| 372 | } |
| 373 | }; |
| 374 | |
| 375 | class LocationContextManager { |
| 376 | llvm::FoldingSet<LocationContext> Contexts; |
| 377 | public: |
| 378 | ~LocationContextManager(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 380 | const StackFrameContext *getStackFrame(AnalysisDeclContext *ctx, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 381 | const LocationContext *parent, |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 382 | const Stmt *s, |
Zhongxing Xu | d706434 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 383 | const CFGBlock *blk, unsigned idx); |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 384 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 385 | const ScopeContext *getScope(AnalysisDeclContext *ctx, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 386 | const LocationContext *parent, |
| 387 | const Stmt *s); |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 388 | |
| 389 | const BlockInvocationContext * |
| 390 | getBlockInvocationContext(AnalysisDeclContext *ctx, |
| 391 | const LocationContext *parent, |
| 392 | const BlockDecl *BD, |
| 393 | const void *ContextData); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 395 | /// Discard all previously created LocationContext objects. |
| 396 | void clear(); |
| 397 | private: |
| 398 | template <typename LOC, typename DATA> |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 399 | const LOC *getLocationContext(AnalysisDeclContext *ctx, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 400 | const LocationContext *parent, |
| 401 | const DATA *d); |
| 402 | }; |
| 403 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 404 | class AnalysisDeclContextManager { |
| 405 | typedef llvm::DenseMap<const Decl*, AnalysisDeclContext*> ContextMap; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 406 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 407 | ContextMap Contexts; |
| 408 | LocationContextManager LocContexts; |
| 409 | CFG::BuildOptions cfgBuildOptions; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 410 | |
| 411 | /// Pointer to an interface that can provide function bodies for |
| 412 | /// declarations from external source. |
| 413 | std::unique_ptr<CodeInjector> Injector; |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 414 | |
| 415 | /// Flag to indicate whether or not bodies should be synthesized |
| 416 | /// for well-known functions. |
| 417 | bool SynthesizeBodies; |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 419 | public: |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 420 | AnalysisDeclContextManager(bool useUnoptimizedCFG = false, |
Jordan Rose | 5a1ffe9 | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 421 | bool addImplicitDtors = false, |
| 422 | bool addInitializers = false, |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 423 | bool addTemporaryDtors = false, |
Ted Kremenek | 02a88c3 | 2013-03-29 00:09:22 +0000 | [diff] [blame] | 424 | bool synthesizeBodies = false, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 425 | bool addStaticInitBranches = false, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 426 | bool addCXXNewAllocator = true, |
| 427 | CodeInjector* injector = nullptr); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 428 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 429 | ~AnalysisDeclContextManager(); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 430 | |
Jordy Rose | d200187 | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 431 | AnalysisDeclContext *getContext(const Decl *D); |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 432 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 433 | bool getUseUnoptimizedCFG() const { |
| 434 | return !cfgBuildOptions.PruneTriviallyFalseEdges; |
| 435 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 437 | CFG::BuildOptions &getCFGBuildOptions() { |
| 438 | return cfgBuildOptions; |
| 439 | } |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 440 | |
| 441 | /// Return true if faux bodies should be synthesized for well-known |
| 442 | /// functions. |
| 443 | bool synthesizeBodies() const { return SynthesizeBodies; } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 445 | const StackFrameContext *getStackFrame(AnalysisDeclContext *Ctx, |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 446 | LocationContext const *Parent, |
| 447 | const Stmt *S, |
| 448 | const CFGBlock *Blk, |
| 449 | unsigned Idx) { |
| 450 | return LocContexts.getStackFrame(Ctx, Parent, S, Blk, Idx); |
| 451 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 453 | // Get the top level stack frame. |
Jordy Rose | d200187 | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 454 | const StackFrameContext *getStackFrame(const Decl *D) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 455 | return LocContexts.getStackFrame(getContext(D), nullptr, nullptr, nullptr, |
| 456 | 0); |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 457 | } |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 459 | // Get a stack frame with parent. |
David Blaikie | ba243b5 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 460 | StackFrameContext const *getStackFrame(const Decl *D, |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 461 | LocationContext const *Parent, |
| 462 | const Stmt *S, |
| 463 | const CFGBlock *Blk, |
| 464 | unsigned Idx) { |
| 465 | return LocContexts.getStackFrame(getContext(D), Parent, S, Blk, Idx); |
| 466 | } |
| 467 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 468 | /// Discard all previously created AnalysisDeclContexts. |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 469 | void clear(); |
| 470 | |
| 471 | private: |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 472 | friend class AnalysisDeclContext; |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 473 | |
| 474 | LocationContextManager &getLocationContextManager() { |
| 475 | return LocContexts; |
| 476 | } |
| 477 | }; |
| 478 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 479 | } // end clang namespace |
| 480 | #endif |