1 //== MemRegion.h - 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 #ifndef LLVM_CLANG_GR_MEMREGION_H
17 #define LLVM_CLANG_GR_MEMREGION_H
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/CharUnits.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/ExprObjC.h"
23 #include "clang/Basic/LLVM.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/ADT/FoldingSet.h"
30 class BumpPtrAllocator;
35 class LocationContext;
36 class StackFrameContext;
40 class MemRegionManager;
46 /// Represent a region's offset within the top level base region.
51 /// The bit offset within the base region. It shouldn't be negative.
55 // We're using a const instead of an enumeration due to the size required;
56 // Visual Studio will only create enumerations of size int, not long long.
57 static const int64_t Symbolic = INT64_MAX;
59 RegionOffset() : R(0) {}
60 RegionOffset(const MemRegion *r, int64_t off) : R(r), Offset(off) {}
62 const MemRegion *getRegion() const { return R; }
64 bool hasSymbolicOffset() const { return Offset == Symbolic; }
66 int64_t getOffset() const {
67 assert(!hasSymbolicOffset());
71 bool isValid() const { return R; }
74 //===----------------------------------------------------------------------===//
75 // Base region classes.
76 //===----------------------------------------------------------------------===//
78 /// MemRegion - The root abstract class for all memory regions.
79 class MemRegion : public llvm::FoldingSetNode {
80 friend class MemRegionManager;
84 GenericMemSpaceRegionKind,
85 StackLocalsSpaceRegionKind,
86 StackArgumentsSpaceRegionKind,
88 UnknownSpaceRegionKind,
89 StaticGlobalSpaceRegionKind,
90 GlobalInternalSpaceRegionKind,
91 GlobalSystemSpaceRegionKind,
92 GlobalImmutableSpaceRegionKind,
93 BEG_NON_STATIC_GLOBAL_MEMSPACES = GlobalInternalSpaceRegionKind,
94 END_NON_STATIC_GLOBAL_MEMSPACES = GlobalImmutableSpaceRegionKind,
95 BEG_GLOBAL_MEMSPACES = StaticGlobalSpaceRegionKind,
96 END_GLOBAL_MEMSPACES = GlobalImmutableSpaceRegionKind,
97 BEG_MEMSPACES = GenericMemSpaceRegionKind,
98 END_MEMSPACES = GlobalImmutableSpaceRegionKind,
105 FunctionTextRegionKind = BEG_TYPED_REGIONS,
107 BEG_TYPED_VALUE_REGIONS,
108 CompoundLiteralRegionKind = BEG_TYPED_VALUE_REGIONS,
111 ObjCStringRegionKind,
115 VarRegionKind = BEG_DECL_REGIONS,
118 END_DECL_REGIONS = ObjCIvarRegionKind,
119 CXXTempObjectRegionKind,
120 CXXBaseObjectRegionKind,
121 END_TYPED_VALUE_REGIONS = CXXBaseObjectRegionKind,
122 END_TYPED_REGIONS = CXXBaseObjectRegionKind
129 MemRegion(Kind k) : kind(k) {}
130 virtual ~MemRegion();
133 ASTContext &getContext() const;
135 virtual void Profile(llvm::FoldingSetNodeID& ID) const = 0;
137 virtual MemRegionManager* getMemRegionManager() const = 0;
139 const MemSpaceRegion *getMemorySpace() const;
141 const MemRegion *getBaseRegion() const;
143 const MemRegion *StripCasts(bool StripBaseCasts = true) const;
145 bool hasGlobalsOrParametersStorage() const;
147 bool hasStackStorage() const;
149 bool hasStackNonParametersStorage() const;
151 bool hasStackParametersStorage() const;
153 /// Compute the offset within the top level memory object.
154 RegionOffset getAsOffset() const;
156 /// \brief Get a string representation of a region for debug use.
157 std::string getString() const;
159 virtual void dumpToStream(raw_ostream &os) const;
163 /// \brief Returns true if this region can be printed in a user-friendly way.
164 virtual bool canPrintPretty() const;
166 /// \brief Print the region for use in diagnostics.
167 virtual void printPretty(raw_ostream &os) const;
169 Kind getKind() const { return kind; }
171 template<typename RegionTy> const RegionTy* getAs() const;
173 virtual bool isBoundable() const { return false; }
175 static bool classof(const MemRegion*) { return true; }
178 /// MemSpaceRegion - A memory region that represents a "memory space";
179 /// for example, the set of global variables, the stack frame, etc.
180 class MemSpaceRegion : public MemRegion {
182 friend class MemRegionManager;
184 MemRegionManager *Mgr;
186 MemSpaceRegion(MemRegionManager *mgr, Kind k = GenericMemSpaceRegionKind)
187 : MemRegion(k), Mgr(mgr) {
188 assert(classof(this));
191 MemRegionManager* getMemRegionManager() const { return Mgr; }
194 bool isBoundable() const { return false; }
196 void Profile(llvm::FoldingSetNodeID &ID) const;
198 static bool classof(const MemRegion *R) {
199 Kind k = R->getKind();
200 return k >= BEG_MEMSPACES && k <= END_MEMSPACES;
204 class GlobalsSpaceRegion : public MemSpaceRegion {
205 virtual void anchor();
207 GlobalsSpaceRegion(MemRegionManager *mgr, Kind k)
208 : MemSpaceRegion(mgr, k) {}
210 static bool classof(const MemRegion *R) {
211 Kind k = R->getKind();
212 return k >= BEG_GLOBAL_MEMSPACES && k <= END_GLOBAL_MEMSPACES;
216 /// \brief The region of the static variables within the current CodeTextRegion
219 /// Currently, only the static locals are placed there, so we know that these
220 /// variables do not get invalidated by calls to other functions.
221 class StaticGlobalSpaceRegion : public GlobalsSpaceRegion {
222 friend class MemRegionManager;
224 const CodeTextRegion *CR;
226 StaticGlobalSpaceRegion(MemRegionManager *mgr, const CodeTextRegion *cr)
227 : GlobalsSpaceRegion(mgr, StaticGlobalSpaceRegionKind), CR(cr) {}
230 void Profile(llvm::FoldingSetNodeID &ID) const;
232 void dumpToStream(raw_ostream &os) const;
234 const CodeTextRegion *getCodeRegion() const { return CR; }
236 static bool classof(const MemRegion *R) {
237 return R->getKind() == StaticGlobalSpaceRegionKind;
241 /// \brief The region for all the non-static global variables.
243 /// This class is further split into subclasses for efficient implementation of
244 /// invalidating a set of related global values as is done in
245 /// RegionStoreManager::invalidateRegions (instead of finding all the dependent
246 /// globals, we invalidate the whole parent region).
247 class NonStaticGlobalSpaceRegion : public GlobalsSpaceRegion {
248 friend class MemRegionManager;
251 NonStaticGlobalSpaceRegion(MemRegionManager *mgr, Kind k)
252 : GlobalsSpaceRegion(mgr, k) {}
256 static bool classof(const MemRegion *R) {
257 Kind k = R->getKind();
258 return k >= BEG_NON_STATIC_GLOBAL_MEMSPACES &&
259 k <= END_NON_STATIC_GLOBAL_MEMSPACES;
263 /// \brief The region containing globals which are defined in system/external
264 /// headers and are considered modifiable by system calls (ex: errno).
265 class GlobalSystemSpaceRegion : public NonStaticGlobalSpaceRegion {
266 friend class MemRegionManager;
268 GlobalSystemSpaceRegion(MemRegionManager *mgr)
269 : NonStaticGlobalSpaceRegion(mgr, GlobalSystemSpaceRegionKind) {}
273 void dumpToStream(raw_ostream &os) const;
275 static bool classof(const MemRegion *R) {
276 return R->getKind() == GlobalSystemSpaceRegionKind;
280 /// \brief The region containing globals which are considered not to be modified
281 /// or point to data which could be modified as a result of a function call
282 /// (system or internal). Ex: Const global scalars would be modeled as part of
283 /// this region. This region also includes most system globals since they have
284 /// low chance of being modified.
285 class GlobalImmutableSpaceRegion : public NonStaticGlobalSpaceRegion {
286 friend class MemRegionManager;
288 GlobalImmutableSpaceRegion(MemRegionManager *mgr)
289 : NonStaticGlobalSpaceRegion(mgr, GlobalImmutableSpaceRegionKind) {}
293 void dumpToStream(raw_ostream &os) const;
295 static bool classof(const MemRegion *R) {
296 return R->getKind() == GlobalImmutableSpaceRegionKind;
300 /// \brief The region containing globals which can be modified by calls to
301 /// "internally" defined functions - (for now just) functions other then system
303 class GlobalInternalSpaceRegion : public NonStaticGlobalSpaceRegion {
304 friend class MemRegionManager;
306 GlobalInternalSpaceRegion(MemRegionManager *mgr)
307 : NonStaticGlobalSpaceRegion(mgr, GlobalInternalSpaceRegionKind) {}
311 void dumpToStream(raw_ostream &os) const;
313 static bool classof(const MemRegion *R) {
314 return R->getKind() == GlobalInternalSpaceRegionKind;
318 class HeapSpaceRegion : public MemSpaceRegion {
319 virtual void anchor();
320 friend class MemRegionManager;
322 HeapSpaceRegion(MemRegionManager *mgr)
323 : MemSpaceRegion(mgr, HeapSpaceRegionKind) {}
326 void dumpToStream(raw_ostream &os) const;
328 static bool classof(const MemRegion *R) {
329 return R->getKind() == HeapSpaceRegionKind;
333 class UnknownSpaceRegion : public MemSpaceRegion {
334 virtual void anchor();
335 friend class MemRegionManager;
336 UnknownSpaceRegion(MemRegionManager *mgr)
337 : MemSpaceRegion(mgr, UnknownSpaceRegionKind) {}
340 void dumpToStream(raw_ostream &os) const;
342 static bool classof(const MemRegion *R) {
343 return R->getKind() == UnknownSpaceRegionKind;
347 class StackSpaceRegion : public MemSpaceRegion {
349 const StackFrameContext *SFC;
352 StackSpaceRegion(MemRegionManager *mgr, Kind k, const StackFrameContext *sfc)
353 : MemSpaceRegion(mgr, k), SFC(sfc) {
354 assert(classof(this));
358 const StackFrameContext *getStackFrame() const { return SFC; }
360 void Profile(llvm::FoldingSetNodeID &ID) const;
362 static bool classof(const MemRegion *R) {
363 Kind k = R->getKind();
364 return k >= StackLocalsSpaceRegionKind &&
365 k <= StackArgumentsSpaceRegionKind;
369 class StackLocalsSpaceRegion : public StackSpaceRegion {
370 virtual void anchor();
371 friend class MemRegionManager;
372 StackLocalsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc)
373 : StackSpaceRegion(mgr, StackLocalsSpaceRegionKind, sfc) {}
376 void dumpToStream(raw_ostream &os) const;
378 static bool classof(const MemRegion *R) {
379 return R->getKind() == StackLocalsSpaceRegionKind;
383 class StackArgumentsSpaceRegion : public StackSpaceRegion {
385 virtual void anchor();
386 friend class MemRegionManager;
387 StackArgumentsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc)
388 : StackSpaceRegion(mgr, StackArgumentsSpaceRegionKind, sfc) {}
391 void dumpToStream(raw_ostream &os) const;
393 static bool classof(const MemRegion *R) {
394 return R->getKind() == StackArgumentsSpaceRegionKind;
399 /// SubRegion - A region that subsets another larger region. Most regions
400 /// are subclasses of SubRegion.
401 class SubRegion : public MemRegion {
403 virtual void anchor();
405 const MemRegion* superRegion;
406 SubRegion(const MemRegion* sReg, Kind k) : MemRegion(k), superRegion(sReg) {}
408 const MemRegion* getSuperRegion() const {
412 /// getExtent - Returns the size of the region in bytes.
413 virtual DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const {
417 MemRegionManager* getMemRegionManager() const;
419 bool isSubRegionOf(const MemRegion* R) const;
421 static bool classof(const MemRegion* R) {
422 return R->getKind() > END_MEMSPACES;
426 //===----------------------------------------------------------------------===//
427 // MemRegion subclasses.
428 //===----------------------------------------------------------------------===//
430 /// AllocaRegion - A region that represents an untyped blob of bytes created
431 /// by a call to 'alloca'.
432 class AllocaRegion : public SubRegion {
433 friend class MemRegionManager;
435 unsigned Cnt; // Block counter. Used to distinguish different pieces of
436 // memory allocated by alloca at the same call site.
439 AllocaRegion(const Expr *ex, unsigned cnt, const MemRegion *superRegion)
440 : SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {}
444 const Expr *getExpr() const { return Ex; }
446 bool isBoundable() const { return true; }
448 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
450 void Profile(llvm::FoldingSetNodeID& ID) const;
452 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Expr *Ex,
453 unsigned Cnt, const MemRegion *superRegion);
455 void dumpToStream(raw_ostream &os) const;
457 static bool classof(const MemRegion* R) {
458 return R->getKind() == AllocaRegionKind;
462 /// TypedRegion - An abstract class representing regions that are typed.
463 class TypedRegion : public SubRegion {
465 virtual void anchor();
467 TypedRegion(const MemRegion* sReg, Kind k) : SubRegion(sReg, k) {}
470 virtual QualType getLocationType() const = 0;
472 QualType getDesugaredLocationType(ASTContext &Context) const {
473 return getLocationType().getDesugaredType(Context);
476 bool isBoundable() const { return true; }
478 static bool classof(const MemRegion* R) {
479 unsigned k = R->getKind();
480 return k >= BEG_TYPED_REGIONS && k <= END_TYPED_REGIONS;
484 /// TypedValueRegion - An abstract class representing regions having a typed value.
485 class TypedValueRegion : public TypedRegion {
487 virtual void anchor();
489 TypedValueRegion(const MemRegion* sReg, Kind k) : TypedRegion(sReg, k) {}
492 virtual QualType getValueType() const = 0;
494 virtual QualType getLocationType() const {
495 // FIXME: We can possibly optimize this later to cache this value.
496 QualType T = getValueType();
497 ASTContext &ctx = getContext();
498 if (T->getAs<ObjCObjectType>())
499 return ctx.getObjCObjectPointerType(T);
500 return ctx.getPointerType(getValueType());
503 QualType getDesugaredValueType(ASTContext &Context) const {
504 QualType T = getValueType();
505 return T.getTypePtrOrNull() ? T.getDesugaredType(Context) : T;
508 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
510 static bool classof(const MemRegion* R) {
511 unsigned k = R->getKind();
512 return k >= BEG_TYPED_VALUE_REGIONS && k <= END_TYPED_VALUE_REGIONS;
517 class CodeTextRegion : public TypedRegion {
519 virtual void anchor();
521 CodeTextRegion(const MemRegion *sreg, Kind k) : TypedRegion(sreg, k) {}
523 bool isBoundable() const { return false; }
525 static bool classof(const MemRegion* R) {
526 Kind k = R->getKind();
527 return k >= FunctionTextRegionKind && k <= BlockTextRegionKind;
531 /// FunctionTextRegion - A region that represents code texts of function.
532 class FunctionTextRegion : public CodeTextRegion {
533 const FunctionDecl *FD;
535 FunctionTextRegion(const FunctionDecl *fd, const MemRegion* sreg)
536 : CodeTextRegion(sreg, FunctionTextRegionKind), FD(fd) {}
538 QualType getLocationType() const {
539 return getContext().getPointerType(FD->getType());
542 const FunctionDecl *getDecl() const {
546 virtual void dumpToStream(raw_ostream &os) const;
548 void Profile(llvm::FoldingSetNodeID& ID) const;
550 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const FunctionDecl *FD,
553 static bool classof(const MemRegion* R) {
554 return R->getKind() == FunctionTextRegionKind;
559 /// BlockTextRegion - A region that represents code texts of blocks (closures).
560 /// Blocks are represented with two kinds of regions. BlockTextRegions
561 /// represent the "code", while BlockDataRegions represent instances of blocks,
562 /// which correspond to "code+data". The distinction is important, because
563 /// like a closure a block captures the values of externally referenced
565 class BlockTextRegion : public CodeTextRegion {
566 friend class MemRegionManager;
569 AnalysisDeclContext *AC;
572 BlockTextRegion(const BlockDecl *bd, CanQualType lTy,
573 AnalysisDeclContext *ac, const MemRegion* sreg)
574 : CodeTextRegion(sreg, BlockTextRegionKind), BD(bd), AC(ac), locTy(lTy) {}
577 QualType getLocationType() const {
581 const BlockDecl *getDecl() const {
585 AnalysisDeclContext *getAnalysisDeclContext() const { return AC; }
587 virtual void dumpToStream(raw_ostream &os) const;
589 void Profile(llvm::FoldingSetNodeID& ID) const;
591 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const BlockDecl *BD,
592 CanQualType, const AnalysisDeclContext*,
595 static bool classof(const MemRegion* R) {
596 return R->getKind() == BlockTextRegionKind;
600 /// BlockDataRegion - A region that represents a block instance.
601 /// Blocks are represented with two kinds of regions. BlockTextRegions
602 /// represent the "code", while BlockDataRegions represent instances of blocks,
603 /// which correspond to "code+data". The distinction is important, because
604 /// like a closure a block captures the values of externally referenced
606 class BlockDataRegion : public SubRegion {
607 friend class MemRegionManager;
608 const BlockTextRegion *BC;
609 const LocationContext *LC; // Can be null */
610 void *ReferencedVars;
613 BlockDataRegion(const BlockTextRegion *bc, const LocationContext *lc,
614 const MemRegion *sreg)
615 : SubRegion(sreg, BlockDataRegionKind), BC(bc), LC(lc),
616 ReferencedVars(0), OriginalVars(0) {}
619 const BlockTextRegion *getCodeRegion() const { return BC; }
621 const BlockDecl *getDecl() const { return BC->getDecl(); }
623 class referenced_vars_iterator {
624 const MemRegion * const *R;
625 const MemRegion * const *OriginalR;
627 explicit referenced_vars_iterator(const MemRegion * const *r,
628 const MemRegion * const *originalR)
629 : R(r), OriginalR(originalR) {}
631 operator const MemRegion * const *() const {
635 const MemRegion *getCapturedRegion() const {
638 const MemRegion *getOriginalRegion() const {
642 const VarRegion* operator*() const {
643 return cast<VarRegion>(*R);
646 bool operator==(const referenced_vars_iterator &I) const {
649 bool operator!=(const referenced_vars_iterator &I) const {
652 referenced_vars_iterator &operator++() {
659 referenced_vars_iterator referenced_vars_begin() const;
660 referenced_vars_iterator referenced_vars_end() const;
662 virtual void dumpToStream(raw_ostream &os) const;
664 void Profile(llvm::FoldingSetNodeID& ID) const;
666 static void ProfileRegion(llvm::FoldingSetNodeID&, const BlockTextRegion *,
667 const LocationContext *, const MemRegion *);
669 static bool classof(const MemRegion* R) {
670 return R->getKind() == BlockDataRegionKind;
673 void LazyInitializeReferencedVars();
676 /// SymbolicRegion - A special, "non-concrete" region. Unlike other region
677 /// clases, SymbolicRegion represents a region that serves as an alias for
678 /// either a real region, a NULL pointer, etc. It essentially is used to
679 /// map the concept of symbolic values into the domain of regions. Symbolic
680 /// regions do not need to be typed.
681 class SymbolicRegion : public SubRegion {
686 SymbolicRegion(const SymbolRef s, const MemRegion* sreg)
687 : SubRegion(sreg, SymbolicRegionKind), sym(s) {}
689 SymbolRef getSymbol() const {
693 bool isBoundable() const { return true; }
695 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
697 void Profile(llvm::FoldingSetNodeID& ID) const;
699 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
701 const MemRegion* superRegion);
703 void dumpToStream(raw_ostream &os) const;
705 static bool classof(const MemRegion* R) {
706 return R->getKind() == SymbolicRegionKind;
710 /// StringRegion - Region associated with a StringLiteral.
711 class StringRegion : public TypedValueRegion {
712 friend class MemRegionManager;
713 const StringLiteral* Str;
716 StringRegion(const StringLiteral* str, const MemRegion* sreg)
717 : TypedValueRegion(sreg, StringRegionKind), Str(str) {}
719 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
720 const StringLiteral* Str,
721 const MemRegion* superRegion);
725 const StringLiteral* getStringLiteral() const { return Str; }
727 QualType getValueType() const {
728 return Str->getType();
731 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
733 bool isBoundable() const { return false; }
735 void Profile(llvm::FoldingSetNodeID& ID) const {
736 ProfileRegion(ID, Str, superRegion);
739 void dumpToStream(raw_ostream &os) const;
741 static bool classof(const MemRegion* R) {
742 return R->getKind() == StringRegionKind;
746 /// The region associated with an ObjCStringLiteral.
747 class ObjCStringRegion : public TypedValueRegion {
748 friend class MemRegionManager;
749 const ObjCStringLiteral* Str;
752 ObjCStringRegion(const ObjCStringLiteral* str, const MemRegion* sreg)
753 : TypedValueRegion(sreg, ObjCStringRegionKind), Str(str) {}
755 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
756 const ObjCStringLiteral* Str,
757 const MemRegion* superRegion);
761 const ObjCStringLiteral* getObjCStringLiteral() const { return Str; }
763 QualType getValueType() const {
764 return Str->getType();
767 bool isBoundable() const { return false; }
769 void Profile(llvm::FoldingSetNodeID& ID) const {
770 ProfileRegion(ID, Str, superRegion);
773 void dumpToStream(raw_ostream &os) const;
775 static bool classof(const MemRegion* R) {
776 return R->getKind() == ObjCStringRegionKind;
780 /// CompoundLiteralRegion - A memory region representing a compound literal.
781 /// Compound literals are essentially temporaries that are stack allocated
782 /// or in the global constant pool.
783 class CompoundLiteralRegion : public TypedValueRegion {
785 friend class MemRegionManager;
786 const CompoundLiteralExpr *CL;
788 CompoundLiteralRegion(const CompoundLiteralExpr *cl, const MemRegion* sReg)
789 : TypedValueRegion(sReg, CompoundLiteralRegionKind), CL(cl) {}
791 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
792 const CompoundLiteralExpr *CL,
793 const MemRegion* superRegion);
795 QualType getValueType() const {
796 return CL->getType();
799 bool isBoundable() const { return !CL->isFileScope(); }
801 void Profile(llvm::FoldingSetNodeID& ID) const;
803 void dumpToStream(raw_ostream &os) const;
805 const CompoundLiteralExpr *getLiteralExpr() const { return CL; }
807 static bool classof(const MemRegion* R) {
808 return R->getKind() == CompoundLiteralRegionKind;
812 class DeclRegion : public TypedValueRegion {
816 DeclRegion(const Decl *d, const MemRegion* sReg, Kind k)
817 : TypedValueRegion(sReg, k), D(d) {}
819 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
820 const MemRegion* superRegion, Kind k);
823 const Decl *getDecl() const { return D; }
824 void Profile(llvm::FoldingSetNodeID& ID) const;
826 static bool classof(const MemRegion* R) {
827 unsigned k = R->getKind();
828 return k >= BEG_DECL_REGIONS && k <= END_DECL_REGIONS;
832 class VarRegion : public DeclRegion {
833 friend class MemRegionManager;
835 // Constructors and private methods.
836 VarRegion(const VarDecl *vd, const MemRegion* sReg)
837 : DeclRegion(vd, sReg, VarRegionKind) {}
839 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const VarDecl *VD,
840 const MemRegion *superRegion) {
841 DeclRegion::ProfileRegion(ID, VD, superRegion, VarRegionKind);
844 void Profile(llvm::FoldingSetNodeID& ID) const;
847 const VarDecl *getDecl() const { return cast<VarDecl>(D); }
849 const StackFrameContext *getStackFrame() const;
851 QualType getValueType() const {
852 // FIXME: We can cache this if needed.
853 return getDecl()->getType();
856 void dumpToStream(raw_ostream &os) const;
858 static bool classof(const MemRegion* R) {
859 return R->getKind() == VarRegionKind;
862 bool canPrintPretty() const;
863 void printPretty(raw_ostream &os) const;
866 /// CXXThisRegion - Represents the region for the implicit 'this' parameter
867 /// in a call to a C++ method. This region doesn't represent the object
868 /// referred to by 'this', but rather 'this' itself.
869 class CXXThisRegion : public TypedValueRegion {
870 friend class MemRegionManager;
871 CXXThisRegion(const PointerType *thisPointerTy,
872 const MemRegion *sReg)
873 : TypedValueRegion(sReg, CXXThisRegionKind), ThisPointerTy(thisPointerTy) {}
875 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
876 const PointerType *PT,
877 const MemRegion *sReg);
879 void Profile(llvm::FoldingSetNodeID &ID) const;
882 QualType getValueType() const {
883 return QualType(ThisPointerTy, 0);
886 void dumpToStream(raw_ostream &os) const;
888 static bool classof(const MemRegion* R) {
889 return R->getKind() == CXXThisRegionKind;
893 const PointerType *ThisPointerTy;
896 class FieldRegion : public DeclRegion {
897 friend class MemRegionManager;
899 FieldRegion(const FieldDecl *fd, const MemRegion* sReg)
900 : DeclRegion(fd, sReg, FieldRegionKind) {}
903 const FieldDecl *getDecl() const { return cast<FieldDecl>(D); }
905 QualType getValueType() const {
906 // FIXME: We can cache this if needed.
907 return getDecl()->getType();
910 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
912 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const FieldDecl *FD,
913 const MemRegion* superRegion) {
914 DeclRegion::ProfileRegion(ID, FD, superRegion, FieldRegionKind);
917 static bool classof(const MemRegion* R) {
918 return R->getKind() == FieldRegionKind;
921 void dumpToStream(raw_ostream &os) const;
923 bool canPrintPretty() const;
924 void printPretty(raw_ostream &os) const;
927 class ObjCIvarRegion : public DeclRegion {
929 friend class MemRegionManager;
931 ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg);
933 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const ObjCIvarDecl *ivd,
934 const MemRegion* superRegion);
937 const ObjCIvarDecl *getDecl() const;
938 QualType getValueType() const;
940 void dumpToStream(raw_ostream &os) const;
942 static bool classof(const MemRegion* R) {
943 return R->getKind() == ObjCIvarRegionKind;
946 //===----------------------------------------------------------------------===//
947 // Auxiliary data classes for use with MemRegions.
948 //===----------------------------------------------------------------------===//
952 class RegionRawOffset {
954 friend class ElementRegion;
956 const MemRegion *Region;
959 RegionRawOffset(const MemRegion* reg, CharUnits offset = CharUnits::Zero())
960 : Region(reg), Offset(offset) {}
963 // FIXME: Eventually support symbolic offsets.
964 CharUnits getOffset() const { return Offset; }
965 const MemRegion *getRegion() const { return Region; }
967 void dumpToStream(raw_ostream &os) const;
971 /// \brief ElementRegin is used to represent both array elements and casts.
972 class ElementRegion : public TypedValueRegion {
973 friend class MemRegionManager;
975 QualType ElementType;
978 ElementRegion(QualType elementType, NonLoc Idx, const MemRegion* sReg)
979 : TypedValueRegion(sReg, ElementRegionKind),
980 ElementType(elementType), Index(Idx) {
981 assert((!isa<nonloc::ConcreteInt>(&Idx) ||
982 cast<nonloc::ConcreteInt>(&Idx)->getValue().isSigned()) &&
983 "The index must be signed");
986 static void ProfileRegion(llvm::FoldingSetNodeID& ID, QualType elementType,
987 SVal Idx, const MemRegion* superRegion);
991 NonLoc getIndex() const { return Index; }
993 QualType getValueType() const {
997 QualType getElementType() const {
1000 /// Compute the offset within the array. The array might also be a subobject.
1001 RegionRawOffset getAsArrayOffset() const;
1003 void dumpToStream(raw_ostream &os) const;
1005 void Profile(llvm::FoldingSetNodeID& ID) const;
1007 static bool classof(const MemRegion* R) {
1008 return R->getKind() == ElementRegionKind;
1012 // C++ temporary object associated with an expression.
1013 class CXXTempObjectRegion : public TypedValueRegion {
1014 friend class MemRegionManager;
1018 CXXTempObjectRegion(Expr const *E, MemRegion const *sReg)
1019 : TypedValueRegion(sReg, CXXTempObjectRegionKind), Ex(E) {}
1021 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
1022 Expr const *E, const MemRegion *sReg);
1025 const Expr *getExpr() const { return Ex; }
1027 QualType getValueType() const {
1028 return Ex->getType();
1031 void dumpToStream(raw_ostream &os) const;
1033 void Profile(llvm::FoldingSetNodeID &ID) const;
1035 static bool classof(const MemRegion* R) {
1036 return R->getKind() == CXXTempObjectRegionKind;
1040 // CXXBaseObjectRegion represents a base object within a C++ object. It is
1041 // identified by the base class declaration and the region of its parent object.
1042 class CXXBaseObjectRegion : public TypedValueRegion {
1043 friend class MemRegionManager;
1045 const CXXRecordDecl *decl;
1047 CXXBaseObjectRegion(const CXXRecordDecl *d, const MemRegion *sReg)
1048 : TypedValueRegion(sReg, CXXBaseObjectRegionKind), decl(d) {}
1050 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
1051 const CXXRecordDecl *decl, const MemRegion *sReg);
1054 const CXXRecordDecl *getDecl() const { return decl; }
1056 QualType getValueType() const;
1058 void dumpToStream(raw_ostream &os) const;
1060 void Profile(llvm::FoldingSetNodeID &ID) const;
1062 static bool classof(const MemRegion *region) {
1063 return region->getKind() == CXXBaseObjectRegionKind;
1067 template<typename RegionTy>
1068 const RegionTy* MemRegion::getAs() const {
1069 if (const RegionTy* RT = dyn_cast<RegionTy>(this))
1075 //===----------------------------------------------------------------------===//
1076 // MemRegionManager - Factory object for creating regions.
1077 //===----------------------------------------------------------------------===//
1079 class MemRegionManager {
1081 llvm::BumpPtrAllocator& A;
1082 llvm::FoldingSet<MemRegion> Regions;
1084 GlobalInternalSpaceRegion *InternalGlobals;
1085 GlobalSystemSpaceRegion *SystemGlobals;
1086 GlobalImmutableSpaceRegion *ImmutableGlobals;
1089 llvm::DenseMap<const StackFrameContext *, StackLocalsSpaceRegion *>
1090 StackLocalsSpaceRegions;
1091 llvm::DenseMap<const StackFrameContext *, StackArgumentsSpaceRegion *>
1092 StackArgumentsSpaceRegions;
1093 llvm::DenseMap<const CodeTextRegion *, StaticGlobalSpaceRegion *>
1094 StaticsGlobalSpaceRegions;
1096 HeapSpaceRegion *heap;
1097 UnknownSpaceRegion *unknown;
1098 MemSpaceRegion *code;
1101 MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator& a)
1102 : C(c), A(a), InternalGlobals(0), SystemGlobals(0), ImmutableGlobals(0),
1103 heap(0), unknown(0), code(0) {}
1105 ~MemRegionManager();
1107 ASTContext &getContext() { return C; }
1109 llvm::BumpPtrAllocator &getAllocator() { return A; }
1111 /// getStackLocalsRegion - Retrieve the memory region associated with the
1112 /// specified stack frame.
1113 const StackLocalsSpaceRegion *
1114 getStackLocalsRegion(const StackFrameContext *STC);
1116 /// getStackArgumentsRegion - Retrieve the memory region associated with
1117 /// function/method arguments of the specified stack frame.
1118 const StackArgumentsSpaceRegion *
1119 getStackArgumentsRegion(const StackFrameContext *STC);
1121 /// getGlobalsRegion - Retrieve the memory region associated with
1122 /// global variables.
1123 const GlobalsSpaceRegion *getGlobalsRegion(
1124 MemRegion::Kind K = MemRegion::GlobalInternalSpaceRegionKind,
1125 const CodeTextRegion *R = 0);
1127 /// getHeapRegion - Retrieve the memory region associated with the
1129 const HeapSpaceRegion *getHeapRegion();
1131 /// getUnknownRegion - Retrieve the memory region associated with unknown
1133 const MemSpaceRegion *getUnknownRegion();
1135 const MemSpaceRegion *getCodeRegion();
1137 /// getAllocaRegion - Retrieve a region associated with a call to alloca().
1138 const AllocaRegion *getAllocaRegion(const Expr *Ex, unsigned Cnt,
1139 const LocationContext *LC);
1141 /// getCompoundLiteralRegion - Retrieve the region associated with a
1142 /// given CompoundLiteral.
1143 const CompoundLiteralRegion*
1144 getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
1145 const LocationContext *LC);
1147 /// getCXXThisRegion - Retrieve the [artificial] region associated with the
1148 /// parameter 'this'.
1149 const CXXThisRegion *getCXXThisRegion(QualType thisPointerTy,
1150 const LocationContext *LC);
1152 /// \brief Retrieve or create a "symbolic" memory region.
1153 const SymbolicRegion* getSymbolicRegion(SymbolRef Sym);
1155 /// \brief Return a unique symbolic region belonging to heap memory space.
1156 const SymbolicRegion *getSymbolicHeapRegion(SymbolRef sym);
1158 const StringRegion *getStringRegion(const StringLiteral* Str);
1160 const ObjCStringRegion *getObjCStringRegion(const ObjCStringLiteral *Str);
1162 /// getVarRegion - Retrieve or create the memory region associated with
1163 /// a specified VarDecl and LocationContext.
1164 const VarRegion* getVarRegion(const VarDecl *D, const LocationContext *LC);
1166 /// getVarRegion - Retrieve or create the memory region associated with
1167 /// a specified VarDecl and super region.
1168 const VarRegion* getVarRegion(const VarDecl *D, const MemRegion *superR);
1170 /// getElementRegion - Retrieve the memory region associated with the
1171 /// associated element type, index, and super region.
1172 const ElementRegion *getElementRegion(QualType elementType, NonLoc Idx,
1173 const MemRegion *superRegion,
1176 const ElementRegion *getElementRegionWithSuper(const ElementRegion *ER,
1177 const MemRegion *superRegion) {
1178 return getElementRegion(ER->getElementType(), ER->getIndex(),
1179 superRegion, ER->getContext());
1182 /// getFieldRegion - Retrieve or create the memory region associated with
1183 /// a specified FieldDecl. 'superRegion' corresponds to the containing
1184 /// memory region (which typically represents the memory representing
1185 /// a structure or class).
1186 const FieldRegion *getFieldRegion(const FieldDecl *fd,
1187 const MemRegion* superRegion);
1189 const FieldRegion *getFieldRegionWithSuper(const FieldRegion *FR,
1190 const MemRegion *superRegion) {
1191 return getFieldRegion(FR->getDecl(), superRegion);
1194 /// getObjCIvarRegion - Retrieve or create the memory region associated with
1195 /// a specified Objective-c instance variable. 'superRegion' corresponds
1196 /// to the containing region (which typically represents the Objective-C
1198 const ObjCIvarRegion *getObjCIvarRegion(const ObjCIvarDecl *ivd,
1199 const MemRegion* superRegion);
1201 const CXXTempObjectRegion *getCXXTempObjectRegion(Expr const *Ex,
1202 LocationContext const *LC);
1204 const CXXBaseObjectRegion *getCXXBaseObjectRegion(const CXXRecordDecl *decl,
1205 const MemRegion *superRegion);
1207 /// Create a CXXBaseObjectRegion with the same CXXRecordDecl but a different
1209 const CXXBaseObjectRegion *
1210 getCXXBaseObjectRegionWithSuper(const CXXBaseObjectRegion *baseReg,
1211 const MemRegion *superRegion) {
1212 return getCXXBaseObjectRegion(baseReg->getDecl(), superRegion);
1215 const FunctionTextRegion *getFunctionTextRegion(const FunctionDecl *FD);
1216 const BlockTextRegion *getBlockTextRegion(const BlockDecl *BD,
1218 AnalysisDeclContext *AC);
1220 /// getBlockDataRegion - Get the memory region associated with an instance
1221 /// of a block. Unlike many other MemRegions, the LocationContext*
1222 /// argument is allowed to be NULL for cases where we have no known
1224 const BlockDataRegion *getBlockDataRegion(const BlockTextRegion *bc,
1225 const LocationContext *lc = NULL);
1228 template <typename RegionTy, typename A1>
1229 RegionTy* getRegion(const A1 a1);
1231 template <typename RegionTy, typename A1>
1232 RegionTy* getSubRegion(const A1 a1, const MemRegion* superRegion);
1234 template <typename RegionTy, typename A1, typename A2>
1235 RegionTy* getRegion(const A1 a1, const A2 a2);
1237 template <typename RegionTy, typename A1, typename A2>
1238 RegionTy* getSubRegion(const A1 a1, const A2 a2,
1239 const MemRegion* superRegion);
1241 template <typename RegionTy, typename A1, typename A2, typename A3>
1242 RegionTy* getSubRegion(const A1 a1, const A2 a2, const A3 a3,
1243 const MemRegion* superRegion);
1245 template <typename REG>
1246 const REG* LazyAllocate(REG*& region);
1248 template <typename REG, typename ARG>
1249 const REG* LazyAllocate(REG*& region, ARG a);
1252 //===----------------------------------------------------------------------===//
1253 // Out-of-line member definitions.
1254 //===----------------------------------------------------------------------===//
1256 inline ASTContext &MemRegion::getContext() const {
1257 return getMemRegionManager()->getContext();
1260 } // end GR namespace
1262 } // end clang namespace
1264 //===----------------------------------------------------------------------===//
1265 // Pretty-printing regions.
1266 //===----------------------------------------------------------------------===//
1269 static inline raw_ostream &operator<<(raw_ostream &os,
1270 const clang::ento::MemRegion* R) {
1271 R->dumpToStream(os);
1274 } // end llvm namespace