AnalysisContext.h revision b8ad5ee345fa1fdd1fa9253f2d01f69becc88a04
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 "clang/AST/Expr.h"
20#include "clang/Analysis/CFG.h"
21#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/Support/Allocator.h"
26
27namespace clang {
28
29class Decl;
30class Stmt;
31class CFGReachabilityAnalysis;
32class CFGStmtMap;
33class LiveVariables;
34class ParentMap;
35class PseudoConstantAnalysis;
36class ImplicitParamDecl;
37class LocationContextManager;
38class StackFrameContext;
39
40namespace idx { class TranslationUnit; }
41
42/// AnalysisContext contains the context data for the function or method under
43/// analysis.
44class AnalysisContext {
45  const Decl *D;
46
47  // TranslationUnit is NULL if we don't have multiple translation units.
48  idx::TranslationUnit *TU;
49
50  llvm::OwningPtr<CFG> cfg, completeCFG;
51  llvm::OwningPtr<CFGStmtMap> cfgStmtMap;
52
53  CFG::BuildOptions cfgBuildOptions;
54  CFG::BuildOptions::ForcedBlkExprs *forcedBlkExprs;
55
56  bool builtCFG, builtCompleteCFG;
57  const bool useUnoptimizedCFG;
58
59  llvm::OwningPtr<LiveVariables> liveness;
60  llvm::OwningPtr<LiveVariables> relaxedLiveness;
61  llvm::OwningPtr<ParentMap> PM;
62  llvm::OwningPtr<PseudoConstantAnalysis> PCA;
63  llvm::OwningPtr<CFGReachabilityAnalysis> CFA;
64
65  llvm::BumpPtrAllocator A;
66
67  // FIXME: remove.
68  llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars;
69
70public:
71  AnalysisContext(const Decl *d, idx::TranslationUnit *tu,
72                  bool useUnoptimizedCFG = false,
73                  bool addehedges = false,
74                  bool addImplicitDtors = false,
75                  bool addInitializers = false);
76
77  ~AnalysisContext();
78
79  ASTContext &getASTContext() { return D->getASTContext(); }
80  const Decl *getDecl() const { return D; }
81
82  idx::TranslationUnit *getTranslationUnit() const { return TU; }
83
84  /// getAddEHEdges - Return true iff we are adding exceptional edges from
85  /// callExprs.  If this is false, then try/catch statements and blocks
86  /// reachable from them can appear to be dead in the CFG, analysis passes must
87  /// cope with that.
88  bool getAddEHEdges() const { return cfgBuildOptions.AddEHEdges; }
89  bool getUseUnoptimizedCFG() const {
90      return cfgBuildOptions.PruneTriviallyFalseEdges;
91  }
92  bool getAddImplicitDtors() const { return cfgBuildOptions.AddImplicitDtors; }
93  bool getAddInitializers() const { return cfgBuildOptions.AddInitializers; }
94
95  Stmt *getBody();
96  CFG *getCFG();
97
98  CFGStmtMap *getCFGStmtMap();
99
100  CFGReachabilityAnalysis *getCFGReachablityAnalysis();
101
102  /// Return a version of the CFG without any edges pruned.
103  CFG *getUnoptimizedCFG();
104
105  void dumpCFG();
106
107  ParentMap &getParentMap();
108  PseudoConstantAnalysis *getPseudoConstantAnalysis();
109  LiveVariables *getLiveVariables();
110  LiveVariables *getRelaxedLiveVariables();
111
112  typedef const VarDecl * const * referenced_decls_iterator;
113
114  std::pair<referenced_decls_iterator, referenced_decls_iterator>
115    getReferencedBlockVars(const BlockDecl *BD);
116
117  /// Return the ImplicitParamDecl* associated with 'self' if this
118  /// AnalysisContext wraps an ObjCMethodDecl.  Returns NULL otherwise.
119  const ImplicitParamDecl *getSelfDecl() const;
120};
121
122class AnalysisContextManager {
123  typedef llvm::DenseMap<const Decl*, AnalysisContext*> ContextMap;
124  ContextMap Contexts;
125  bool UseUnoptimizedCFG;
126  bool AddImplicitDtors;
127  bool AddInitializers;
128public:
129  AnalysisContextManager(bool useUnoptimizedCFG = false,
130      bool addImplicitDtors = false, bool addInitializers = false)
131    : UseUnoptimizedCFG(useUnoptimizedCFG), AddImplicitDtors(addImplicitDtors),
132      AddInitializers(addInitializers) {}
133
134  ~AnalysisContextManager();
135
136  AnalysisContext *getContext(const Decl *D, idx::TranslationUnit *TU = 0);
137
138  bool getUseUnoptimizedCFG() const { return UseUnoptimizedCFG; }
139  bool getAddImplicitDtors() const { return AddImplicitDtors; }
140  bool getAddInitializers() const { return AddInitializers; }
141
142  // Discard all previously created AnalysisContexts.
143  void clear();
144};
145
146class LocationContext : public llvm::FoldingSetNode {
147public:
148  enum ContextKind { StackFrame, Scope, Block };
149
150private:
151  ContextKind Kind;
152
153  // AnalysisContext can't be const since some methods may modify its member.
154  AnalysisContext *Ctx;
155
156  const LocationContext *Parent;
157
158protected:
159  LocationContext(ContextKind k, AnalysisContext *ctx,
160                  const LocationContext *parent)
161    : Kind(k), Ctx(ctx), Parent(parent) {}
162
163public:
164  virtual ~LocationContext();
165
166  ContextKind getKind() const { return Kind; }
167
168  AnalysisContext *getAnalysisContext() const { return Ctx; }
169
170  idx::TranslationUnit *getTranslationUnit() const {
171    return Ctx->getTranslationUnit();
172  }
173
174  const LocationContext *getParent() const { return Parent; }
175
176  bool isParentOf(const LocationContext *LC) const;
177
178  const Decl *getDecl() const { return getAnalysisContext()->getDecl(); }
179
180  CFG *getCFG() const { return getAnalysisContext()->getCFG(); }
181
182  LiveVariables *getLiveVariables() const {
183    return getAnalysisContext()->getLiveVariables();
184  }
185
186  ParentMap &getParentMap() const {
187    return getAnalysisContext()->getParentMap();
188  }
189
190  const ImplicitParamDecl *getSelfDecl() const {
191    return Ctx->getSelfDecl();
192  }
193
194  const StackFrameContext *getCurrentStackFrame() const;
195  const StackFrameContext *
196    getStackFrameForDeclContext(const DeclContext *DC) const;
197
198  virtual void Profile(llvm::FoldingSetNodeID &ID) = 0;
199
200  static bool classof(const LocationContext*) { return true; }
201
202public:
203  static void ProfileCommon(llvm::FoldingSetNodeID &ID,
204                            ContextKind ck,
205                            AnalysisContext *ctx,
206                            const LocationContext *parent,
207                            const void* data);
208};
209
210class StackFrameContext : public LocationContext {
211  // The callsite where this stack frame is established.
212  const Stmt *CallSite;
213
214  // The parent block of the callsite.
215  const CFGBlock *Block;
216
217  // The index of the callsite in the CFGBlock.
218  unsigned Index;
219
220  friend class LocationContextManager;
221  StackFrameContext(AnalysisContext *ctx, const LocationContext *parent,
222                    const Stmt *s, const CFGBlock *blk,
223                    unsigned idx)
224    : LocationContext(StackFrame, ctx, parent), CallSite(s),
225      Block(blk), Index(idx) {}
226
227public:
228  ~StackFrameContext() {}
229
230  const Stmt *getCallSite() const { return CallSite; }
231
232  const CFGBlock *getCallSiteBlock() const { return Block; }
233
234  unsigned getIndex() const { return Index; }
235
236  void Profile(llvm::FoldingSetNodeID &ID);
237
238  static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
239                      const LocationContext *parent, const Stmt *s,
240                      const CFGBlock *blk, unsigned idx) {
241    ProfileCommon(ID, StackFrame, ctx, parent, s);
242    ID.AddPointer(blk);
243    ID.AddInteger(idx);
244  }
245
246  static bool classof(const LocationContext* Ctx) {
247    return Ctx->getKind() == StackFrame;
248  }
249};
250
251class ScopeContext : public LocationContext {
252  const Stmt *Enter;
253
254  friend class LocationContextManager;
255  ScopeContext(AnalysisContext *ctx, const LocationContext *parent,
256               const Stmt *s)
257    : LocationContext(Scope, ctx, parent), Enter(s) {}
258
259public:
260  ~ScopeContext() {}
261
262  void Profile(llvm::FoldingSetNodeID &ID);
263
264  static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
265                      const LocationContext *parent, const Stmt *s) {
266    ProfileCommon(ID, Scope, ctx, parent, s);
267  }
268
269  static bool classof(const LocationContext* Ctx) {
270    return Ctx->getKind() == Scope;
271  }
272};
273
274class BlockInvocationContext : public LocationContext {
275  // FIXME: Add back context-sensivity (we don't want libAnalysis to know
276  //  about MemRegion).
277  const BlockDecl *BD;
278
279  friend class LocationContextManager;
280
281  BlockInvocationContext(AnalysisContext *ctx, const LocationContext *parent,
282                         const BlockDecl *bd)
283    : LocationContext(Block, ctx, parent), BD(bd) {}
284
285public:
286  ~BlockInvocationContext() {}
287
288  const BlockDecl *getBlockDecl() const { return BD; }
289
290  void Profile(llvm::FoldingSetNodeID &ID);
291
292  static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
293                      const LocationContext *parent, const BlockDecl *bd) {
294    ProfileCommon(ID, Block, ctx, parent, bd);
295  }
296
297  static bool classof(const LocationContext* Ctx) {
298    return Ctx->getKind() == Block;
299  }
300};
301
302class LocationContextManager {
303  llvm::FoldingSet<LocationContext> Contexts;
304public:
305  ~LocationContextManager();
306
307  const StackFrameContext *getStackFrame(AnalysisContext *ctx,
308                                         const LocationContext *parent,
309                                         const Stmt *s,
310                                         const CFGBlock *blk, unsigned idx);
311
312  const ScopeContext *getScope(AnalysisContext *ctx,
313                               const LocationContext *parent,
314                               const Stmt *s);
315
316  /// Discard all previously created LocationContext objects.
317  void clear();
318private:
319  template <typename LOC, typename DATA>
320  const LOC *getLocationContext(AnalysisContext *ctx,
321                                const LocationContext *parent,
322                                const DATA *d);
323};
324
325} // end clang namespace
326#endif
327