1 //== Environment.h - Map from Stmt* to Locations/Values ---------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defined the Environment and EnvironmentManager classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
17 #include "clang/Analysis/AnalysisContext.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19 #include "llvm/ADT/ImmutableMap.h"
27 class EnvironmentManager;
31 /// An entry in the environment consists of a Stmt and an LocationContext.
32 /// This allows the environment to manage context-sensitive bindings,
33 /// which is essentially for modeling recursive function analysis, among
35 class EnvironmentEntry : public std::pair<const Stmt*,
36 const StackFrameContext *> {
38 EnvironmentEntry(const Stmt *s, const LocationContext *L);
40 const Stmt *getStmt() const { return first; }
41 const LocationContext *getLocationContext() const { return second; }
43 /// Profile an EnvironmentEntry for inclusion in a FoldingSet.
44 static void Profile(llvm::FoldingSetNodeID &ID,
45 const EnvironmentEntry &E) {
46 ID.AddPointer(E.getStmt());
47 ID.AddPointer(E.getLocationContext());
50 void Profile(llvm::FoldingSetNodeID &ID) const {
55 /// An immutable map from EnvironemntEntries to SVals.
58 friend class EnvironmentManager;
61 typedef llvm::ImmutableMap<EnvironmentEntry, SVal> BindingsTy;
64 BindingsTy ExprBindings;
66 Environment(BindingsTy eb)
69 SVal lookupExpr(const EnvironmentEntry &E) const;
72 typedef BindingsTy::iterator iterator;
73 iterator begin() const { return ExprBindings.begin(); }
74 iterator end() const { return ExprBindings.end(); }
76 /// Fetches the current binding of the expression in the
78 SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const;
80 /// Profile - Profile the contents of an Environment object for use
82 static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) {
83 env->ExprBindings.Profile(ID);
86 /// Profile - Used to profile the contents of this object for inclusion
88 void Profile(llvm::FoldingSetNodeID& ID) const {
92 bool operator==(const Environment& RHS) const {
93 return ExprBindings == RHS.ExprBindings;
96 void print(raw_ostream &Out, const char *NL, const char *Sep) const;
99 void printAux(raw_ostream &Out, bool printLocations,
100 const char *NL, const char *Sep) const;
103 class EnvironmentManager {
105 typedef Environment::BindingsTy::Factory FactoryTy;
109 EnvironmentManager(llvm::BumpPtrAllocator& Allocator) : F(Allocator) {}
111 Environment getInitialEnvironment() {
112 return Environment(F.getEmptyMap());
115 /// Bind a symbolic value to the given environment entry.
116 Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V,
119 Environment removeDeadBindings(Environment Env,
120 SymbolReaper &SymReaper,
121 ProgramStateRef state);
124 } // end GR namespace
126 } // end clang namespace