don't use #pragma mark, it isn't portable.
[clang.git] / lib / StaticAnalyzer / EntoSA / MemRegion.cpp
blobe465224f4270546e38901ee21df30f7e4907c655
1 //== MemRegion.cpp - Abstract memory regions for static analysis --*- C++ -*--//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines MemRegion and its subclasses. MemRegion defines a
11 // partially-typed abstraction of memory useful for path-sensitive dataflow
12 // analyses.
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/PathSensitive/MemRegion.h"
17 #include "clang/StaticAnalyzer/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;
25 using namespace ento;
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);
41 void* InsertPos;
42 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
43 InsertPos));
45 if (!R) {
46 R = (RegionTy*) A.Allocate<RegionTy>();
47 new (R) RegionTy(a1, superRegion);
48 Regions.InsertNode(R, InsertPos);
51 return R;
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);
59 void* InsertPos;
60 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
61 InsertPos));
63 if (!R) {
64 R = (RegionTy*) A.Allocate<RegionTy>();
65 new (R) RegionTy(a1, superRegion);
66 Regions.InsertNode(R, InsertPos);
69 return R;
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);
80 void* InsertPos;
81 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
82 InsertPos));
84 if (!R) {
85 R = (RegionTy*) A.Allocate<RegionTy>();
86 new (R) RegionTy(a1, a2, superRegion);
87 Regions.InsertNode(R, InsertPos);
90 return R;
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);
99 void* InsertPos;
100 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
101 InsertPos));
103 if (!R) {
104 R = (RegionTy*) A.Allocate<RegionTy>();
105 new (R) RegionTy(a1, a2, superRegion);
106 Regions.InsertNode(R, InsertPos);
109 return R;
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);
118 void* InsertPos;
119 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
120 InsertPos));
122 if (!R) {
123 R = (RegionTy*) A.Allocate<RegionTy>();
124 new (R) RegionTy(a1, a2, a3, superRegion);
125 Regions.InsertNode(R, InsertPos);
128 return R;
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 //===----------------------------------------------------------------------===//
143 // Basic methods.
144 //===----------------------------------------------------------------------===//
146 bool SubRegion::isSubRegionOf(const MemRegion* R) const {
147 const MemRegion* r = getSuperRegion();
148 while (r != 0) {
149 if (r == R)
150 return true;
151 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
152 r = sr->getSuperRegion();
153 else
154 break;
156 return false;
159 MemRegionManager* SubRegion::getMemRegionManager() const {
160 const SubRegion* r = this;
161 do {
162 const MemRegion *superRegion = r->getSuperRegion();
163 if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
164 r = sr;
165 continue;
167 return superRegion->getMemRegionManager();
168 } while (1);
171 const StackFrameContext *VarRegion::getStackFrame() const {
172 const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
173 return SSR ? SSR->getStackFrame() : NULL;
176 //===----------------------------------------------------------------------===//
177 // Region extents.
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))
187 return UnknownVal();
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))
203 return UnknownVal();
206 return Extent;
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);
248 ID.AddPointer(Str);
249 ID.AddPointer(superRegion);
252 void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
253 const Expr* Ex, unsigned cnt,
254 const MemRegion *) {
255 ID.AddInteger((unsigned) AllocaRegionKind);
256 ID.AddPointer(Ex);
257 ID.AddInteger(cnt);
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);
272 ID.AddPointer(CL);
273 ID.AddPointer(superRegion);
276 void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
277 const PointerType *PT,
278 const MemRegion *sRegion) {
279 ID.AddInteger((unsigned) CXXThisRegionKind);
280 ID.AddPointer(PT);
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);
291 ID.AddPointer(D);
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);
306 ID.Add(sym);
307 ID.AddPointer(sreg);
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);
318 ID.Add(ElementType);
319 ID.AddPointer(superRegion);
320 Idx.Profile(ID);
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,
329 const MemRegion*) {
330 ID.AddInteger(MemRegion::FunctionTextRegionKind);
331 ID.AddPointer(FD);
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,
341 const MemRegion*) {
342 ID.AddInteger(MemRegion::BlockTextRegionKind);
343 ID.AddPointer(BD);
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);
355 ID.AddPointer(BC);
356 ID.AddPointer(LC);
357 ID.AddPointer(sReg);
360 void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
361 BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
364 void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
365 Expr const *Ex,
366 const MemRegion *sReg) {
367 ID.AddPointer(Ex);
368 ID.AddPointer(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) {
378 ID.AddPointer(decl);
379 ID.AddPointer(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 {
395 std::string s;
396 llvm::raw_string_ostream os(s);
397 dumpToStream(os);
398 return os.str();
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 {
427 os << "temp_object";
430 void CXXBaseObjectRegion::dumpToStream(llvm::raw_ostream &os) const {
431 os << "base " << decl->getName();
434 void CXXThisRegion::dumpToStream(llvm::raw_ostream &os) const {
435 os << "this";
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() << ',' << getByteOffset() << '}';
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) {
485 if (!region) {
486 region = (REG*) A.Allocate<REG>();
487 new (region) REG(this);
490 return region;
493 template <typename REG, typename ARG>
494 const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
495 if (!region) {
496 region = (REG*) A.Allocate<REG>();
497 new (region) REG(this, a);
500 return region;
503 const StackLocalsSpaceRegion*
504 MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
505 assert(STC);
506 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
508 if (R)
509 return R;
511 R = A.Allocate<StackLocalsSpaceRegion>();
512 new (R) StackLocalsSpaceRegion(this, STC);
513 return R;
516 const StackArgumentsSpaceRegion *
517 MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
518 assert(STC);
519 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
521 if (R)
522 return R;
524 R = A.Allocate<StackArgumentsSpaceRegion>();
525 new (R) StackArgumentsSpaceRegion(this, STC);
526 return R;
529 const GlobalsSpaceRegion
530 *MemRegionManager::getGlobalsRegion(const CodeTextRegion *CR) {
531 if (!CR)
532 return LazyAllocate(globals);
534 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
535 if (R)
536 return R;
538 R = A.Allocate<StaticGlobalSpaceRegion>();
539 new (R) StaticGlobalSpaceRegion(this, CR);
540 return R;
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();
569 else {
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);
575 if (!STC)
576 sReg = getUnknownRegion();
577 else {
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));
583 else {
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);
595 else {
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;
617 if (LC) {
618 // FIXME: Once we implement scope handling, we want the parent region
619 // to be the scope.
620 const StackFrameContext *STC = LC->getCurrentStackFrame();
621 assert(STC);
622 sReg = getStackLocalsRegion(STC);
624 else {
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();
641 else {
642 const StackFrameContext *STC = LC->getCurrentStackFrame();
643 assert(STC);
644 sReg = getStackLocalsRegion(STC);
647 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
650 const ElementRegion*
651 MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
652 const MemRegion* superRegion,
653 ASTContext& Ctx){
655 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
657 llvm::FoldingSetNodeID ID;
658 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
660 void* InsertPos;
661 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
662 ElementRegion* R = cast_or_null<ElementRegion>(data);
664 if (!R) {
665 R = (ElementRegion*) A.Allocate<ElementRegion>();
666 new (R) ElementRegion(T, Idx, superRegion);
667 Regions.InsertNode(R, InsertPos);
670 return R;
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());
690 const FieldRegion*
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();
706 assert(SFC);
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);
716 const CXXThisRegion*
717 MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
718 const LocationContext *LC) {
719 const StackFrameContext *STC = LC->getCurrentStackFrame();
720 assert(STC);
721 const PointerType *PT = thisPointerTy->getAs<PointerType>();
722 assert(PT);
723 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
726 const AllocaRegion*
727 MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt,
728 const LocationContext *LC) {
729 const StackFrameContext *STC = LC->getCurrentStackFrame();
730 assert(STC);
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);
738 while (SR) {
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
765 // of them.
766 const MemRegion *MemRegion::getBaseRegion() const {
767 const MemRegion *R = this;
768 while (true) {
769 switch (R->getKind()) {
770 case MemRegion::ElementRegionKind:
771 case MemRegion::FieldRegionKind:
772 case MemRegion::ObjCIvarRegionKind:
773 R = cast<SubRegion>(R)->getSuperRegion();
774 continue;
775 default:
776 break;
778 break;
780 return R;
783 //===----------------------------------------------------------------------===//
784 // View handling.
785 //===----------------------------------------------------------------------===//
787 const MemRegion *MemRegion::StripCasts() const {
788 const MemRegion *R = this;
789 while (true) {
790 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
791 // FIXME: generalize. Essentially we want to strip away ElementRegions
792 // that were layered on a symbolic region because of casts. We only
793 // want to strip away ElementRegions, however, where the index is 0.
794 SVal index = ER->getIndex();
795 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
796 if (CI->getValue().getSExtValue() == 0) {
797 R = ER->getSuperRegion();
798 continue;
802 break;
804 return R;
807 // FIXME: Merge with the implementation of the same method in Store.cpp
808 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
809 if (const RecordType *RT = Ty->getAs<RecordType>()) {
810 const RecordDecl *D = RT->getDecl();
811 if (!D->getDefinition())
812 return false;
815 return true;
818 RegionRawOffset ElementRegion::getAsArrayOffset() const {
819 CharUnits offset = CharUnits::Zero();
820 const ElementRegion *ER = this;
821 const MemRegion *superR = NULL;
822 ASTContext &C = getContext();
824 // FIXME: Handle multi-dimensional arrays.
826 while (ER) {
827 superR = ER->getSuperRegion();
829 // FIXME: generalize to symbolic offsets.
830 SVal index = ER->getIndex();
831 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
832 // Update the offset.
833 int64_t i = CI->getValue().getSExtValue();
835 if (i != 0) {
836 QualType elemType = ER->getElementType();
838 // If we are pointing to an incomplete type, go no further.
839 if (!IsCompleteType(C, elemType)) {
840 superR = ER;
841 break;
844 CharUnits size = C.getTypeSizeInChars(elemType);
845 offset += (i * size);
848 // Go to the next ElementRegion (if any).
849 ER = dyn_cast<ElementRegion>(superR);
850 continue;
853 return NULL;
856 assert(superR && "super region cannot be NULL");
857 return RegionRawOffset(superR, offset.getQuantity());
860 RegionOffset MemRegion::getAsOffset() const {
861 const MemRegion *R = this;
862 int64_t Offset = 0;
864 while (1) {
865 switch (R->getKind()) {
866 default:
867 return RegionOffset(0);
868 case SymbolicRegionKind:
869 case AllocaRegionKind:
870 case CompoundLiteralRegionKind:
871 case CXXThisRegionKind:
872 case StringRegionKind:
873 case VarRegionKind:
874 case CXXTempObjectRegionKind:
875 goto Finish;
876 case ElementRegionKind: {
877 const ElementRegion *ER = cast<ElementRegion>(R);
878 QualType EleTy = ER->getValueType();
880 if (!IsCompleteType(getContext(), EleTy))
881 return RegionOffset(0);
883 SVal Index = ER->getIndex();
884 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) {
885 int64_t i = CI->getValue().getSExtValue();
886 CharUnits Size = getContext().getTypeSizeInChars(EleTy);
887 Offset += i * Size.getQuantity() * 8;
888 } else {
889 // We cannot compute offset for non-concrete index.
890 return RegionOffset(0);
892 R = ER->getSuperRegion();
893 break;
895 case FieldRegionKind: {
896 const FieldRegion *FR = cast<FieldRegion>(R);
897 const RecordDecl *RD = FR->getDecl()->getParent();
898 if (!RD->isDefinition())
899 // We cannot compute offset for incomplete type.
900 return RegionOffset(0);
901 // Get the field number.
902 unsigned idx = 0;
903 for (RecordDecl::field_iterator FI = RD->field_begin(),
904 FE = RD->field_end(); FI != FE; ++FI, ++idx)
905 if (FR->getDecl() == *FI)
906 break;
908 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
909 // This is offset in bits.
910 Offset += Layout.getFieldOffset(idx);
911 R = FR->getSuperRegion();
912 break;
917 Finish:
918 return RegionOffset(R, Offset);
921 //===----------------------------------------------------------------------===//
922 // BlockDataRegion
923 //===----------------------------------------------------------------------===//
925 void BlockDataRegion::LazyInitializeReferencedVars() {
926 if (ReferencedVars)
927 return;
929 AnalysisContext *AC = getCodeRegion()->getAnalysisContext();
930 AnalysisContext::referenced_decls_iterator I, E;
931 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
933 if (I == E) {
934 ReferencedVars = (void*) 0x1;
935 return;
938 MemRegionManager &MemMgr = *getMemRegionManager();
939 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
940 BumpVectorContext BC(A);
942 typedef BumpVector<const MemRegion*> VarVec;
943 VarVec *BV = (VarVec*) A.Allocate<VarVec>();
944 new (BV) VarVec(BC, E - I);
946 for ( ; I != E; ++I) {
947 const VarDecl *VD = *I;
948 const VarRegion *VR = 0;
950 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
951 VR = MemMgr.getVarRegion(VD, this);
952 else {
953 if (LC)
954 VR = MemMgr.getVarRegion(VD, LC);
955 else {
956 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
960 assert(VR);
961 BV->push_back(VR, BC);
964 ReferencedVars = BV;
967 BlockDataRegion::referenced_vars_iterator
968 BlockDataRegion::referenced_vars_begin() const {
969 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
971 BumpVector<const MemRegion*> *Vec =
972 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
974 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
975 NULL : Vec->begin());
978 BlockDataRegion::referenced_vars_iterator
979 BlockDataRegion::referenced_vars_end() const {
980 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
982 BumpVector<const MemRegion*> *Vec =
983 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
985 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
986 NULL : Vec->end());