Environment.h revision df9cdf8fce5bb43b335994f946f7c8e3a3bca7fa
18133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek//== Environment.h - Map from Expr* 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"
228133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "clang/Analysis/PathSensitive/RValues.h"
238133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "llvm/Support/Allocator.h"
248133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "llvm/ADT/FoldingSet.h"
258133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
268133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremeneknamespace clang {
278133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
288133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager;
29d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenekclass BasicValueFactory;
30df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenekclass LiveVariables;
31df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
328133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass Environment : public llvm::FoldingSetNode {
338133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
348133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  friend class EnvironmentManager;
368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
378133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Type definitions.
388133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef llvm::ImmutableMap<Expr*,RVal> 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
578133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  RVal LookupSubExpr(Expr* E) const {
588133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    const RVal* X = SubExprBindings.lookup(E);
598133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
608133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
618133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
628133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  RVal LookupBlkExpr(Expr* E) const {
638133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    const RVal* X = BlkExprBindings.lookup(E);
648133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
658133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
668133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
678133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  RVal LookupExpr(Expr* E) const {
688133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    const RVal* 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
74d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  RVal GetRVal(Expr* Ex, BasicValueFactory& BasicVals) const;
75d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  RVal GetBlkExprRVal(Expr* 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
1078133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  the provided environment except with any bindings for the provided Expr*
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.
1118133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment RemoveBlkExpr(const Environment& Env, Expr* E) {
1128133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(Env.SubExprBindings, F.Remove(Env.BlkExprBindings, E));
1138133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1148133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1158133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment RemoveSubExpr(const Environment& Env, Expr* E) {
1168133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.Remove(Env.SubExprBindings, E), Env.BlkExprBindings);
1178133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1188133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1198133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment AddBlkExpr(const Environment& Env, Expr* E, RVal V) {
1208133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(Env.SubExprBindings, F.Add(Env.BlkExprBindings, E, V));
1218133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1228133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1238133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment AddSubExpr(const Environment& Env, Expr* E, RVal 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
138d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  Environment SetRVal(const Environment& Env, Expr* E, RVal V,
139d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek                      bool isBlkExpr, bool Invalidate);
140df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
141df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek  Environment RemoveDeadBindings(Environment Env,
142df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek                                 Stmt* Loc,
143df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek                                 const LiveVariables& Liveness,
144df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek                                 StoreManager::DeclRootsTy& DRoots,
145df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek                                 StoreManager::LiveSymbolsTy& LSymbols);
1468133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
1478133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1488133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek} // end clang namespace
1498133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1508133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#endif
151