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/AnalysisDeclContext.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
20 #include "llvm/ADT/ImmutableMap.h"
32 /// An entry in the environment consists of a Stmt and an LocationContext.
33 /// This allows the environment to manage context-sensitive bindings,
34 /// which is essentially for modeling recursive function analysis, among
36 class EnvironmentEntry : public std::pair<const Stmt *,
37 const StackFrameContext *> {
39 EnvironmentEntry(const Stmt *s, const LocationContext *L);
41 const Stmt *getStmt() const { return first; }
42 const LocationContext *getLocationContext() const { return second; }
44 /// Profile an EnvironmentEntry for inclusion in a FoldingSet.
45 static void Profile(llvm::FoldingSetNodeID &ID,
46 const EnvironmentEntry &E) {
47 ID.AddPointer(E.getStmt());
48 ID.AddPointer(E.getLocationContext());
51 void Profile(llvm::FoldingSetNodeID &ID) const {
56 /// An immutable map from EnvironemntEntries to SVals.
59 friend class EnvironmentManager;
61 using BindingsTy = llvm::ImmutableMap<EnvironmentEntry, SVal>;
63 BindingsTy ExprBindings;
65 Environment(BindingsTy eb) : ExprBindings(eb) {}
67 SVal lookupExpr(const EnvironmentEntry &E) const;
70 using iterator = BindingsTy::iterator;
72 iterator begin() const { return ExprBindings.begin(); }
73 iterator end() const { return ExprBindings.end(); }
75 /// Fetches the current binding of the expression in the
77 SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const;
79 /// Profile - Profile the contents of an Environment object for use
81 static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) {
82 env->ExprBindings.Profile(ID);
85 /// Profile - Used to profile the contents of this object for inclusion
87 void Profile(llvm::FoldingSetNodeID& ID) const {
91 bool operator==(const Environment& RHS) const {
92 return ExprBindings == RHS.ExprBindings;
95 void print(raw_ostream &Out, const char *NL, const char *Sep,
96 const ASTContext &Context,
97 const LocationContext *WithLC = nullptr) const;
100 class EnvironmentManager {
102 using FactoryTy = Environment::BindingsTy::Factory;
107 EnvironmentManager(llvm::BumpPtrAllocator &Allocator) : F(Allocator) {}
109 Environment getInitialEnvironment() {
110 return Environment(F.getEmptyMap());
113 /// Bind a symbolic value to the given environment entry.
114 Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V,
117 Environment removeDeadBindings(Environment Env,
118 SymbolReaper &SymReaper,
119 ProgramStateRef state);
126 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H