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/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
18 #include "clang/Analysis/AnalysisContext.h"
19 #include "clang/Analysis/Support/BumpVector.h"
20 #include "clang/AST/CharUnits.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
27 //===----------------------------------------------------------------------===//
28 // MemRegion Construction.
29 //===----------------------------------------------------------------------===//
31 template<typename RegionTy> struct MemRegionManagerTrait;
33 template <typename RegionTy, typename A1>
34 RegionTy* MemRegionManager::getRegion(const A1 a1) {
36 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
37 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
39 llvm::FoldingSetNodeID ID;
40 RegionTy::ProfileRegion(ID, a1, superRegion);
42 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
46 R = (RegionTy*) A.Allocate<RegionTy>();
47 new (R) RegionTy(a1, superRegion);
48 Regions.InsertNode(R, InsertPos);
54 template <typename RegionTy, typename A1>
55 RegionTy* MemRegionManager::getSubRegion(const A1 a1,
56 const MemRegion *superRegion) {
57 llvm::FoldingSetNodeID ID;
58 RegionTy::ProfileRegion(ID, a1, superRegion);
60 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
64 R = (RegionTy*) A.Allocate<RegionTy>();
65 new (R) RegionTy(a1, superRegion);
66 Regions.InsertNode(R, InsertPos);
72 template <typename RegionTy, typename A1, typename A2>
73 RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
75 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
76 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
78 llvm::FoldingSetNodeID ID;
79 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
81 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
85 R = (RegionTy*) A.Allocate<RegionTy>();
86 new (R) RegionTy(a1, a2, superRegion);
87 Regions.InsertNode(R, InsertPos);
93 template <typename RegionTy, typename A1, typename A2>
94 RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
95 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 = (RegionTy*) 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) {
116 llvm::FoldingSetNodeID ID;
117 RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion);
119 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
123 R = (RegionTy*) A.Allocate<RegionTy>();
124 new (R) RegionTy(a1, a2, a3, superRegion);
125 Regions.InsertNode(R, InsertPos);
131 //===----------------------------------------------------------------------===//
132 // Object destruction.
133 //===----------------------------------------------------------------------===//
135 MemRegion::~MemRegion() {}
137 MemRegionManager::~MemRegionManager() {
138 // All regions and their data are BumpPtrAllocated. No need to call
139 // their destructors.
142 //===----------------------------------------------------------------------===//
144 //===----------------------------------------------------------------------===//
146 bool SubRegion::isSubRegionOf(const MemRegion* R) const {
147 const MemRegion* r = getSuperRegion();
151 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
152 r = sr->getSuperRegion();
159 MemRegionManager* SubRegion::getMemRegionManager() const {
160 const SubRegion* r = this;
162 const MemRegion *superRegion = r->getSuperRegion();
163 if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
167 return superRegion->getMemRegionManager();
171 const StackFrameContext *VarRegion::getStackFrame() const {
172 const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
173 return SSR ? SSR->getStackFrame() : NULL;
176 //===----------------------------------------------------------------------===//
178 //===----------------------------------------------------------------------===//
180 DefinedOrUnknownSVal DeclRegion::getExtent(SValBuilder &svalBuilder) const {
181 ASTContext &Ctx = svalBuilder.getContext();
182 QualType T = getDesugaredValueType(Ctx);
184 if (isa<VariableArrayType>(T))
185 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
186 if (isa<IncompleteArrayType>(T))
189 CharUnits size = Ctx.getTypeSizeInChars(T);
190 QualType sizeTy = svalBuilder.getArrayIndexType();
191 return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
194 DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
195 DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
197 // A zero-length array at the end of a struct often stands for dynamically-
198 // allocated extra memory.
199 if (Extent.isZeroConstant()) {
200 QualType T = getDesugaredValueType(svalBuilder.getContext());
202 if (isa<ConstantArrayType>(T))
209 DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
210 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
213 DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
214 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
217 DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
218 return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
219 svalBuilder.getArrayIndexType());
222 QualType CXXBaseObjectRegion::getValueType() const {
223 return QualType(decl->getTypeForDecl(), 0);
226 //===----------------------------------------------------------------------===//
227 // FoldingSet profiling.
228 //===----------------------------------------------------------------------===//
230 void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
231 ID.AddInteger((unsigned)getKind());
234 void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
235 ID.AddInteger((unsigned)getKind());
236 ID.AddPointer(getStackFrame());
239 void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
240 ID.AddInteger((unsigned)getKind());
241 ID.AddPointer(getCodeRegion());
244 void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
245 const StringLiteral* Str,
246 const MemRegion* superRegion) {
247 ID.AddInteger((unsigned) StringRegionKind);
249 ID.AddPointer(superRegion);
252 void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
253 const Expr *Ex, unsigned cnt,
255 ID.AddInteger((unsigned) AllocaRegionKind);
260 void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
261 ProfileRegion(ID, Ex, Cnt, superRegion);
264 void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
265 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
268 void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
269 const CompoundLiteralExpr *CL,
270 const MemRegion* superRegion) {
271 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
273 ID.AddPointer(superRegion);
276 void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
277 const PointerType *PT,
278 const MemRegion *sRegion) {
279 ID.AddInteger((unsigned) CXXThisRegionKind);
281 ID.AddPointer(sRegion);
284 void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
285 CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
288 void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
289 const MemRegion* superRegion, Kind k) {
290 ID.AddInteger((unsigned) k);
292 ID.AddPointer(superRegion);
295 void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
296 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
299 void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
300 VarRegion::ProfileRegion(ID, getDecl(), superRegion);
303 void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
304 const MemRegion *sreg) {
305 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
310 void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
311 SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
314 void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
315 QualType ElementType, SVal Idx,
316 const MemRegion* superRegion) {
317 ID.AddInteger(MemRegion::ElementRegionKind);
319 ID.AddPointer(superRegion);
323 void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
324 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
327 void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
328 const FunctionDecl *FD,
330 ID.AddInteger(MemRegion::FunctionTextRegionKind);
334 void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
335 FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
338 void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
339 const BlockDecl *BD, CanQualType,
340 const AnalysisContext *AC,
342 ID.AddInteger(MemRegion::BlockTextRegionKind);
346 void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
347 BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
350 void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
351 const BlockTextRegion *BC,
352 const LocationContext *LC,
353 const MemRegion *sReg) {
354 ID.AddInteger(MemRegion::BlockDataRegionKind);
360 void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
361 BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
364 void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
366 const MemRegion *sReg) {
371 void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
372 ProfileRegion(ID, Ex, getSuperRegion());
375 void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
376 const CXXRecordDecl *decl,
377 const MemRegion *sReg) {
382 void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
383 ProfileRegion(ID, decl, superRegion);
386 //===----------------------------------------------------------------------===//
387 // Region pretty-printing.
388 //===----------------------------------------------------------------------===//
390 void MemRegion::dump() const {
391 dumpToStream(llvm::errs());
394 std::string MemRegion::getString() const {
396 llvm::raw_string_ostream os(s);
401 void MemRegion::dumpToStream(raw_ostream &os) const {
402 os << "<Unknown Region>";
405 void AllocaRegion::dumpToStream(raw_ostream &os) const {
406 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
409 void FunctionTextRegion::dumpToStream(raw_ostream &os) const {
410 os << "code{" << getDecl()->getDeclName().getAsString() << '}';
413 void BlockTextRegion::dumpToStream(raw_ostream &os) const {
414 os << "block_code{" << (void*) this << '}';
417 void BlockDataRegion::dumpToStream(raw_ostream &os) const {
418 os << "block_data{" << BC << '}';
421 void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
422 // FIXME: More elaborate pretty-printing.
423 os << "{ " << (void*) CL << " }";
426 void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
427 os << "temp_object{" << getValueType().getAsString() << ','
428 << (void*) Ex << '}';
431 void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
432 os << "base " << decl->getName();
435 void CXXThisRegion::dumpToStream(raw_ostream &os) const {
439 void ElementRegion::dumpToStream(raw_ostream &os) const {
440 os << "element{" << superRegion << ','
441 << Index << ',' << getElementType().getAsString() << '}';
444 void FieldRegion::dumpToStream(raw_ostream &os) const {
445 os << superRegion << "->" << *getDecl();
448 void NonStaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
449 os << "NonStaticGlobalSpaceRegion";
452 void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
453 os << "ivar{" << superRegion << ',' << *getDecl() << '}';
456 void StringRegion::dumpToStream(raw_ostream &os) const {
457 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions()));
460 void SymbolicRegion::dumpToStream(raw_ostream &os) const {
461 os << "SymRegion{" << sym << '}';
464 void VarRegion::dumpToStream(raw_ostream &os) const {
465 os << *cast<VarDecl>(D);
468 void RegionRawOffset::dump() const {
469 dumpToStream(llvm::errs());
472 void RegionRawOffset::dumpToStream(raw_ostream &os) const {
473 os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
476 void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
477 os << "StaticGlobalsMemSpace{" << CR << '}';
480 //===----------------------------------------------------------------------===//
481 // MemRegionManager methods.
482 //===----------------------------------------------------------------------===//
484 template <typename REG>
485 const REG *MemRegionManager::LazyAllocate(REG*& region) {
487 region = (REG*) A.Allocate<REG>();
488 new (region) REG(this);
494 template <typename REG, typename ARG>
495 const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
497 region = (REG*) A.Allocate<REG>();
498 new (region) REG(this, a);
504 const StackLocalsSpaceRegion*
505 MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
507 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
512 R = A.Allocate<StackLocalsSpaceRegion>();
513 new (R) StackLocalsSpaceRegion(this, STC);
517 const StackArgumentsSpaceRegion *
518 MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
520 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
525 R = A.Allocate<StackArgumentsSpaceRegion>();
526 new (R) StackArgumentsSpaceRegion(this, STC);
530 const GlobalsSpaceRegion
531 *MemRegionManager::getGlobalsRegion(const CodeTextRegion *CR) {
533 return LazyAllocate(globals);
535 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
539 R = A.Allocate<StaticGlobalSpaceRegion>();
540 new (R) StaticGlobalSpaceRegion(this, CR);
544 const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
545 return LazyAllocate(heap);
548 const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
549 return LazyAllocate(unknown);
552 const MemSpaceRegion *MemRegionManager::getCodeRegion() {
553 return LazyAllocate(code);
556 //===----------------------------------------------------------------------===//
557 // Constructing regions.
558 //===----------------------------------------------------------------------===//
560 const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
561 return getSubRegion<StringRegion>(Str, getGlobalsRegion());
564 const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
565 const LocationContext *LC) {
566 const MemRegion *sReg = 0;
568 if (D->hasGlobalStorage() && !D->isStaticLocal())
569 sReg = getGlobalsRegion();
571 // FIXME: Once we implement scope handling, we will need to properly lookup
572 // 'D' to the proper LocationContext.
573 const DeclContext *DC = D->getDeclContext();
574 const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC);
577 sReg = getUnknownRegion();
579 if (D->hasLocalStorage()) {
580 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
581 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
582 : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
585 assert(D->isStaticLocal());
586 const Decl *D = STC->getDecl();
587 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
588 sReg = getGlobalsRegion(getFunctionTextRegion(FD));
589 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
590 const BlockTextRegion *BTR =
591 getBlockTextRegion(BD,
592 C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
593 STC->getAnalysisContext());
594 sReg = getGlobalsRegion(BTR);
597 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now
598 // just use the main global memspace.
599 sReg = getGlobalsRegion();
605 return getSubRegion<VarRegion>(D, sReg);
608 const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
609 const MemRegion *superR) {
610 return getSubRegion<VarRegion>(D, superR);
613 const BlockDataRegion *
614 MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
615 const LocationContext *LC) {
616 const MemRegion *sReg = 0;
619 // FIXME: Once we implement scope handling, we want the parent region
621 const StackFrameContext *STC = LC->getCurrentStackFrame();
623 sReg = getStackLocalsRegion(STC);
626 // We allow 'LC' to be NULL for cases where want BlockDataRegions
627 // without context-sensitivity.
628 sReg = getUnknownRegion();
631 return getSubRegion<BlockDataRegion>(BC, LC, sReg);
634 const CompoundLiteralRegion*
635 MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
636 const LocationContext *LC) {
638 const MemRegion *sReg = 0;
640 if (CL->isFileScope())
641 sReg = getGlobalsRegion();
643 const StackFrameContext *STC = LC->getCurrentStackFrame();
645 sReg = getStackLocalsRegion(STC);
648 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
652 MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
653 const MemRegion* superRegion,
656 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
658 llvm::FoldingSetNodeID ID;
659 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
662 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
663 ElementRegion* R = cast_or_null<ElementRegion>(data);
666 R = (ElementRegion*) A.Allocate<ElementRegion>();
667 new (R) ElementRegion(T, Idx, superRegion);
668 Regions.InsertNode(R, InsertPos);
674 const FunctionTextRegion *
675 MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) {
676 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
679 const BlockTextRegion *
680 MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
681 AnalysisContext *AC) {
682 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
686 /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
687 const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
688 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
692 MemRegionManager::getFieldRegion(const FieldDecl *d,
693 const MemRegion* superRegion){
694 return getSubRegion<FieldRegion>(d, superRegion);
697 const ObjCIvarRegion*
698 MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
699 const MemRegion* superRegion) {
700 return getSubRegion<ObjCIvarRegion>(d, superRegion);
703 const CXXTempObjectRegion*
704 MemRegionManager::getCXXTempObjectRegion(Expr const *E,
705 LocationContext const *LC) {
706 const StackFrameContext *SFC = LC->getCurrentStackFrame();
708 return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
711 const CXXBaseObjectRegion *
712 MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *decl,
713 const MemRegion *superRegion) {
714 return getSubRegion<CXXBaseObjectRegion>(decl, superRegion);
718 MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
719 const LocationContext *LC) {
720 const StackFrameContext *STC = LC->getCurrentStackFrame();
722 const PointerType *PT = thisPointerTy->getAs<PointerType>();
724 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
728 MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
729 const LocationContext *LC) {
730 const StackFrameContext *STC = LC->getCurrentStackFrame();
732 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
735 const MemSpaceRegion *MemRegion::getMemorySpace() const {
736 const MemRegion *R = this;
737 const SubRegion* SR = dyn_cast<SubRegion>(this);
740 R = SR->getSuperRegion();
741 SR = dyn_cast<SubRegion>(R);
744 return dyn_cast<MemSpaceRegion>(R);
747 bool MemRegion::hasStackStorage() const {
748 return isa<StackSpaceRegion>(getMemorySpace());
751 bool MemRegion::hasStackNonParametersStorage() const {
752 return isa<StackLocalsSpaceRegion>(getMemorySpace());
755 bool MemRegion::hasStackParametersStorage() const {
756 return isa<StackArgumentsSpaceRegion>(getMemorySpace());
759 bool MemRegion::hasGlobalsOrParametersStorage() const {
760 const MemSpaceRegion *MS = getMemorySpace();
761 return isa<StackArgumentsSpaceRegion>(MS) ||
762 isa<GlobalsSpaceRegion>(MS);
765 // getBaseRegion strips away all elements and fields, and get the base region
767 const MemRegion *MemRegion::getBaseRegion() const {
768 const MemRegion *R = this;
770 switch (R->getKind()) {
771 case MemRegion::ElementRegionKind:
772 case MemRegion::FieldRegionKind:
773 case MemRegion::ObjCIvarRegionKind:
774 case MemRegion::CXXBaseObjectRegionKind:
775 R = cast<SubRegion>(R)->getSuperRegion();
785 //===----------------------------------------------------------------------===//
787 //===----------------------------------------------------------------------===//
789 const MemRegion *MemRegion::StripCasts() const {
790 const MemRegion *R = this;
792 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
793 // FIXME: generalize. Essentially we want to strip away ElementRegions
794 // that were layered on a symbolic region because of casts. We only
795 // want to strip away ElementRegions, however, where the index is 0.
796 SVal index = ER->getIndex();
797 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
798 if (CI->getValue().getSExtValue() == 0) {
799 R = ER->getSuperRegion();
809 // FIXME: Merge with the implementation of the same method in Store.cpp
810 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
811 if (const RecordType *RT = Ty->getAs<RecordType>()) {
812 const RecordDecl *D = RT->getDecl();
813 if (!D->getDefinition())
820 RegionRawOffset ElementRegion::getAsArrayOffset() const {
821 CharUnits offset = CharUnits::Zero();
822 const ElementRegion *ER = this;
823 const MemRegion *superR = NULL;
824 ASTContext &C = getContext();
826 // FIXME: Handle multi-dimensional arrays.
829 superR = ER->getSuperRegion();
831 // FIXME: generalize to symbolic offsets.
832 SVal index = ER->getIndex();
833 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
834 // Update the offset.
835 int64_t i = CI->getValue().getSExtValue();
838 QualType elemType = ER->getElementType();
840 // If we are pointing to an incomplete type, go no further.
841 if (!IsCompleteType(C, elemType)) {
846 CharUnits size = C.getTypeSizeInChars(elemType);
847 offset += (i * size);
850 // Go to the next ElementRegion (if any).
851 ER = dyn_cast<ElementRegion>(superR);
858 assert(superR && "super region cannot be NULL");
859 return RegionRawOffset(superR, offset);
862 RegionOffset MemRegion::getAsOffset() const {
863 const MemRegion *R = this;
867 switch (R->getKind()) {
869 return RegionOffset(0);
870 case SymbolicRegionKind:
871 case AllocaRegionKind:
872 case CompoundLiteralRegionKind:
873 case CXXThisRegionKind:
874 case StringRegionKind:
876 case CXXTempObjectRegionKind:
878 case ElementRegionKind: {
879 const ElementRegion *ER = cast<ElementRegion>(R);
880 QualType EleTy = ER->getValueType();
882 if (!IsCompleteType(getContext(), EleTy))
883 return RegionOffset(0);
885 SVal Index = ER->getIndex();
886 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) {
887 int64_t i = CI->getValue().getSExtValue();
888 CharUnits Size = getContext().getTypeSizeInChars(EleTy);
889 Offset += i * Size.getQuantity() * 8;
891 // We cannot compute offset for non-concrete index.
892 return RegionOffset(0);
894 R = ER->getSuperRegion();
897 case FieldRegionKind: {
898 const FieldRegion *FR = cast<FieldRegion>(R);
899 const RecordDecl *RD = FR->getDecl()->getParent();
900 if (!RD->isCompleteDefinition())
901 // We cannot compute offset for incomplete type.
902 return RegionOffset(0);
903 // Get the field number.
905 for (RecordDecl::field_iterator FI = RD->field_begin(),
906 FE = RD->field_end(); FI != FE; ++FI, ++idx)
907 if (FR->getDecl() == *FI)
910 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
911 // This is offset in bits.
912 Offset += Layout.getFieldOffset(idx);
913 R = FR->getSuperRegion();
920 return RegionOffset(R, Offset);
923 //===----------------------------------------------------------------------===//
925 //===----------------------------------------------------------------------===//
927 void BlockDataRegion::LazyInitializeReferencedVars() {
931 AnalysisContext *AC = getCodeRegion()->getAnalysisContext();
932 AnalysisContext::referenced_decls_iterator I, E;
933 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
936 ReferencedVars = (void*) 0x1;
940 MemRegionManager &MemMgr = *getMemRegionManager();
941 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
942 BumpVectorContext BC(A);
944 typedef BumpVector<const MemRegion*> VarVec;
945 VarVec *BV = (VarVec*) A.Allocate<VarVec>();
946 new (BV) VarVec(BC, E - I);
948 for ( ; I != E; ++I) {
949 const VarDecl *VD = *I;
950 const VarRegion *VR = 0;
952 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
953 VR = MemMgr.getVarRegion(VD, this);
956 VR = MemMgr.getVarRegion(VD, LC);
958 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
963 BV->push_back(VR, BC);
969 BlockDataRegion::referenced_vars_iterator
970 BlockDataRegion::referenced_vars_begin() const {
971 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
973 BumpVector<const MemRegion*> *Vec =
974 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
976 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
977 NULL : Vec->begin());
980 BlockDataRegion::referenced_vars_iterator
981 BlockDataRegion::referenced_vars_end() const {
982 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
984 BumpVector<const MemRegion*> *Vec =
985 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
987 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?