Environment.h revision 241677a13cc46647a8f5098b3e3239bd9480dca2
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
338133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass Environment : public llvm::FoldingSetNode {
348133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  friend class EnvironmentManager;
378133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
388133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Type definitions.
39d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  typedef llvm::ImmutableMap<Stmt*,SVal> BindingsTy;
408133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
418133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Data.
428133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  BindingsTy SubExprBindings;
438133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  BindingsTy BlkExprBindings;
448133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
458133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment(BindingsTy seb, BindingsTy beb)
468133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    : SubExprBindings(seb), BlkExprBindings(beb) {}
478133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
488133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekpublic:
498133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
508133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef BindingsTy::iterator seb_iterator;
518133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  seb_iterator seb_begin() const { return SubExprBindings.begin(); }
528133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  seb_iterator seb_end() const { return SubExprBindings.end(); }
538133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
548133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef BindingsTy::iterator beb_iterator;
558133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  beb_iterator beb_begin() const { return BlkExprBindings.begin(); }
568133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  beb_iterator beb_end() const { return BlkExprBindings.end(); }
578133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
58d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal LookupSubExpr(Stmt* E) const {
59d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek    const SVal* X = SubExprBindings.lookup(cast<Expr>(E));
608133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
618133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
628133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
63d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal LookupBlkExpr(Stmt* E) const {
641c96b24285d05c0eac455ae96d7c9ff43d42bc96Zhongxing Xu    const SVal* X = BlkExprBindings.lookup(E);
658133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
668133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
678133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
68d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal LookupExpr(Stmt* E) const {
691c96b24285d05c0eac455ae96d7c9ff43d42bc96Zhongxing Xu    const SVal* X = SubExprBindings.lookup(E);
708133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    if (X) return *X;
718133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    X = BlkExprBindings.lookup(E);
728133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
738133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
748133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
75d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal GetSVal(Stmt* Ex, BasicValueFactory& BasicVals) const;
76d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  SVal GetBlkExprSVal(Stmt* Ex, BasicValueFactory& BasicVals) const;
77d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek
788133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Profile the contents of an Environment object for use
798133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
808133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
818133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    E->SubExprBindings.Profile(ID);
828133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    E->BlkExprBindings.Profile(ID);
838133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
848133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
858133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Used to profile the contents of this object for inclusion
868133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
878133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  void Profile(llvm::FoldingSetNodeID& ID) const {
888133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    Profile(ID, this);
89d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
90d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek
91d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  bool operator==(const Environment& RHS) const {
92d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek    return SubExprBindings == RHS.SubExprBindings &&
93d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek           BlkExprBindings == RHS.BlkExprBindings;
94d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
958133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
968133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
978133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager {
988133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
998133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef Environment::BindingsTy::Factory FactoryTy;
1008133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  FactoryTy F;
1018133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1028133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekpublic:
1038133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1048133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
1058133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ~EnvironmentManager() {}
1068133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1078133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// RemoveBlkExpr - Return a new environment object with the same bindings as
108d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  ///  the provided environment except with any bindings for the provided Stmt*
1098133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  removed.  This method only removes bindings for block-level expressions.
1108133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  Using this method on a non-block level expression will return the
1118133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  same environment object.
112d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment RemoveBlkExpr(const Environment& Env, Stmt* E) {
1138133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(Env.SubExprBindings, F.Remove(Env.BlkExprBindings, E));
1148133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1158133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
116d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment RemoveSubExpr(const Environment& Env, Stmt* E) {
1178133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.Remove(Env.SubExprBindings, E), Env.BlkExprBindings);
1188133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1198133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
120d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment AddBlkExpr(const Environment& Env, Stmt* E, SVal V) {
1211c96b24285d05c0eac455ae96d7c9ff43d42bc96Zhongxing Xu    return Environment(Env.SubExprBindings, F.Add(Env.BlkExprBindings, E, V));
1228133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1238133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
124d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment AddSubExpr(const Environment& Env, Stmt* E, SVal V) {
1258133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.Add(Env.SubExprBindings, E, V), Env.BlkExprBindings);
1268133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1278133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1288133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// RemoveSubExprBindings - Return a new environment object with
1298133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  the same bindings as the provided environment except with all the
1308133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  subexpression bindings removed.
1318133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment RemoveSubExprBindings(const Environment& Env) {
1328133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.GetEmptyMap(), Env.BlkExprBindings);
1338133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
1348133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  Environment getInitialEnvironment() {
1368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return Environment(F.GetEmptyMap(), F.GetEmptyMap());
1378133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
138d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek
139d4931632946fe86fc2b09496f2b62443440a7da4Ted Kremenek  Environment BindExpr(const Environment& Env, Stmt* E, SVal V,
1408cd5aaea92f12c25d65ecff5fc820def4c81c1aaZhongxing Xu                       bool isBlkExpr, bool Invalidate);
141df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
142241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek  Environment
143241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek  RemoveDeadBindings(Environment Env, Stmt* Loc, SymbolReaper& SymReaper,
144241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek                     llvm::SmallVectorImpl<const MemRegion*>& DRoots);
145241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek
1468133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
1478133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1488133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek} // end clang namespace
1498133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1508133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#endif
151