1 //== Environment.h - Map from Stmt* to Locations/Values ---------*- C++ -*--==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defined the Environment and EnvironmentManager classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_GR_ENVIRONMENT_H 15 #define LLVM_CLANG_GR_ENVIRONMENT_H 16 17 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 18 #include "llvm/ADT/ImmutableMap.h" 19 20 namespace clang { 21 22 class LiveVariables; 23 24 namespace ento { 25 26 class EnvironmentManager; 27 class SValBuilder; 28 29 /// Environment - An immutable map from Stmts to their current 30 /// symbolic values (SVals). 31 /// 32 class Environment { 33 private: 34 friend class EnvironmentManager; 35 36 // Type definitions. 37 typedef llvm::ImmutableMap<const Stmt*,SVal> BindingsTy; 38 39 // Data. 40 BindingsTy ExprBindings; 41 42 Environment(BindingsTy eb) 43 : ExprBindings(eb) {} 44 45 SVal lookupExpr(const Stmt *E) const; 46 47 public: 48 typedef BindingsTy::iterator iterator; 49 iterator begin() const { return ExprBindings.begin(); } 50 iterator end() const { return ExprBindings.end(); } 51 52 53 /// getSVal - Fetches the current binding of the expression in the 54 /// Environment. 55 SVal getSVal(const Stmt *Ex, SValBuilder& svalBuilder, 56 bool useOnlyDirectBindings = false) const; 57 58 /// Profile - Profile the contents of an Environment object for use 59 /// in a FoldingSet. 60 static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) { 61 env->ExprBindings.Profile(ID); 62 } 63 64 /// Profile - Used to profile the contents of this object for inclusion 65 /// in a FoldingSet. 66 void Profile(llvm::FoldingSetNodeID& ID) const { 67 Profile(ID, this); 68 } 69 70 bool operator==(const Environment& RHS) const { 71 return ExprBindings == RHS.ExprBindings; 72 } 73 }; 74 75 class EnvironmentManager { 76 private: 77 typedef Environment::BindingsTy::Factory FactoryTy; 78 FactoryTy F; 79 80 public: 81 EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {} 82 ~EnvironmentManager() {} 83 84 Environment getInitialEnvironment() { 85 return Environment(F.getEmptyMap()); 86 } 87 88 /// Bind the value 'V' to the statement 'S'. 89 Environment bindExpr(Environment Env, const Stmt *S, SVal V, 90 bool Invalidate); 91 92 /// Bind the location 'location' and value 'V' to the statement 'S'. This 93 /// is used when simulating loads/stores. 94 Environment bindExprAndLocation(Environment Env, const Stmt *S, SVal location, 95 SVal V); 96 97 Environment removeDeadBindings(Environment Env, 98 SymbolReaper &SymReaper, const ProgramState *ST); 99 }; 100 101 } // end GR namespace 102 103 } // end clang namespace 104 105 #endif 106