AnalysisContext.h revision 69b81941aa4211ba6b8eaa89093f9e45aff81392
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
25namespace clang {
26
27class Decl;
28class Stmt;
29class CFG;
30class CFGBlock;
31class LiveVariables;
32class ParentMap;
33class ImplicitParamDecl;
34class LocationContextManager;
35class StackFrameContext;
36
37namespace idx { class TranslationUnit; }
38
39/// AnalysisContext contains the context data for the function or method under
40/// analysis.
41class AnalysisContext {
42  const Decl *D;
43
44  // TranslationUnit is NULL if we don't have multiple translation units.
45  const idx::TranslationUnit *TU;
46
47  // AnalysisContext owns the following data.
48  CFG *cfg;
49  bool builtCFG;
50  LiveVariables *liveness;
51  ParentMap *PM;
52  llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars;
53  llvm::BumpPtrAllocator A;
54  bool AddEHEdges;
55public:
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),
59      ReferencedBlockVars(0), AddEHEdges(addehedges) {}
60
61  ~AnalysisContext();
62
63  ASTContext &getASTContext() { return D->getASTContext(); }
64  const Decl *getDecl() const { return D; }
65
66  const idx::TranslationUnit *getTranslationUnit() const { return TU; }
67
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);
82
83  /// Return the ImplicitParamDecl* associated with 'self' if this
84  /// AnalysisContext wraps an ObjCMethodDecl.  Returns NULL otherwise.
85  const ImplicitParamDecl *getSelfDecl() const;
86};
87
88class AnalysisContextManager {
89  typedef llvm::DenseMap<const Decl*, AnalysisContext*> ContextMap;
90  ContextMap Contexts;
91public:
92  ~AnalysisContextManager();
93
94  AnalysisContext *getContext(const Decl *D,const idx::TranslationUnit *TU = 0);
95
96  // Discard all previously created AnalysisContexts.
97  void clear();
98};
99
100class LocationContext : public llvm::FoldingSetNode {
101public:
102  enum ContextKind { StackFrame, Scope, Block };
103
104private:
105  ContextKind Kind;
106  AnalysisContext *Ctx;
107  const LocationContext *Parent;
108
109protected:
110  LocationContext(ContextKind k, AnalysisContext *ctx,
111                  const LocationContext *parent)
112    : Kind(k), Ctx(ctx), Parent(parent) {}
113
114public:
115  virtual ~LocationContext();
116
117  ContextKind getKind() const { return Kind; }
118
119  AnalysisContext *getAnalysisContext() const { return Ctx; }
120
121  const idx::TranslationUnit *getTranslationUnit() const {
122    return Ctx->getTranslationUnit();
123  }
124
125  const LocationContext *getParent() const { return Parent; }
126
127  bool isParentOf(const LocationContext *LC) const;
128
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
137  ParentMap &getParentMap() const {
138    return getAnalysisContext()->getParentMap();
139  }
140
141  const ImplicitParamDecl *getSelfDecl() const {
142    return Ctx->getSelfDecl();
143  }
144
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
153public:
154  static void ProfileCommon(llvm::FoldingSetNodeID &ID,
155                            ContextKind ck,
156                            AnalysisContext *ctx,
157                            const LocationContext *parent,
158                            const void* data);
159};
160
161class 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)
174    : LocationContext(StackFrame, ctx, parent), CallSite(s), Block(blk),
175      Index(idx) {}
176
177public:
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);
187
188  static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
189                      const LocationContext *parent, const Stmt *s,
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
201class ScopeContext : public LocationContext {
202  const Stmt *Enter;
203
204  friend class LocationContextManager;
205  ScopeContext(AnalysisContext *ctx, const LocationContext *parent,
206               const Stmt *s)
207    : LocationContext(Scope, ctx, parent), Enter(s) {}
208
209public:
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
224class 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
235public:
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  }
246
247  static bool classof(const LocationContext* Ctx) {
248    return Ctx->getKind() == Block;
249  }
250};
251
252class LocationContextManager {
253  llvm::FoldingSet<LocationContext> Contexts;
254public:
255  ~LocationContextManager();
256
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);
265
266  /// Discard all previously created LocationContext objects.
267  void clear();
268private:
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
277