Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/MemRegion.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MemRegion.cpp - Abstract memory regions for static analysis --------===//
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 defines MemRegion and its subclasses.  MemRegion defines a
10
//  partially-typed abstraction of memory useful for path-sensitive dataflow
11
//  analyses.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/AST/Attr.h"
18
#include "clang/AST/CharUnits.h"
19
#include "clang/AST/Decl.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclObjC.h"
22
#include "clang/AST/Expr.h"
23
#include "clang/AST/PrettyPrinter.h"
24
#include "clang/AST/RecordLayout.h"
25
#include "clang/AST/Type.h"
26
#include "clang/Analysis/AnalysisDeclContext.h"
27
#include "clang/Analysis/Support/BumpVector.h"
28
#include "clang/Basic/IdentifierTable.h"
29
#include "clang/Basic/LLVM.h"
30
#include "clang/Basic/SourceManager.h"
31
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
32
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
33
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
34
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
35
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
36
#include "llvm/ADT/APInt.h"
37
#include "llvm/ADT/FoldingSet.h"
38
#include "llvm/ADT/PointerUnion.h"
39
#include "llvm/ADT/SmallString.h"
40
#include "llvm/ADT/StringRef.h"
41
#include "llvm/ADT/Twine.h"
42
#include "llvm/ADT/iterator_range.h"
43
#include "llvm/Support/Allocator.h"
44
#include "llvm/Support/Casting.h"
45
#include "llvm/Support/CheckedArithmetic.h"
46
#include "llvm/Support/Compiler.h"
47
#include "llvm/Support/Debug.h"
48
#include "llvm/Support/ErrorHandling.h"
49
#include "llvm/Support/raw_ostream.h"
50
#include <cassert>
51
#include <cstdint>
52
#include <functional>
53
#include <iterator>
54
#include <optional>
55
#include <string>
56
#include <tuple>
57
#include <utility>
58
59
using namespace clang;
60
using namespace ento;
61
62
#define DEBUG_TYPE "MemRegion"
63
64
//===----------------------------------------------------------------------===//
65
// MemRegion Construction.
66
//===----------------------------------------------------------------------===//
67
68
template <typename RegionTy, typename SuperTy, typename Arg1Ty>
69
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1,
70
872k
                                         const SuperTy *superRegion) {
71
872k
  llvm::FoldingSetNodeID ID;
72
872k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
872k
  void *InsertPos;
74
872k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
872k
  if (!R) {
77
128k
    R = new (A) RegionTy(arg1, superRegion);
78
128k
    Regions.InsertNode(R, InsertPos);
79
128k
  }
80
81
872k
  return R;
82
872k
}
clang::ento::StringRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::StringRegion, clang::ento::GlobalInternalSpaceRegion, clang::StringLiteral const*>(clang::StringLiteral const*, clang::ento::GlobalInternalSpaceRegion const*)
Line
Count
Source
70
12.1k
                                         const SuperTy *superRegion) {
71
12.1k
  llvm::FoldingSetNodeID ID;
72
12.1k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
12.1k
  void *InsertPos;
74
12.1k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
12.1k
  if (!R) {
77
5.35k
    R = new (A) RegionTy(arg1, superRegion);
78
5.35k
    Regions.InsertNode(R, InsertPos);
79
5.35k
  }
80
81
12.1k
  return R;
82
12.1k
}
clang::ento::ObjCStringRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::ObjCStringRegion, clang::ento::GlobalInternalSpaceRegion, clang::ObjCStringLiteral const*>(clang::ObjCStringLiteral const*, clang::ento::GlobalInternalSpaceRegion const*)
Line
Count
Source
70
1.12k
                                         const SuperTy *superRegion) {
71
1.12k
  llvm::FoldingSetNodeID ID;
72
1.12k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
1.12k
  void *InsertPos;
74
1.12k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
1.12k
  if (!R) {
77
288
    R = new (A) RegionTy(arg1, superRegion);
78
288
    Regions.InsertNode(R, InsertPos);
79
288
  }
80
81
1.12k
  return R;
82
1.12k
}
clang::ento::NonParamVarRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::NonParamVarRegion, clang::ento::MemRegion, clang::VarDecl const*>(clang::VarDecl const*, clang::ento::MemRegion const*)
Line
Count
Source
70
277k
                                         const SuperTy *superRegion) {
71
277k
  llvm::FoldingSetNodeID ID;
72
277k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
277k
  void *InsertPos;
74
277k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
277k
  if (!R) {
77
34.9k
    R = new (A) RegionTy(arg1, superRegion);
78
34.9k
    Regions.InsertNode(R, InsertPos);
79
34.9k
  }
80
81
277k
  return R;
82
277k
}
clang::ento::CompoundLiteralRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CompoundLiteralRegion, clang::ento::MemSpaceRegion, clang::CompoundLiteralExpr const*>(clang::CompoundLiteralExpr const*, clang::ento::MemSpaceRegion const*)
Line
Count
Source
70
63
                                         const SuperTy *superRegion) {
71
63
  llvm::FoldingSetNodeID ID;
72
63
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
63
  void *InsertPos;
74
63
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
63
  if (!R) {
77
59
    R = new (A) RegionTy(arg1, superRegion);
78
59
    Regions.InsertNode(R, InsertPos);
79
59
  }
80
81
63
  return R;
82
63
}
clang::ento::FunctionCodeRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::FunctionCodeRegion, clang::ento::CodeSpaceRegion, clang::NamedDecl const*>(clang::NamedDecl const*, clang::ento::CodeSpaceRegion const*)
Line
Count
Source
70
82.0k
                                         const SuperTy *superRegion) {
71
82.0k
  llvm::FoldingSetNodeID ID;
72
82.0k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
82.0k
  void *InsertPos;
74
82.0k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
82.0k
  if (!R) {
77
30.5k
    R = new (A) RegionTy(arg1, superRegion);
78
30.5k
    Regions.InsertNode(R, InsertPos);
79
30.5k
  }
80
81
82.0k
  return R;
82
82.0k
}
clang::ento::SymbolicRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::SymbolicRegion, clang::ento::MemSpaceRegion, clang::ento::SymExpr const*>(clang::ento::SymExpr const*, clang::ento::MemSpaceRegion const*)
Line
Count
Source
70
298k
                                         const SuperTy *superRegion) {
71
298k
  llvm::FoldingSetNodeID ID;
72
298k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
298k
  void *InsertPos;
74
298k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
298k
  if (!R) {
77
18.8k
    R = new (A) RegionTy(arg1, superRegion);
78
18.8k
    Regions.InsertNode(R, InsertPos);
79
18.8k
  }
80
81
298k
  return R;
82
298k
}
clang::ento::SymbolicRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::SymbolicRegion, clang::ento::HeapSpaceRegion, clang::ento::SymExpr const*>(clang::ento::SymExpr const*, clang::ento::HeapSpaceRegion const*)
Line
Count
Source
70
1.74k
                                         const SuperTy *superRegion) {
71
1.74k
  llvm::FoldingSetNodeID ID;
72
1.74k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
1.74k
  void *InsertPos;
74
1.74k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
1.74k
  if (!R) {
77
1.73k
    R = new (A) RegionTy(arg1, superRegion);
78
1.73k
    Regions.InsertNode(R, InsertPos);
79
1.73k
  }
80
81
1.74k
  return R;
82
1.74k
}
clang::ento::FieldRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::FieldRegion, clang::ento::SubRegion, clang::FieldDecl const*>(clang::FieldDecl const*, clang::ento::SubRegion const*)
Line
Count
Source
70
113k
                                         const SuperTy *superRegion) {
71
113k
  llvm::FoldingSetNodeID ID;
72
113k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
113k
  void *InsertPos;
74
113k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
113k
  if (!R) {
77
18.6k
    R = new (A) RegionTy(arg1, superRegion);
78
18.6k
    Regions.InsertNode(R, InsertPos);
79
18.6k
  }
80
81
113k
  return R;
82
113k
}
clang::ento::ObjCIvarRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::ObjCIvarRegion, clang::ento::SubRegion, clang::ObjCIvarDecl const*>(clang::ObjCIvarDecl const*, clang::ento::SubRegion const*)
Line
Count
Source
70
1.46k
                                         const SuperTy *superRegion) {
71
1.46k
  llvm::FoldingSetNodeID ID;
72
1.46k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
1.46k
  void *InsertPos;
74
1.46k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
1.46k
  if (!R) {
77
663
    R = new (A) RegionTy(arg1, superRegion);
78
663
    Regions.InsertNode(R, InsertPos);
79
663
  }
80
81
1.46k
  return R;
82
1.46k
}
clang::ento::CXXTempObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXTempObjectRegion, clang::ento::StackLocalsSpaceRegion, clang::Expr const*>(clang::Expr const*, clang::ento::StackLocalsSpaceRegion const*)
Line
Count
Source
70
3.87k
                                         const SuperTy *superRegion) {
71
3.87k
  llvm::FoldingSetNodeID ID;
72
3.87k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
3.87k
  void *InsertPos;
74
3.87k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
3.87k
  if (!R) {
77
2.75k
    R = new (A) RegionTy(arg1, superRegion);
78
2.75k
    Regions.InsertNode(R, InsertPos);
79
2.75k
  }
80
81
3.87k
  return R;
82
3.87k
}
clang::ento::CXXDerivedObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXDerivedObjectRegion, clang::ento::SubRegion, clang::CXXRecordDecl const*>(clang::CXXRecordDecl const*, clang::ento::SubRegion const*)
Line
Count
Source
70
25
                                         const SuperTy *superRegion) {
71
25
  llvm::FoldingSetNodeID ID;
72
25
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
25
  void *InsertPos;
74
25
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
25
  if (!R) {
77
24
    R = new (A) RegionTy(arg1, superRegion);
78
24
    Regions.InsertNode(R, InsertPos);
79
24
  }
80
81
25
  return R;
82
25
}
clang::ento::CXXThisRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXThisRegion, clang::ento::StackArgumentsSpaceRegion, clang::PointerType const*>(clang::PointerType const*, clang::ento::StackArgumentsSpaceRegion const*)
Line
Count
Source
70
80.2k
                                         const SuperTy *superRegion) {
71
80.2k
  llvm::FoldingSetNodeID ID;
72
80.2k
  RegionTy::ProfileRegion(ID, arg1, superRegion);
73
80.2k
  void *InsertPos;
74
80.2k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
75
76
80.2k
  if (!R) {
77
14.5k
    R = new (A) RegionTy(arg1, superRegion);
78
14.5k
    Regions.InsertNode(R, InsertPos);
79
14.5k
  }
80
81
80.2k
  return R;
82
80.2k
}
83
84
template <typename RegionTy, typename SuperTy, typename Arg1Ty, typename Arg2Ty>
85
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
86
87.8k
                                         const SuperTy *superRegion) {
87
87.8k
  llvm::FoldingSetNodeID ID;
88
87.8k
  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
89
87.8k
  void *InsertPos;
90
87.8k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
91
92
87.8k
  if (!R) {
93
20.9k
    R = new (A) RegionTy(arg1, arg2, superRegion);
94
20.9k
    Regions.InsertNode(R, InsertPos);
95
20.9k
  }
96
97
87.8k
  return R;
98
87.8k
}
clang::ento::ParamVarRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::ParamVarRegion, clang::ento::StackArgumentsSpaceRegion, clang::Expr const*, unsigned int>(clang::Expr const*, unsigned int, clang::ento::StackArgumentsSpaceRegion const*)
Line
Count
Source
86
85.2k
                                         const SuperTy *superRegion) {
87
85.2k
  llvm::FoldingSetNodeID ID;
88
85.2k
  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
89
85.2k
  void *InsertPos;
90
85.2k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
91
92
85.2k
  if (!R) {
93
19.3k
    R = new (A) RegionTy(arg1, arg2, superRegion);
94
19.3k
    Regions.InsertNode(R, InsertPos);
95
19.3k
  }
96
97
85.2k
  return R;
98
85.2k
}
clang::ento::CXXLifetimeExtendedObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXLifetimeExtendedObjectRegion, clang::ento::StackLocalsSpaceRegion, clang::Expr const*, clang::ValueDecl const*>(clang::Expr const*, clang::ValueDecl const*, clang::ento::StackLocalsSpaceRegion const*)
Line
Count
Source
86
339
                                         const SuperTy *superRegion) {
87
339
  llvm::FoldingSetNodeID ID;
88
339
  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
89
339
  void *InsertPos;
90
339
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
91
92
339
  if (!R) {
93
321
    R = new (A) RegionTy(arg1, arg2, superRegion);
94
321
    Regions.InsertNode(R, InsertPos);
95
321
  }
96
97
339
  return R;
98
339
}
clang::ento::CXXLifetimeExtendedObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXLifetimeExtendedObjectRegion, clang::ento::GlobalsSpaceRegion, clang::Expr const*, clang::ValueDecl const*>(clang::Expr const*, clang::ValueDecl const*, clang::ento::GlobalsSpaceRegion const*)
Line
Count
Source
86
15
                                         const SuperTy *superRegion) {
87
15
  llvm::FoldingSetNodeID ID;
88
15
  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
89
15
  void *InsertPos;
90
15
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
91
92
15
  if (!R) {
93
15
    R = new (A) RegionTy(arg1, arg2, superRegion);
94
15
    Regions.InsertNode(R, InsertPos);
95
15
  }
96
97
15
  return R;
98
15
}
clang::ento::CXXBaseObjectRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::CXXBaseObjectRegion, clang::ento::SubRegion, clang::CXXRecordDecl const*, bool>(clang::CXXRecordDecl const*, bool, clang::ento::SubRegion const*)
Line
Count
Source
86
2.10k
                                         const SuperTy *superRegion) {
87
2.10k
  llvm::FoldingSetNodeID ID;
88
2.10k
  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
89
2.10k
  void *InsertPos;
90
2.10k
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
91
92
2.10k
  if (!R) {
93
1.12k
    R = new (A) RegionTy(arg1, arg2, superRegion);
94
1.12k
    Regions.InsertNode(R, InsertPos);
95
1.12k
  }
96
97
2.10k
  return R;
98
2.10k
}
clang::ento::AllocaRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::AllocaRegion, clang::ento::StackLocalsSpaceRegion, clang::Expr const*, unsigned int>(clang::Expr const*, unsigned int, clang::ento::StackLocalsSpaceRegion const*)
Line
Count
Source
86
93
                                         const SuperTy *superRegion) {
87
93
  llvm::FoldingSetNodeID ID;
88
93
  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
89
93
  void *InsertPos;
90
93
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
91
92
93
  if (!R) {
93
93
    R = new (A) RegionTy(arg1, arg2, superRegion);
94
93
    Regions.InsertNode(R, InsertPos);
95
93
  }
96
97
93
  return R;
98
93
}
99
100
template <typename RegionTy, typename SuperTy,
101
          typename Arg1Ty, typename Arg2Ty, typename Arg3Ty>
