Environment.h revision fdf6a56339b3df94d46a49c4977e0a21e8922cf3
1d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek//== Environment.h - Map from Stmt* to Locations/Values ---------*- C++ -*--==//
28133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//
38133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//                     The LLVM Compiler Infrastructure
48133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//
58133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek// This file is distributed under the University of Illinois Open Source
68133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek// License. See LICENSE.TXT for details.
78133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//
88133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//===----------------------------------------------------------------------===//
98133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//
108133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//  This file defined the Environment and EnvironmentManager classes.
118133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//
128133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//===----------------------------------------------------------------------===//
138133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
148133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#ifndef LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
158133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#define LLVM_CLANG_ANALYSIS_ENVIRONMENT_H
168133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
17df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek// For using typedefs in StoreManager. Should find a better place for these
18df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek// typedefs.
19df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek#include "clang/Analysis/PathSensitive/Store.h"
20df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
218133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "llvm/ADT/ImmutableMap.h"
229e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek#include "llvm/ADT/SmallVector.h"
2322ab7a4d900ed53285fd0b6720e7b43af84724d8Zhongxing Xu#include "clang/Analysis/PathSensitive/SVals.h"
248133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "llvm/Support/Allocator.h"
258133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "llvm/ADT/FoldingSet.h"
268133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
278133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremeneknamespace clang {
288133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
298133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager;
30d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenekclass BasicValueFactory;
31df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenekclass LiveVariables;
32df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
33fdf6a56339b3df94d46a49c4977e0a21e8922cf3Ted Kremenekclass Environment {
348133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  friend class EnvironmentManager;
368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
378133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Type definitions.
38d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  typedef llvm::ImmutableMap<Stmt*,SVal> BindingsTy;
398133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
408133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Data.
418133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  BindingsTy SubExprBindings;
428133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  BindingsTy BlkExprBindings;
438133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
448133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment(BindingsTy seb, BindingsTy beb)
458133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    : SubExprBindings(seb), BlkExprBindings(beb) {}
468133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
478133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekpublic:
488133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
498133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef BindingsTy::iterator seb_iterator;
508133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  seb_iterator seb_begin() const { return SubExprBindings.begin(); }
518133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  seb_iterator seb_end() const { return SubExprBindings.end(); }
528133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
538133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef BindingsTy::iterator beb_iterator;
548133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  beb_iterator beb_begin() const { return BlkExprBindings.begin(); }
558133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  beb_iterator beb_end() const { return BlkExprBindings.end(); }
568133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
57d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal LookupSubExpr(Stmt* E) const {
58d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek    const SVal* X = SubExprBindings.lookup(cast<Expr>(E));
598133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
608133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
618133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
62d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal LookupBlkExpr(Stmt* E) const {
631c96b24285d05c0eac455ae96d7c9ff43d42bc96Zhongxing Xu    const SVal* X = BlkExprBindings.lookup(E);
648133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
658133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
668133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
67d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal LookupExpr(Stmt* E) const {
681c96b24285d05c0eac455ae96d7c9ff43d42bc96Zhongxing Xu    const SVal* X = SubExprBindings.lookup(E);
698133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    if (X) return *X;
708133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    X = BlkExprBindings.lookup(E);
718133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
728133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
738133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
74d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal GetSVal(Stmt* Ex, BasicValueFactory& BasicVals) const;
75d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal GetBlkExprSVal(Stmt* Ex, BasicValueFactory& BasicVals) const;
76d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek
778133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Profile the contents of an Environment object for use
788133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
798133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
808133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    E->SubExprBindings.Profile(ID);
818133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    E->BlkExprBindings.Profile(ID);
828133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
838133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
848133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Used to profile the contents of this object for inclusion
858133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
868133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  void Profile(llvm::FoldingSetNodeID& ID) const {
878133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    Profile(ID, this);
88d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
89d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek
90d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  bool operator==(const Environment& RHS) const {
91d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek    return SubExprBindings == RHS.SubExprBindings &&
92d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek           BlkExprBindings == RHS.BlkExprBindings;
93d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
948133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
958133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
968133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager {
978133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
988133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef Environment::BindingsTy::Factory FactoryTy;
998133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  FactoryTy F;
1008133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1018133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekpublic:
1028133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1038133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
1048133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ~EnvironmentManager() {}
1058133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1068133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// RemoveBlkExpr - Return a new environment object with the same bindings as
107d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  ///  the provided environment except with any bindings for the provided Stmt*
1088133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  removed.  This method only removes bindings for block-level expressions.
1098133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  Using this method on a non-block level expression will return the
1108133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  same environment object.
111d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment RemoveBlkExpr(const Environment& Env, Stmt* E) {
1128133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(Env.SubExprBindings, F.Remove(Env.BlkExprBindings, E));
1138133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1148133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
115d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment RemoveSubExpr(const Environment& Env, Stmt* E) {
1168133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.Remove(Env.SubExprBindings, E), Env.BlkExprBindings);
1178133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1188133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
119d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment AddBlkExpr(const Environment& Env, Stmt* E, SVal V) {
1201c96b24285d05c0eac455ae96d7c9ff43d42bc96Zhongxing Xu    return Environment(Env.SubExprBindings, F.Add(Env.BlkExprBindings, E, V));
1218133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1228133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
123d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment AddSubExpr(const Environment& Env, Stmt* E, SVal V) {
1248133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.Add(Env.SubExprBindings, E, V), Env.BlkExprBindings);
1258133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1268133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1278133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// RemoveSubExprBindings - Return a new environment object with
1288133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  the same bindings as the provided environment except with all the
1298133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  subexpression bindings removed.
1308133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment RemoveSubExprBindings(const Environment& Env) {
1318133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.GetEmptyMap(), Env.BlkExprBindings);
1328133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1338133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1348133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment getInitialEnvironment() {
1358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.GetEmptyMap(), F.GetEmptyMap());
1368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
137d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek
138d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment BindExpr(const Environment& Env, Stmt* E, SVal V,
1398cd5aaea92f12c25d65ecff5fc820def4c81c1aaZhongxing Xu                       bool isBlkExpr, bool Invalidate);
140df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
141241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek  Environment
142241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek  RemoveDeadBindings(Environment Env, Stmt* Loc, SymbolReaper& SymReaper,
1435dc2746b6c759d2fdab050d3d41ba60ad141a0a3Ted Kremenek                     GRStateManager& StateMgr, const GRState *state,
144241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek                     llvm::SmallVectorImpl<const MemRegion*>& DRoots);
145241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek
1468133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
1478133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1488133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek} // end clang namespace
1498133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1508133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#endif
151