Environment.h revision 9b663716449b618ba0390b1dbebc54fa8e971124
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 145a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis#ifndef LLVM_CLANG_GR_ENVIRONMENT_H 155a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis#define LLVM_CLANG_GR_ENVIRONMENT_H 168133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek 179b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h" 189b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 195e2d2c2ee3cf410643e0f9a5701708e51409d973Benjamin Kramer#include "llvm/ADT/ImmutableMap.h" 208133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek 218133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremeneknamespace clang { 228133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek 235a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidisclass LiveVariables; 245a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis 259ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremeneknamespace ento { 265a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis 278133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager; 28c8413fd03f73084a5c93028f8b4db619fc388087Ted Kremenekclass SValBuilder; 29df9cdf8fce5bb43b335994f946f7c8e3a3bca7faTed Kremenek 30834f9de3d3d76986d09f41725a70ba45a3e2aecdZhanyong Wan/// Environment - An immutable map from Stmts to their current 31834f9de3d3d76986d09f41725a70ba45a3e2aecdZhanyong Wan/// symbolic values (SVals). 32834f9de3d3d76986d09f41725a70ba45a3e2aecdZhanyong Wan/// 33fdf6a56339b3df94d46a49c4977e0a21e8922cf3Ted Kremenekclass Environment { 348133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate: 358133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek friend class EnvironmentManager; 361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 378133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek // Type definitions. 3823ec48cd3369c8d7d1ab3c3f2226cfcffd2cd3d3Ted Kremenek typedef llvm::ImmutableMap<const Stmt*,SVal> BindingsTy; 398133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek 408133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek // Data. 410fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek BindingsTy ExprBindings; 421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 43c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu Environment(BindingsTy eb) 44c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu : ExprBindings(eb) {} 451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46465846324f412055dd1ce270d757bfeead0811dcTed Kremenek SVal lookupExpr(const Stmt* E) const; 47465846324f412055dd1ce270d757bfeead0811dcTed Kremenek 481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic: 490fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek typedef BindingsTy::iterator iterator; 500fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek iterator begin() const { return ExprBindings.begin(); } 510fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek iterator end() const { return ExprBindings.end(); } 521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 54834f9de3d3d76986d09f41725a70ba45a3e2aecdZhanyong Wan /// GetSVal - Fetches the current binding of the expression in the 55834f9de3d3d76986d09f41725a70ba45a3e2aecdZhanyong Wan /// Environment. 56c8413fd03f73084a5c93028f8b4db619fc388087Ted Kremenek SVal getSVal(const Stmt* Ex, SValBuilder& svalBuilder) const; 571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 588133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek /// Profile - Profile the contents of an Environment object for use 598133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek /// in a FoldingSet. 60c8413fd03f73084a5c93028f8b4db619fc388087Ted Kremenek static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) { 61c8413fd03f73084a5c93028f8b4db619fc388087Ted Kremenek env->ExprBindings.Profile(ID); 628133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek } 631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 648133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek /// Profile - Used to profile the contents of this object for inclusion 658133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek /// in a FoldingSet. 668133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek void Profile(llvm::FoldingSetNodeID& ID) const { 678133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek Profile(ID, this); 68d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek } 691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 70d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek bool operator==(const Environment& RHS) const { 710fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek return ExprBindings == RHS.ExprBindings; 72d72ee907f76000446c706471e93d1f299104f9a7Ted Kremenek } 738133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek}; 741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 758133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekclass EnvironmentManager { 768133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenekprivate: 778133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek typedef Environment::BindingsTy::Factory FactoryTy; 788133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek FactoryTy F; 791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic: 818133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {} 828133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek ~EnvironmentManager() {} 831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 84c179a7fbb294fe3ff6cf5479f6239a10f39628c7Zhongxing Xu Environment getInitialEnvironment() { 853baf672378f105602d2b12f03f00277ae1936fe9Ted Kremenek return Environment(F.getEmptyMap()); 868133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek } 871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 886d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek /// Bind the value 'V' to the statement 'S'. 896d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek Environment bindExpr(Environment Env, const Stmt *S, SVal V, 900fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek bool Invalidate); 916d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek 926d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek /// Bind the location 'location' and value 'V' to the statement 'S'. This 936d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek /// is used when simulating loads/stores. 946d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek Environment bindExprAndLocation(Environment Env, const Stmt *S, SVal location, 956d4c022695c2d780ccb63ce43a50588412b80813Ted Kremenek SVal V); 96241677a13cc46647a8f5098b3e3239bd9480dca2Ted Kremenek 97db0594bfc013131f88429add4eb653c285fa94fbTed Kremenek Environment removeDeadBindings(Environment Env, 980fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek SymbolReaper &SymReaper, const GRState *ST, 990fb0bc4067d6c9d7c0e655300ef309b05d3adfc9Ted Kremenek llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); 1008133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek}; 1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1025a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis} // end GR namespace 1035a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis 1048133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek} // end clang namespace 1058133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek 1068133a26c166be89fb4f0a339db1e2fe923c51a70Ted Kremenek#endif 107