102
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
103
                                         const Arg3Ty arg3,
104
809
                                         const SuperTy *superRegion) {
105
809
  llvm::FoldingSetNodeID ID;
106
809
  RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
107
809
  void *InsertPos;
108
809
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
109
110
809
  if (!R) {
111
761
    R = new (A) RegionTy(arg1, arg2, arg3, superRegion);
112
761
    Regions.InsertNode(R, InsertPos);
113
761
  }
114
115
809
  return R;
116
809
}
clang::ento::BlockDataRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::BlockDataRegion, clang::ento::MemSpaceRegion, clang::ento::BlockCodeRegion const*, clang::LocationContext const*, unsigned int>(clang::ento::BlockCodeRegion const*, clang::LocationContext const*, unsigned int, clang::ento::MemSpaceRegion const*)
Line
Count
Source
104
398
                                         const SuperTy *superRegion) {
105
398
  llvm::FoldingSetNodeID ID;
106
398
  RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
107
398
  void *InsertPos;
108
398
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
109
110
398
  if (!R) {
111
384
    R = new (A) RegionTy(arg1, arg2, arg3, superRegion);
112
384
    Regions.InsertNode(R, InsertPos);
113
384
  }
114
115
398
  return R;
116
398
}
clang::ento::BlockCodeRegion* clang::ento::MemRegionManager::getSubRegion<clang::ento::BlockCodeRegion, clang::ento::CodeSpaceRegion, clang::BlockDecl const*, clang::CanQual<clang::Type>, clang::AnalysisDeclContext*>(clang::BlockDecl const*, clang::CanQual<clang::Type>, clang::AnalysisDeclContext*, clang::ento::CodeSpaceRegion const*)
Line
Count
Source
104
411
                                         const SuperTy *superRegion) {
105
411
  llvm::FoldingSetNodeID ID;
106
411
  RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
107
411
  void *InsertPos;
108
411
  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
109
110
411
  if (!R) {
111
377
    R = new (A) RegionTy(arg1, arg2, arg3, superRegion);
112
377
    Regions.InsertNode(R, InsertPos);
113
377
  }
114
115
411
  return R;
116
411
}
117
118
//===----------------------------------------------------------------------===//
119
// Object destruction.
120
//===----------------------------------------------------------------------===//
121
122
0
MemRegion::~MemRegion() = default;
123
124
// All regions and their data are BumpPtrAllocated.  No need to call their
125
// destructors.
126
16.2k
MemRegionManager::~MemRegionManager() = default;
127
128
//===----------------------------------------------------------------------===//
129
// Basic methods.
130
//===----------------------------------------------------------------------===//
131
132
9.45k
bool SubRegion::isSubRegionOf(const MemRegion* R) const {
133
9.45k
  const MemRegion* r = this;
134
15.6k
  do {
135
15.6k
    if (r == R)
136
5.05k
      return true;
137
10.5k
    if (const auto *sr = dyn_cast<SubRegion>(r))
138
6.14k
      r = sr->getSuperRegion();
139
4.40k
    else
140
4.40k
      break;
141
10.5k
  } while (
r != nullptr6.14k
);
142
4.40k
  return false;
143
9.45k
}
144
145
1.71M
MemRegionManager &SubRegion::getMemRegionManager() const {
146
1.71M
  const SubRegion* r = this;
147
2.04M
  do {
148
2.04M
    const MemRegion *superRegion = r->getSuperRegion();
149
2.04M
    if (const auto *sr = dyn_cast<SubRegion>(superRegion)) {
150
328k
      r = sr;
151
328k
      continue;
152
328k
    }
153
1.71M
    return superRegion->getMemRegionManager();
154
2.04M
  } while (
true328k
);
155
1.71M
}
156
157
2.09M
const StackFrameContext *VarRegion::getStackFrame() const {
158
2.09M
  const auto *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
159
2.09M
  return SSR ? 
SSR->getStackFrame()2.00M
:
nullptr93.5k
;
160
2.09M
}
161
162
const StackFrameContext *
163
0
CXXLifetimeExtendedObjectRegion::getStackFrame() const {
164
0
  const auto *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
165
0
  return SSR ? SSR->getStackFrame() : nullptr;
166
0
}
167
168
0
const StackFrameContext *CXXTempObjectRegion::getStackFrame() const {
169
0
  assert(isa<StackSpaceRegion>(getMemorySpace()) &&
170
0
         "A temporary object can only be allocated on the stack");
171
0
  return cast<StackSpaceRegion>(getMemorySpace())->getStackFrame();
172
0
}
173
174
ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg)
175
663
    : DeclRegion(sReg, ObjCIvarRegionKind), IVD(ivd) {
176
663
  assert(IVD);
177
663
}
178
179
22.1k
const ObjCIvarDecl *ObjCIvarRegion::getDecl() const { return IVD; }
180
181
20.1k
QualType ObjCIvarRegion::getValueType() const {
182
20.1k
  return getDecl()->getType();
183
20.1k
}
184
185
7.25k
QualType CXXBaseObjectRegion::getValueType() const {
186
7.25k
  return QualType(getDecl()->getTypeForDecl(), 0);
187
7.25k
}
188
189
521
QualType CXXDerivedObjectRegion::getValueType() const {
190
521
  return QualType(getDecl()->getTypeForDecl(), 0);
191
521
}
192
193
336k
QualType ParamVarRegion::getValueType() const {
194
336k
  assert(getDecl() &&
195
336k
         "`ParamVarRegion` support functions without `Decl` not implemented"
196
336k
         " yet.");
197
336k
  return getDecl()->getType();
198
336k
}
199
200
780k
const ParmVarDecl *ParamVarRegion::getDecl() const {
201
780k
  const Decl *D = getStackFrame()->getDecl();
202
203
780k
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
204
776k
    assert(Index < FD->param_size());
205
776k
    return FD->parameters()[Index];
206
776k
  } else 
if (const auto *3.85k
BD3.85k
= dyn_cast<BlockDecl>(D)) {
207
1.22k
    assert(Index < BD->param_size());
208
1.22k
    return BD->parameters()[Index];
209
2.63k
  } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
210
2.63k
    assert(Index < MD->param_size());
211
2.63k
    return MD->parameters()[Index];
212
2.63k
  } else 
if (const auto *0
CD0
= dyn_cast<CXXConstructorDecl>(D)) {
213
0
    assert(Index < CD->param_size());
214
0
    return CD->parameters()[Index];
215
0
  } else {
216
0
    llvm_unreachable("Unexpected Decl kind!");
217
0
  }
218
780k
}
219
220
//===----------------------------------------------------------------------===//
221
// FoldingSet profiling.
222
//===----------------------------------------------------------------------===//
223
224
0
void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
225
0
  ID.AddInteger(static_cast<unsigned>(getKind()));
226
0
}
227
228
0
void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
229
0
  ID.AddInteger(static_cast<unsigned>(getKind()));
230
0
  ID.AddPointer(getStackFrame());
231
0
}
232
233
0
void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
234
0
  ID.AddInteger(static_cast<unsigned>(getKind()));
235
0
  ID.AddPointer(getCodeRegion());
236
0
}
237
238
void StringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
239
                                 const StringLiteral *Str,
240
20.7k
                                 const MemRegion *superRegion) {
241
20.7k
  ID.AddInteger(static_cast<unsigned>(StringRegionKind));
242
20.7k
  ID.AddPointer(Str);
243
20.7k
  ID.AddPointer(superRegion);
244
20.7k
}
245
246
void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
247
                                     const ObjCStringLiteral *Str,
