1 //== MemRegion.cpp - Abstract memory regions for static analysis --*- C++ -*--//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines MemRegion and its subclasses. MemRegion defines a
11 // partially-typed abstraction of memory useful for path-sensitive dataflow
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/RecordLayout.h"
21 #include "clang/Analysis/AnalysisContext.h"
22 #include "clang/Analysis/Support/BumpVector.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // MemRegion Construction.
32 //===----------------------------------------------------------------------===//
34 template<typename RegionTy> struct MemRegionManagerTrait;
36 template <typename RegionTy, typename A1>
37 RegionTy* MemRegionManager::getRegion(const A1 a1) {
38 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
39 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
41 llvm::FoldingSetNodeID ID;
42 RegionTy::ProfileRegion(ID, a1, superRegion);
44 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
48 R = A.Allocate<RegionTy>();
49 new (R) RegionTy(a1, superRegion);
50 Regions.InsertNode(R, InsertPos);
56 template <typename RegionTy, typename A1>
57 RegionTy* MemRegionManager::getSubRegion(const A1 a1,
58 const MemRegion *superRegion) {
59 llvm::FoldingSetNodeID ID;
60 RegionTy::ProfileRegion(ID, a1, superRegion);
62 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
66 R = A.Allocate<RegionTy>();
67 new (R) RegionTy(a1, superRegion);
68 Regions.InsertNode(R, InsertPos);
74 template <typename RegionTy, typename A1, typename A2>
75 RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
76 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
77 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
79 llvm::FoldingSetNodeID ID;
80 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
82 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
86 R = A.Allocate<RegionTy>();
87 new (R) RegionTy(a1, a2, superRegion);
88 Regions.InsertNode(R, InsertPos);
94 template <typename RegionTy, typename A1, typename A2>
95 RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
96 const MemRegion *superRegion) {
97 llvm::FoldingSetNodeID ID;
98 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
100 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
104 R = A.Allocate<RegionTy>();
105 new (R) RegionTy(a1, a2, superRegion);
106 Regions.InsertNode(R, InsertPos);
112 template <typename RegionTy, typename A1, typename A2, typename A3>
113 RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3,
114 const MemRegion *superRegion) {
115 llvm::FoldingSetNodeID ID;
116 RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion);
118 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
122 R = A.Allocate<RegionTy>();
123 new (R) RegionTy(a1, a2, a3, superRegion);
124 Regions.InsertNode(R, InsertPos);
130 //===----------------------------------------------------------------------===//
131 // Object destruction.
132 //===----------------------------------------------------------------------===//
134 MemRegion::~MemRegion() {}
136 MemRegionManager::~MemRegionManager() {
137 // All regions and their data are BumpPtrAllocated. No need to call
138 // their destructors.
141 //===----------------------------------------------------------------------===//
143 //===----------------------------------------------------------------------===//
145 bool SubRegion::isSubRegionOf(const MemRegion* R) const {
146 const MemRegion* r = getSuperRegion();
147 while (r != nullptr) {
150 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
151 r = sr->getSuperRegion();
158 MemRegionManager* SubRegion::getMemRegionManager() const {
159 const SubRegion* r = this;
161 const MemRegion *superRegion = r->getSuperRegion();
162 if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
166 return superRegion->getMemRegionManager();
170 const StackFrameContext *VarRegion::getStackFrame() const {
171 const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
172 return SSR ? SSR->getStackFrame() : nullptr;
175 //===----------------------------------------------------------------------===//
177 //===----------------------------------------------------------------------===//
179 DefinedOrUnknownSVal TypedValueRegion::getExtent(SValBuilder &svalBuilder) const {
180 ASTContext &Ctx = svalBuilder.getContext();
181 QualType T = getDesugaredValueType(Ctx);
183 if (isa<VariableArrayType>(T))
184 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
185 if (T->isIncompleteType())
188 CharUnits size = Ctx.getTypeSizeInChars(T);
189 QualType sizeTy = svalBuilder.getArrayIndexType();
190 return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
193 DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
194 // Force callers to deal with bitfields explicitly.
195 if (getDecl()->isBitField())
198 DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
200 // A zero-length array at the end of a struct often stands for dynamically-
201 // allocated extra memory.
202 if (Extent.isZeroConstant()) {
203 QualType T = getDesugaredValueType(svalBuilder.getContext());
205 if (isa<ConstantArrayType>(T))
212 DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
213 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
216 DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
217 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
220 DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
221 return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
222 svalBuilder.getArrayIndexType());
225 ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg)
226 : DeclRegion(ivd, sReg, ObjCIvarRegionKind) {}
228 const ObjCIvarDecl *ObjCIvarRegion::getDecl() const {
229 return cast<ObjCIvarDecl>(D);
232 QualType ObjCIvarRegion::getValueType() const {
233 return getDecl()->getType();
236 QualType CXXBaseObjectRegion::getValueType() const {
237 return QualType(getDecl()->getTypeForDecl(), 0);
240 //===----------------------------------------------------------------------===//
241 // FoldingSet profiling.
242 //===----------------------------------------------------------------------===//
244 void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
245 ID.AddInteger(static_cast<unsigned>(getKind()));
248 void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
249 ID.AddInteger(static_cast<unsigned>(getKind()));
250 ID.AddPointer(getStackFrame());
253 void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
254 ID.AddInteger(static_cast<unsigned>(getKind()));
255 ID.AddPointer(getCodeRegion());
258 void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
259 const StringLiteral* Str,
260 const MemRegion* superRegion) {
261 ID.AddInteger(static_cast<unsigned>(StringRegionKind));
263 ID.AddPointer(superRegion);
266 void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
267 const ObjCStringLiteral* Str,
268 const MemRegion* superRegion) {
269 ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind));
271 ID.AddPointer(superRegion);
274 void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
275 const Expr *Ex, unsigned cnt,
276 const MemRegion *superRegion) {
277 ID.AddInteger(static_cast<unsigned>(AllocaRegionKind));
280 ID.AddPointer(superRegion);
283 void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
284 ProfileRegion(ID, Ex, Cnt, superRegion);
287 void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
288 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
291 void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
292 const CompoundLiteralExpr *CL,
293 const MemRegion* superRegion) {
294 ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind));
296 ID.AddPointer(superRegion);
299 void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
300 const PointerType *PT,
301 const MemRegion *sRegion) {
302 ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind));
304 ID.AddPointer(sRegion);
307 void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
308 CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
311 void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
312 const ObjCIvarDecl *ivd,
313 const MemRegion* superRegion) {
314 DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind);
317 void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
318 const MemRegion* superRegion, Kind k) {
319 ID.AddInteger(static_cast<unsigned>(k));
321 ID.AddPointer(superRegion);
324 void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
325 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
328 void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
329 VarRegion::ProfileRegion(ID, getDecl(), superRegion);
332 void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
333 const MemRegion *sreg) {
334 ID.AddInteger(static_cast<unsigned>(MemRegion::SymbolicRegionKind));
339 void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
340 SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
343 void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
344 QualType ElementType, SVal Idx,
345 const MemRegion* superRegion) {
346 ID.AddInteger(MemRegion::ElementRegionKind);
348 ID.AddPointer(superRegion);
352 void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
353 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
356 void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
359 ID.AddInteger(MemRegion::FunctionCodeRegionKind);
363 void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
364 FunctionCodeRegion::ProfileRegion(ID, FD, superRegion);
367 void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
368 const BlockDecl *BD, CanQualType,
369 const AnalysisDeclContext *AC,
371 ID.AddInteger(MemRegion::BlockCodeRegionKind);
375 void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
376 BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
379 void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
380 const BlockCodeRegion *BC,
381 const LocationContext *LC,
383 const MemRegion *sReg) {
384 ID.AddInteger(MemRegion::BlockDataRegionKind);
387 ID.AddInteger(BlkCount);
391 void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
392 BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion());
395 void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
397 const MemRegion *sReg) {
402 void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
403 ProfileRegion(ID, Ex, getSuperRegion());
406 void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
407 const CXXRecordDecl *RD,
409 const MemRegion *SReg) {
411 ID.AddBoolean(IsVirtual);
415 void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
416 ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
419 //===----------------------------------------------------------------------===//
421 //===----------------------------------------------------------------------===//
423 void GlobalsSpaceRegion::anchor() { }
424 void HeapSpaceRegion::anchor() { }
425 void UnknownSpaceRegion::anchor() { }
426 void StackLocalsSpaceRegion::anchor() { }
427 void StackArgumentsSpaceRegion::anchor() { }
428 void TypedRegion::anchor() { }
429 void TypedValueRegion::anchor() { }
430 void CodeTextRegion::anchor() { }
431 void SubRegion::anchor() { }
433 //===----------------------------------------------------------------------===//
434 // Region pretty-printing.
435 //===----------------------------------------------------------------------===//
437 LLVM_DUMP_METHOD void MemRegion::dump() const {
438 dumpToStream(llvm::errs());
441 std::string MemRegion::getString() const {
443 llvm::raw_string_ostream os(s);
448 void MemRegion::dumpToStream(raw_ostream &os) const {
449 os << "<Unknown Region>";
452 void AllocaRegion::dumpToStream(raw_ostream &os) const {
453 os << "alloca{" << static_cast<const void*>(Ex) << ',' << Cnt << '}';
456 void FunctionCodeRegion::dumpToStream(raw_ostream &os) const {
457 os << "code{" << getDecl()->getDeclName().getAsString() << '}';
460 void BlockCodeRegion::dumpToStream(raw_ostream &os) const {
461 os << "block_code{" << static_cast<const void*>(this) << '}';
464 void BlockDataRegion::dumpToStream(raw_ostream &os) const {
465 os << "block_data{" << BC;
467 for (BlockDataRegion::referenced_vars_iterator
468 I = referenced_vars_begin(),
469 E = referenced_vars_end(); I != E; ++I)
470 os << "(" << I.getCapturedRegion() << "," <<
471 I.getOriginalRegion() << ") ";
475 void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
476 // FIXME: More elaborate pretty-printing.
477 os << "{ " << static_cast<const void*>(CL) << " }";
480 void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
481 os << "temp_object{" << getValueType().getAsString() << ','
482 << static_cast<const void*>(Ex) << '}';
485 void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
486 os << "base{" << superRegion << ',' << getDecl()->getName() << '}';
489 void CXXThisRegion::dumpToStream(raw_ostream &os) const {
493 void ElementRegion::dumpToStream(raw_ostream &os) const {
494 os << "element{" << superRegion << ','
495 << Index << ',' << getElementType().getAsString() << '}';
498 void FieldRegion::dumpToStream(raw_ostream &os) const {
499 os << superRegion << "->" << *getDecl();
502 void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
503 os << "ivar{" << superRegion << ',' << *getDecl() << '}';
506 void StringRegion::dumpToStream(raw_ostream &os) const {
507 assert(Str != nullptr && "Expecting non-null StringLiteral");
508 Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
511 void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
512 assert(Str != nullptr && "Expecting non-null ObjCStringLiteral");
513 Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
516 void SymbolicRegion::dumpToStream(raw_ostream &os) const {
517 os << "SymRegion{" << sym << '}';
520 void VarRegion::dumpToStream(raw_ostream &os) const {
521 os << *cast<VarDecl>(D);
524 LLVM_DUMP_METHOD void RegionRawOffset::dump() const {
525 dumpToStream(llvm::errs());
528 void RegionRawOffset::dumpToStream(raw_ostream &os) const {
529 os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
532 void CodeSpaceRegion::dumpToStream(raw_ostream &os) const {
533 os << "CodeSpaceRegion";
536 void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
537 os << "StaticGlobalsMemSpace{" << CR << '}';
540 void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
541 os << "GlobalInternalSpaceRegion";
544 void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
545 os << "GlobalSystemSpaceRegion";
548 void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
549 os << "GlobalImmutableSpaceRegion";
552 void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
553 os << "HeapSpaceRegion";
556 void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
557 os << "UnknownSpaceRegion";
560 void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
561 os << "StackArgumentsSpaceRegion";
564 void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
565 os << "StackLocalsSpaceRegion";
568 bool MemRegion::canPrintPretty() const {
569 return canPrintPrettyAsExpr();
572 bool MemRegion::canPrintPrettyAsExpr() const {
576 void MemRegion::printPretty(raw_ostream &os) const {
577 assert(canPrintPretty() && "This region cannot be printed pretty.");
579 printPrettyAsExpr(os);
583 void MemRegion::printPrettyAsExpr(raw_ostream &os) const {
584 llvm_unreachable("This region cannot be printed pretty.");
587 bool VarRegion::canPrintPrettyAsExpr() const {
591 void VarRegion::printPrettyAsExpr(raw_ostream &os) const {
592 os << getDecl()->getName();
595 bool ObjCIvarRegion::canPrintPrettyAsExpr() const {
599 void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const {
600 os << getDecl()->getName();
603 bool FieldRegion::canPrintPretty() const {
607 bool FieldRegion::canPrintPrettyAsExpr() const {
608 return superRegion->canPrintPrettyAsExpr();
611 void FieldRegion::printPrettyAsExpr(raw_ostream &os) const {
612 assert(canPrintPrettyAsExpr());
613 superRegion->printPrettyAsExpr(os);
614 os << "." << getDecl()->getName();
617 void FieldRegion::printPretty(raw_ostream &os) const {
618 if (canPrintPrettyAsExpr()) {
620 printPrettyAsExpr(os);
623 os << "field " << "\'" << getDecl()->getName() << "'";
627 bool CXXBaseObjectRegion::canPrintPrettyAsExpr() const {
628 return superRegion->canPrintPrettyAsExpr();
631 void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
632 superRegion->printPrettyAsExpr(os);
635 std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
636 std::string VariableName;
637 std::string ArrayIndices;
638 const MemRegion *R = this;
640 llvm::raw_svector_ostream os(buf);
642 // Obtain array indices to add them to the variable name.
643 const ElementRegion *ER = nullptr;
644 while ((ER = R->getAs<ElementRegion>())) {
645 // Index is a ConcreteInt.
646 if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) {
647 llvm::SmallString<2> Idx;
648 CI->getValue().toString(Idx);
649 ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str();
651 // If not a ConcreteInt, try to obtain the variable
652 // name by calling 'getDescriptiveName' recursively.
654 std::string Idx = ER->getDescriptiveName(false);
656 ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str();
659 R = ER->getSuperRegion();
662 // Get variable name.
663 if (R && R->canPrintPrettyAsExpr()) {
664 R->printPrettyAsExpr(os);
666 return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str();
668 return (llvm::Twine(os.str()) + ArrayIndices).str();
675 SourceRange MemRegion::sourceRange() const {
676 const VarRegion *const VR = dyn_cast<VarRegion>(this->getBaseRegion());
677 const FieldRegion *const FR = dyn_cast<FieldRegion>(this);
679 // Check for more specific regions first.
682 return FR->getDecl()->getSourceRange();
686 return VR->getDecl()->getSourceRange();
688 // Return invalid source range (can be checked by client).
690 return SourceRange{};
694 //===----------------------------------------------------------------------===//
695 // MemRegionManager methods.
696 //===----------------------------------------------------------------------===//
698 template <typename REG>
699 const REG *MemRegionManager::LazyAllocate(REG*& region) {
701 region = A.Allocate<REG>();
702 new (region) REG(this);
708 template <typename REG, typename ARG>
709 const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
711 region = A.Allocate<REG>();
712 new (region) REG(this, a);
718 const StackLocalsSpaceRegion*
719 MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
721 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
726 R = A.Allocate<StackLocalsSpaceRegion>();
727 new (R) StackLocalsSpaceRegion(this, STC);
731 const StackArgumentsSpaceRegion *
732 MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
734 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
739 R = A.Allocate<StackArgumentsSpaceRegion>();
740 new (R) StackArgumentsSpaceRegion(this, STC);
744 const GlobalsSpaceRegion
745 *MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
746 const CodeTextRegion *CR) {
748 if (K == MemRegion::GlobalSystemSpaceRegionKind)
749 return LazyAllocate(SystemGlobals);
750 if (K == MemRegion::GlobalImmutableSpaceRegionKind)
751 return LazyAllocate(ImmutableGlobals);
752 assert(K == MemRegion::GlobalInternalSpaceRegionKind);
753 return LazyAllocate(InternalGlobals);
756 assert(K == MemRegion::StaticGlobalSpaceRegionKind);
757 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
761 R = A.Allocate<StaticGlobalSpaceRegion>();
762 new (R) StaticGlobalSpaceRegion(this, CR);
766 const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
767 return LazyAllocate(heap);
770 const UnknownSpaceRegion *MemRegionManager::getUnknownRegion() {
771 return LazyAllocate(unknown);
774 const CodeSpaceRegion *MemRegionManager::getCodeRegion() {
775 return LazyAllocate(code);
778 //===----------------------------------------------------------------------===//
779 // Constructing regions.
780 //===----------------------------------------------------------------------===//
781 const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
782 return getSubRegion<StringRegion>(Str, getGlobalsRegion());
785 const ObjCStringRegion *
786 MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){
787 return getSubRegion<ObjCStringRegion>(Str, getGlobalsRegion());
790 /// Look through a chain of LocationContexts to either find the
791 /// StackFrameContext that matches a DeclContext, or find a VarRegion
792 /// for a variable captured by a block.
793 static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
794 getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
795 const DeclContext *DC,
798 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
799 if (cast<DeclContext>(SFC->getDecl()) == DC)
802 if (const BlockInvocationContext *BC =
803 dyn_cast<BlockInvocationContext>(LC)) {
804 const BlockDataRegion *BR =
805 static_cast<const BlockDataRegion*>(BC->getContextData());
806 // FIXME: This can be made more efficient.
807 for (BlockDataRegion::referenced_vars_iterator
808 I = BR->referenced_vars_begin(),
809 E = BR->referenced_vars_end(); I != E; ++I) {
810 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getOriginalRegion()))
811 if (VR->getDecl() == VD)
812 return cast<VarRegion>(I.getCapturedRegion());
816 LC = LC->getParent();
818 return (const StackFrameContext *)nullptr;
821 const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
822 const LocationContext *LC) {
823 const MemRegion *sReg = nullptr;
825 if (D->hasGlobalStorage() && !D->isStaticLocal()) {
827 // First handle the globals defined in system headers.
828 if (C.getSourceManager().isInSystemHeader(D->getLocation())) {
829 // Whitelist the system globals which often DO GET modified, assume the
830 // rest are immutable.
831 if (D->getName().find("errno") != StringRef::npos)
832 sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
834 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
836 // Treat other globals as GlobalInternal unless they are constants.
838 QualType GQT = D->getType();
839 const Type *GT = GQT.getTypePtrOrNull();
840 // TODO: We could walk the complex types here and see if everything is
842 if (GT && GQT.isConstQualified() && GT->isArithmeticType())
843 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
845 sReg = getGlobalsRegion();
848 // Finally handle static locals.
850 // FIXME: Once we implement scope handling, we will need to properly lookup
851 // 'D' to the proper LocationContext.
852 const DeclContext *DC = D->getDeclContext();
853 llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
854 getStackOrCaptureRegionForDeclContext(LC, DC, D);
856 if (V.is<const VarRegion*>())
857 return V.get<const VarRegion*>();
859 const StackFrameContext *STC = V.get<const StackFrameContext*>();
862 sReg = getUnknownRegion();
864 if (D->hasLocalStorage()) {
865 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
866 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
867 : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
870 assert(D->isStaticLocal());
871 const Decl *STCD = STC->getDecl();
872 if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD))
873 sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
874 getFunctionCodeRegion(cast<NamedDecl>(STCD)));
875 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(STCD)) {
876 // FIXME: The fallback type here is totally bogus -- though it should
877 // never be queried, it will prevent uniquing with the real
878 // BlockCodeRegion. Ideally we'd fix the AST so that we always had a
881 if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
884 T = getContext().VoidTy;
885 if (!T->getAs<FunctionType>())
886 T = getContext().getFunctionNoProtoType(T);
887 T = getContext().getBlockPointerType(T);
889 const BlockCodeRegion *BTR =
890 getBlockCodeRegion(BD, C.getCanonicalType(T),
891 STC->getAnalysisDeclContext());
892 sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
896 sReg = getGlobalsRegion();
902 return getSubRegion<VarRegion>(D, sReg);
905 const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
906 const MemRegion *superR) {
907 return getSubRegion<VarRegion>(D, superR);
910 const BlockDataRegion *
911 MemRegionManager::getBlockDataRegion(const BlockCodeRegion *BC,
912 const LocationContext *LC,
913 unsigned blockCount) {
914 const MemRegion *sReg = nullptr;
915 const BlockDecl *BD = BC->getDecl();
916 if (!BD->hasCaptures()) {
917 // This handles 'static' blocks.
918 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
922 // FIXME: Once we implement scope handling, we want the parent region
924 const StackFrameContext *STC = LC->getCurrentStackFrame();
926 sReg = getStackLocalsRegion(STC);
929 // We allow 'LC' to be NULL for cases where want BlockDataRegions
930 // without context-sensitivity.
931 sReg = getUnknownRegion();
935 return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg);
938 const CXXTempObjectRegion *
939 MemRegionManager::getCXXStaticTempObjectRegion(const Expr *Ex) {
940 return getSubRegion<CXXTempObjectRegion>(
941 Ex, getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr));
944 const CompoundLiteralRegion*
945 MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
946 const LocationContext *LC) {
947 const MemRegion *sReg = nullptr;
949 if (CL->isFileScope())
950 sReg = getGlobalsRegion();
952 const StackFrameContext *STC = LC->getCurrentStackFrame();
954 sReg = getStackLocalsRegion(STC);
957 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
961 MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
962 const MemRegion* superRegion,
964 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
966 llvm::FoldingSetNodeID ID;
967 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
970 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
971 ElementRegion* R = cast_or_null<ElementRegion>(data);
974 R = A.Allocate<ElementRegion>();
975 new (R) ElementRegion(T, Idx, superRegion);
976 Regions.InsertNode(R, InsertPos);
982 const FunctionCodeRegion *
983 MemRegionManager::getFunctionCodeRegion(const NamedDecl *FD) {
984 return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion());
987 const BlockCodeRegion *
988 MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy,
989 AnalysisDeclContext *AC) {
990 return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion());
994 /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
995 const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
996 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
999 const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
1000 return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
1004 MemRegionManager::getFieldRegion(const FieldDecl *d,
1005 const MemRegion* superRegion){
1006 return getSubRegion<FieldRegion>(d, superRegion);
1009 const ObjCIvarRegion*
1010 MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
1011 const MemRegion* superRegion) {
1012 return getSubRegion<ObjCIvarRegion>(d, superRegion);
1015 const CXXTempObjectRegion*
1016 MemRegionManager::getCXXTempObjectRegion(Expr const *E,
1017 LocationContext const *LC) {
1018 const StackFrameContext *SFC = LC->getCurrentStackFrame();
1020 return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
1023 /// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
1024 /// class of the type of \p Super.
1025 static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
1026 const TypedValueRegion *Super,
1028 BaseClass = BaseClass->getCanonicalDecl();
1030 const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
1035 return Class->isVirtuallyDerivedFrom(BaseClass);
1037 for (const auto &I : Class->bases()) {
1038 if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
1045 const CXXBaseObjectRegion *
1046 MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
1047 const MemRegion *Super,
1049 if (isa<TypedValueRegion>(Super)) {
1050 assert(isValidBaseClass(RD, dyn_cast<TypedValueRegion>(Super), IsVirtual));
1051 (void)&isValidBaseClass;
1054 // Virtual base regions should not be layered, since the layout rules
1056 while (const CXXBaseObjectRegion *Base =
1057 dyn_cast<CXXBaseObjectRegion>(Super)) {
1058 Super = Base->getSuperRegion();
1060 assert(Super && !isa<MemSpaceRegion>(Super));
1064 return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
1067 const CXXThisRegion*
1068 MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
1069 const LocationContext *LC) {
1070 const PointerType *PT = thisPointerTy->getAs<PointerType>();
1072 // Inside the body of the operator() of a lambda a this expr might refer to an
1073 // object in one of the parent location contexts.
1074 const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl());
1075 // FIXME: when operator() of lambda is analyzed as a top level function and
1076 // 'this' refers to a this to the enclosing scope, there is no right region to
1078 while (!LC->inTopFrame() &&
1079 (!D || D->isStatic() ||
1080 PT != D->getThisType(getContext())->getAs<PointerType>())) {
1081 LC = LC->getParent();
1082 D = dyn_cast<CXXMethodDecl>(LC->getDecl());
1084 const StackFrameContext *STC = LC->getCurrentStackFrame();
1086 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
1090 MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
1091 const LocationContext *LC) {
1092 const StackFrameContext *STC = LC->getCurrentStackFrame();
1094 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
1097 const MemSpaceRegion *MemRegion::getMemorySpace() const {
1098 const MemRegion *R = this;
1099 const SubRegion* SR = dyn_cast<SubRegion>(this);
1102 R = SR->getSuperRegion();
1103 SR = dyn_cast<SubRegion>(R);
1106 return dyn_cast<MemSpaceRegion>(R);
1109 bool MemRegion::hasStackStorage() const {
1110 return isa<StackSpaceRegion>(getMemorySpace());
1113 bool MemRegion::hasStackNonParametersStorage() const {
1114 return isa<StackLocalsSpaceRegion>(getMemorySpace());
1117 bool MemRegion::hasStackParametersStorage() const {
1118 return isa<StackArgumentsSpaceRegion>(getMemorySpace());
1121 bool MemRegion::hasGlobalsOrParametersStorage() const {
1122 const MemSpaceRegion *MS = getMemorySpace();
1123 return isa<StackArgumentsSpaceRegion>(MS) ||
1124 isa<GlobalsSpaceRegion>(MS);
1127 // getBaseRegion strips away all elements and fields, and get the base region
1129 const MemRegion *MemRegion::getBaseRegion() const {
1130 const MemRegion *R = this;
1132 switch (R->getKind()) {
1133 case MemRegion::ElementRegionKind:
1134 case MemRegion::FieldRegionKind:
1135 case MemRegion::ObjCIvarRegionKind:
1136 case MemRegion::CXXBaseObjectRegionKind:
1137 R = cast<SubRegion>(R)->getSuperRegion();
1147 bool MemRegion::isSubRegionOf(const MemRegion *R) const {
1151 //===----------------------------------------------------------------------===//
1153 //===----------------------------------------------------------------------===//
1155 const MemRegion *MemRegion::StripCasts(bool StripBaseCasts) const {
1156 const MemRegion *R = this;
1158 switch (R->getKind()) {
1159 case ElementRegionKind: {
1160 const ElementRegion *ER = cast<ElementRegion>(R);
1161 if (!ER->getIndex().isZeroConstant())
1163 R = ER->getSuperRegion();
1166 case CXXBaseObjectRegionKind:
1167 if (!StripBaseCasts)
1169 R = cast<CXXBaseObjectRegion>(R)->getSuperRegion();
1177 const SymbolicRegion *MemRegion::getSymbolicBase() const {
1178 const SubRegion *SubR = dyn_cast<SubRegion>(this);
1181 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR))
1183 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
1188 RegionRawOffset ElementRegion::getAsArrayOffset() const {
1189 CharUnits offset = CharUnits::Zero();
1190 const ElementRegion *ER = this;
1191 const MemRegion *superR = nullptr;
1192 ASTContext &C = getContext();
1194 // FIXME: Handle multi-dimensional arrays.
1197 superR = ER->getSuperRegion();
1199 // FIXME: generalize to symbolic offsets.
1200 SVal index = ER->getIndex();
1201 if (Optional<nonloc::ConcreteInt> CI = index.getAs<nonloc::ConcreteInt>()) {
1202 // Update the offset.
1203 int64_t i = CI->getValue().getSExtValue();
1206 QualType elemType = ER->getElementType();
1208 // If we are pointing to an incomplete type, go no further.
1209 if (elemType->isIncompleteType()) {
1214 CharUnits size = C.getTypeSizeInChars(elemType);
1215 offset += (i * size);
1218 // Go to the next ElementRegion (if any).
1219 ER = dyn_cast<ElementRegion>(superR);
1226 assert(superR && "super region cannot be NULL");
1227 return RegionRawOffset(superR, offset);
1231 /// Returns true if \p Base is an immediate base class of \p Child
1232 static bool isImmediateBase(const CXXRecordDecl *Child,
1233 const CXXRecordDecl *Base) {
1234 assert(Child && "Child must not be null");
1235 // Note that we do NOT canonicalize the base class here, because
1236 // ASTRecordLayout doesn't either. If that leads us down the wrong path,
1237 // so be it; at least we won't crash.
1238 for (const auto &I : Child->bases()) {
1239 if (I.getType()->getAsCXXRecordDecl() == Base)
1246 RegionOffset MemRegion::getAsOffset() const {
1247 const MemRegion *R = this;
1248 const MemRegion *SymbolicOffsetBase = nullptr;
1252 switch (R->getKind()) {
1253 case CodeSpaceRegionKind:
1254 case StackLocalsSpaceRegionKind:
1255 case StackArgumentsSpaceRegionKind:
1256 case HeapSpaceRegionKind:
1257 case UnknownSpaceRegionKind:
1258 case StaticGlobalSpaceRegionKind:
1259 case GlobalInternalSpaceRegionKind:
1260 case GlobalSystemSpaceRegionKind:
1261 case GlobalImmutableSpaceRegionKind:
1262 // Stores can bind directly to a region space to set a default value.
1263 assert(Offset == 0 && !SymbolicOffsetBase);
1266 case FunctionCodeRegionKind:
1267 case BlockCodeRegionKind:
1268 case BlockDataRegionKind:
1269 // These will never have bindings, but may end up having values requested
1270 // if the user does some strange casting.
1272 SymbolicOffsetBase = R;
1275 case SymbolicRegionKind:
1276 case AllocaRegionKind:
1277 case CompoundLiteralRegionKind:
1278 case CXXThisRegionKind:
1279 case StringRegionKind:
1280 case ObjCStringRegionKind:
1282 case CXXTempObjectRegionKind:
1283 // Usual base regions.
1286 case ObjCIvarRegionKind:
1287 // This is a little strange, but it's a compromise between
1288 // ObjCIvarRegions having unknown compile-time offsets (when using the
1289 // non-fragile runtime) and yet still being distinct, non-overlapping
1290 // regions. Thus we treat them as "like" base regions for the purposes
1291 // of computing offsets.
1294 case CXXBaseObjectRegionKind: {
1295 const CXXBaseObjectRegion *BOR = cast<CXXBaseObjectRegion>(R);
1296 R = BOR->getSuperRegion();
1299 bool RootIsSymbolic = false;
1300 if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) {
1301 Ty = TVR->getDesugaredValueType(getContext());
1302 } else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1303 // If our base region is symbolic, we don't know what type it really is.
1304 // Pretend the type of the symbol is the true dynamic type.
1305 // (This will at least be self-consistent for the life of the symbol.)
1306 Ty = SR->getSymbol()->getType()->getPointeeType();
1307 RootIsSymbolic = true;
1310 const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
1312 // We cannot compute the offset of the base class.
1313 SymbolicOffsetBase = R;
1315 if (RootIsSymbolic) {
1316 // Base layers on symbolic regions may not be type-correct.
1317 // Double-check the inheritance here, and revert to a symbolic offset
1318 // if it's invalid (e.g. due to a reinterpret_cast).
1319 if (BOR->isVirtual()) {
1320 if (!Child->isVirtuallyDerivedFrom(BOR->getDecl()))
1321 SymbolicOffsetBase = R;
1323 if (!isImmediateBase(Child, BOR->getDecl()))
1324 SymbolicOffsetBase = R;
1329 // Don't bother calculating precise offsets if we already have a
1330 // symbolic offset somewhere in the chain.
1331 if (SymbolicOffsetBase)
1334 CharUnits BaseOffset;
1335 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Child);
1336 if (BOR->isVirtual())
1337 BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
1339 BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
1341 // The base offset is in chars, not in bits.
1342 Offset += BaseOffset.getQuantity() * getContext().getCharWidth();
1345 case ElementRegionKind: {
1346 const ElementRegion *ER = cast<ElementRegion>(R);
1347 R = ER->getSuperRegion();
1349 QualType EleTy = ER->getValueType();
1350 if (EleTy->isIncompleteType()) {
1351 // We cannot compute the offset of the base class.
1352 SymbolicOffsetBase = R;
1356 SVal Index = ER->getIndex();
1357 if (Optional<nonloc::ConcreteInt> CI =
1358 Index.getAs<nonloc::ConcreteInt>()) {
1359 // Don't bother calculating precise offsets if we already have a
1360 // symbolic offset somewhere in the chain.
1361 if (SymbolicOffsetBase)
1364 int64_t i = CI->getValue().getSExtValue();
1365 // This type size is in bits.
1366 Offset += i * getContext().getTypeSize(EleTy);
1368 // We cannot compute offset for non-concrete index.
1369 SymbolicOffsetBase = R;
1373 case FieldRegionKind: {
1374 const FieldRegion *FR = cast<FieldRegion>(R);
1375 R = FR->getSuperRegion();
1377 const RecordDecl *RD = FR->getDecl()->getParent();
1378 if (RD->isUnion() || !RD->isCompleteDefinition()) {
1379 // We cannot compute offset for incomplete type.
1380 // For unions, we could treat everything as offset 0, but we'd rather
1381 // treat each field as a symbolic offset so they aren't stored on top
1382 // of each other, since we depend on things in typed regions actually
1383 // matching their types.
1384 SymbolicOffsetBase = R;
1387 // Don't bother calculating precise offsets if we already have a
1388 // symbolic offset somewhere in the chain.
1389 if (SymbolicOffsetBase)
1392 // Get the field number.
1394 for (RecordDecl::field_iterator FI = RD->field_begin(),
1395 FE = RD->field_end(); FI != FE; ++FI, ++idx) {
1396 if (FR->getDecl() == *FI)
1399 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1400 // This is offset in bits.
1401 Offset += Layout.getFieldOffset(idx);
1408 if (SymbolicOffsetBase)
1409 return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
1410 return RegionOffset(R, Offset);
1413 //===----------------------------------------------------------------------===//
1415 //===----------------------------------------------------------------------===//
1417 std::pair<const VarRegion *, const VarRegion *>
1418 BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
1419 MemRegionManager &MemMgr = *getMemRegionManager();
1420 const VarRegion *VR = nullptr;
1421 const VarRegion *OriginalVR = nullptr;
1423 if (!VD->hasAttr<BlocksAttr>() && VD->hasLocalStorage()) {
1424 VR = MemMgr.getVarRegion(VD, this);
1425 OriginalVR = MemMgr.getVarRegion(VD, LC);
1429 VR = MemMgr.getVarRegion(VD, LC);
1433 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
1434 OriginalVR = MemMgr.getVarRegion(VD, LC);
1437 return std::make_pair(VR, OriginalVR);
1440 void BlockDataRegion::LazyInitializeReferencedVars() {
1444 AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
1445 const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl());
1447 std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end());
1449 if (NumBlockVars == 0) {
1450 ReferencedVars = (void*) 0x1;
1454 MemRegionManager &MemMgr = *getMemRegionManager();
1455 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
1456 BumpVectorContext BC(A);
1458 typedef BumpVector<const MemRegion*> VarVec;
1459 VarVec *BV = A.Allocate<VarVec>();
1460 new (BV) VarVec(BC, NumBlockVars);
1461 VarVec *BVOriginal = A.Allocate<VarVec>();
1462 new (BVOriginal) VarVec(BC, NumBlockVars);
1464 for (const VarDecl *VD : ReferencedBlockVars) {
1465 const VarRegion *VR = nullptr;
1466 const VarRegion *OriginalVR = nullptr;
1467 std::tie(VR, OriginalVR) = getCaptureRegions(VD);
1470 BV->push_back(VR, BC);
1471 BVOriginal->push_back(OriginalVR, BC);
1474 ReferencedVars = BV;
1475 OriginalVars = BVOriginal;
1478 BlockDataRegion::referenced_vars_iterator
1479 BlockDataRegion::referenced_vars_begin() const {
1480 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
1482 BumpVector<const MemRegion*> *Vec =
1483 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
1485 if (Vec == (void*) 0x1)
1486 return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
1488 BumpVector<const MemRegion*> *VecOriginal =
1489 static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
1491 return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
1492 VecOriginal->begin());
1495 BlockDataRegion::referenced_vars_iterator
1496 BlockDataRegion::referenced_vars_end() const {
1497 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
1499 BumpVector<const MemRegion*> *Vec =
1500 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
1502 if (Vec == (void*) 0x1)
1503 return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
1505 BumpVector<const MemRegion*> *VecOriginal =
1506 static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
1508 return BlockDataRegion::referenced_vars_iterator(Vec->end(),
1509 VecOriginal->end());
1512 const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
1513 for (referenced_vars_iterator I = referenced_vars_begin(),
1514 E = referenced_vars_end();
1516 if (I.getCapturedRegion() == R)
1517 return I.getOriginalRegion();
1522 //===----------------------------------------------------------------------===//
1523 // RegionAndSymbolInvalidationTraits
1524 //===----------------------------------------------------------------------===//
1526 void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym,
1527 InvalidationKinds IK) {
1528 SymTraitsMap[Sym] |= IK;
1531 void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR,
1532 InvalidationKinds IK) {
1534 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1535 setTrait(SR->getSymbol(), IK);
1537 MRTraitsMap[MR] |= IK;
1540 bool RegionAndSymbolInvalidationTraits::hasTrait(SymbolRef Sym,
1541 InvalidationKinds IK) const {
1542 const_symbol_iterator I = SymTraitsMap.find(Sym);
1543 if (I != SymTraitsMap.end())
1544 return I->second & IK;
1549 bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR,
1550 InvalidationKinds IK) const {
1554 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1555 return hasTrait(SR->getSymbol(), IK);
1557 const_region_iterator I = MRTraitsMap.find(MR);
1558 if (I != MRTraitsMap.end())
1559 return I->second & IK;