aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Analysis/PathSensitive/AnalysisContext.h
blob: 66e850a91444d7d64383541c26187061d0c79c03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//=== AnalysisContext.h - Analysis context for Path Sens analysis --*- C++ -*-//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines AnalysisContext, a class that manages the analysis context
// data for path sensitive analysis.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H
#define LLVM_CLANG_ANALYSIS_ANALYSISCONTEXT_H

#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/DenseMap.h"

namespace clang {

class Decl;
class Stmt;
class CFG;
class LiveVariables;
class ParentMap;
class ImplicitParamDecl;

/// AnalysisContext contains the context data for the function or method under
/// analysis.
class AnalysisContext {
  const Decl *D;

  // AnalysisContext owns the following data.
  CFG *cfg;
  LiveVariables *liveness;
  ParentMap *PM;

public:
  AnalysisContext(const Decl *d) : D(d), cfg(0), liveness(0), PM(0) {}
  ~AnalysisContext();

  const Decl *getDecl() { return D; }
  Stmt *getBody();
  CFG *getCFG();
  ParentMap &getParentMap();
  LiveVariables *getLiveVariables();

  /// Return the ImplicitParamDecl* associated with 'self' if this
  /// AnalysisContext wraps an ObjCMethodDecl.  Returns NULL otherwise.
  const ImplicitParamDecl *getSelfDecl() const;
};

class AnalysisContextManager {
  typedef llvm::DenseMap<const Decl*, AnalysisContext*> ContextMap;
  ContextMap Contexts;
public:
  ~AnalysisContextManager();

  AnalysisContext *getContext(const Decl *D);
  
  // Discard all previously created AnalysisContexts.
  void clear();
};

class LocationContext : public llvm::FoldingSetNode {
public:
  enum ContextKind { StackFrame, Scope };

private:
  ContextKind Kind;
  AnalysisContext *Ctx;
  const LocationContext *Parent;

protected:
  LocationContext(ContextKind k, AnalysisContext *ctx,
                  const LocationContext *parent)
    : Kind(k), Ctx(ctx), Parent(parent) {}

public:
  virtual ~LocationContext() {}
  
  ContextKind getKind() const { return Kind; }

  AnalysisContext *getAnalysisContext() const { return Ctx; }

  const LocationContext *getParent() const { return Parent; }

  const Decl *getDecl() const { return getAnalysisContext()->getDecl(); }

  CFG *getCFG() const { return getAnalysisContext()->getCFG(); }

  LiveVariables *getLiveVariables() const {
    return getAnalysisContext()->getLiveVariables();
  }

  ParentMap &getParentMap() const { 
    return getAnalysisContext()->getParentMap();
  }

  const ImplicitParamDecl *getSelfDecl() const {
    return Ctx->getSelfDecl();
  }

  virtual void Profile(llvm::FoldingSetNodeID &ID) {
    Profile(ID, Kind, Ctx, Parent);
  }

  static void Profile(llvm::FoldingSetNodeID &ID, ContextKind k,
                      AnalysisContext *ctx, const LocationContext *parent);

  static bool classof(const LocationContext*) { return true; }
};

class StackFrameContext : public LocationContext {
  const Stmt *CallSite;

public:
  StackFrameContext(AnalysisContext *ctx, const LocationContext *parent,
                    const Stmt *s)
    : LocationContext(StackFrame, ctx, parent), CallSite(s) {}
  
  virtual ~StackFrameContext() {}


  Stmt const *getCallSite() const { return CallSite; }

  virtual void Profile(llvm::FoldingSetNodeID &ID) {
    Profile(ID, getAnalysisContext(), getParent(), CallSite);
  }

  static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
                      const LocationContext *parent, const Stmt *s);

  static bool classof(const LocationContext* Ctx) {
    return Ctx->getKind() == StackFrame;
  }
};

class ScopeContext : public LocationContext {
  const Stmt *Enter;

public:
  ScopeContext(AnalysisContext *ctx, const LocationContext *parent,
               const Stmt *s)
    : LocationContext(Scope, ctx, parent), Enter(s) {}
  
  virtual ~ScopeContext() {}

  virtual void Profile(llvm::FoldingSetNodeID &ID) {
    Profile(ID, getAnalysisContext(), getParent(), Enter);
  }

  static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
                      const LocationContext *parent, const Stmt *s);

  static bool classof(const LocationContext* Ctx) {
    return Ctx->getKind() == Scope;
  }
};

class LocationContextManager {
  llvm::FoldingSet<LocationContext> Contexts;

public:
  ~LocationContextManager();
  
  StackFrameContext *getStackFrame(AnalysisContext *ctx,
                                   const LocationContext *parent,
                                   const Stmt *s);

  ScopeContext *getScope(AnalysisContext *ctx, const LocationContext *parent,
                         const Stmt *s);
  
  /// Discard all previously created LocationContext objects.
  void clear();
};

} // end clang namespace
#endif