Environment.h revision c179a7fbb294fe3ff6cf5479f6239a10f39628c7
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.
191309f9a3b225ea846e5822691c39a77423125505Ted Kremenek#include "clang/Checker/PathSensitive/Store.h"
20df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
218133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#include "llvm/ADT/ImmutableMap.h"
229e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek#include "llvm/ADT/SmallVector.h"
231309f9a3b225ea846e5822691c39a77423125505Ted Kremenek#include "clang/Checker/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;
30d91ee27950ef5c321db1ac2aa5becb75ffe7cb14Zhongxing Xuclass ValueManager;
31df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenekclass LiveVariables;
32df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
330fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek
34fdf6a56339b3df94d46a49c4977e0a21e8922cf3Ted Kremenekclass Environment {
358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  friend class EnvironmentManager;
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
388133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Type definitions.
3923ec48cd3369c8d7d1ab3c3f2226cfcffd2cd3d3Ted Kremenek  typedef llvm::ImmutableMap<const Stmt*,SVal> BindingsTy;
408133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
418133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Data.
420fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  BindingsTy ExprBindings;
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu  Environment(BindingsTy eb)
45c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu    : ExprBindings(eb) {}
461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
480fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  typedef BindingsTy::iterator iterator;
490fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  iterator begin() const { return ExprBindings.begin(); }
500fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  iterator end() const { return ExprBindings.end(); }
511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5223ec48cd3369c8d7d1ab3c3f2226cfcffd2cd3d3Ted Kremenek  SVal LookupExpr(const Stmt* E) const {
530fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek    const SVal* X = ExprBindings.lookup(E);
548133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
558133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57d91ee27950ef5c321db1ac2aa5becb75ffe7cb14Zhongxing Xu  SVal GetSVal(const Stmt* Ex, ValueManager& ValMgr) const;
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
598133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Profile the contents of an Environment object for use
608133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
618133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
620fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek    E->ExprBindings.Profile(ID);
638133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
658133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Used to profile the contents of this object for inclusion
668133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
678133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  void Profile(llvm::FoldingSetNodeID& ID) const {
688133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    Profile(ID, this);
69d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  bool operator==(const Environment& RHS) const {
720fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek    return ExprBindings == RHS.ExprBindings;
73d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
748133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
768133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager {
778133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
788133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef Environment::BindingsTy::Factory FactoryTy;
798133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  FactoryTy F;
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
828133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
838133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ~EnvironmentManager() {}
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu  Environment getInitialEnvironment() {
86c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu    return Environment(F.GetEmptyMap());
878133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
890fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  Environment BindExpr(Environment Env, const Stmt *S, SVal V,
900fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek                       bool Invalidate);
91241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek
920fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  Environment RemoveDeadBindings(Environment Env, const Stmt *S,
930fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek                                 SymbolReaper &SymReaper, const GRState *ST,
940fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek                          llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
958133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
978133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek} // end clang namespace
988133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
998133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#endif
100