248
2.04k
                                     const MemRegion *superRegion) {
249
2.04k
  ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind));
250
2.04k
  ID.AddPointer(Str);
251
2.04k
  ID.AddPointer(superRegion);
252
2.04k
}
253
254
void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
255
                                 const Expr *Ex, unsigned cnt,
256
153
                                 const MemRegion *superRegion) {
257
153
  ID.AddInteger(static_cast<unsigned>(AllocaRegionKind));
258
153
  ID.AddPointer(Ex);
259
153
  ID.AddInteger(cnt);
260
153
  ID.AddPointer(superRegion);
261
153
}
262
263
60
void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
264
60
  ProfileRegion(ID, Ex, Cnt, superRegion);
265
60
}
266
267
9
void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
268
9
  CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
269
9
}
270
271
void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
272
                                          const CompoundLiteralExpr *CL,
273
72
                                          const MemRegion* superRegion) {
274
72
  ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind));
275
72
  ID.AddPointer(CL);
276
72
  ID.AddPointer(superRegion);
277
72
}
278
279
void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
280
                                  const PointerType *PT,
281
165k
                                  const MemRegion *sRegion) {
282
165k
  ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind));
283
165k
  ID.AddPointer(PT);
284
165k
  ID.AddPointer(sRegion);
285
165k
}
286
287
84.9k
void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
288
84.9k
  CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
289
84.9k
}
290
291
160k
void FieldRegion::Profile(llvm::FoldingSetNodeID &ID) const {
292
160k
  ProfileRegion(ID, getDecl(), superRegion);
293
160k
}
294
295
void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
296
                                   const ObjCIvarDecl *ivd,
297
2.49k
                                   const MemRegion* superRegion) {
298
2.49k
  ID.AddInteger(static_cast<unsigned>(ObjCIvarRegionKind));
299
2.49k
  ID.AddPointer(ivd);
300
2.49k
  ID.AddPointer(superRegion);
301
2.49k
}
302
303
1.02k
void ObjCIvarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
304
1.02k
  ProfileRegion(ID, getDecl(), superRegion);
305
1.02k
}
306
307
void NonParamVarRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
308
                                      const VarDecl *VD,
309
552k
                                      const MemRegion *superRegion) {
310
552k
  ID.AddInteger(static_cast<unsigned>(NonParamVarRegionKind));
311
552k
  ID.AddPointer(VD);
312
552k
  ID.AddPointer(superRegion);
313
552k
}
314
315
274k
void NonParamVarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
316
274k
  ProfileRegion(ID, getDecl(), superRegion);
317
274k
}
318
319
void ParamVarRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, const Expr *OE,
320
213k
                                   unsigned Idx, const MemRegion *SReg) {
321
213k
  ID.AddInteger(static_cast<unsigned>(ParamVarRegionKind));
322
213k
  ID.AddPointer(OE);
323
213k
  ID.AddInteger(Idx);
324
213k
  ID.AddPointer(SReg);
325
213k
}
326
327
127k
void ParamVarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
328
127k
  ProfileRegion(ID, getOriginExpr(), getIndex(), superRegion);
329
127k
}
330
331
void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
332
601k
                                   const MemRegion *sreg) {
333
601k
  ID.AddInteger(static_cast<unsigned>(MemRegion::SymbolicRegionKind));
334
601k
  ID.Add(sym);
335
601k
  ID.AddPointer(sreg);
336
601k
}
337
338
301k
void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
339
301k
  SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
340
301k
}
341
342
void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
343
                                  QualType ElementType, SVal Idx,
344
259k
                                  const MemRegion* superRegion) {
345
259k
  ID.AddInteger(MemRegion::ElementRegionKind);
346
259k
  ID.Add(ElementType);
347
259k
  ID.AddPointer(superRegion);
348
259k
  Idx.Profile(ID);
349
259k
}
350
351
190k
void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
352
190k
  ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
353
190k
}
354
355
void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
356
                                       const NamedDecl *FD,
357
147k
                                       const MemRegion*) {
358
147k
  ID.AddInteger(MemRegion::FunctionCodeRegionKind);
359
147k
  ID.AddPointer(FD);
360
147k
}
361
362
65.9k
void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
363
65.9k
  FunctionCodeRegion::ProfileRegion(ID, FD, superRegion);
364
65.9k
}
365
366
void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
367
                                    const BlockDecl *BD, CanQualType,
368
                                    const AnalysisDeclContext *AC,
369
497
                                    const MemRegion*) {
370
497
  ID.AddInteger(MemRegion::BlockCodeRegionKind);
371
497
  ID.AddPointer(BD);
372
497
}
373
374
86
void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
375
86
  BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
376
86
}
377
378
void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
379
                                    const BlockCodeRegion *BC,
380
                                    const LocationContext *LC,
381
                                    unsigned BlkCount,
382
447
                                    const MemRegion *sReg) {
383
447
  ID.AddInteger(MemRegion::BlockDataRegionKind);
384
447
  ID.AddPointer(BC);
385
447
  ID.AddPointer(LC);
386
447
  ID.AddInteger(BlkCount);
387
447
  ID.AddPointer(sReg);
388
447
}
389
390
49
void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
391
49
  BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion());
392
49
}
393
394
void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
395
                                        Expr const *Ex,
396
8.61k
                                        const MemRegion *sReg) {
397
8.61k
  ID.AddPointer(Ex);
398
8.61k
  ID.AddPointer(sReg);
399
8.61k
}
400
401
4.73k
void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
402
4.73k
  ProfileRegion(ID, Ex, getSuperRegion());
403
4.73k
}
404
405
void CXXLifetimeExtendedObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
406
                                                    const Expr *E,
407
                                                    const ValueDecl *D,
408
482
                                                    const MemRegion *sReg) {
409
482
  ID.AddPointer(E);
410
482
  ID.AddPointer(D);
411
482
  ID.AddPointer(sReg);
412
482
}
413
414
void CXXLifetimeExtendedObjectRegion::Profile(
415
128
    llvm::FoldingSetNodeID &ID) const {
416
128
  ProfileRegion(ID, Ex, ExD, getSuperRegion());
417
128
}
418
419
void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
420
                                        const CXXRecordDecl *RD,
421
                                        bool IsVirtual,
422
3.48k
                                        const MemRegion *SReg) {
423
3.48k
  ID.AddPointer(RD);
424
3.48k
  ID.AddBoolean(IsVirtual);
425
3.48k
  ID.AddPointer(SReg);
426
3.48k
}
427
428
1.38k
void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
429
1.38k
  ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
430
1.38k
}
431
432
void CXXDerivedObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
433
                                           const CXXRecordDecl *RD,
434
31
                                           const MemRegion *SReg) {
435
31
  ID.AddPointer(RD);
436
31
  ID.AddPointer(SReg);
437
31
}
438
439
6
void CXXDerivedObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
440
6
  ProfileRegion(ID, getDecl(), superRegion);
441
6
}
442
443
//===----------------------------------------------------------------------===//
444
// Region anchors.
445
//===----------------------------------------------------------------------===//
446
447
0
void GlobalsSpaceRegion::anchor() {}
448
449
0
void NonStaticGlobalSpaceRegion::anchor() {}
450
451
0
void StackSpaceRegion::anchor() {}
452
453
0
void TypedRegion::anchor() {}
454
455
0
void TypedValueRegion::anchor() {}
456
457
0
void CodeTextRegion::anchor() {}
458
459
0
void SubRegion::anchor() {}
460
461
//===----------------------------------------------------------------------===//
462
// Region pretty-printing.
463
//===----------------------------------------------------------------------===//
464
465
0
LLVM_DUMP_METHOD void MemRegion::dump() const {
466
0
  dumpToStream(llvm::errs());
467
0
}
468
469
482
std::string MemRegion::getString() const {
470
482
  std::string s;
471
482
  llvm::raw_string_ostream os(s);
472
482
  dumpToStream(os);
473
482
  return s;
474
482
}
475
476
0
void MemRegion::dumpToStream(raw_ostream &os) const {
477
0
  os << "<Unknown Region>";
478
0
}
479
480
0
void AllocaRegion::dumpToStream(raw_ostream &os) const {
481
0
  os << "alloca{S" << Ex->getID(getContext()) << ',' << Cnt << '}';
482
0
}
483
484
72
void FunctionCodeRegion::dumpToStream(raw_ostream &os) const {
485
72
  os << "code{" << getDecl()->getDeclName().getAsString() << '}';
486
72
}
487
488
0
void BlockCodeRegion::dumpToStream(raw_ostream &os) const {
489
0
  os << "block_code{" << static_cast<const void *>(this) << '}';
490
0
}
491
492
0
void BlockDataRegion::dumpToStream(raw_ostream &os) const {
493
0
  os << "block_data{" << BC;
494
0
  os << "; ";
495
0
  for (auto Var : referenced_vars())
496
0
    os << "(" << Var.getCapturedRegion() << "<-" << Var.getOriginalRegion()
497
0
       << ") ";
498
0
  os << '}';
499
0
}
500
501
0
void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
502
  // FIXME: More elaborate pretty-printing.
503
0
  os << "{ S" << CL->getID(getContext()) <<  " }";
504
0
}
505
506
24
void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
507
24
  os << "temp_object{" << getValueType() << ", "
508
24
     << "S" << Ex->getID(getContext()) << '}';
509
24
}
510
511
90
void CXXLifetimeExtendedObjectRegion::dumpToStream(raw_ostream &os) const {
512
90
  os << "lifetime_extended_object{" << getValueType() << ", ";
513
90
  if (const IdentifierInfo *ID = ExD->getIdentifier())
514
78
    os << ID->getName();
515
12
  else
516
12
    os << "D" << ExD->getID();
517
90
  os << ", "
518
90
     << "S" << Ex->getID(getContext()) << '}';
519
90
}
520
521
3
void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
522
3
  os << "Base{" << superRegion << ',' << getDecl()->getName() << '}';
523
3
}
524
525
0
void CXXDerivedObjectRegion::dumpToStream(raw_ostream &os) const {
526
0
  os << "Derived{" << superRegion << ',' << getDecl()->getName() << '}';
527
0
}
528
529
24
void CXXThisRegion::dumpToStream(raw_ostream &os) const {
530
24
  os << "this";
531
24
}
532
533
136
void ElementRegion::dumpToStream(raw_ostream &os) const {
534
136
  os << "Element{" << superRegion << ',' << Index << ',' << getElementType()
535
136
     << '}';
536
136
}
537
538
111
void FieldRegion::dumpToStream(raw_ostream &os) const {
539
111
  os << superRegion << "." << *getDecl();
540
111
}
541
542
0
void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
543
0
  os << "Ivar{" << superRegion << ',' << *getDecl() << '}';
544
0
}
545
546
52
void StringRegion::dumpToStream(raw_ostream &os) const {
547
52
  assert(Str != nullptr && "Expecting non-null StringLiteral");
548
52
  Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
549
52
}
550
551
0
void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
552
0
  assert(Str != nullptr && "Expecting non-null ObjCStringLiteral");
