summaryrefslogtreecommitdiffstats
path: root/include/clang/Analysis/PathSensitive/AnalysisContext.h
blob: db281fec58ed7752daffac521175378aec897dd7 (plain)
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
//=== 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 <map>

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 {
  Decl *D;
  Stmt *Body;

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

public:
  AnalysisContext() : D(0), Body(0), cfg(0), liveness(0), PM(0) {}
  ~AnalysisContext();

  void setDecl(Decl* d) { D = d; }
  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 {
  std::map<Decl*, AnalysisContext> Contexts;

public:
  typedef std::map<Decl*, AnalysisContext>::iterator iterator;

  AnalysisContext *getContext(Decl *D);
};

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:
  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();
  }
  
  const ImplicitParamDecl *getSelfDecl() const {
    return Ctx->getSelfDecl();
  }

  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) {}

  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) {}

  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:
  StackFrameContext *getStackFrame(AnalysisContext *ctx,
                                   const LocationContext *parent,
                                   const Stmt *s);

  ScopeContext *getScope(AnalysisContext *ctx, const LocationContext *parent,
                         const Stmt *s);
};
  
} // end clang namespace
#endif