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 | // |
| 10 | // This file defines AnalysisContext, a class that manages the analysis context |
| 11 | // data for path sensitive analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H |
| 16 | #define LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H |
| 17 | |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "llvm/ADT/OwningPtr.h" |
| 20 | #include "llvm/ADT/FoldingSet.h" |
| 21 | #include "llvm/ADT/PointerUnion.h" |
| 22 | #include "llvm/ADT/DenseMap.h" |
| 23 | #include "llvm/Support/Allocator.h" |
| 24 | |
| 25 | namespace clang { |
| 26 | |
| 27 | class Decl; |
| 28 | class Stmt; |
| 29 | class CFG; |
| 30 | class CFGBlock; |
| 31 | class LiveVariables; |
| 32 | class ParentMap; |
| 33 | class ImplicitParamDecl; |
| 34 | class LocationContextManager; |
| 35 | class StackFrameContext; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 36 | |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 37 | namespace idx { class TranslationUnit; } |
| 38 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 39 | /// AnalysisContext contains the context data for the function or method under |
| 40 | /// analysis. |
| 41 | class AnalysisContext { |
| 42 | const Decl *D; |
| 43 | |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 44 | // TranslationUnit is NULL if we don't have multiple translation units. |
| 45 | const idx::TranslationUnit *TU; |
| 46 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 47 | // AnalysisContext owns the following data. |
| 48 | CFG *cfg; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 49 | bool builtCFG; |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 50 | LiveVariables *liveness; |
| 51 | ParentMap *PM; |
| 52 | llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars; |
| 53 | llvm::BumpPtrAllocator A; |
| 54 | bool AddEHEdges; |
| 55 | public: |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 56 | AnalysisContext(const Decl *d, const idx::TranslationUnit *tu, |
| 57 | bool addehedges = false) |
| 58 | : D(d), TU(tu), cfg(0), builtCFG(false), liveness(0), PM(0), |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 59 | ReferencedBlockVars(0), AddEHEdges(addehedges) {} |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 60 | |
| 61 | ~AnalysisContext(); |
| 62 | |
| 63 | ASTContext &getASTContext() { return D->getASTContext(); } |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 64 | const Decl *getDecl() const { return D; } |
| 65 | |
| 66 | const idx::TranslationUnit *getTranslationUnit() const { return TU; } |
| 67 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 68 | /// getAddEHEdges - Return true iff we are adding exceptional edges from |
| 69 | /// callExprs. If this is false, then try/catch statements and blocks |
| 70 | /// reachable from them can appear to be dead in the CFG, analysis passes must |
| 71 | /// cope with that. |
| 72 | bool getAddEHEdges() const { return AddEHEdges; } |
| 73 | Stmt *getBody(); |
| 74 | CFG *getCFG(); |
| 75 | ParentMap &getParentMap(); |
| 76 | LiveVariables *getLiveVariables(); |
| 77 | |
| 78 | typedef const VarDecl * const * referenced_decls_iterator; |
| 79 | |
| 80 | std::pair<referenced_decls_iterator, referenced_decls_iterator> |
| 81 | getReferencedBlockVars(const BlockDecl *BD); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 83 | /// Return the ImplicitParamDecl* associated with 'self' if this |
| 84 | /// AnalysisContext wraps an ObjCMethodDecl. Returns NULL otherwise. |
| 85 | const ImplicitParamDecl *getSelfDecl() const; |
| 86 | }; |
| 87 | |
| 88 | class AnalysisContextManager { |
| 89 | typedef llvm::DenseMap<const Decl*, AnalysisContext*> ContextMap; |
| 90 | ContextMap Contexts; |
| 91 | public: |
| 92 | ~AnalysisContextManager(); |
| 93 | |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 94 | AnalysisContext *getContext(const Decl *D,const idx::TranslationUnit *TU = 0); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 96 | // Discard all previously created AnalysisContexts. |
| 97 | void clear(); |
| 98 | }; |
| 99 | |
| 100 | class LocationContext : public llvm::FoldingSetNode { |
| 101 | public: |
| 102 | enum ContextKind { StackFrame, Scope, Block }; |
| 103 | |
| 104 | private: |
| 105 | ContextKind Kind; |
| 106 | AnalysisContext *Ctx; |
| 107 | const LocationContext *Parent; |
| 108 | |
| 109 | protected: |
| 110 | LocationContext(ContextKind k, AnalysisContext *ctx, |
| 111 | const LocationContext *parent) |
| 112 | : Kind(k), Ctx(ctx), Parent(parent) {} |
| 113 | |
| 114 | public: |
| 115 | virtual ~LocationContext(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 117 | ContextKind getKind() const { return Kind; } |
| 118 | |
| 119 | AnalysisContext *getAnalysisContext() const { return Ctx; } |
| 120 | |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 121 | const idx::TranslationUnit *getTranslationUnit() const { |
| 122 | return Ctx->getTranslationUnit(); |
| 123 | } |
| 124 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 125 | const LocationContext *getParent() const { return Parent; } |
| 126 | |
Zhongxing Xu | 8ddf7ce | 2010-02-17 08:45:06 +0000 | [diff] [blame] | 127 | bool isParentOf(const LocationContext *LC) const; |
| 128 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 129 | const Decl *getDecl() const { return getAnalysisContext()->getDecl(); } |
| 130 | |
| 131 | CFG *getCFG() const { return getAnalysisContext()->getCFG(); } |
| 132 | |
| 133 | LiveVariables *getLiveVariables() const { |
| 134 | return getAnalysisContext()->getLiveVariables(); |
| 135 | } |
| 136 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 137 | ParentMap &getParentMap() const { |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 138 | return getAnalysisContext()->getParentMap(); |
| 139 | } |
| 140 | |
| 141 | const ImplicitParamDecl *getSelfDecl() const { |
| 142 | return Ctx->getSelfDecl(); |
| 143 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 145 | const StackFrameContext *getCurrentStackFrame() const; |
| 146 | const StackFrameContext * |
| 147 | getStackFrameForDeclContext(const DeclContext *DC) const; |
| 148 | |
| 149 | virtual void Profile(llvm::FoldingSetNodeID &ID) = 0; |
| 150 | |
| 151 | static bool classof(const LocationContext*) { return true; } |
| 152 | |
| 153 | public: |
| 154 | static void ProfileCommon(llvm::FoldingSetNodeID &ID, |
| 155 | ContextKind ck, |
| 156 | AnalysisContext *ctx, |
| 157 | const LocationContext *parent, |
| 158 | const void* data); |
| 159 | }; |
| 160 | |
| 161 | class StackFrameContext : public LocationContext { |
| 162 | // The callsite where this stack frame is established. |
| 163 | const Stmt *CallSite; |
| 164 | |
| 165 | // The parent block of the callsite. |
| 166 | const CFGBlock *Block; |
| 167 | |
| 168 | // The index of the callsite in the CFGBlock. |
| 169 | unsigned Index; |
| 170 | |
| 171 | friend class LocationContextManager; |
| 172 | StackFrameContext(AnalysisContext *ctx, const LocationContext *parent, |
| 173 | const Stmt *s, const CFGBlock *blk, unsigned idx) |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 174 | : LocationContext(StackFrame, ctx, parent), CallSite(s), Block(blk), |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 175 | Index(idx) {} |
| 176 | |
| 177 | public: |
| 178 | ~StackFrameContext() {} |
| 179 | |
| 180 | const Stmt *getCallSite() const { return CallSite; } |
| 181 | |
| 182 | const CFGBlock *getCallSiteBlock() const { return Block; } |
| 183 | |
| 184 | unsigned getIndex() const { return Index; } |
| 185 | |
| 186 | void Profile(llvm::FoldingSetNodeID &ID); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 188 | static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx, |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 189 | const LocationContext *parent, const Stmt *s, |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 190 | const CFGBlock *blk, unsigned idx) { |
| 191 | ProfileCommon(ID, StackFrame, ctx, parent, s); |
| 192 | ID.AddPointer(blk); |
| 193 | ID.AddInteger(idx); |
| 194 | } |
| 195 | |
| 196 | static bool classof(const LocationContext* Ctx) { |
| 197 | return Ctx->getKind() == StackFrame; |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | class ScopeContext : public LocationContext { |
| 202 | const Stmt *Enter; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 204 | friend class LocationContextManager; |
| 205 | ScopeContext(AnalysisContext *ctx, const LocationContext *parent, |
| 206 | const Stmt *s) |
| 207 | : LocationContext(Scope, ctx, parent), Enter(s) {} |
| 208 | |
| 209 | public: |
| 210 | ~ScopeContext() {} |
| 211 | |
| 212 | void Profile(llvm::FoldingSetNodeID &ID); |
| 213 | |
| 214 | static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx, |
| 215 | const LocationContext *parent, const Stmt *s) { |
| 216 | ProfileCommon(ID, Scope, ctx, parent, s); |
| 217 | } |
| 218 | |
| 219 | static bool classof(const LocationContext* Ctx) { |
| 220 | return Ctx->getKind() == Scope; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | class BlockInvocationContext : public LocationContext { |
| 225 | // FIXME: Add back context-sensivity (we don't want libAnalysis to know |
| 226 | // about MemRegion). |
| 227 | const BlockDecl *BD; |
| 228 | |
| 229 | friend class LocationContextManager; |
| 230 | |
| 231 | BlockInvocationContext(AnalysisContext *ctx, const LocationContext *parent, |
| 232 | const BlockDecl *bd) |
| 233 | : LocationContext(Block, ctx, parent), BD(bd) {} |
| 234 | |
| 235 | public: |
| 236 | ~BlockInvocationContext() {} |
| 237 | |
| 238 | const BlockDecl *getBlockDecl() const { return BD; } |
| 239 | |
| 240 | void Profile(llvm::FoldingSetNodeID &ID); |
| 241 | |
| 242 | static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx, |
| 243 | const LocationContext *parent, const BlockDecl *bd) { |
| 244 | ProfileCommon(ID, Block, ctx, parent, bd); |
| 245 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 247 | static bool classof(const LocationContext* Ctx) { |
| 248 | return Ctx->getKind() == Block; |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | class LocationContextManager { |
| 253 | llvm::FoldingSet<LocationContext> Contexts; |
| 254 | public: |
| 255 | ~LocationContextManager(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 257 | const StackFrameContext *getStackFrame(AnalysisContext *ctx, |
| 258 | const LocationContext *parent, |
| 259 | const Stmt *s, const CFGBlock *blk, |
| 260 | unsigned idx); |
| 261 | |
| 262 | const ScopeContext *getScope(AnalysisContext *ctx, |
| 263 | const LocationContext *parent, |
| 264 | const Stmt *s); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 326be56 | 2010-01-25 05:19:37 +0000 | [diff] [blame] | 266 | /// Discard all previously created LocationContext objects. |
| 267 | void clear(); |
| 268 | private: |
| 269 | template <typename LOC, typename DATA> |
| 270 | const LOC *getLocationContext(AnalysisContext *ctx, |
| 271 | const LocationContext *parent, |
| 272 | const DATA *d); |
| 273 | }; |
| 274 | |
| 275 | } // end clang namespace |
| 276 | #endif |