553
0
  Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
554
0
}
555
556
244
void SymbolicRegion::dumpToStream(raw_ostream &os) const {
557
244
  if (isa<HeapSpaceRegion>(getSuperRegion()))
558
59
    os << "Heap";
559
244
  os << "SymRegion{" << sym << '}';
560
244
}
561
562
3.57k
void NonParamVarRegion::dumpToStream(raw_ostream &os) const {
563
3.57k
  if (const IdentifierInfo *ID = VD->getIdentifier())
564
3.57k
    os << ID->getName();
565
0
  else
566
0
    os << "NonParamVarRegion{D" << VD->getID() << '}';
567
3.57k
}
568
569
0
LLVM_DUMP_METHOD void RegionRawOffset::dump() const {
570
0
  dumpToStream(llvm::errs());
571
0
}
572
573
0
void RegionRawOffset::dumpToStream(raw_ostream &os) const {
574
0
  os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
575
0
}
576
577
0
void CodeSpaceRegion::dumpToStream(raw_ostream &os) const {
578
0
  os << "CodeSpaceRegion";
579
0
}
580
581
0
void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
582
0
  os << "StaticGlobalsMemSpace{" << CR << '}';
583
0
}
584
585
5
void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
586
5
  os << "GlobalInternalSpaceRegion";
587
5
}
588
589
36
void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
590
36
  os << "GlobalSystemSpaceRegion";
591
36
}
592
593
0
void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
594
0
  os << "GlobalImmutableSpaceRegion";
595
0
}
596
597
0
void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
598
0
  os << "HeapSpaceRegion";
599
0
}
600
601
0
void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
602
0
  os << "UnknownSpaceRegion";
603
0
}
604
605
0
void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
606
0
  os << "StackArgumentsSpaceRegion";
607
0
}
608
609
0
void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
610
0
  os << "StackLocalsSpaceRegion";
611
0
}
612
613
26
void ParamVarRegion::dumpToStream(raw_ostream &os) const {
614
26
  const ParmVarDecl *PVD = getDecl();
615
26
  assert(PVD &&
616
26
         "`ParamVarRegion` support functions without `Decl` not implemented"
617
26
         " yet.");
618
26
  if (const IdentifierInfo *ID = PVD->getIdentifier()) {
619
25
    os << ID->getName();
620
25
  } else {
621
1
    os << "ParamVarRegion{P" << PVD->getID() << '}';
622
1
  }
623
26
}
624
625
4.98k
bool MemRegion::canPrintPretty() const {
626
4.98k
  return canPrintPrettyAsExpr();
627
4.98k
}
628
629
148
bool MemRegion::canPrintPrettyAsExpr() const {
630
148
  return false;
631
148
}
632
633
2.50k
void MemRegion::printPretty(raw_ostream &os) const {
634
2.50k
  assert(canPrintPretty() && "This region cannot be printed pretty.");
635
2.50k
  os << "'";
636
2.50k
  printPrettyAsExpr(os);
637
2.50k
  os << "'";
638
2.50k
}
639
640
0
void MemRegion::printPrettyAsExpr(raw_ostream &) const {
641
0
  llvm_unreachable("This region cannot be printed pretty.");
642
0
}
643
644
5.51k
bool NonParamVarRegion::canPrintPrettyAsExpr() const { return true; }
645
646
3.11k
void NonParamVarRegion::printPrettyAsExpr(raw_ostream &os) const {
647
3.11k
  os << getDecl()->getName();
648
3.11k
}
649
650
260
bool ParamVarRegion::canPrintPrettyAsExpr() const { return true; }
651
652
130
void ParamVarRegion::printPrettyAsExpr(raw_ostream &os) const {
653
130
  assert(getDecl() &&
654
130
         "`ParamVarRegion` support functions without `Decl` not implemented"
655
130
         " yet.");
656
130
  os << getDecl()->getName();
657
130
}
658
659
28
bool ObjCIvarRegion::canPrintPrettyAsExpr() const {
660
28
  return true;
661
28
}
662
663
14
void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const {
664
14
  os << getDecl()->getName();
665
14
}
666
667
154
bool FieldRegion::canPrintPretty() const {
668
154
  return true;
669
154
}
670
671
370
bool FieldRegion::canPrintPrettyAsExpr() const {
672
370
  return superRegion->canPrintPrettyAsExpr();
673
370
}
674
675
152
void FieldRegion::printPrettyAsExpr(raw_ostream &os) const {
676
152
  assert(canPrintPrettyAsExpr());
677
152
  superRegion->printPrettyAsExpr(os);
678
152
  os << "." << getDecl()->getName();
679
152
}
680
681
162
void FieldRegion::printPretty(raw_ostream &os) const {
682
162
  if (canPrintPrettyAsExpr()) {
683
128
    os << "\'";
684
128
    printPrettyAsExpr(os);
685
128
    os << "'";
686
128
  } else {
687
34
    os << "field " << "\'" << getDecl()->getName() << "'";
688
34
  }
689
162
}
690
691
7
bool CXXBaseObjectRegion::canPrintPrettyAsExpr() const {
692
7
  return superRegion->canPrintPrettyAsExpr();
693
7
}
694
695
3
void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
696
3
  superRegion->printPrettyAsExpr(os);
697
3
}
698
699
0
bool CXXDerivedObjectRegion::canPrintPrettyAsExpr() const {
700
0
  return superRegion->canPrintPrettyAsExpr();
701
0
}
702
703
0
void CXXDerivedObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
704
0
  superRegion->printPrettyAsExpr(os);
705
0
}
706
707
650
std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
708
650
  std::string VariableName;
709
650
  std::string ArrayIndices;
710
650
  const MemRegion *R = this;
711
650
  SmallString<50> buf;
712
650
  llvm::raw_svector_ostream os(buf);
713
714
  // Obtain array indices to add them to the variable name.
715
650
  const ElementRegion *ER = nullptr;
716
677
  while ((ER = R->getAs<ElementRegion>())) {
717
    // Index is a ConcreteInt.
718
27
    if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) {
719
27
      llvm::SmallString<2> Idx;
720
27
      CI->getValue().toString(Idx);
721
27
      ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str();
722
27
    }
723
    // If not a ConcreteInt, try to obtain the variable
724
    // name by calling 'getDescriptiveName' recursively.
725
0
    else {
726
0
      std::string Idx = ER->getDescriptiveName(false);
727
0
      if (!Idx.empty()) {
728
0
        ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str();
729
0
      }
730
0
    }
731
27
    R = ER->getSuperRegion();
732
27
  }
733
734
  // Get variable name.
735
650
  if (R && R->canPrintPrettyAsExpr()) {
736
621
    R->printPrettyAsExpr(os);
737
621
    if (UseQuotes)
738
117
      return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str();
739
504
    else
740
504
      return (llvm::Twine(os.str()) + ArrayIndices).str();
741
621
  }
742
743
29
  return VariableName;
744
650
}
745
746
34
SourceRange MemRegion::sourceRange() const {
747
  // Check for more specific regions first.
748
34
  if (auto *FR = dyn_cast<FieldRegion>(this)) {
749
3
    return FR->getDecl()->getSourceRange();
750
3
  }
751
752
31
  if (auto *VR = dyn_cast<VarRegion>(this->getBaseRegion())) {
753
31
    return VR->getDecl()->getSourceRange();
754
31
  }
755
756
  // Return invalid source range (can be checked by client).
757
0
  return {};
758
31
}
759
760
//===----------------------------------------------------------------------===//
761
// MemRegionManager methods.
762
//===----------------------------------------------------------------------===//
763
764
DefinedOrUnknownSVal MemRegionManager::getStaticSize(const MemRegion *MR,
765
38.0k
                                                     SValBuilder &SVB) const {
766
38.0k
  const auto *SR = cast<SubRegion>(MR);
767
38.0k
  SymbolManager &SymMgr = SVB.getSymbolManager();
768
769
38.0k
  switch (SR->getKind()) {
770
0
  case MemRegion::AllocaRegionKind:
771
9.26k
  case MemRegion::SymbolicRegionKind:
772
9.26k
    return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
773
16
  case MemRegion::StringRegionKind:
774
16
    return SVB.makeIntVal(
775
16
        cast<StringRegion>(SR)->getStringLiteral()->getByteLength() + 1,
776
16
        SVB.getArrayIndexType());
777
1
  case MemRegion::CompoundLiteralRegionKind:
778
18
  case MemRegion::CXXBaseObjectRegionKind:
779
18
  case MemRegion::CXXDerivedObjectRegionKind:
780
122
  case MemRegion::CXXTempObjectRegionKind:
781
130
  case MemRegion::CXXLifetimeExtendedObjectRegionKind:
782
130
  case MemRegion::CXXThisRegionKind:
783
188
  case MemRegion::ObjCIvarRegionKind:
784
2.96k
  case MemRegion::NonParamVarRegionKind:
785
3.65k
  case MemRegion::ParamVarRegionKind:
786
11.7k
  case MemRegion::ElementRegionKind:
787
11.7k
  case MemRegion::ObjCStringRegionKind: {
788
11.7k
    QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx);
789
11.7k
    if (isa<VariableArrayType>(Ty))
790
12
      return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
791
792
11.7k
    if (Ty->isIncompleteType())
793
2
      return UnknownVal();
794
795
11.7k
    return getElementExtent(Ty, SVB);
796
11.7k
  }
797
17.0k
  case MemRegion::FieldRegionKind: {
798
    // Force callers to deal with bitfields explicitly.
799
17.0k
    if (cast<FieldRegion>(SR)->getDecl()->isBitField())
800
37
      return UnknownVal();
801
802
17.0k
    QualType Ty = cast<TypedValueRegion>(SR)->getDesugaredValueType(Ctx);
803
17.0k
    const DefinedOrUnknownSVal Size = getElementExtent(Ty, SVB);
804
805
    // We currently don't model flexible array members (FAMs), which are:
806
    //  - int array[]; of IncompleteArrayType
807
    //  - int array[0]; of ConstantArrayType with size 0
808
    //  - int array[1]; of ConstantArrayType with size 1
809
    // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
810
17.0k
    const auto isFlexibleArrayMemberCandidate =
811
17.0k
        [this](const ArrayType *AT) -> bool {
812
17.0k
      if (!AT)
813
16.7k
        return false;
814
815
285
      auto IsIncompleteArray = [](const ArrayType *AT) {
816
283
        return isa<IncompleteArrayType>(AT);
817
283
      };
818
285
      auto IsArrayOfZero = [](const ArrayType *AT) {
819
285
        const auto *CAT = dyn_cast<ConstantArrayType>(AT);
820
285
        return CAT && 
CAT->getSize() == 0281
;
821
285
      };
822
285
      auto IsArrayOfOne = [](const ArrayType *AT) {
823
285
        const auto *CAT = dyn_cast<ConstantArrayType>(AT);
824
285
        return CAT && 
CAT->getSize() == 1281
;
825
285
      };
826
827
285
      using FAMKind = LangOptions::StrictFlexArraysLevelKind;
828
285
      const FAMKind StrictFlexArraysLevel =
829
285
          Ctx.getLangOpts().getStrictFlexArraysLevel();
830
831
      // "Default": Any trailing array member is a FAM.
832
      // Since we cannot tell at this point if this array is a trailing member
833
      // or not, let's just do the same as for "OneZeroOrIncomplete".
834
285
      if (StrictFlexArraysLevel == FAMKind::Default)
835
285
        return IsArrayOfOne(AT) || IsArrayOfZero(AT) || 
IsIncompleteArray(AT)283
;
836
837
0
      if (StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
838
0
        return IsArrayOfOne(AT) || IsArrayOfZero(AT) || IsIncompleteArray(AT);
839
840
0
      if (StrictFlexArraysLevel == FAMKind::ZeroOrIncomplete)
841
0
        return IsArrayOfZero(AT) || IsIncompleteArray(AT);
842
843
0
      assert(StrictFlexArraysLevel == FAMKind::IncompleteOnly);
844
0
      return IsIncompleteArray(AT);
845
0
    };
846
847
17.0k
    if (isFlexibleArrayMemberCandidate(Ctx.getAsArrayType(Ty)))
848
6
      return UnknownVal();
849
850
16.9k
    return Size;
851
17.0k
  }
852
    // FIXME: The following are being used in 'SimpleSValBuilder' and in
853
    // 'ArrayBoundChecker::checkLocation' because there is no symbol to
854
    // represent the regions more appropriately.
855
0
  case MemRegion::BlockDataRegionKind:
856
0
  case MemRegion::BlockCodeRegionKind:
857
6
  case MemRegion::FunctionCodeRegionKind:
858
6
    return nonloc::SymbolVal(SymMgr.getExtentSymbol(SR));
859
0
  default:
860
0
    llvm_unreachable("Unhandled region");
861
38.0k
  }
862
38.0k
}
863
864
template <typename REG>
865
477k
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
477k
  if (!region) {
867
39.6k
    region = new (A) REG(*this);
868
39.6k
  }
