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(llvm::raw_ostream& os) const {
402 os << "<Unknown Region>";
405 void AllocaRegion::dumpToStream(llvm::raw_ostream& os) const {
406 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
409 void FunctionTextRegion::dumpToStream(llvm::raw_ostream& os) const {
410 os << "code{" << getDecl()->getDeclName().getAsString() << '}';
413 void BlockTextRegion::dumpToStream(llvm::raw_ostream& os) const {
414 os << "block_code{" << (void*) this << '}';
417 void BlockDataRegion::dumpToStream(llvm::raw_ostream& os) const {
418 os << "block_data{" << BC << '}';
421 void CompoundLiteralRegion::dumpToStream(llvm::raw_ostream& os) const {
422 // FIXME: More elaborate pretty-printing.
423 os << "{ " << (void*) CL << " }";
426 void CXXTempObjectRegion::dumpToStream(llvm::raw_ostream &os) const {
430 void CXXBaseObjectRegion::dumpToStream(llvm::raw_ostream &os) const {
431 os << "base " << decl->getName();
434 void CXXThisRegion::dumpToStream(llvm::raw_ostream &os) const {
438 void ElementRegion::dumpToStream(llvm::raw_ostream& os) const {
439 os << "element{" << superRegion << ','
440 << Index << ',' << getElementType().getAsString() << '}';
443 void FieldRegion::dumpToStream(llvm::raw_ostream& os) const {
444 os << superRegion << "->" << getDecl();
447 void NonStaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
448 os << "NonStaticGlobalSpaceRegion";
451 void ObjCIvarRegion::dumpToStream(llvm::raw_ostream& os) const {
452 os << "ivar{" << superRegion << ',' << getDecl() << '}';
455 void StringRegion::dumpToStream(llvm::raw_ostream& os) const {
456 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions()));
459 void SymbolicRegion::dumpToStream(llvm::raw_ostream& os) const {
460 os << "SymRegion{" << sym << '}';
463 void VarRegion::dumpToStream(llvm::raw_ostream& os) const {
464 os << cast<VarDecl>(D);
467 void RegionRawOffset::dump() const {
468 dumpToStream(llvm::errs());
471 void RegionRawOffset::dumpToStream(llvm::raw_ostream& os) const {
472 os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
475 void StaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
476 os << "StaticGlobalsMemSpace{" << CR << '}';
479 //===----------------------------------------------------------------------===//
480 // MemRegionManager methods.
481 //===----------------------------------------------------------------------===//
483 template <typename REG>
484 const REG *MemRegionManager::LazyAllocate(REG*& region) {
486 region = (REG*) A.Allocate<REG>();
487 new (region) REG(this);
493 template <typename REG, typename ARG>
494 const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
496 region = (REG*) A.Allocate<REG>();
497 new (region) REG(this, a);
503 const StackLocalsSpaceRegion*
504 MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
506 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
511 R = A.Allocate<StackLocalsSpaceRegion>();
512 new (R) StackLocalsSpaceRegion(this, STC);
516 const StackArgumentsSpaceRegion *
517 MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
519 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
524 R = A.Allocate<StackArgumentsSpaceRegion>();
525 new (R) StackArgumentsSpaceRegion(this, STC);
529 const GlobalsSpaceRegion
530 *MemRegionManager::getGlobalsRegion(const CodeTextRegion *CR) {
532 return LazyAllocate(globals);
534 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
538 R = A.Allocate<StaticGlobalSpaceRegion>();
539 new (R) StaticGlobalSpaceRegion(this, CR);
543 const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
544 return LazyAllocate(heap);
547 const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
548 return LazyAllocate(unknown);
551 const MemSpaceRegion *MemRegionManager::getCodeRegion() {
552 return LazyAllocate(code);
555 //===----------------------------------------------------------------------===//
556 // Constructing regions.
557 //===----------------------------------------------------------------------===//
559 const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
560 return getSubRegion<StringRegion>(Str, getGlobalsRegion());
563 const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
564 const LocationContext *LC) {
565 const MemRegion *sReg = 0;
567 if (D->hasGlobalStorage() && !D->isStaticLocal())
568 sReg = getGlobalsRegion();
570 // FIXME: Once we implement scope handling, we will need to properly lookup
571 // 'D' to the proper LocationContext.
572 const DeclContext *DC = D->getDeclContext();
573 const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC);
576 sReg = getUnknownRegion();
578 if (D->hasLocalStorage()) {
579 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
580 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
581 : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
584 assert(D->isStaticLocal());
585 const Decl *D = STC->getDecl();
586 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
587 sReg = getGlobalsRegion(getFunctionTextRegion(FD));
588 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
589 const BlockTextRegion *BTR =
590 getBlockTextRegion(BD,
591 C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
592 STC->getAnalysisContext());
593 sReg = getGlobalsRegion(BTR);
596 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now
597 // just use the main global memspace.
598 sReg = getGlobalsRegion();
604 return getSubRegion<VarRegion>(D, sReg);
607 const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
608 const MemRegion *superR) {
609 return getSubRegion<VarRegion>(D, superR);
612 const BlockDataRegion *
613 MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
614 const LocationContext *LC) {
615 const MemRegion *sReg = 0;
618 // FIXME: Once we implement scope handling, we want the parent region
620 const StackFrameContext *STC = LC->getCurrentStackFrame();
622 sReg = getStackLocalsRegion(STC);
625 // We allow 'LC' to be NULL for cases where want BlockDataRegions
626 // without context-sensitivity.
627 sReg = getUnknownRegion();
630 return getSubRegion<BlockDataRegion>(BC, LC, sReg);
633 const CompoundLiteralRegion*
634 MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL,
635 const LocationContext *LC) {
637 const MemRegion *sReg = 0;
639 if (CL->isFileScope())
640 sReg = getGlobalsRegion();
642 const StackFrameContext *STC = LC->getCurrentStackFrame();
644 sReg = getStackLocalsRegion(STC);
647 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
651 MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
652 const MemRegion* superRegion,
655 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
657 llvm::FoldingSetNodeID ID;
658 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
661 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
662 ElementRegion* R = cast_or_null<ElementRegion>(data);
665 R = (ElementRegion*) A.Allocate<ElementRegion>();
666 new (R) ElementRegion(T, Idx, superRegion);
667 Regions.InsertNode(R, InsertPos);
673 const FunctionTextRegion *
674 MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) {
675 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
678 const BlockTextRegion *
679 MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
680 AnalysisContext *AC) {
681 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
685 /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
686 const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
687 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
691 MemRegionManager::getFieldRegion(const FieldDecl* d,
692 const MemRegion* superRegion){
693 return getSubRegion<FieldRegion>(d, superRegion);
696 const ObjCIvarRegion*
697 MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
698 const MemRegion* superRegion) {
699 return getSubRegion<ObjCIvarRegion>(d, superRegion);
702 const CXXTempObjectRegion*
703 MemRegionManager::getCXXTempObjectRegion(Expr const *E,
704 LocationContext const *LC) {
705 const StackFrameContext *SFC = LC->getCurrentStackFrame();
707 return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
710 const CXXBaseObjectRegion *
711 MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *decl,
712 const MemRegion *superRegion) {
713 return getSubRegion<CXXBaseObjectRegion>(decl, superRegion);
717 MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
718 const LocationContext *LC) {
719 const StackFrameContext *STC = LC->getCurrentStackFrame();
721 const PointerType *PT = thisPointerTy->getAs<PointerType>();
723 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
727 MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt,
728 const LocationContext *LC) {
729 const StackFrameContext *STC = LC->getCurrentStackFrame();
731 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
734 const MemSpaceRegion *MemRegion::getMemorySpace() const {
735 const MemRegion *R = this;
736 const SubRegion* SR = dyn_cast<SubRegion>(this);
739 R = SR->getSuperRegion();
740 SR = dyn_cast<SubRegion>(R);
743 return dyn_cast<MemSpaceRegion>(R);
746 bool MemRegion::hasStackStorage() const {
747 return isa<StackSpaceRegion>(getMemorySpace());
750 bool MemRegion::hasStackNonParametersStorage() const {
751 return isa<StackLocalsSpaceRegion>(getMemorySpace());
754 bool MemRegion::hasStackParametersStorage() const {
755 return isa<StackArgumentsSpaceRegion>(getMemorySpace());
758 bool MemRegion::hasGlobalsOrParametersStorage() const {
759 const MemSpaceRegion *MS = getMemorySpace();
760 return isa<StackArgumentsSpaceRegion>(MS) ||
761 isa<GlobalsSpaceRegion>(MS);
764 // getBaseRegion strips away all elements and fields, and get the base region
766 const MemRegion *MemRegion::getBaseRegion() const {
767 const MemRegion *R = this;
769 switch (R->getKind()) {
770 case MemRegion::ElementRegionKind:
771 case MemRegion::FieldRegionKind:
772 case MemRegion::ObjCIvarRegionKind:
773 case MemRegion::CXXBaseObjectRegionKind:
774 R = cast<SubRegion>(R)->getSuperRegion();
784 //===----------------------------------------------------------------------===//
786 //===----------------------------------------------------------------------===//
788 const MemRegion *MemRegion::StripCasts() const {
789 const MemRegion *R = this;
791 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
792 // FIXME: generalize. Essentially we want to strip away ElementRegions
793 // that were layered on a symbolic region because of casts. We only
794 // want to strip away ElementRegions, however, where the index is 0.
795 SVal index = ER->getIndex();
796 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
797 if (CI->getValue().getSExtValue() == 0) {
798 R = ER->getSuperRegion();
808 // FIXME: Merge with the implementation of the same method in Store.cpp
809 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
810 if (const RecordType *RT = Ty->getAs<RecordType>()) {
811 const RecordDecl *D = RT->getDecl();
812 if (!D->getDefinition())
819 RegionRawOffset ElementRegion::getAsArrayOffset() const {
820 CharUnits offset = CharUnits::Zero();
821 const ElementRegion *ER = this;
822 const MemRegion *superR = NULL;
823 ASTContext &C = getContext();
825 // FIXME: Handle multi-dimensional arrays.
828 superR = ER->getSuperRegion();
830 // FIXME: generalize to symbolic offsets.
831 SVal index = ER->getIndex();
832 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
833 // Update the offset.
834 int64_t i = CI->getValue().getSExtValue();
837 QualType elemType = ER->getElementType();
839 // If we are pointing to an incomplete type, go no further.
840 if (!IsCompleteType(C, elemType)) {
845 CharUnits size = C.getTypeSizeInChars(elemType);
846 offset += (i * size);
849 // Go to the next ElementRegion (if any).
850 ER = dyn_cast<ElementRegion>(superR);
857 assert(superR && "super region cannot be NULL");
858 return RegionRawOffset(superR, offset);
861 RegionOffset MemRegion::getAsOffset() const {
862 const MemRegion *R = this;
866 switch (R->getKind()) {
868 return RegionOffset(0);
869 case SymbolicRegionKind:
870 case AllocaRegionKind:
871 case CompoundLiteralRegionKind:
872 case CXXThisRegionKind:
873 case StringRegionKind:
875 case CXXTempObjectRegionKind:
877 case ElementRegionKind: {
878 const ElementRegion *ER = cast<ElementRegion>(R);
879 QualType EleTy = ER->getValueType();
881 if (!IsCompleteType(getContext(), EleTy))
882 return RegionOffset(0);
884 SVal Index = ER->getIndex();
885 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) {
886 int64_t i = CI->getValue().getSExtValue();
887 CharUnits Size = getContext().getTypeSizeInChars(EleTy);
888 Offset += i * Size.getQuantity() * 8;
890 // We cannot compute offset for non-concrete index.
891 return RegionOffset(0);
893 R = ER->getSuperRegion();
896 case FieldRegionKind: {
897 const FieldRegion *FR = cast<FieldRegion>(R);
898 const RecordDecl *RD = FR->getDecl()->getParent();
899 if (!RD->isDefinition())
900 // We cannot compute offset for incomplete type.
901 return RegionOffset(0);
902 // Get the field number.
904 for (RecordDecl::field_iterator FI = RD->field_begin(),
905 FE = RD->field_end(); FI != FE; ++FI, ++idx)
906 if (FR->getDecl() == *FI)
909 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
910 // This is offset in bits.
911 Offset += Layout.getFieldOffset(idx);
912 R = FR->getSuperRegion();
919 return RegionOffset(R, Offset);
922 //===----------------------------------------------------------------------===//
924 //===----------------------------------------------------------------------===//
926 void BlockDataRegion::LazyInitializeReferencedVars() {
930 AnalysisContext *AC = getCodeRegion()->getAnalysisContext();
931 AnalysisContext::referenced_decls_iterator I, E;
932 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
935 ReferencedVars = (void*) 0x1;
939 MemRegionManager &MemMgr = *getMemRegionManager();
940 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
941 BumpVectorContext BC(A);
943 typedef BumpVector<const MemRegion*> VarVec;
944 VarVec *BV = (VarVec*) A.Allocate<VarVec>();
945 new (BV) VarVec(BC, E - I);
947 for ( ; I != E; ++I) {
948 const VarDecl *VD = *I;
949 const VarRegion *VR = 0;
951 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
952 VR = MemMgr.getVarRegion(VD, this);
955 VR = MemMgr.getVarRegion(VD, LC);
957 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
962 BV->push_back(VR, BC);
968 BlockDataRegion::referenced_vars_iterator
969 BlockDataRegion::referenced_vars_begin() const {
970 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
972 BumpVector<const MemRegion*> *Vec =
973 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
975 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
976 NULL : Vec->begin());
979 BlockDataRegion::referenced_vars_iterator
980 BlockDataRegion::referenced_vars_end() const {
981 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
983 BumpVector<const MemRegion*> *Vec =
984 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
986 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?