clang 8.0.0

Environment.h
Go to the documentation of this file.
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_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
16
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"
21 #include <utility>
22
23 namespace clang {
24
25 class Stmt;
26
27 namespace ento {
28
29 class SValBuilder;
30 class SymbolReaper;
31
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
35 /// other things.
36 class EnvironmentEntry : public std::pair<const Stmt *,
37 const StackFrameContext *> {
38 public:
39 EnvironmentEntry(const Stmt *s, const LocationContext *L);
40
41 const Stmt *getStmt() const { return first; }
42 const LocationContext *getLocationContext() const { return second; }
43
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());
49 }
50
51 void Profile(llvm::FoldingSetNodeID &ID) const {
52 Profile(ID, *this);
53 }
54 };
55
56 /// An immutable map from EnvironemntEntries to SVals.
57 class Environment {
58 private:
59 friend class EnvironmentManager;
60
61 using BindingsTy = llvm::ImmutableMap<EnvironmentEntry, SVal>;
62
63 BindingsTy ExprBindings;
64
65 Environment(BindingsTy eb) : ExprBindings(eb) {}
66
67 SVal lookupExpr(const EnvironmentEntry &E) const;
68
69 public:
70 using iterator = BindingsTy::iterator;
71
72 iterator begin() const { return ExprBindings.begin(); }
73 iterator end() const { return ExprBindings.end(); }
74
75 /// Fetches the current binding of the expression in the
76 /// Environment.
77 SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const;
78
79 /// Profile - Profile the contents of an Environment object for use
80 /// in a FoldingSet.
81 static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) {
82 env->ExprBindings.Profile(ID);
83 }
84
85 /// Profile - Used to profile the contents of this object for inclusion
86 /// in a FoldingSet.
87 void Profile(llvm::FoldingSetNodeID& ID) const {
88 Profile(ID, this);
89 }
90
91 bool operator==(const Environment& RHS) const {
92 return ExprBindings == RHS.ExprBindings;
93 }
94
95 void print(raw_ostream &Out, const char *NL, const char *Sep,
96 const ASTContext &Context,
97 const LocationContext *WithLC = nullptr) const;
98 };
99
100 class EnvironmentManager {
101 private:
102 using FactoryTy = Environment::BindingsTy::Factory;
103
104 FactoryTy F;
105
106 public:
107 EnvironmentManager(llvm::BumpPtrAllocator &Allocator) : F(Allocator) {}
108
109 Environment getInitialEnvironment() {
110 return Environment(F.getEmptyMap());
111 }
112
113 /// Bind a symbolic value to the given environment entry.
114 Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V,
115 bool Invalidate);
116
117 Environment removeDeadBindings(Environment Env,
118 SymbolReaper &SymReaper,
119 ProgramStateRef state);
120 };
121
122 } // namespace ento
123
124 } // namespace clang
125
126 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
clang::Builtin::ID
ID
Definition: Builtins.h:49
ento
clang::ento::EnvironmentEntry::EnvironmentEntry
EnvironmentEntry(const Stmt *s, const LocationContext *L)
Definition: Environment.cpp:70
SVals.h
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::LocationContext
Definition: AnalysisDeclContext.h:218
clang::ento::Environment::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Profile - Used to profile the contents of this object for inclusion in a FoldingSet.
Definition: Environment.h:87
clang::ento::EnvironmentEntry::getStmt
const Stmt * getStmt() const
Definition: Environment.h:41
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
ProgramState_Fwd.h
clang::ento::EnvironmentEntry::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Environment.h:51
clang::CodeGen::state
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
Definition: CGOpenMPRuntime.h:748
clang::ento::EnvironmentManager
Definition: Environment.h:100
clang::ento::Environment::end
iterator end() const
Definition: Environment.h:73
clang::ento::Environment::begin
iterator begin() const
Definition: Environment.h:72
clang::ento::Environment::iterator
BindingsTy::iterator iterator
Definition: Environment.h:70
AnalysisDeclContext.h
clang::ento::EnvironmentEntry::Profile
static void Profile(llvm::FoldingSetNodeID &ID, const EnvironmentEntry &E)
Profile an EnvironmentEntry for inclusion in a FoldingSet.
Definition: Environment.h:45
clang::ento::EnvironmentManager::getInitialEnvironment
Environment getInitialEnvironment()
Definition: Environment.h:109
clang::ento::Environment::Profile
static void Profile(llvm::FoldingSetNodeID &ID, const Environment *env)
Profile - Profile the contents of an Environment object for use in a FoldingSet.
Definition: Environment.h:81
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:45
clang::ento::EnvironmentEntry
An entry in the environment consists of a Stmt and an LocationContext.
Definition: Environment.h:36
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:76
clang::ento::SymbolReaper
A class responsible for cleaning up unused symbols.
Definition: SymbolManager.h:549
clang::ento::Environment
An immutable map from EnvironemntEntries to SVals.
Definition: Environment.h:57
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::ento::SValBuilder
Definition: SValBuilder.h:53
clang::ento::EnvironmentManager::EnvironmentManager
EnvironmentManager(llvm::BumpPtrAllocator &Allocator)
Definition: Environment.h:107
clang::ento::Environment::operator==
bool operator==(const Environment &RHS) const
Definition: Environment.h:91
clang::ento::EnvironmentEntry::getLocationContext
const LocationContext * getLocationContext() const
Definition: Environment.h:42