Coverage Report

Created: 2020-09-19 12:23

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