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