clang  14.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 
22 namespace clang {
23 
24 class Stmt;
25 
26 namespace ento {
27 
28 class SValBuilder;
29 class 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 class EnvironmentEntry : public std::pair<const Stmt *,
36  const StackFrameContext *> {
37 public:
38  EnvironmentEntry(const Stmt *s, const LocationContext *L);
39 
40  const Stmt *getStmt() const { return first; }
41  const LocationContext *getLocationContext() const { return second; }
42 
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());
48  }
49 
50  void Profile(llvm::FoldingSetNodeID &ID) const {
51  Profile(ID, *this);
52  }
53 };
54 
55 /// An immutable map from EnvironemntEntries to SVals.
56 class Environment {
57 private:
58  friend class EnvironmentManager;
59 
60  using BindingsTy = llvm::ImmutableMap<EnvironmentEntry, SVal>;
61 
62  BindingsTy ExprBindings;
63 
64  Environment(BindingsTy eb) : ExprBindings(eb) {}
65 
66  SVal lookupExpr(const EnvironmentEntry &E) const;
67 
68 public:
69  using iterator = BindingsTy::iterator;
70 
71  iterator begin() const { return ExprBindings.begin(); }
72  iterator end() const { return ExprBindings.end(); }
73 
74  /// Fetches the current binding of the expression in the
75  /// Environment.
76  SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const;
77 
78  /// Profile - Profile the contents of an Environment object for use
79  /// in a FoldingSet.
80  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) {
81  env->ExprBindings.Profile(ID);
82  }
83 
84  /// Profile - Used to profile the contents of this object for inclusion
85  /// in a FoldingSet.
86  void Profile(llvm::FoldingSetNodeID& ID) const {
87  Profile(ID, this);
88  }
89 
90  bool operator==(const Environment& RHS) const {
91  return ExprBindings == RHS.ExprBindings;
92  }
93 
94  void printJson(raw_ostream &Out, const ASTContext &Ctx,
95  const LocationContext *LCtx = nullptr, const char *NL = "\n",
96  unsigned int Space = 0, bool IsDot = false) const;
97 };
98 
100 private:
101  using FactoryTy = Environment::BindingsTy::Factory;
102 
103  FactoryTy F;
104 
105 public:
106  EnvironmentManager(llvm::BumpPtrAllocator &Allocator) : F(Allocator) {}
107 
109  return Environment(F.getEmptyMap());
110  }
111 
112  /// Bind a symbolic value to the given environment entry.
114  bool Invalidate);
115 
117  SymbolReaper &SymReaper,
119 };
120 
121 } // namespace ento
122 
123 } // namespace clang
124 
125 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H
clang::LocationContext
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Definition: AnalysisDeclContext.h:215
SVals.h
clang::ento::EnvironmentEntry::getLocationContext
const LocationContext * getLocationContext() const
Definition: Environment.h:41
clang::ento::EnvironmentEntry::Profile
static void Profile(llvm::FoldingSetNodeID &ID, const EnvironmentEntry &E)
Profile an EnvironmentEntry for inclusion in a FoldingSet.
Definition: Environment.h:44
AnalysisDeclContext.h
clang::ento::EnvironmentManager::EnvironmentManager
EnvironmentManager(llvm::BumpPtrAllocator &Allocator)
Definition: Environment.h:106
clang::ento::EnvironmentManager::bindExpr
Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V, bool Invalidate)
Bind a symbolic value to the given environment entry.
Definition: Environment.cpp:136
clang::ento::EnvironmentEntry::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Environment.h:50
ProgramState_Fwd.h
clang::ento::EnvironmentManager
Definition: Environment.h:99
V
#define V(N, I)
Definition: ASTContext.h:3121
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::ento::Environment::begin
iterator begin() const
Definition: Environment.h:71
clang::ento::Environment::iterator
BindingsTy::iterator iterator
Definition: Environment.h:69
clang::ento::Environment::end
iterator end() const
Definition: Environment.h:72
clang::ento::SymbolReaper
A class responsible for cleaning up unused symbols.
Definition: SymbolManager.h:505
clang::ento::Environment::getSVal
SVal getSVal(const EnvironmentEntry &E, SValBuilder &svalBuilder) const
Fetches the current binding of the expression in the Environment.
Definition: Environment.cpp:86
clang::ento::EnvironmentEntry::getStmt
const Stmt * getStmt() const
Definition: Environment.h:40
state
and static some checkers Checker The latter are built on top of the former via the Checker and CheckerVisitor and attempts to isolate them from much of the gore of the internal analysis the analyzer is basically a source code simulator that traces out possible paths of execution The state of the and the combination of state and program point is a node in an exploded which has the entry program point and initial state
Definition: README.txt:30
clang::ento::EnvironmentManager::removeDeadBindings
Environment removeDeadBindings(Environment Env, SymbolReaper &SymReaper, ProgramStateRef state)
Definition: Environment.cpp:178
clang::ento::SValBuilder
Definition: SValBuilder.h:53
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:80
clang::ento::EnvironmentEntry::EnvironmentEntry
EnvironmentEntry(const Stmt *s, const LocationContext *L)
Definition: Environment.cpp:71
clang::Builtin::ID
ID
Definition: Builtins.h:48
clang
Definition: CalledOnceCheck.h:17
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:86
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::ento::Environment::printJson
void printJson(raw_ostream &Out, const ASTContext &Ctx, const LocationContext *LCtx=nullptr, const char *NL="\n", unsigned int Space=0, bool IsDot=false) const
Definition: Environment.cpp:215
s
__device__ __2f16 float bool s
Definition: __clang_hip_libdevice_declares.h:315
clang::ento::SVal
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:75
clang::ento::Environment
An immutable map from EnvironemntEntries to SVals.
Definition: Environment.h:56
clang::ento::EnvironmentManager::getInitialEnvironment
Environment getInitialEnvironment()
Definition: Environment.h:108
clang::ento::Environment::operator==
bool operator==(const Environment &RHS) const
Definition: Environment.h:90
clang::ento::EnvironmentEntry
An entry in the environment consists of a Stmt and an LocationContext.
Definition: Environment.h:35
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47