clang 23.0.0git
Environment.h
Go to the documentation of this file.
1//===- Environment.h - Map from Stmt* to Locations/Values -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defined the Environment and EnvironmentManager classes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
14#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
15
19#include "llvm/ADT/ImmutableMap.h"
20#include <utility>
21
22namespace clang {
23
24class Stmt;
25
26namespace ento {
27
28class SValBuilder;
29class SymbolReaper;
30
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
34/// other things.
35/// FIXME: Use 'Expr' instead of 'Stmt' because associating a result with a
36/// non-expression statement does not make sense. Currently the environment
37/// only containts 'Expr's; and there is only one easy-to-eliminate hack in
38/// 'processCallExit' and 'Environment::getSVal' that constructs and handles
39/// 'EnvironmentEntry' instances with a 'ReturnStmt' as the 'first' part.
40class EnvironmentEntry : public std::pair<const Stmt *,
41 const StackFrameContext *> {
42public:
43 EnvironmentEntry(const Stmt *s, const LocationContext *L);
44
45 const Stmt *getStmt() const { return first; }
46 const LocationContext *getLocationContext() const { return second; }
47
48 /// Profile an EnvironmentEntry for inclusion in a FoldingSet.
49 static void Profile(llvm::FoldingSetNodeID &ID,
50 const EnvironmentEntry &E) {
51 ID.AddPointer(E.getStmt());
52 ID.AddPointer(E.getLocationContext());
53 }
54
55 void Profile(llvm::FoldingSetNodeID &ID) const {
56 Profile(ID, *this);
57 }
58};
59
60/// An immutable map from EnvironmentEntries to SVals.
61class Environment {
62private:
63 friend class EnvironmentManager;
64
65 using BindingsTy = llvm::ImmutableMap<EnvironmentEntry, SVal>;
66
67 BindingsTy ExprBindings;
68
69 Environment(BindingsTy eb) : ExprBindings(eb) {}
70
71 SVal lookupExpr(const EnvironmentEntry &E) const;
72
73public:
74 using iterator = BindingsTy::iterator;
75
76 iterator begin() const { return ExprBindings.begin(); }
77 iterator end() const { return ExprBindings.end(); }
78
79 /// Fetches the current binding of the expression in the
80 /// Environment.
81 SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const;
82
83 /// Profile - Profile the contents of an Environment object for use
84 /// in a FoldingSet.
85 static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) {
86 env->ExprBindings.Profile(ID);
87 }
88
89 /// Profile - Used to profile the contents of this object for inclusion
90 /// in a FoldingSet.
91 void Profile(llvm::FoldingSetNodeID& ID) const {
92 Profile(ID, this);
93 }
94
95 bool operator==(const Environment& RHS) const {
96 return ExprBindings == RHS.ExprBindings;
97 }
98
99 void printJson(raw_ostream &Out, const ASTContext &Ctx,
100 const LocationContext *LCtx = nullptr, const char *NL = "\n",
101 unsigned int Space = 0, bool IsDot = false) const;
102};
103
105private:
106 using FactoryTy = Environment::BindingsTy::Factory;
107
108 FactoryTy F;
109
110public:
111 EnvironmentManager(llvm::BumpPtrAllocator &Allocator) : F(Allocator) {}
112
114 return Environment(F.getEmptyMap());
115 }
116
117 /// Bind a symbolic value to the given environment entry.
119 bool Invalidate);
120
122 SymbolReaper &SymReaper,
123 ProgramStateRef state);
124};
125
126} // namespace ento
127
128} // namespace clang
129
130#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
#define V(N, I)
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
__device__ __2f16 float __ockl_bool s
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Stmt - This represents one statement.
Definition Stmt.h:86
An entry in the environment consists of a Stmt and an LocationContext.
Definition Environment.h:41
const Stmt * getStmt() const
Definition Environment.h:45
EnvironmentEntry(const Stmt *s, const LocationContext *L)
const LocationContext * getLocationContext() const
Definition Environment.h:46
void Profile(llvm::FoldingSetNodeID &ID) const
Definition Environment.h:55
static void Profile(llvm::FoldingSetNodeID &ID, const EnvironmentEntry &E)
Profile an EnvironmentEntry for inclusion in a FoldingSet.
Definition Environment.h:49
Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V, bool Invalidate)
Bind a symbolic value to the given environment entry.
Environment removeDeadBindings(Environment Env, SymbolReaper &SymReaper, ProgramStateRef state)
EnvironmentManager(llvm::BumpPtrAllocator &Allocator)
An immutable map from EnvironmentEntries to SVals.
Definition Environment.h:61
SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const
Fetches the current binding of the expression in the Environment.
void Profile(llvm::FoldingSetNodeID &ID) const
Profile - Used to profile the contents of this object for inclusion in a FoldingSet.
Definition Environment.h:91
bool operator==(const Environment &RHS) const
Definition Environment.h:95
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:85
friend class EnvironmentManager
Definition Environment.h:63
void printJson(raw_ostream &Out, const ASTContext &Ctx, const LocationContext *LCtx=nullptr, const char *NL="\n", unsigned int Space=0, bool IsDot=false) const
iterator end() const
Definition Environment.h:77
iterator begin() const
Definition Environment.h:76
BindingsTy::iterator iterator
Definition Environment.h:74
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:56
A class responsible for cleaning up unused symbols.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The JSON file list parser is used to communicate input to InstallAPI.