Environment.h revision 1eb4433ac451dc16f4133a88af2d002ac26c58ef
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
290fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenekclass AnalysisContext;
308133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager;
31d91ee27950ef5c321db1ac2aa5becb75ffe7cb14Zhongxing Xuclass ValueManager;
32df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenekclass LiveVariables;
33df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek
340fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek
35fdf6a56339b3df94d46a49c4977e0a21e8922cf3Ted Kremenekclass Environment {
368133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
378133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  friend class EnvironmentManager;
381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
398133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Type definitions.
4023ec48cd3369c8d7d1ab3c3f2226cfcffd2cd3d3Ted Kremenek  typedef llvm::ImmutableMap<const Stmt*,SVal> BindingsTy;
418133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
428133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  // Data.
430fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  BindingsTy ExprBindings;
446d2c657b528d9ac1bacd382281f0bc49975aa89aTed Kremenek  AnalysisContext *ACtx;
451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
466d2c657b528d9ac1bacd382281f0bc49975aa89aTed Kremenek  Environment(BindingsTy eb, AnalysisContext *aCtx)
476d2c657b528d9ac1bacd382281f0bc49975aa89aTed Kremenek    : ExprBindings(eb), ACtx(aCtx) {}
481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
500fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  typedef BindingsTy::iterator iterator;
510fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  iterator begin() const { return ExprBindings.begin(); }
520fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  iterator end() const { return ExprBindings.end(); }
531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5423ec48cd3369c8d7d1ab3c3f2226cfcffd2cd3d3Ted Kremenek  SVal LookupExpr(const Stmt* E) const {
550fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek    const SVal* X = ExprBindings.lookup(E);
568133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    return X ? *X : UnknownVal();
578133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59d91ee27950ef5c321db1ac2aa5becb75ffe7cb14Zhongxing Xu  SVal GetSVal(const Stmt* Ex, ValueManager& ValMgr) const;
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
616d2c657b528d9ac1bacd382281f0bc49975aa89aTed Kremenek  AnalysisContext &getAnalysisContext() const { return *ACtx; }
621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
638133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Profile the contents of an Environment object for use
648133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
658133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* E) {
660fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek    E->ExprBindings.Profile(ID);
678133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
698133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  /// Profile - Used to profile the contents of this object for inclusion
708133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ///  in a FoldingSet.
718133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  void Profile(llvm::FoldingSetNodeID& ID) const {
728133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek    Profile(ID, this);
73d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
75d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  bool operator==(const Environment& RHS) const {
760fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek    return ExprBindings == RHS.ExprBindings;
77d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek  }
788133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
808133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager {
818133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate:
828133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  typedef Environment::BindingsTy::Factory FactoryTy;
838133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  FactoryTy F;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
868133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
878133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  ~EnvironmentManager() {}
881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896d2c657b528d9ac1bacd382281f0bc49975aa89aTed Kremenek  Environment getInitialEnvironment(AnalysisContext *ACtx) {
906d2c657b528d9ac1bacd382281f0bc49975aa89aTed Kremenek    return Environment(F.GetEmptyMap(), ACtx);
918133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek  }
921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
930fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  Environment BindExpr(Environment Env, const Stmt *S, SVal V,
940fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek                       bool Invalidate);
95241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek
960fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek  Environment RemoveDeadBindings(Environment Env, const Stmt *S,
970fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek                                 SymbolReaper &SymReaper, const GRState *ST,
980fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek                          llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
998133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek};
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1018133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek} // end clang namespace
1028133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek
1038133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#endif
104