869
870
477k
  return region;
871
477k
}
clang::ento::GlobalSystemSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::GlobalSystemSpaceRegion>(clang::ento::GlobalSystemSpaceRegion*&)
Line
Count
Source
865
38.5k
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
38.5k
  if (!region) {
867
8.29k
    region = new (A) REG(*this);
868
8.29k
  }
869
870
38.5k
  return region;
871
38.5k
}
clang::ento::GlobalImmutableSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::GlobalImmutableSpaceRegion>(clang::ento::GlobalImmutableSpaceRegion*&)
Line
Count
Source
865
865
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
865
  if (!region) {
867
470
    region = new (A) REG(*this);
868
470
  }
869
870
865
  return region;
871
865
}
clang::ento::GlobalInternalSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::GlobalInternalSpaceRegion>(clang::ento::GlobalInternalSpaceRegion*&)
Line
Count
Source
865
52.8k
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
52.8k
  if (!region) {
867
8.33k
    region = new (A) REG(*this);
868
8.33k
  }
869
870
52.8k
  return region;
871
52.8k
}
clang::ento::HeapSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::HeapSpaceRegion>(clang::ento::HeapSpaceRegion*&)
Line
Count
Source
865
1.74k
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
1.74k
  if (!region) {
867
1.37k
    region = new (A) REG(*this);
868
1.37k
  }
869
870
1.74k
  return region;
871
1.74k
}
clang::ento::UnknownSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::UnknownSpaceRegion>(clang::ento::UnknownSpaceRegion*&)
Line
Count
Source
865
300k
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
300k
  if (!region) {
867
9.74k
    region = new (A) REG(*this);
868
9.74k
  }
869
870
300k
  return region;
871
300k
}
clang::ento::CodeSpaceRegion const* clang::ento::MemRegionManager::LazyAllocate<clang::ento::CodeSpaceRegion>(clang::ento::CodeSpaceRegion*&)
Line
Count
Source
865
82.4k
const REG *MemRegionManager::LazyAllocate(REG*& region) {
866
82.4k
  if (!region) {
867
11.4k
    region = new (A) REG(*this);
868
11.4k
  }
869
870
82.4k
  return region;
871
82.4k
}
872
873
template <typename REG, typename ARG>
874
const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
875
  if (!region) {
876
    region = new (A) REG(this, a);
877
  }
878
879
  return region;
880
}
881
882
const StackLocalsSpaceRegion*
883
193k
MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
884
193k
  assert(STC);
885
193k
  StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
886
887
193k
  if (R)
888
179k
    return R;
889
890
13.6k
  R = new (A) StackLocalsSpaceRegion(*this, STC);
891
13.6k
  return R;
892
193k
}
893
894
const StackArgumentsSpaceRegion *
895
243k
MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
896
243k
  assert(STC);
897
243k
  StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
898
899
243k
  if (R)
900
212k
    return R;
901
902
31.0k
  R = new (A) StackArgumentsSpaceRegion(*this, STC);
903
31.0k
  return R;
904
243k
}
905
906
const GlobalsSpaceRegion
907
*MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
908
92.9k
                                    const CodeTextRegion *CR) {
909
92.9k
  if (!CR) {
910
92.2k
    if (K == MemRegion::GlobalSystemSpaceRegionKind)
911
38.5k
      return LazyAllocate(SystemGlobals);
912
53.7k
    if (K == MemRegion::GlobalImmutableSpaceRegionKind)
913
865
      return LazyAllocate(ImmutableGlobals);
914
52.8k
    assert(K == MemRegion::GlobalInternalSpaceRegionKind);
915
52.8k
    return LazyAllocate(InternalGlobals);
916
52.8k
  }
917
918
683
  assert(K == MemRegion::StaticGlobalSpaceRegionKind);
919
683
  StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
920
683
  if (R)
921
502
    return R;
922
923
181
  R = new (A) StaticGlobalSpaceRegion(*this, CR);
924
181
  return R;
925
683
}
926
927
1.74k
const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
928
1.74k
  return LazyAllocate(heap);
929
1.74k
}
930
931
300k
const UnknownSpaceRegion *MemRegionManager::getUnknownRegion() {
932
300k
  return LazyAllocate(unknown);
933
300k
}
934
935
82.4k
const CodeSpaceRegion *MemRegionManager::getCodeRegion() {
936
82.4k
  return LazyAllocate(code);
937
82.4k
}
938
939
//===----------------------------------------------------------------------===//
940
// Constructing regions.
941
//===----------------------------------------------------------------------===//
942
943
12.1k
const StringRegion *MemRegionManager::getStringRegion(const StringLiteral *Str){
944
12.1k
  return getSubRegion<StringRegion>(
945
12.1k
      Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
946
12.1k
}
947
948
const ObjCStringRegion *
949
1.12k
MemRegionManager::getObjCStringRegion(const ObjCStringLiteral *Str){
950
1.12k
  return getSubRegion<ObjCStringRegion>(
951
1.12k
      Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
952
1.12k
}
953
954
/// Look through a chain of LocationContexts to either find the
955
/// StackFrameContext that matches a DeclContext, or find a VarRegion
956
/// for a variable captured by a block.
957
static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
958
getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
959
                                      const DeclContext *DC,
960
267k
                                      const VarDecl *VD) {
961
268k
  while (LC) {
962
268k
    if (const auto *SFC = dyn_cast<StackFrameContext>(LC)) {
963
267k
      if (cast<DeclContext>(SFC->getDecl()) == DC)
964
267k
        return SFC;
965
267k
    }
966
705
    if (const auto *BC = dyn_cast<BlockInvocationContext>(LC)) {
967
171
      const auto *BR = static_cast<const BlockDataRegion *>(BC->getData());
968
      // FIXME: This can be made more efficient.
969
215
      for (auto Var : BR->referenced_vars()) {
970
215
        const TypedValueRegion *OrigR = Var.getOriginalRegion();
971
215
        if (const auto *VR = dyn_cast<VarRegion>(OrigR)) {
972
215
          if (VR->getDecl() == VD)
973
171
            return cast<VarRegion>(Var.getCapturedRegion());
974
215
        }
975
215
      }
976
171
    }
977
978
534
    LC = LC->getParent();
979
534
  }
980
239
  return (const StackFrameContext *)nullptr;
981
267k
}
982
983
const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
984
318k
                                                const LocationContext *LC) {
985
318k
  const auto *PVD = dyn_cast<ParmVarDecl>(D);
986
318k
  if (PVD) {
987
112k
    unsigned Index = PVD->getFunctionScopeIndex();
988
112k
    const StackFrameContext *SFC = LC->getStackFrame();
989
112k
    const Stmt *CallSite = SFC->getCallSite();
990
112k
    if (CallSite) {
991
41.1k
      const Decl *D = SFC->getDecl();
992
41.1k
      if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
993
40.9k
        if (Index < FD->param_size() && 
FD->parameters()[Index] == PVD40.9k
)
994
40.9k
          return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index,
995
40.9k
                                              getStackArgumentsRegion(SFC));
996
40.9k
      } else 
if (const auto *239
BD239
= dyn_cast<BlockDecl>(D)) {
997
88
        if (Index < BD->param_size() && 
BD->parameters()[Index] == PVD66
)
998
59
          return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index,
999
59
                                              getStackArgumentsRegion(SFC));
1000
151
      } else {
1001
151
        return getSubRegion<ParamVarRegion>(cast<Expr>(CallSite), Index,
1002
151
                                            getStackArgumentsRegion(SFC));
1003
151
      }
1004
41.1k
    }
1005
112k
  }
1006
1007
277k
  D = D->getCanonicalDecl();
1008
277k
  const MemRegion *sReg = nullptr;
1009
1010
277k
  if (D->hasGlobalStorage() && 
!D->isStaticLocal()10.5k
) {
1011
9.86k
    QualType Ty = D->getType();
1012
9.86k
    assert(!Ty.isNull());
1013
9.86k
    if (Ty.isConstQualified()) {
1014
706
      sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
1015
9.15k
    } else if (Ctx.getSourceManager().isInSystemHeader(D->getLocation())) {
1016
396
      sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
1017
8.76k
    } else {
1018
8.76k
      sReg = getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind);
1019
8.76k
    }
1020
1021
  // Finally handle static locals.
