Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/Environment.h
Line
Count
Source
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
16
#include "clang/Analysis/AnalysisDeclContext.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
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
7.29M
  const Stmt *getStmt() const { return first; }
41
7.29M
  const LocationContext *getLocationContext() const { return second; }
42
43
  /// Profile an EnvironmentEntry for inclusion in a FoldingSet.
44
  static void Profile(llvm::FoldingSetNodeID &ID,
45
3.99M
                      const EnvironmentEntry &E) {
46
3.99M
    ID.AddPointer(E.getStmt());
47
3.99M
    ID.AddPointer(E.getLocationContext());
48
3.99M
  }
49
50
3.99M
  void Profile(llvm::FoldingSetNodeID &ID) const {
51
3.99M
    Profile(ID, *this);
52
3.99M
  }
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
1.09M
  Environment(BindingsTy eb) : ExprBindings(eb) {}
65
66
  SVal lookupExpr(const EnvironmentEntry &E) const;
67
68
public:
69
  using iterator = BindingsTy::iterator;
70
71
322k
  iterator begin() const { return ExprBindings.begin(); }
72
322k
  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
4.75M
  static void Profile(llvm::FoldingSetNodeID& ID, const Environment* env) {
81
4.75M
    env->ExprBindings.Profile(ID);
82
4.75M
  }
83
84
  /// Profile - Used to profile the contents of this object for inclusion
85
  ///  in a FoldingSet.
86
4.75M
  void Profile(llvm::FoldingSetNodeID& ID) const {
87
4.75M
    Profile(ID, this);
88
4.75M
  }
89
90
761k
  bool operator==(const Environment& RHS) const {
91
761k
    return ExprBindings == RHS.ExprBindings;
92
761k
  }
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
99
class EnvironmentManager {
100
private:
101
  using FactoryTy = Environment::BindingsTy::Factory;
102
103
  FactoryTy F;
104
105
public:
106
10.8k
  EnvironmentManager(llvm::BumpPtrAllocator &Allocator) : F(Allocator) {}
107
108
331k
  Environment getInitialEnvironment() {
109
331k
    return Environment(F.getEmptyMap());
110
331k
  }
111
112
  /// Bind a symbolic value to the given environment entry.
113
  Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V,
114
                       bool Invalidate);
115
116
  Environment removeDeadBindings(Environment Env,
117
                                 SymbolReaper &SymReaper,
118
                                 ProgramStateRef state);
119
};
120
121
} // namespace ento
122
123
} // namespace clang
124
125
#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_ENVIRONMENT_H