16 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H 17 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H 31 #include "llvm/ADT/DenseMap.h" 32 #include "llvm/ADT/FoldingSet.h" 33 #include "llvm/ADT/Optional.h" 34 #include "llvm/ADT/PointerIntPair.h" 35 #include "llvm/Support/Allocator.h" 36 #include "llvm/Support/Casting.h" 45 class AnalysisDeclContext;
48 class LocationContext;
49 class StackFrameContext;
55 class MemRegionManager;
97 #define REGION(Id, Parent) Id ## Kind, 98 #define REGION_RANGE(Id, First, Last) BEGIN_##Id = First, END_##Id = Last, 99 #include "clang/StaticAnalyzer/Core/PathSensitive/Regions.def" 104 mutable Optional<RegionOffset> cachedOffset;
113 virtual void Profile(llvm::FoldingSetNodeID&
ID)
const = 0;
123 virtual bool isSubRegionOf(
const MemRegion *R)
const;
125 const MemRegion *StripCasts(
bool StripBaseCasts =
true)
const;
131 bool hasGlobalsOrParametersStorage()
const;
133 bool hasStackStorage()
const;
135 bool hasStackNonParametersStorage()
const;
137 bool hasStackParametersStorage()
const;
143 std::string getString()
const;
145 virtual void dumpToStream(raw_ostream &os)
const;
150 virtual bool canPrintPretty()
const;
153 virtual void printPretty(raw_ostream &os)
const;
157 virtual bool canPrintPrettyAsExpr()
const;
163 virtual void printPrettyAsExpr(raw_ostream &os)
const;
167 template<
typename RegionTy>
const RegionTy* getAs()
const;
179 std::string getDescriptiveName(
bool UseQuotes =
true)
const;
208 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
212 return k >= BEGIN_MEMSPACES && k <= END_MEMSPACES;
225 void dumpToStream(raw_ostream &os)
const override;
228 return R->
getKind() == CodeSpaceRegionKind;
233 virtual void anchor();
243 return k >= BEGIN_GLOBAL_MEMSPACES && k <= END_GLOBAL_MEMSPACES;
263 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
265 void dumpToStream(raw_ostream &os)
const override;
270 return R->
getKind() == StaticGlobalSpaceRegionKind;
281 void anchor()
override;
292 return k >= BEGIN_NON_STATIC_GLOBAL_MEMSPACES &&
293 k <= END_NON_STATIC_GLOBAL_MEMSPACES;
306 void dumpToStream(raw_ostream &os)
const override;
309 return R->
getKind() == GlobalSystemSpaceRegionKind;
325 void dumpToStream(raw_ostream &os)
const override;
328 return R->
getKind() == GlobalImmutableSpaceRegionKind;
342 void dumpToStream(raw_ostream &os)
const override;
345 return R->
getKind() == GlobalInternalSpaceRegionKind;
356 void dumpToStream(raw_ostream &os)
const override;
359 return R->
getKind() == HeapSpaceRegionKind;
370 void dumpToStream(raw_ostream &os)
const override;
373 return R->
getKind() == UnknownSpaceRegionKind;
378 virtual void anchor();
392 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
396 return k >= BEGIN_STACK_MEMSPACES && k <= END_STACK_MEMSPACES;
407 void dumpToStream(raw_ostream &os)
const override;
410 return R->
getKind() == StackLocalsSpaceRegionKind;
422 void dumpToStream(raw_ostream &os)
const override;
425 return R->
getKind() == StackArgumentsSpaceRegionKind;
432 virtual void anchor();
454 bool isSubRegionOf(
const MemRegion* R)
const override;
457 return R->
getKind() > END_MEMSPACES;
477 :
SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {
481 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const Expr *Ex,
482 unsigned Cnt,
const MemRegion *superRegion);
491 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
493 void dumpToStream(raw_ostream &os)
const override;
496 return R->
getKind() == AllocaRegionKind;
502 void anchor()
override;
510 virtual QualType getLocationType()
const = 0;
520 return k >= BEGIN_TYPED_REGIONS && k <= END_TYPED_REGIONS;
526 void anchor()
override;
534 virtual QualType getValueType()
const = 0;
554 return k >= BEGIN_TYPED_VALUE_REGIONS && k <= END_TYPED_VALUE_REGIONS;
559 void anchor()
override;
571 return k >= BEGIN_CODE_TEXT_REGIONS && k <= END_CODE_TEXT_REGIONS;
583 assert(isa<ObjCMethodDecl>(fd) || isa<FunctionDecl>(fd));
586 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const NamedDecl *FD,
592 if (
const auto *D = dyn_cast<FunctionDecl>(FD)) {
596 assert(isa<ObjCMethodDecl>(FD));
597 assert(
false &&
"Getting the type of ObjCMethod is not supported yet");
608 void dumpToStream(raw_ostream &os)
const override;
610 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
613 return R->
getKind() == FunctionCodeRegionKind;
632 :
CodeTextRegion(sreg, BlockCodeRegionKind), BD(bd), AC(ac), locTy(lTy) {
638 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const BlockDecl *BD,
653 void dumpToStream(raw_ostream &os)
const override;
655 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
658 return R->
getKind() == BlockCodeRegionKind;
674 void *ReferencedVars =
nullptr;
675 void *OriginalVars =
nullptr;
679 :
TypedRegion(sreg, BlockDataRegionKind), BC(bc), LC(lc),
683 assert(isa<GlobalImmutableSpaceRegion>(sreg) ||
684 isa<StackLocalsSpaceRegion>(sreg) ||
685 isa<UnknownSpaceRegion>(sreg));
688 static void ProfileRegion(llvm::FoldingSetNodeID&,
const BlockCodeRegion *,
706 : R(r), OriginalR(originalR) {}
709 return cast<VarRegion>(*R);
713 return cast<VarRegion>(*OriginalR);
717 assert((R ==
nullptr) == (I.R ==
nullptr));
722 assert((R ==
nullptr) == (I.R ==
nullptr));
740 void dumpToStream(raw_ostream &os)
const override;
742 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
745 return R->
getKind() == BlockDataRegionKind;
749 void LazyInitializeReferencedVars();
750 std::pair<const VarRegion *, const VarRegion *>
751 getCaptureRegions(
const VarDecl *VD);
765 :
SubRegion(sreg, SymbolicRegionKind), sym(s) {
770 assert(isa<UnknownSpaceRegion>(sreg) || isa<HeapSpaceRegion>(sreg));
780 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
782 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
786 void dumpToStream(raw_ostream &os)
const override;
789 return R->
getKind() == SymbolicRegionKind;
804 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
817 void Profile(llvm::FoldingSetNodeID&
ID)
const override {
818 ProfileRegion(ID, Str, superRegion);
821 void dumpToStream(raw_ostream &os)
const override;
824 return R->
getKind() == StringRegionKind;
840 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
851 void Profile(llvm::FoldingSetNodeID&
ID)
const override {
852 ProfileRegion(ID, Str, superRegion);
855 void dumpToStream(raw_ostream &os)
const override;
858 return R->
getKind() == ObjCStringRegionKind;
874 assert(isa<GlobalInternalSpaceRegion>(sReg) ||
875 isa<StackLocalsSpaceRegion>(sReg));
878 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
887 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
889 void dumpToStream(raw_ostream &os)
const override;
894 return R->
getKind() == CompoundLiteralRegionKind;
908 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const Decl *D,
913 void Profile(llvm::FoldingSetNodeID& ID)
const override;
917 return k >= BEGIN_DECL_REGIONS && k <= END_DECL_REGIONS;
931 assert(isa<GlobalsSpaceRegion>(sReg) || isa<StackSpaceRegion>(sReg) ||
932 isa<BlockDataRegion>(sReg) || isa<UnknownSpaceRegion>(sReg));
935 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const VarDecl *VD,
941 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
949 return getDecl()->getType();
952 void dumpToStream(raw_ostream &os)
const override;
954 bool canPrintPrettyAsExpr()
const override;
956 void printPrettyAsExpr(raw_ostream &os)
const override;
959 return R->
getKind() == VarRegionKind;
972 ThisPointerTy(thisPointerTy) {
973 assert(ThisPointerTy->getPointeeType()->getAsCXXRecordDecl() &&
974 "Invalid region type!");
977 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
982 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
988 void dumpToStream(raw_ostream &os)
const override;
991 return R->
getKind() == CXXThisRegionKind;
1004 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const FieldDecl *FD,
1014 return getDecl()->getType();
1019 void dumpToStream(raw_ostream &os)
const override;
1021 bool canPrintPretty()
const override;
1022 void printPretty(raw_ostream &os)
const override;
1023 bool canPrintPrettyAsExpr()
const override;
1024 void printPrettyAsExpr(raw_ostream &os)
const override;
1027 return R->
getKind() == FieldRegionKind;
1036 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
const ObjCIvarDecl *ivd,
1041 QualType getValueType()
const override;
1043 bool canPrintPrettyAsExpr()
const override;
1044 void printPrettyAsExpr(raw_ostream &os)
const override;
1046 void dumpToStream(raw_ostream &os)
const override;
1049 return R->
getKind() == ObjCIvarRegionKind;
1064 : Region(reg), Offset(offset) {}
1071 void dumpToStream(raw_ostream &os)
const;
1087 "The index must be signed");
1089 "Invalid region type!");
1092 static void ProfileRegion(llvm::FoldingSetNodeID&
ID,
QualType elementType,
1105 void dumpToStream(raw_ostream &os)
const override;
1107 void Profile(llvm::FoldingSetNodeID&
ID)
const override;
1110 return R->
getKind() == ElementRegionKind;
1123 assert(isa<StackLocalsSpaceRegion>(sReg) ||
1124 isa<GlobalInternalSpaceRegion>(sReg));
1127 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
1135 void dumpToStream(raw_ostream &os)
const override;
1137 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
1140 return R->
getKind() == CXXTempObjectRegionKind;
1149 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> Data;
1157 static void ProfileRegion(llvm::FoldingSetNodeID &
ID,
const CXXRecordDecl *RD,
1164 QualType getValueType()
const override;
1166 void dumpToStream(raw_ostream &os)
const override;
1168 void Profile(llvm::FoldingSetNodeID &
ID)
const override;
1170 bool canPrintPrettyAsExpr()
const override;
1172 void printPrettyAsExpr(raw_ostream &os)
const override;
1175 return region->
getKind() == CXXBaseObjectRegionKind;
1179 template<
typename RegionTy>
1181 if (
const auto *RT = dyn_cast<RegionTy>(
this))
1193 llvm::BumpPtrAllocator& A;
1194 llvm::FoldingSet<MemRegion> Regions;
1200 llvm::DenseMap<const StackFrameContext *, StackLocalsSpaceRegion *>
1201 StackLocalsSpaceRegions;
1202 llvm::DenseMap<const StackFrameContext *, StackArgumentsSpaceRegion *>
1203 StackArgumentsSpaceRegions;
1204 llvm::DenseMap<const CodeTextRegion *, StaticGlobalSpaceRegion *>
1205 StaticsGlobalSpaceRegions;
1299 return getFieldRegion(FR->
getDecl(), superRegion);
1325 return getCXXBaseObjectRegion(baseReg->
getDecl(), superRegion,
1340 unsigned blockCount);
1348 template <
typename RegionTy,
typename SuperTy,
1350 RegionTy* getSubRegion(
const Arg1Ty arg1,
1351 const SuperTy* superRegion);
1353 template <
typename RegionTy,
typename SuperTy,
1354 typename Arg1Ty,
typename Arg2Ty>
1355 RegionTy* getSubRegion(
const Arg1Ty arg1,
const Arg2Ty arg2,
1356 const SuperTy* superRegion);
1358 template <
typename RegionTy,
typename SuperTy,
1359 typename Arg1Ty,
typename Arg2Ty,
typename Arg3Ty>
1360 RegionTy* getSubRegion(
const Arg1Ty arg1,
const Arg2Ty arg2,
1362 const SuperTy* superRegion);
1364 template <
typename REG>
1365 const REG* LazyAllocate(REG*& region);
1367 template <
typename REG,
typename ARG>
1368 const REG* LazyAllocate(REG*& region, ARG a);
1376 return getMemRegionManager()->getContext();
1385 using StorageTypeForKinds =
unsigned char;
1387 llvm::DenseMap<const MemRegion *, StorageTypeForKinds> MRTraitsMap;
1388 llvm::DenseMap<SymbolRef, StorageTypeForKinds> SymTraitsMap;
1390 using const_region_iterator =
1391 llvm::DenseMap<const MemRegion *, StorageTypeForKinds>::const_iterator;
1392 using const_symbol_iterator =
1393 llvm::DenseMap<SymbolRef, StorageTypeForKinds>::const_iterator;
1399 TK_PreserveContents = 0x1,
1402 TK_SuppressEscape = 0x2,
1405 TK_DoNotInvalidateSuperRegion = 0x4,
1409 TK_EntireMemSpace = 0x8
1433 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H 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)
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
C Language Family Type Representation.
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)
const ValueDecl * getDecl() const
QualType getLocationType() const override
const NamedDecl * getDecl() const
const MemRegion * getRegion() const
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
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()
Represents a variable declaration or definition.
CompoundLiteralExpr - [C99 6.5.2.5].
const T * getAs() const
Member-template getAs<specific type>'.
DeclRegion(const ValueDecl *d, const MemRegion *sReg, Kind k)
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
Represents a member of a struct/union/class.
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.
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Expr - This represents one expression.
TypedValueRegion(const MemRegion *sReg, Kind k)
static const int64_t Symbolic
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)
bool isNull() const
Return true if this QualType doesn't point to a type yet.
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
const Expr * getExpr() const
Defines the clang::SourceLocation class and associated facilities.
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.
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.
__DEVICE__ int max(int __a, int __b)
QualType getValueType() const override
MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a)
const llvm::APSInt & getValue() const
A trivial tuple used to represent a source range.
This represents a decl that may have 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)