16 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H 17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H 27 #include "llvm/ADT/FoldingSet.h" 28 #include "llvm/Support/Allocator.h" 29 #include "llvm/Support/ErrorHandling.h" 34 class LocationContext;
35 class StackFrameContext;
40 class MemRegionManager;
82 #define REGION(Id, Parent) Id ## Kind, 83 #define REGION_RANGE(Id, First, Last) BEGIN_##Id = First, END_##Id = Last, 84 #include "clang/StaticAnalyzer/Core/PathSensitive/Regions.def" 97 virtual void Profile(llvm::FoldingSetNodeID&
ID)
const = 0;
106 virtual bool isSubRegionOf(
const MemRegion *R)
const;
108 const MemRegion *StripCasts(
bool StripBaseCasts =
true)
const;
114 bool hasGlobalsOrParametersStorage()
const;
116 bool hasStackStorage()
const;
118 bool hasStackNonParametersStorage()
const;
120 bool hasStackParametersStorage()
const;
126 std::string getString()
const;
128 virtual void dumpToStream(raw_ostream &os)
const;
133 virtual bool canPrintPretty()
const;
136 virtual void printPretty(raw_ostream &os)
const;
140 virtual bool canPrintPrettyAsExpr()
const;
146 virtual void printPrettyAsExpr(raw_ostream &os)
const;
150 template<
typename RegionTy>
const RegionTy* getAs()
const;
163 std::string getDescriptiveName(
bool UseQuotes =
true)
const;
193 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
197 return k >= BEGIN_MEMSPACES && k <= END_MEMSPACES;
210 void dumpToStream(raw_ostream &os)
const override;
213 return R->
getKind() == CodeSpaceRegionKind;
218 virtual void anchor();
228 return k >= BEGIN_GLOBAL_MEMSPACES && k <= END_GLOBAL_MEMSPACES;
248 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
250 void dumpToStream(raw_ostream &os)
const override;
255 return R->
getKind() == StaticGlobalSpaceRegionKind;
266 virtual void anchor()
override;
278 return k >= BEGIN_NON_STATIC_GLOBAL_MEMSPACES &&
279 k <= END_NON_STATIC_GLOBAL_MEMSPACES;
293 void dumpToStream(raw_ostream &os)
const override;
296 return R->
getKind() == GlobalSystemSpaceRegionKind;
313 void dumpToStream(raw_ostream &os)
const override;
316 return R->
getKind() == GlobalImmutableSpaceRegionKind;
331 void dumpToStream(raw_ostream &os)
const override;
334 return R->
getKind() == GlobalInternalSpaceRegionKind;
345 void dumpToStream(raw_ostream &os)
const override;
348 return R->
getKind() == HeapSpaceRegionKind;
359 void dumpToStream(raw_ostream &os)
const override;
362 return R->
getKind() == UnknownSpaceRegionKind;
367 virtual void anchor();
381 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
385 return k >= BEGIN_STACK_MEMSPACES && k <= END_STACK_MEMSPACES;
395 void dumpToStream(raw_ostream &os)
const override;
398 return R->
getKind() == StackLocalsSpaceRegionKind;
409 void dumpToStream(raw_ostream &os)
const override;
412 return R->
getKind() == StackArgumentsSpaceRegionKind;
420 virtual void anchor();
441 bool isSubRegionOf(
const MemRegion* R)
const override;
444 return R->
getKind() > END_MEMSPACES;
462 :
SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {
466 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const Expr *Ex,
467 unsigned Cnt,
const MemRegion *superRegion);
477 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
479 void dumpToStream(raw_ostream &os)
const override;
482 return R->
getKind() == AllocaRegionKind;
488 virtual void anchor()
override;
496 virtual QualType getLocationType()
const = 0;
506 return k >= BEGIN_TYPED_REGIONS && k <= END_TYPED_REGIONS;
512 virtual void anchor()
override;
520 virtual QualType getValueType()
const = 0;
540 return k >= BEGIN_TYPED_VALUE_REGIONS && k <= END_TYPED_VALUE_REGIONS;
546 virtual void anchor()
override;
558 return k >= BEGIN_CODE_TEXT_REGIONS && k <= END_CODE_TEXT_REGIONS;
570 assert(isa<ObjCMethodDecl>(fd) || isa<FunctionDecl>(fd));
573 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const NamedDecl *FD,
579 if (
const FunctionDecl *D = dyn_cast<FunctionDecl>(FD)) {
583 assert(isa<ObjCMethodDecl>(FD));
584 assert(
false &&
"Getting the type of ObjCMethod is not supported yet");
595 void dumpToStream(raw_ostream &os)
const override;
597 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
600 return R->
getKind() == FunctionCodeRegionKind;
620 :
CodeTextRegion(sreg, BlockCodeRegionKind), BD(bd), AC(ac), locTy(lTy) {
626 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const BlockDecl *BD,
641 void dumpToStream(raw_ostream &os)
const override;
643 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
646 return R->
getKind() == BlockCodeRegionKind;
662 void *ReferencedVars;
667 :
TypedRegion(sreg, BlockDataRegionKind), BC(bc), LC(lc),
668 BlockCount(count), ReferencedVars(nullptr), OriginalVars(nullptr) {
671 assert(isa<GlobalImmutableSpaceRegion>(sreg) ||
672 isa<StackLocalsSpaceRegion>(sreg) ||
673 isa<UnknownSpaceRegion>(sreg));
676 static void ProfileRegion(llvm::FoldingSetNodeID&,
const BlockCodeRegion *,
693 : R(r), OriginalR(originalR) {}
696 return cast<VarRegion>(*R);
699 return cast<VarRegion>(*OriginalR);
703 assert((R ==
nullptr) == (I.R ==
nullptr));
707 assert((R ==
nullptr) == (I.R ==
nullptr));
724 void dumpToStream(raw_ostream &os)
const override;
726 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
729 return R->
getKind() == BlockDataRegionKind;
732 void LazyInitializeReferencedVars();
733 std::pair<const VarRegion *, const VarRegion *>
734 getCaptureRegions(
const VarDecl *VD);
748 :
SubRegion(sreg, SymbolicRegionKind), sym(s) {
753 assert(isa<UnknownSpaceRegion>(sreg) || isa<HeapSpaceRegion>(sreg));
765 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
767 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
771 void dumpToStream(raw_ostream &os)
const override;
774 return R->
getKind() == SymbolicRegionKind;
789 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
805 void Profile(llvm::FoldingSetNodeID&
ID)
const override {
806 ProfileRegion(ID, Str, superRegion);
809 void dumpToStream(raw_ostream &os)
const override;
812 return R->
getKind() == StringRegionKind;
828 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
842 void Profile(llvm::FoldingSetNodeID&
ID)
const override {
843 ProfileRegion(ID, Str, superRegion);
846 void dumpToStream(raw_ostream &os)
const override;
849 return R->
getKind() == ObjCStringRegionKind;
865 assert(isa<GlobalInternalSpaceRegion>(sReg) ||
866 isa<StackLocalsSpaceRegion>(sReg));
869 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
879 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
881 void dumpToStream(raw_ostream &os)
const override;
886 return R->
getKind() == CompoundLiteralRegionKind;
900 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const Decl *D,
905 void Profile(llvm::FoldingSetNodeID& ID)
const override;
909 return k >= BEGIN_DECL_REGIONS && k <= END_DECL_REGIONS;
923 assert(isa<GlobalsSpaceRegion>(sReg) || isa<StackSpaceRegion>(sReg) ||
924 isa<BlockDataRegion>(sReg) || isa<UnknownSpaceRegion>(sReg));
927 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const VarDecl *VD,
933 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
941 return getDecl()->getType();
944 void dumpToStream(raw_ostream &os)
const override;
947 return R->
getKind() == VarRegionKind;
950 bool canPrintPrettyAsExpr()
const override;
952 void printPrettyAsExpr(raw_ostream &os)
const override;
964 ThisPointerTy(thisPointerTy) {}
966 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
971 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
977 void dumpToStream(raw_ostream &os)
const override;
980 return R->
getKind() == CXXThisRegionKind;
993 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const FieldDecl *FD,
1003 return getDecl()->getType();
1009 return R->
getKind() == FieldRegionKind;
1012 void dumpToStream(raw_ostream &os)
const override;
1014 bool canPrintPretty()
const override;
1015 void printPretty(raw_ostream &os)
const override;
1016 bool canPrintPrettyAsExpr()
const override;
1017 void printPrettyAsExpr(raw_ostream &os)
const override;
1025 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const ObjCIvarDecl *ivd,
1030 QualType getValueType()
const override;
1032 bool canPrintPrettyAsExpr()
const override;
1033 void printPrettyAsExpr(raw_ostream &os)
const override;
1035 void dumpToStream(raw_ostream &os)
const override;
1038 return R->
getKind() == ObjCIvarRegionKind;
1054 : Region(reg), Offset(offset) {}
1061 void dumpToStream(raw_ostream &os)
const;
1074 ElementType(elementType), Index(Idx) {
1077 "The index must be signed");
1080 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
QualType elementType,
1097 void dumpToStream(raw_ostream &os)
const override;
1099 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
1102 return R->
getKind() == ElementRegionKind;
1115 assert(isa<StackLocalsSpaceRegion>(sReg) ||
1116 isa<GlobalInternalSpaceRegion>(sReg));
1119 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
1129 void dumpToStream(raw_ostream &os)
const override;
1131 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
1134 return R->
getKind() == CXXTempObjectRegionKind;
1143 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> Data;
1151 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
const CXXRecordDecl *RD,
1158 QualType getValueType()
const override;
1160 void dumpToStream(raw_ostream &os)
const override;
1162 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
1165 return region->
getKind() == CXXBaseObjectRegionKind;
1168 bool canPrintPrettyAsExpr()
const override;
1170 void printPrettyAsExpr(raw_ostream &os)
const override;
1173 template<
typename RegionTy>
1175 if (
const RegionTy* RT = dyn_cast<RegionTy>(
this))
1187 llvm::BumpPtrAllocator& A;
1188 llvm::FoldingSet<MemRegion> Regions;
1195 llvm::DenseMap<const StackFrameContext *, StackLocalsSpaceRegion *>
1196 StackLocalsSpaceRegions;
1197 llvm::DenseMap<const StackFrameContext *, StackArgumentsSpaceRegion *>
1198 StackArgumentsSpaceRegions;
1199 llvm::DenseMap<const CodeTextRegion *, StaticGlobalSpaceRegion *>
1200 StaticsGlobalSpaceRegions;
1208 : C(c), A(a), InternalGlobals(nullptr), SystemGlobals(nullptr),
1209 ImmutableGlobals(nullptr), heap(nullptr), unknown(nullptr),
1298 return getFieldRegion(FR->
getDecl(), superRegion);
1324 return getCXXBaseObjectRegion(baseReg->
getDecl(), superRegion,
1339 unsigned blockCount);
1347 template <
typename RegionTy,
typename SuperTy,
1349 RegionTy* getSubRegion(
const Arg1Ty arg1,
1350 const SuperTy* superRegion);
1352 template <
typename RegionTy,
typename SuperTy,
1353 typename Arg1Ty,
typename Arg2Ty>
1354 RegionTy* getSubRegion(
const Arg1Ty arg1,
const Arg2Ty arg2,
1355 const SuperTy* superRegion);
1357 template <
typename RegionTy,
typename SuperTy,
1358 typename Arg1Ty,
typename Arg2Ty,
typename Arg3Ty>
1359 RegionTy* getSubRegion(
const Arg1Ty arg1,
const Arg2Ty arg2,
1361 const SuperTy* superRegion);
1363 template <
typename REG>
1364 const REG* LazyAllocate(REG*& region);
1366 template <
typename REG,
typename ARG>
1367 const REG* LazyAllocate(REG*& region, ARG a);
1375 return getMemRegionManager()->getContext();
1384 typedef unsigned char StorageTypeForKinds;
1385 llvm::DenseMap<const MemRegion *, StorageTypeForKinds> MRTraitsMap;
1386 llvm::DenseMap<SymbolRef, StorageTypeForKinds> SymTraitsMap;
1388 typedef llvm::DenseMap<const MemRegion *, StorageTypeForKinds>::const_iterator
1389 const_region_iterator;
1390 typedef llvm::DenseMap<SymbolRef, StorageTypeForKinds>::const_iterator
1391 const_symbol_iterator;
1397 TK_PreserveContents = 0x1,
1399 TK_SuppressEscape = 0x2,
1401 TK_DoNotInvalidateSuperRegion = 0x4,
1404 TK_EntireMemSpace = 0x8
RegionOffset(const MemRegion *r, int64_t off)
const ElementRegion * getElementRegionWithSuper(const ElementRegion *ER, const SubRegion *superRegion)
bool isBoundable() const override
Defines the clang::ASTContext interface.
CodeTextRegion(const MemSpaceRegion *sreg, Kind k)
An instance of this class is created to represent a function declaration or definition.
TypedValueRegion - An abstract class representing regions having a typed value.
static bool classof(const MemRegion *R)
CompoundLiteralRegion - A memory region representing a compound literal.
QualType getValueType() const override
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
MemRegion - The root abstract class for all memory regions.
bool isBlockPointerType() const
QualType getDesugaredType(const ASTContext &Context) const
Return the specified type with any "sugar" removed from the type.
SubRegion(const MemRegion *sReg, Kind k)
Information about invalidation for a particular region/symbol.
MemRegionManager * getMemRegionManager() const override
virtual void dumpToStream(raw_ostream &os) const
BlockCodeRegion - A region that represents code texts of blocks (closures).
Decl - This represents one declaration (or definition), e.g.
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
QualType getLocationType() const override
const NamedDecl * getDecl() const
const MemRegion * getRegion() const
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
static bool classof(const MemRegion *region)
const ObjCStringLiteral * getObjCStringLiteral() const
static bool classof(const MemRegion *R)
Value representing integer constant.
const CXXBaseObjectRegion * getCXXBaseObjectRegionWithSuper(const CXXBaseObjectRegion *baseReg, const SubRegion *superRegion)
Create a CXXBaseObjectRegion with the same CXXRecordDecl but a different super region.
AllocaRegion - A region that represents an untyped blob of bytes created by a call to 'alloca'...
static bool classof(const MemRegion *R)
bool operator==(const referenced_vars_iterator &I) const
CodeSpaceRegion - The memory space that holds the executable code of functions and blocks...
CharUnits getOffset() const
ASTContext & getContext()
VarDecl - An instance of this class is created to represent a variable declaration or definition...
CompoundLiteralExpr - [C99 6.5.2.5].
const T * getAs() const
Member-template getAs<specific type>'.
QualType getElementType() const
AnalysisDeclContext * getAnalysisDeclContext() const
CXXThisRegion - Represents the region for the implicit 'this' parameter in a call to a C++ method...
const MemRegion * getSuperRegion() const
GlobalsSpaceRegion(MemRegionManager *mgr, Kind k)
The region containing globals which can be modified by calls to "internally" defined functions - (for...
The region associated with an ObjCStringLiteral.
Represents a class type in Objective C.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const VarRegion * getOriginalRegion() const
const FieldDecl * getDecl() const
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
ASTContext & getContext() const
AnalysisDeclContext contains the context data for the function or method under analysis.
QualType getLocationType() const override
bool isReferenceType() const
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
virtual DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const
getExtent - Returns the size of the region in bytes.
QualType getValueType() const override
bool isBoundable() const override
The region containing globals which are considered not to be modified or point to data which could be...
static bool classof(const MemRegion *R)
BlockDataRegion - A region that represents a block instance.
static bool classof(const MemRegion *R)
CharUnits - This is an opaque type for sizes expressed in character units.
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
const CXXRecordDecl * getDecl() const
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
referenced_vars_iterator(const MemRegion *const *r, const MemRegion *const *originalR)
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
ObjCStringLiteral, used for Objective-C string literals i.e.
static bool classof(const MemRegion *R)
const StringLiteral * getStringLiteral() const
QualType getValueType() const override
Represent a region's offset within the top level base region.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
static void ProfileRegion(llvm::FoldingSetNodeID &ID, const Decl *D, const MemRegion *superRegion, Kind k)
virtual QualType getType() const =0
static bool classof(const MemRegion *R)
QualType getValueType() const override
const VarRegion * getCapturedRegion() const
bool operator!=(const referenced_vars_iterator &I) const
static bool classof(const MemRegion *R)
const RegionTy * getAs() const
SymbolicRegion - A special, "non-concrete" region.
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Expr - This represents one expression.
TypedValueRegion(const MemRegion *sReg, Kind k)
const FunctionProtoType * T
static const int64_t Symbolic
const Decl * getDecl() const
llvm::BumpPtrAllocator & getAllocator()
static bool classof(const MemRegion *R)
QualType getDesugaredValueType(ASTContext &Context) const
const BlockCodeRegion * getCodeRegion() const
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
TypedRegion(const MemRegion *sReg, Kind k)
const StackFrameContext * getStackFrame() const
The region of the static variables within the current CodeTextRegion scope.
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
const VarDecl * getDecl() const
virtual bool isBoundable() const
The region for all the non-static global variables.
static bool classof(const MemRegion *R)
const BlockDecl * getDecl() const
QualType getValueType() const override
static bool classof(const MemRegion *R)
FunctionCodeRegion - A region that represents code texts of function.
SymbolRef getSymbol() const
const CompoundLiteralExpr * getLiteralExpr() const
bool isBoundable() const override
static bool classof(const MemRegion *R)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
bool isBoundable() const override
bool isAnyPointerType() const
QualType getLocationType() const override
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
bool isBoundable() const override
static bool classof(const MemRegion *R)
Dataflow Directional Tag Classes.
raw_ostream & operator<<(raw_ostream &Out, const CheckerBase &Checker)
Dump checker name to stream.
QualType getValueType() const override
InvalidationKinds
Describes different invalidation traits.
const MemRegion * getRegion() const
bool isBoundable() const override
QualType getLocationType() const override
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
const Expr * getExpr() const
static bool classof(const MemRegion *R)
static bool classof(const OMPClause *T)
bool isBoundable() const override
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
SubRegion - A region that subsets another larger region.
The region containing globals which are defined in system/external headers and are considered modifia...
StackSpaceRegion(MemRegionManager *mgr, Kind k, const StackFrameContext *sfc)
int64_t getOffset() const
referenced_vars_iterator & operator++()
const Type * getTypePtrOrNull() const
const CodeTextRegion * getCodeRegion() const
const BlockDecl * getDecl() const
MemSpaceRegion(MemRegionManager *mgr, Kind k)
void Profile(llvm::FoldingSetNodeID &ID) const override
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
const Expr * getExpr() const
Represents a C++ struct/union/class.
static bool classof(const MemRegion *R)
void Profile(llvm::FoldingSetNodeID &ID) const override
ObjCIvarDecl - Represents an ObjC instance variable.
QualType getValueType() const override
static bool classof(const MemRegion *R)
static bool classof(const MemRegion *R)
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
DeclRegion(const Decl *d, const MemRegion *sReg, Kind k)
unsigned kind
All of the diagnostics that can be emitted by the frontend.
StringLiteral - This represents a string literal expression, e.g.
QualType getDesugaredLocationType(ASTContext &Context) const
StringRegion - Region associated with a StringLiteral.
const FieldRegion * getFieldRegionWithSuper(const FieldRegion *FR, const SubRegion *superRegion)
ElementRegin is used to represent both array elements and casts.
QualType getValueType() const override
MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a)
const llvm::APSInt & getValue() const
A trivial tuple used to represent a source range.
NamedDecl - This represents a decl with a name.
QualType getObjCObjectPointerType(QualType OIT) const
Return a ObjCObjectPointerType type for the given ObjCObjectType.
static bool classof(const MemRegion *R)
const MemRegion * superRegion
static bool classof(const MemRegion *R)
bool isBoundable() const override
bool hasSymbolicOffset() const
NonStaticGlobalSpaceRegion(MemRegionManager *mgr, Kind k)
TypedRegion - An abstract class representing regions that are typed.
static bool classof(const MemRegion *R)