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