1022
267k
  } else {
1023
    // FIXME: Once we implement scope handling, we will need to properly lookup
1024
    // 'D' to the proper LocationContext.
1025
267k
    const DeclContext *DC = D->getDeclContext();
1026
267k
    llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
1027
267k
      getStackOrCaptureRegionForDeclContext(LC, DC, D);
1028
1029
267k
    if (V.is<const VarRegion*>())
1030
171
      return V.get<const VarRegion*>();
1031
1032
267k
    const auto *STC = V.get<const StackFrameContext *>();
1033
1034
267k
    if (!STC) {
1035
      // FIXME: Assign a more sensible memory space to static locals
1036
      // we see from within blocks that we analyze as top-level declarations.
1037
239
      sReg = getUnknownRegion();
1038
267k
    } else {
1039
267k
      if (D->hasLocalStorage()) {
1040
266k
        sReg =
1041
266k
            isa<ParmVarDecl, ImplicitParamDecl>(D)
1042
266k
                ? 
static_cast<const MemRegion *>(getStackArgumentsRegion(STC))78.0k
1043
266k
                : 
static_cast<const MemRegion *>(getStackLocalsRegion(STC))188k
;
1044
266k
      }
1045
683
      else {
1046
683
        assert(D->isStaticLocal());
1047
683
        const Decl *STCD = STC->getDecl();
1048
683
        if (isa<FunctionDecl, ObjCMethodDecl>(STCD))
1049
670
          sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
1050
670
                                  getFunctionCodeRegion(cast<NamedDecl>(STCD)));
1051
13
        else if (const auto *BD = dyn_cast<BlockDecl>(STCD)) {
1052
          // FIXME: The fallback type here is totally bogus -- though it should
1053
          // never be queried, it will prevent uniquing with the real
1054
          // BlockCodeRegion. Ideally we'd fix the AST so that we always had a
1055
          // signature.
1056
13
          QualType T;
1057
13
          if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
1058
11
            T = TSI->getType();
1059
13
          if (T.isNull())
1060
2
            T = getContext().VoidTy;
1061
13
          if (!T->getAs<FunctionType>()) {
1062
5
            FunctionProtoType::ExtProtoInfo Ext;
1063
5
            T = getContext().getFunctionType(T, std::nullopt, Ext);
1064
5
          }
1065
13
          T = getContext().getBlockPointerType(T);
1066
1067
13
          const BlockCodeRegion *BTR =
1068
13
            getBlockCodeRegion(BD, Ctx.getCanonicalType(T),
1069
13
                               STC->getAnalysisDeclContext());
1070
13
          sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
1071
13
                                  BTR);
1072
13
        }
1073
0
        else {
1074
0
          sReg = getGlobalsRegion();
1075
0
        }
1076
683
      }
1077
267k
    }
1078
267k
  }
1079
1080
277k
  return getNonParamVarRegion(D, sReg);
1081
277k
}
1082
1083
const NonParamVarRegion *
1084
MemRegionManager::getNonParamVarRegion(const VarDecl *D,
1085
277k
                                       const MemRegion *superR) {
1086
  // Prefer the definition over the canonical decl as the canonical form.
1087
277k
  D = D->getCanonicalDecl();
1088
277k
  if (const VarDecl *Def = D->getDefinition())
1089
275k
    D = Def;
1090
277k
  return getSubRegion<NonParamVarRegion>(D, superR);
1091
277k
}
1092
1093
const ParamVarRegion *
1094
MemRegionManager::getParamVarRegion(const Expr *OriginExpr, unsigned Index,
1095
44.1k
                                    const LocationContext *LC) {
1096
44.1k
  const StackFrameContext *SFC = LC->getStackFrame();
1097
44.1k
  assert(SFC);
1098
44.1k
  return getSubRegion<ParamVarRegion>(OriginExpr, Index,
1099
44.1k
                                      getStackArgumentsRegion(SFC));
1100
44.1k
}
1101
1102
const BlockDataRegion *
1103
MemRegionManager::getBlockDataRegion(const BlockCodeRegion *BC,
1104
                                     const LocationContext *LC,
1105
398
                                     unsigned blockCount) {
1106
398
  const MemSpaceRegion *sReg = nullptr;
1107
398
  const BlockDecl *BD = BC->getDecl();
1108
398
  if (!BD->hasCaptures()) {
1109
    // This handles 'static' blocks.
1110
159
    sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
1111
159
  }
1112
239
  else {
1113
239
    bool IsArcManagedBlock = Ctx.getLangOpts().ObjCAutoRefCount;
1114
1115
    // ARC managed blocks can be initialized on stack or directly in heap
1116
    // depending on the implementations.  So we initialize them with
1117
    // UnknownRegion.
1118
239
    if (!IsArcManagedBlock && 
LC187
) {
1119
      // FIXME: Once we implement scope handling, we want the parent region
1120
      // to be the scope.
1121
187
      const StackFrameContext *STC = LC->getStackFrame();
1122
187
      assert(STC);
1123
187
      sReg = getStackLocalsRegion(STC);
1124
187
    } else {
1125
      // We allow 'LC' to be NULL for cases where want BlockDataRegions
1126
      // without context-sensitivity.
1127
52
      sReg = getUnknownRegion();
1128
52
    }
1129
239
  }
1130
1131
398
  return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg);
1132
398
}
1133
1134
const CompoundLiteralRegion*
1135
MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
1136
63
                                           const LocationContext *LC) {
1137
63
  const MemSpaceRegion *sReg = nullptr;
1138
1139
63
  if (CL->isFileScope())
1140
0
    sReg = getGlobalsRegion();
1141
63
  else {
1142
63
    const StackFrameContext *STC = LC->getStackFrame();
1143
63
    assert(STC);
1144
63
    sReg = getStackLocalsRegion(STC);
1145
63
  }
1146
1147
63
  return getSubRegion<CompoundLiteralRegion>(CL, sReg);
1148
63
}
1149
1150
const ElementRegion*
1151
MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
1152
                                   const SubRegion* superRegion,
1153
69.3k
                                   ASTContext &Ctx){
1154
69.3k
  QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
1155
1156
69.3k
  llvm::FoldingSetNodeID ID;
1157
69.3k
  ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
1158
1159
69.3k
  void *InsertPos;
1160
69.3k
  MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
1161
69.3k
  auto *R = cast_or_null<ElementRegion>(data);
1162
1163
69.3k
  if (!R) {
1164
21.0k
    R = new (A) ElementRegion(T, Idx, superRegion);
1165
21.0k
    Regions.InsertNode(R, InsertPos);
1166
21.0k
  }
1167
1168
69.3k
  return R;
1169
69.3k
}
1170
1171
const FunctionCodeRegion *
1172
82.0k
MemRegionManager::getFunctionCodeRegion(const NamedDecl *FD) {
1173
  // To think: should we canonicalize the declaration here?
1174
82.0k
  return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion());
1175
82.0k
}
1176
1177
const BlockCodeRegion *
1178
MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy,
1179
411
                                     AnalysisDeclContext *AC) {
1180
411
  return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion());
1181
411
}
1182
1183
const SymbolicRegion *
1184
MemRegionManager::getSymbolicRegion(SymbolRef sym,
1185
298k
                                    const MemSpaceRegion *MemSpace) {
1186
298k
  if (MemSpace == nullptr)
1187
298k
    MemSpace = getUnknownRegion();
1188
298k
  return getSubRegion<SymbolicRegion>(sym, MemSpace);
1189
298k
}
1190
1191
1.74k
const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
1192
1.74k
  return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
1193
1.74k
}
1194
1195
const FieldRegion*
1196
MemRegionManager::getFieldRegion(const FieldDecl *d,
1197
113k
                                 const SubRegion* superRegion){
1198
113k
  return getSubRegion<FieldRegion>(d, superRegion);
1199
113k
}
1200
1201
const ObjCIvarRegion*
1202
MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
1203
1.46k
                                    const SubRegion* superRegion) {
1204
1.46k
  return getSubRegion<ObjCIvarRegion>(d, superRegion);
1205
1.46k
}
1206
1207
const CXXTempObjectRegion*
1208
MemRegionManager::getCXXTempObjectRegion(Expr const *E,
1209
3.87k
                                         LocationContext const *LC) {
1210
3.87k
  const StackFrameContext *SFC = LC->getStackFrame();
1211
3.87k
  assert(SFC);
1212
3.87k
  return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
1213
3.87k
}
1214
1215
const CXXLifetimeExtendedObjectRegion *
1216
MemRegionManager::getCXXLifetimeExtendedObjectRegion(
1217
339
    const Expr *Ex, const ValueDecl *VD, const LocationContext *LC) {
1218
339
  const StackFrameContext *SFC = LC->getStackFrame();
1219
339
  assert(SFC);
1220
339
  return getSubRegion<CXXLifetimeExtendedObjectRegion>(
1221
339
      Ex, VD, getStackLocalsRegion(SFC));
1222
339
}
1223
1224
const CXXLifetimeExtendedObjectRegion *
1225
MemRegionManager::getCXXStaticLifetimeExtendedObjectRegion(
1226
15
    const Expr *Ex, const ValueDecl *VD) {
1227
15
  return getSubRegion<CXXLifetimeExtendedObjectRegion>(
1228
15
      Ex, VD,
1229
15
      getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr));
1230
15
}
1231
1232
/// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
1233
/// class of the type of \p Super.
1234
static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
1235
                             const TypedValueRegion *Super,
1236
1.63k
                             bool IsVirtual) {
1237
1.63k
  BaseClass = BaseClass->getCanonicalDecl();
1238
1239
1.63k
  const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
1240
1.63k
  if (!Class)
1241
0
    return true;
1242
1243
1.63k
  if (IsVirtual)
1244
157
    return Class->isVirtuallyDerivedFrom(BaseClass);
1245
1246
1.65k
  
for (const auto &I : Class->bases())1.48k
{
1247
1.65k
    if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
1248
1.48k
      return true;
1249
1.65k
  }
1250
1251
0
  return false;
1252
1.48k
}
1253
1254
const CXXBaseObjectRegion *
1255
MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
1256
                                         const SubRegion *Super,
1257
2.10k
                                         bool IsVirtual) {
1258
2.10k
  if (isa<TypedValueRegion>(Super)) {
1259
1.63k
    assert(isValidBaseClass(RD, cast<TypedValueRegion>(Super), IsVirtual));
1260
1.63k
    (void)&isValidBaseClass;
1261
1262
1.63k
    if (IsVirtual) {
1263
      // Virtual base regions should not be layered, since the layout rules
1264
      // are different.
1265
195
      while (const auto *Base = dyn_cast<CXXBaseObjectRegion>(Super))
1266
38
        Super = cast<SubRegion>(Base->getSuperRegion());
1267
157
      assert(Super && !isa<MemSpaceRegion>(Super));
1268
157
    }
1269
1.63k
  }
1270
1271
2.10k
  return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
1272
2.10k
}
1273
1274
const CXXDerivedObjectRegion *
1275
MemRegionManager::getCXXDerivedObjectRegion(const CXXRecordDecl *RD,
1276
25
                                            const SubRegion *Super) {
1277
25
  return getSubRegion<CXXDerivedObjectRegion>(RD, Super);
1278
25
}
1279
1280
const CXXThisRegion*
1281
MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
1282
80.2k
                                   const LocationContext *LC) {
1283
80.2k
  const auto *PT = thisPointerTy->getAs<PointerType>();
1284
80.2k
  assert(PT);
1285
  // Inside the body of the operator() of a lambda a this expr might refer to an
1286
  // object in one of the parent location contexts.
1287
80.2k
  const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl());
1288
  // FIXME: when operator() of lambda is analyzed as a top level function and
1289
  // 'this' refers to a this to the enclosing scope, there is no right region to
1290
  // return.
1291
80.3k
  while (!LC->inTopFrame() && 
(76.5k
!D76.5k
||
D->isStatic()76.5k
||
1292
76.5k
                               
PT != D->getThisType()->getAs<PointerType>()76.5k
)) {
1293
80
    LC = LC->getParent();
1294
80
    D = dyn_cast<CXXMethodDecl>(LC->getDecl());
1295
80
  }
