aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Analysis/PathSensitive/Environment.h
blob: 6f8a126427699e37798fef7490241e3634340410 (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
//== Environment.h - Map from Stmt* to Locations/Values ---------*- C++ -*--==//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defined the Environment and EnvironmentManager classes.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
#define LLVM_CLANG_ANALYSIS_ENVIRONMENT_H

// For using typedefs in StoreManager. Should find a better place for these
// typedefs.
#include "clang/Analysis/PathSensitive/Store.h"

#include "llvm/ADT/ImmutableMap.h"
#include "llvm/ADT/SmallVector.h"
#include "clang/Analysis/PathSensitive/SVals.h"
#include "llvm/Support/Allocator.h"
#include "llvm/ADT/FoldingSet.h"

namespace clang {

class EnvironmentManager;
class ValueManager;
class LiveVariables;

class Environment {
private:
  friend class EnvironmentManager;
  
  // Type definitions.
  typedef llvm::ImmutableMap<const Stmt*,SVal> BindingsTy;

  // Data.
  BindingsTy SubExprBindings;
  BindingsTy BlkExprBindings;
  
  Environment(BindingsTy seb, BindingsTy beb)
    : SubExprBindings(seb), BlkExprBindings(beb) {}
  
public:
    
  typedef BindingsTy::iterator seb_iterator;
  seb_iterator seb_begin() const { return SubExprBindings.begin(); }
  seb_iterator seb_end() const { return SubExprBindings.end(); }
  
  typedef BindingsTy::iterator beb_iterator;
  beb_iterator beb_begin() const { return BlkExprBindings.begin(); }
  beb_iterator beb_end() const { return BlkExprBindings.end(); }      
  
  SVal LookupSubExpr(const Stmt* E) const {
    const SVal* X = SubExprBindings.lookup(cast<Expr>(E));
    return X ? *X : UnknownVal();
  }
  
  SVal LookupBlkExpr(const Stmt* E) const {
    const SVal* X = BlkExprBindings.lookup(E);
    return X ? *X : UnknownVal();
  }
  
  SVal LookupExpr(const Stmt* E) const {
    const SVal* X = SubExprBindings.lookup(E);
    if (X) return *X;
    X = BlkExprBindings.lookup(E);
    return X ? *X : UnknownVal();
  }
  
  SVal GetSVal(const Stmt* Ex, ValueManager& ValMgr) const;
  SVal GetBlkExprSVal(const Stmt* Ex, ValueManager& ValMgr) const; 
  
  /// Profile - Profile the contents of an Environment object for use
  ///  in a FoldingSet.
  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
    E->SubExprBindings.Profile(ID);
    E->BlkExprBindings.Profile(ID);
  }
  
  /// Profile - Used to profile the contents of this object for inclusion
  ///  in a FoldingSet.
  void Profile(llvm::FoldingSetNodeID& ID) const {
    Profile(ID, this);
  }
  
  bool operator==(const Environment& RHS) const {
    return SubExprBindings == RHS.SubExprBindings &&
           BlkExprBindings == RHS.BlkExprBindings;
  }
};
  
class EnvironmentManager {
private:
  typedef Environment::BindingsTy::Factory FactoryTy;
  FactoryTy F;
  
public:
  
  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
  ~EnvironmentManager() {}

  /// RemoveBlkExpr - Return a new environment object with the same bindings as
  ///  the provided environment except with any bindings for the provided Stmt*
  ///  removed.  This method only removes bindings for block-level expressions.
  ///  Using this method on a non-block level expression will return the
  ///  same environment object.
  Environment RemoveBlkExpr(const Environment& Env, const Stmt* E) {
    return Environment(Env.SubExprBindings, F.Remove(Env.BlkExprBindings, E));
  }
  
  Environment RemoveSubExpr(const Environment& Env, const Stmt* E) {
    return Environment(F.Remove(Env.SubExprBindings, E), Env.BlkExprBindings);
  }
  
  Environment AddBlkExpr(const Environment& Env, const Stmt *E, SVal V) {
    return Environment(Env.SubExprBindings, F.Add(Env.BlkExprBindings, E, V));
  }
  
  Environment AddSubExpr(const Environment& Env, const Stmt *E, SVal V) {
    return Environment(F.Add(Env.SubExprBindings, E, V), Env.BlkExprBindings);
  }
  
  /// RemoveSubExprBindings - Return a new environment object with
  ///  the same bindings as the provided environment except with all the
  ///  subexpression bindings removed.
  Environment RemoveSubExprBindings(const Environment& Env) {
    return Environment(F.GetEmptyMap(), Env.BlkExprBindings);
  }
  
  Environment getInitialEnvironment() {
    return Environment(F.GetEmptyMap(), F.GetEmptyMap());
  }
  
  Environment BindExpr(const Environment& Env, const Stmt* E, SVal V,
                       bool isBlkExpr, bool Invalidate);

  Environment
  RemoveDeadBindings(Environment Env, Stmt* Loc, SymbolReaper& SymReaper,
                     GRStateManager& StateMgr, const GRState *state,
                     llvm::SmallVectorImpl<const MemRegion*>& DRoots);

};
  
} // end clang namespace

#endif