1296
80.2k
  const StackFrameContext *STC = LC->getStackFrame();
1297
80.2k
  assert(STC);
1298
80.2k
  return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
1299
80.2k
}
1300
1301
const AllocaRegion*
1302
MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
1303
93
                                  const LocationContext *LC) {
1304
93
  const StackFrameContext *STC = LC->getStackFrame();
1305
93
  assert(STC);
1306
93
  return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
1307
93
}
1308
1309
3.06M
const MemSpaceRegion *MemRegion::getMemorySpace() const {
1310
3.06M
  const MemRegion *R = this;
1311
3.06M
  const auto *SR = dyn_cast<SubRegion>(this);
1312
1313
6.26M
  while (SR) {
1314
3.19M
    R = SR->getSuperRegion();
1315
3.19M
    SR = dyn_cast<SubRegion>(R);
1316
3.19M
  }
1317
1318
3.06M
  return cast<MemSpaceRegion>(R);
1319
3.06M
}
1320
1321
2.44k
bool MemRegion::hasStackStorage() const {
1322
2.44k
  return isa<StackSpaceRegion>(getMemorySpace());
1323
2.44k
}
1324
1325
75.5k
bool MemRegion::hasStackNonParametersStorage() const {
1326
75.5k
  return isa<StackLocalsSpaceRegion>(getMemorySpace());
1327
75.5k
}
1328
1329
80.3k
bool MemRegion::hasStackParametersStorage() const {
1330
80.3k
  return isa<StackArgumentsSpaceRegion>(getMemorySpace());
1331
80.3k
}
1332
1333
// Strips away all elements and fields.
1334
// Returns the base region of them.
1335
9.71M
const MemRegion *MemRegion::getBaseRegion() const {
1336
9.71M
  const MemRegion *R = this;
1337
12.5M
  while (true) {
1338
12.5M
    switch (R->getKind()) {
1339
1.60M
      case MemRegion::ElementRegionKind:
1340
2.85M
      case MemRegion::FieldRegionKind:
1341
2.86M
      case MemRegion::ObjCIvarRegionKind:
1342
2.87M
      case MemRegion::CXXBaseObjectRegionKind:
1343
2.87M
      case MemRegion::CXXDerivedObjectRegionKind:
1344
2.87M
        R = cast<SubRegion>(R)->getSuperRegion();
1345
2.87M
        continue;
1346
9.71M
      default:
1347
9.71M
        break;
1348
12.5M
    }
1349
9.71M
    break;
1350
12.5M
  }
1351
9.71M
  return R;
1352
9.71M
}
1353
1354
// Returns the region of the root class of a C++ class hierarchy.
1355
34.4k
const MemRegion *MemRegion::getMostDerivedObjectRegion() const {
1356
34.4k
  const MemRegion *R = this;
1357
34.5k
  while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
1358
101
    R = BR->getSuperRegion();
1359
34.4k
  return R;
1360
34.4k
}
1361
1362
0
bool MemRegion::isSubRegionOf(const MemRegion *) const {
1363
0
  return false;
1364
0
}
1365
1366
//===----------------------------------------------------------------------===//
1367
// View handling.
1368
//===----------------------------------------------------------------------===//
1369
1370
571k
const MemRegion *MemRegion::StripCasts(bool StripBaseAndDerivedCasts) const {
1371
571k
  const MemRegion *R = this;
1372
609k
  while (true) {
1373
609k
    switch (R->getKind()) {
1374
69.2k
    case ElementRegionKind: {
1375
69.2k
      const auto *ER = cast<ElementRegion>(R);
1376
69.2k
      if (!ER->getIndex().isZeroConstant())
1377
34.1k
        return R;
1378
35.1k
      R = ER->getSuperRegion();
1379
35.1k
      break;
1380
69.2k
    }
1381
2.69k
    case CXXBaseObjectRegionKind:
1382
2.88k
    case CXXDerivedObjectRegionKind:
1383
2.88k
      if (!StripBaseAndDerivedCasts)
1384
0
        return R;
1385
2.88k
      R = cast<TypedValueRegion>(R)->getSuperRegion();
1386
2.88k
      break;
1387
537k
    default:
1388
537k
      return R;
1389
609k
    }
1390
609k
  }
1391
571k
}
1392
1393
1.23M
const SymbolicRegion *MemRegion::getSymbolicBase() const {
1394
1.23M
  const auto *SubR = dyn_cast<SubRegion>(this);
1395
1396
2.55M
  while (SubR) {
1397
1.62M
    if (const auto *SymR = dyn_cast<SymbolicRegion>(SubR))
1398
313k
      return SymR;
1399
1.31M
    SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
1400
1.31M
  }
1401
922k
  return nullptr;
1402
1.23M
}
1403
1404
27.5k
RegionRawOffset ElementRegion::getAsArrayOffset() const {
1405
27.5k
  int64_t offset = 0;
1406
27.5k
  const ElementRegion *ER = this;
1407
27.5k
  const MemRegion *superR = nullptr;
1408
27.5k
  ASTContext &C = getContext();
1409
1410
  // FIXME: Handle multi-dimensional arrays.
1411
1412
46.5k
  while (ER) {
1413
28.0k
    superR = ER->getSuperRegion();
1414
1415
    // FIXME: generalize to symbolic offsets.
1416
28.0k
    SVal index = ER->getIndex();
1417
28.0k
    if (auto CI = index.getAs<nonloc::ConcreteInt>()) {
1418
      // Update the offset.
1419
19.0k
      int64_t i = CI->getValue().getSExtValue();
1420
1421
19.0k
      if (i != 0) {
1422
5.90k
        QualType elemType = ER->getElementType();
1423
1424
        // If we are pointing to an incomplete type, go no further.
1425
5.90k
        if (elemType->isIncompleteType()) {
1426
0
          superR = ER;
1427
0
          break;
1428
0
        }
1429
1430
5.90k
        int64_t size = C.getTypeSizeInChars(elemType).getQuantity();
1431
5.90k
        if (auto NewOffset = llvm::checkedMulAdd(i, size, offset)) {
1432
5.89k
          offset = *NewOffset;
1433
5.89k
        } else {
1434
6
          LLVM_DEBUG(llvm::dbgs() << "MemRegion::getAsArrayOffset: "
1435
6
                                  << "offset overflowing, returning unknown\n");
1436
1437
6
          return nullptr;
1438
6
        }
1439
5.90k
      }
1440
1441
      // Go to the next ElementRegion (if any).
1442
19.0k
      ER = dyn_cast<ElementRegion>(superR);
1443
19.0k
      continue;
1444
19.0k
    }
1445
1446
9.06k
    return nullptr;
1447
28.0k
  }
1448
1449
18.4k
  assert(superR && "super region cannot be NULL");
1450
18.4k
  return RegionRawOffset(superR, CharUnits::fromQuantity(offset));
1451
18.4k
}
1452
1453
/// Returns true if \p Base is an immediate base class of \p Child
1454
static bool isImmediateBase(const CXXRecordDecl *Child,
1455
215
                            const CXXRecordDecl *Base) {
1456
215
  assert(Child && "Child must not be null");
1457
  // Note that we do NOT canonicalize the base class here, because
1458
  // ASTRecordLayout doesn't either. If that leads us down the wrong path,
1459
  // so be it; at least we won't crash.
1460
247
  
for (const auto &I : Child->bases())215
{
1461
247
    if (I.getType()->getAsCXXRecordDecl() == Base)
1462
213
      return true;
1463
247
  }
1464
1465
2
  return false;
1466
215
}
1467
1468
131k
static RegionOffset calculateOffset(const MemRegion *R) {
1469
131k
  const MemRegion *SymbolicOffsetBase = nullptr;
1470
131k
  int64_t Offset = 0;
1471
1472
169k
  while (true) {
1473
169k
    switch (R->getKind()) {
1474
0
    case MemRegion::CodeSpaceRegionKind:
1475
2.71k
    case MemRegion::StackLocalsSpaceRegionKind:
1476
3.15k
    case MemRegion::StackArgumentsSpaceRegionKind:
1477
3.17k
    case MemRegion::HeapSpaceRegionKind:
1478
5.57k
    case MemRegion::UnknownSpaceRegionKind:
1479
5.61k
    case MemRegion::StaticGlobalSpaceRegionKind:
1480
12.3k
    case MemRegion::GlobalInternalSpaceRegionKind:
1481
20.5k
    case MemRegion::GlobalSystemSpaceRegionKind:
1482
20.6k
    case MemRegion::GlobalImmutableSpaceRegionKind:
1483
      // Stores can bind directly to a region space to set a default value.
1484
20.6k
      assert(Offset == 0 && !SymbolicOffsetBase);
1485
20.6k
      goto Finish;
1486
1487
20.6k
    case MemRegion::FunctionCodeRegionKind:
1488
72
    case MemRegion::BlockCodeRegionKind:
1489
76
    case MemRegion::BlockDataRegionKind:
1490
      // These will never have bindings, but may end up having values requested
1491
      // if the user does some strange casting.
1492
76
      if (Offset != 0)
1493
6
        SymbolicOffsetBase = R;
1494
76
      goto Finish;
1495
1496
20.2k
    case MemRegion::SymbolicRegionKind:
1497
20.2k
    case MemRegion::AllocaRegionKind:
1498
20.3k
    case MemRegion::CompoundLiteralRegionKind:
1499
34.8k
    case MemRegion::CXXThisRegionKind:
1500
36.1k
    case MemRegion::StringRegionKind:
1501
36.2k
    case MemRegion::ObjCStringRegionKind:
1502
82.7k
    case MemRegion::NonParamVarRegionKind:
1503
103k
    case MemRegion::ParamVarRegionKind:
1504
108k
    case MemRegion::CXXTempObjectRegionKind:
1505
109k
    case MemRegion::CXXLifetimeExtendedObjectRegionKind:
1506
      // Usual base regions.
1507
109k
      goto Finish;
1508
1509
654
    case MemRegion::ObjCIvarRegionKind:
1510
      // This is a little strange, but it's a compromise between
1511
      // ObjCIvarRegions having unknown compile-time offsets (when using the
1512
      // non-fragile runtime) and yet still being distinct, non-overlapping
1513
      // regions. Thus we treat them as "like" base regions for the purposes
1514
      // of computing offsets.
1515
654
      goto Finish;
1516
1517
1.51k
    case MemRegion::CXXBaseObjectRegionKind: {
1518
1.51k
      const auto *BOR = cast<CXXBaseObjectRegion>(R);
1519
1.51k
      R = BOR->getSuperRegion();
1520
1521
1.51k
      QualType Ty;
1522
1.51k
      bool RootIsSymbolic = false;
1523
1.51k
      if (const auto *TVR = dyn_cast<TypedValueRegion>(R)) {
1524
1.26k
        Ty = TVR->getDesugaredValueType(R->getContext());
1525
1.26k
      } else 
if (const auto *250
SR250
= dyn_cast<SymbolicRegion>(R)) {
1526
        // If our base region is symbolic, we don't know what type it really is.
1527
        // Pretend the type of the symbol is the true dynamic type.
1528
        // (This will at least be self-consistent for the life of the symbol.)
1529
250
        Ty = SR->getPointeeStaticType();
1530
250
        RootIsSymbolic = true;
1531
250
      }
1532
1533
1.51k
      const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
1534
1.51k
      if (!Child) {
1535
        // We cannot compute the offset of the base class.
1536
0
        SymbolicOffsetBase = R;
1537
1.51k
      } else {
1538
1.51k
        if (RootIsSymbolic) {
1539
          // Base layers on symbolic regions may not be type-correct.
1540
          // Double-check the inheritance here, and revert to a symbolic offset
1541
          // if it's invalid (e.g. due to a reinterpret_cast).
1542
250
          if (BOR->isVirtual()) {
1543
35
            if (!Child->isVirtuallyDerivedFrom(BOR->getDecl()))
1544
0
              SymbolicOffsetBase = R;
1545
215
          } else {
1546
215
            if (!isImmediateBase(Child, BOR->getDecl()))
1547
2
              SymbolicOffsetBase = R;
1548
215
          }
1549
250
        }
1550
1.51k
      }
1551
1552
      // Don't bother calculating precise offsets if we already have a
1553
      // symbolic offset somewhere in the chain.
1554
1.51k
      if (SymbolicOffsetBase)
1555
5
        continue;
1556
1557
1.50k
      CharUnits BaseOffset;
1558
1.50k
      const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(Child);
1559
1.50k
      if (BOR->isVirtual())
1560
96
        BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
1561
1.41k
      else
1562
1.41k
        BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
1563
1564
      // The base offset is in chars, not in bits.
1565
1.50k
      Offset += BaseOffset.getQuantity() * R->getContext().getCharWidth();
1566
1.50k
      break;
1567
1.51k
    }
1568
1569
4
    case MemRegion::CXXDerivedObjectRegionKind: {
1570
      // TODO: Store the base type in the CXXDerivedObjectRegion and use it.
1571
4
      goto Finish;
1572
1.51k
    }
1573
1574
16.8k
    case MemRegion::ElementRegionKind: {
1575
16.8k
      const auto *ER = cast<ElementRegion>(R);
1576
16.8k
      R = ER->getSuperRegion();
1577
1578
16.8k
      QualType EleTy = ER->getValueType();
1579
16.8k
      if (EleTy->isIncompleteType()) {
1580
        // We cannot compute the offset of the base class.
1581
10
        SymbolicOffsetBase = R;
1582
10
        continue;
1583
10
      }
1584
1585
16.8k
      SVal Index = ER->getIndex();
1586
16.8k
      if (std::optional<nonloc::ConcreteInt> CI =
1587
16.8k
              Index.getAs<nonloc::ConcreteInt>()) {
1588
        // Don't bother calculating precise offsets if we already have a
1589
        // symbolic offset somewhere in the chain.
1590
16.2k
        if (SymbolicOffsetBase)
1591
84
          continue;
1592
1593
16.1k
        int64_t i = CI->getValue().getSExtValue();
1594
        // This type size is in bits.
1595
16.1k
        Offset += i * R->getContext().getTypeSize(EleTy);
1596
16.1k
      } else {
1597
        // We cannot compute offset for non-concrete index.
1598
655
        SymbolicOffsetBase = R;
1599
655
      }
1600
16.8k
      break;
1601
16.8k
    }
1602
19.9k
    case MemRegion::FieldRegionKind: {
1603
19.9k
      const auto *FR = cast<FieldRegion>(R);
1604
19.9k
      R = FR->getSuperRegion();
1605
19.9k
      assert(R);
1606
1607
19.9k
      const RecordDecl *RD = FR->getDecl()->getParent();
1608
19.9k
      if (RD->isUnion() || 
!RD->isCompleteDefinition()19.7k
) {
1609
        // We cannot compute offset for incomplete type.
1610
        // For unions, we could treat everything as offset 0, but we'd rather
1611
        // treat each field as a symbolic offset so they aren't stored on top
1612
        // of each other, since we depend on things in typed regions actually
1613
        // matching their types.
1614
126
        SymbolicOffsetBase = R;
1615
126
      }
1616
1617
      // Don't bother calculating precise offsets if we already have a
1618
      // symbolic offset somewhere in the chain.
1619
19.9k
      if (SymbolicOffsetBase)
1620
267
        continue;
1621
1622
      // Get the field number.
1623
19.6k
      unsigned idx = 0;
1624
19.6k
      for (RecordDecl::field_iterator FI = RD->field_begin(),
1625
26.5k
             FE = RD->field_end(); FI != FE; 
++FI, ++idx6.94k
) {
1626
26.5k
        if (FR->getDecl() == *FI)
1627
19.6k
          break;
1628
26.5k
      }
1629
19.6k
      const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(RD);
1630
      // This is offset in bits.
1631
19.6k
      Offset += Layout.getFieldOffset(idx);
1632
19.6k
      break;
1633
19.9k
    }
1634
169k
    }
1635
169k
  }
1636
1637
131k
 Finish:
1638
131k
  if (SymbolicOffsetBase)
1639
791
    return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
1640
130k
  return RegionOffset(R, Offset);
1641
131k
}
1642
1643
1.96M
RegionOffset MemRegion::getAsOffset() const {
1644
1.96M
  if (!cachedOffset)
1645
131k
    cachedOffset = calculateOffset(this);
1646
1.96M
  return *cachedOffset;
1647
1.96M
}
1648
1649
//===----------------------------------------------------------------------===//
1650
// BlockDataRegion
1651
//===----------------------------------------------------------------------===//
1652
1653
std::pair<const VarRegion *, const VarRegion *>
1654
308
BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
1655
308
  MemRegionManager &MemMgr = getMemRegionManager();
1656
308
  const VarRegion *VR = nullptr;
1657
308
  const VarRegion *OriginalVR = nullptr;
1658
1659
308
  if (!VD->hasAttr<BlocksAttr>() && 
VD->hasLocalStorage()241
) {
1660
201
    VR = MemMgr.getNonParamVarRegion(VD, this);
1661
201
    OriginalVR = MemMgr.getVarRegion(VD, LC);
1662
201
  }
1663
107
  else {
1664
107
    if (LC) {
1665
107
      VR = MemMgr.getVarRegion(VD, LC);
1666
107
      OriginalVR = VR;
1667
107
    }
1668
0
    else {
1669
0
      VR = MemMgr.getNonParamVarRegion(VD, MemMgr.getUnknownRegion());
1670
0
      OriginalVR = MemMgr.getVarRegion(VD, LC);
1671
0
    }
1672
107
  }
1673
308
  return std::make_pair(VR, OriginalVR);
1674
308
}
1675
1676
6.30k
void BlockDataRegion::LazyInitializeReferencedVars() {
1677
6.30k
  if (ReferencedVars)
1678
5.91k
    return;
1679
1680
384
  AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
1681
384
  const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl());
1682
384
  auto NumBlockVars =
1683
384
      std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end());
1684
1685
384
  if (NumBlockVars == 0) {
1686
139
    ReferencedVars = (void*) 0x1;
1687
139
    return;
1688
139
  }
1689
1690
245
  MemRegionManager &MemMgr = getMemRegionManager();
1691
245
  llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
1692
245
  BumpVectorContext BC(A);
1693
1694
245
  using VarVec = BumpVector<const MemRegion *>;
1695
1696
245
  auto *BV = new (A) VarVec(BC, NumBlockVars);
1697
245
  auto *BVOriginal = new (A) VarVec(BC, NumBlockVars);
1698
1699
308
  for (const auto *VD : ReferencedBlockVars) {
1700
308
    const VarRegion *VR = nullptr;
1701
308
    const VarRegion *OriginalVR = nullptr;
1702
308
    std::tie(VR, OriginalVR) = getCaptureRegions(VD);
1703
308
    assert(VR);
1704
308
    assert(OriginalVR);
1705
308
    BV->push_back(VR, BC);
1706
308
    BVOriginal->push_back(OriginalVR, BC);
1707
308
  }
1708
1709
245
  ReferencedVars = BV;
1710
245
  OriginalVars = BVOriginal;
1711
245
}
1712
1713
BlockDataRegion::referenced_vars_iterator
1714
3.15k
BlockDataRegion::referenced_vars_begin() const {
1715
3.15k
  const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
1716
1717
3.15k
  auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
1718
1719
3.15k
  if (Vec == (void*) 0x1)
1720
761
    return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
1721
1722
2.38k
  auto *VecOriginal =
1723
2.38k
      static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
1724
1725
2.38k
  return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
1726
2.38k
                                                   VecOriginal->begin());
1727
3.15k
}
1728
1729
BlockDataRegion::referenced_vars_iterator
1730
3.15k
BlockDataRegion::referenced_vars_end() const {
1731
3.15k
  const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
1732
1733
3.15k
  auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
1734
1735
3.15k
  if (Vec == (void*) 0x1)
1736
761
    return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
1737
1738
2.38k
  auto *VecOriginal =
1739
2.38k
      static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
1740
1741
2.38k
  return BlockDataRegion::referenced_vars_iterator(Vec->end(),
1742
2.38k
                                                   VecOriginal->end());
1743
3.15k
}
1744
1745
llvm::iterator_range<BlockDataRegion::referenced_vars_iterator>
1746
3.15k
BlockDataRegion::referenced_vars() const {
1747
3.15k
  return llvm::make_range(referenced_vars_begin(), referenced_vars_end());
1748
3.15k
}
1749
1750
4
const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
1751
6
  for (const auto &I : referenced_vars()) {
1752
6
    if (I.getCapturedRegion() == R)
1753
4
      return I.getOriginalRegion();
1754
6
  }
1755
0
  return nullptr;
1756
4
}
1757
1758
//===----------------------------------------------------------------------===//
1759
// RegionAndSymbolInvalidationTraits
1760
//===----------------------------------------------------------------------===//
1761
1762
void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym,
1763
2.86k
                                                 InvalidationKinds IK) {
1764
2.86k
  SymTraitsMap[Sym] |= IK;
1765
2.86k
}
1766
1767
void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR,
1768
13.0k
                                                 InvalidationKinds IK) {
1769
13.0k
  assert(MR);
1770
13.0k
  if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
1771
2.75k
    setTrait(SR->getSymbol(), IK);
1772
10.2k
  else
1773
10.2k
    MRTraitsMap[MR] |= IK;
1774
13.0k
}
1775
1776
bool RegionAndSymbolInvalidationTraits::hasTrait(SymbolRef Sym,
1777
125k
                                                 InvalidationKinds IK) const {
1778
125k
  const_symbol_iterator I = SymTraitsMap.find(Sym);
1779
125k
  if (I != SymTraitsMap.end())
1780
9.81k
    return I->second & IK;
1781
1782
115k
  return false;
1783
125k
}
1784
1785
bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR,
1786
208k
                                                 InvalidationKinds IK) const {
1787
208k
  if (!MR)
1788
0
    return false;
1789
1790
208k
  if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
1791
22.7k
    return hasTrait(SR->getSymbol(), IK);
1792
1793
185k
  const_region_iterator I = MRTraitsMap.find(MR);
1794
185k
  if (I != MRTraitsMap.end())
1795
16.1k
    return I->second & IK;
1796
1797
169k
  return false;
1798
185k
}