2008-10-04 13:50:14 +08:00
|
|
|
//== MemRegion.cpp - Abstract memory regions for static analysis --*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines MemRegion and its subclasses. MemRegion defines a
|
|
|
|
// partially-typed abstraction of memory useful for path-sensitive dataflow
|
|
|
|
// analyses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2010-01-12 01:06:35 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-08-02 12:56:14 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
|
|
|
#include "clang/Analysis/Support/BumpVector.h"
|
2012-01-05 07:54:01 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegion Construction.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template<typename RegionTy> struct MemRegionManagerTrait;
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1>
|
|
|
|
RegionTy* MemRegionManager::getRegion(const A1 a1) {
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
|
|
|
|
MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-04 08:05:57 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const A1 a1,
|
|
|
|
const MemRegion *superRegion) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-04 08:05:57 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1, typename A2>
|
|
|
|
RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
|
|
|
|
MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, a2, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-04 08:05:57 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, a2, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1, typename A2>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
|
|
|
|
const MemRegion *superRegion) {
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, a2, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-04 08:05:57 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, a2, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
template <typename RegionTy, typename A1, typename A2, typename A3>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3,
|
|
|
|
const MemRegion *superRegion) {
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-08 06:05:27 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, a2, a3, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
2009-12-04 08:05:57 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return R;
|
|
|
|
}
|
2009-12-04 08:05:57 +08:00
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-11-26 10:34:36 +08:00
|
|
|
// Object destruction.
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
MemRegion::~MemRegion() {}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
MemRegionManager::~MemRegionManager() {
|
|
|
|
// All regions and their data are BumpPtrAllocated. No need to call
|
|
|
|
// their destructors.
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-08 21:17:14 +08:00
|
|
|
bool SubRegion::isSubRegionOf(const MemRegion* R) const {
|
|
|
|
const MemRegion* r = getSuperRegion();
|
|
|
|
while (r != 0) {
|
|
|
|
if (r == R)
|
|
|
|
return true;
|
|
|
|
if (const SubRegion* sr = dyn_cast<SubRegion>(r))
|
|
|
|
r = sr->getSuperRegion();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-23 08:46:41 +08:00
|
|
|
MemRegionManager* SubRegion::getMemRegionManager() const {
|
|
|
|
const SubRegion* r = this;
|
|
|
|
do {
|
|
|
|
const MemRegion *superRegion = r->getSuperRegion();
|
|
|
|
if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
|
|
|
|
r = sr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return superRegion->getMemRegionManager();
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2009-12-15 06:15:06 +08:00
|
|
|
const StackFrameContext *VarRegion::getStackFrame() const {
|
|
|
|
const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
|
|
|
|
return SSR ? SSR->getStackFrame() : NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-04 08:00:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region extents.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
DefinedOrUnknownSVal TypedValueRegion::getExtent(SValBuilder &svalBuilder) const {
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
2010-12-10 19:01:00 +08:00
|
|
|
QualType T = getDesugaredValueType(Ctx);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2010-07-05 08:50:15 +08:00
|
|
|
if (isa<VariableArrayType>(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
|
2010-07-05 08:50:15 +08:00
|
|
|
if (isa<IncompleteArrayType>(T))
|
2010-07-04 08:00:41 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2010-12-23 10:42:36 +08:00
|
|
|
CharUnits size = Ctx.getTypeSizeInChars(T);
|
|
|
|
QualType sizeTy = svalBuilder.getArrayIndexType();
|
|
|
|
return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
|
|
|
|
DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
|
|
|
// A zero-length array at the end of a struct often stands for dynamically-
|
|
|
|
// allocated extra memory.
|
|
|
|
if (Extent.isZeroConstant()) {
|
2010-12-10 19:01:00 +08:00
|
|
|
QualType T = getDesugaredValueType(svalBuilder.getContext());
|
2010-07-04 08:00:41 +08:00
|
|
|
|
|
|
|
if (isa<ConstantArrayType>(T))
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Extent;
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
|
|
|
|
return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
|
|
|
|
return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
|
2010-12-23 10:42:36 +08:00
|
|
|
return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
|
|
|
|
svalBuilder.getArrayIndexType());
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg)
|
|
|
|
: DeclRegion(ivd, sReg, ObjCIvarRegionKind) {}
|
|
|
|
|
|
|
|
const ObjCIvarDecl *ObjCIvarRegion::getDecl() const {
|
|
|
|
return cast<ObjCIvarDecl>(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType ObjCIvarRegion::getValueType() const {
|
|
|
|
return getDecl()->getType();
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:21:53 +08:00
|
|
|
QualType CXXBaseObjectRegion::getValueType() const {
|
2013-02-21 11:12:32 +08:00
|
|
|
return QualType(getDecl()->getTypeForDecl(), 0);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 06:15:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned)getKind());
|
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger((unsigned)getKind());
|
|
|
|
ID.AddPointer(getStackFrame());
|
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
ID.AddInteger((unsigned)getKind());
|
|
|
|
ID.AddPointer(getCodeRegion());
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const StringLiteral* Str,
|
2008-10-25 22:13:41 +08:00
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger((unsigned) StringRegionKind);
|
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const ObjCStringLiteral* Str,
|
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger((unsigned) ObjCStringRegionKind);
|
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2008-11-02 08:34:33 +08:00
|
|
|
void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *Ex, unsigned cnt,
|
2012-11-27 03:11:46 +08:00
|
|
|
const MemRegion *superRegion) {
|
2008-11-02 08:34:33 +08:00
|
|
|
ID.AddInteger((unsigned) AllocaRegionKind);
|
|
|
|
ID.AddPointer(Ex);
|
|
|
|
ID.AddInteger(cnt);
|
2012-11-27 03:11:46 +08:00
|
|
|
ID.AddPointer(superRegion);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-06-23 08:15:41 +08:00
|
|
|
ProfileRegion(ID, Ex, Cnt, superRegion);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2008-10-28 04:57:58 +08:00
|
|
|
void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2011-08-13 07:37:29 +08:00
|
|
|
const CompoundLiteralExpr *CL,
|
2008-10-28 04:57:58 +08:00
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger((unsigned) CompoundLiteralRegionKind);
|
|
|
|
ID.AddPointer(CL);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const PointerType *PT,
|
|
|
|
const MemRegion *sRegion) {
|
|
|
|
ID.AddInteger((unsigned) CXXThisRegionKind);
|
|
|
|
ID.AddPointer(PT);
|
|
|
|
ID.AddPointer(sRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const ObjCIvarDecl *ivd,
|
|
|
|
const MemRegion* superRegion) {
|
|
|
|
DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind);
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
|
2008-10-04 13:50:14 +08:00
|
|
|
const MemRegion* superRegion, Kind k) {
|
|
|
|
ID.AddInteger((unsigned) k);
|
|
|
|
ID.AddPointer(D);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
|
|
|
|
}
|
|
|
|
|
2009-08-22 06:28:32 +08:00
|
|
|
void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2009-12-08 06:05:27 +08:00
|
|
|
VarRegion::ProfileRegion(ID, getDecl(), superRegion);
|
2009-08-22 06:28:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
|
|
|
|
const MemRegion *sreg) {
|
2008-10-18 04:28:54 +08:00
|
|
|
ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
|
2008-12-05 10:39:38 +08:00
|
|
|
ID.Add(sym);
|
2009-06-23 07:13:13 +08:00
|
|
|
ID.AddPointer(sreg);
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-06-23 07:13:13 +08:00
|
|
|
SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2009-05-04 14:18:28 +08:00
|
|
|
void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType ElementType, SVal Idx,
|
2008-10-21 13:27:10 +08:00
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger(MemRegion::ElementRegionKind);
|
2009-05-04 14:18:28 +08:00
|
|
|
ID.Add(ElementType);
|
2008-10-21 13:27:10 +08:00
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
Idx.Profile(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-05-04 14:18:28 +08:00
|
|
|
ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
}
|
2008-10-27 21:17:02 +08:00
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2012-09-18 03:13:56 +08:00
|
|
|
const NamedDecl *FD,
|
2009-11-25 09:32:22 +08:00
|
|
|
const MemRegion*) {
|
|
|
|
ID.AddInteger(MemRegion::FunctionTextRegionKind);
|
2009-08-28 12:49:15 +08:00
|
|
|
ID.AddPointer(FD);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-12-08 06:05:27 +08:00
|
|
|
const BlockDecl *BD, CanQualType,
|
2011-10-24 09:32:45 +08:00
|
|
|
const AnalysisDeclContext *AC,
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion*) {
|
2009-11-25 09:32:22 +08:00
|
|
|
ID.AddInteger(MemRegion::BlockTextRegionKind);
|
|
|
|
ID.AddPointer(BD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-12-08 06:05:27 +08:00
|
|
|
BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 07:53:07 +08:00
|
|
|
void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const BlockTextRegion *BC,
|
|
|
|
const LocationContext *LC,
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion *sReg) {
|
2009-11-26 07:53:07 +08:00
|
|
|
ID.AddInteger(MemRegion::BlockDataRegionKind);
|
|
|
|
ID.AddPointer(BC);
|
|
|
|
ID.AddPointer(LC);
|
2009-12-08 06:05:27 +08:00
|
|
|
ID.AddPointer(sReg);
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-12-08 06:05:27 +08:00
|
|
|
BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
Expr const *Ex,
|
|
|
|
const MemRegion *sReg) {
|
2010-01-09 17:16:47 +08:00
|
|
|
ID.AddPointer(Ex);
|
2009-12-16 19:27:52 +08:00
|
|
|
ID.AddPointer(sReg);
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2010-01-09 17:16:47 +08:00
|
|
|
ProfileRegion(ID, Ex, getSuperRegion());
|
2009-12-16 19:27:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:21:53 +08:00
|
|
|
void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
2013-02-21 11:12:32 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
bool IsVirtual,
|
|
|
|
const MemRegion *SReg) {
|
|
|
|
ID.AddPointer(RD);
|
|
|
|
ID.AddBoolean(IsVirtual);
|
|
|
|
ID.AddPointer(SReg);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2013-02-21 11:12:32 +08:00
|
|
|
ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region anchors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GlobalsSpaceRegion::anchor() { }
|
|
|
|
void HeapSpaceRegion::anchor() { }
|
|
|
|
void UnknownSpaceRegion::anchor() { }
|
|
|
|
void StackLocalsSpaceRegion::anchor() { }
|
|
|
|
void StackArgumentsSpaceRegion::anchor() { }
|
|
|
|
void TypedRegion::anchor() { }
|
|
|
|
void TypedValueRegion::anchor() { }
|
|
|
|
void CodeTextRegion::anchor() { }
|
|
|
|
void SubRegion::anchor() { }
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void MemRegion::dump() const {
|
|
|
|
dumpToStream(llvm::errs());
|
2009-07-03 01:24:10 +08:00
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
std::string MemRegion::getString() const {
|
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2009-07-14 07:31:04 +08:00
|
|
|
dumpToStream(os);
|
2008-10-04 13:50:14 +08:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void MemRegion::dumpToStream(raw_ostream &os) const {
|
2008-10-04 13:50:14 +08:00
|
|
|
os << "<Unknown Region>";
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void AllocaRegion::dumpToStream(raw_ostream &os) const {
|
2012-09-06 23:59:27 +08:00
|
|
|
os << "alloca{" << (const void*) Ex << ',' << Cnt << '}';
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void FunctionTextRegion::dumpToStream(raw_ostream &os) const {
|
2009-08-28 12:49:15 +08:00
|
|
|
os << "code{" << getDecl()->getDeclName().getAsString() << '}';
|
2009-04-22 03:56:58 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void BlockTextRegion::dumpToStream(raw_ostream &os) const {
|
2012-09-06 23:59:27 +08:00
|
|
|
os << "block_code{" << (const void*) this << '}';
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void BlockDataRegion::dumpToStream(raw_ostream &os) const {
|
2009-11-26 07:53:07 +08:00
|
|
|
os << "block_data{" << BC << '}';
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
|
2009-04-22 02:09:22 +08:00
|
|
|
// FIXME: More elaborate pretty-printing.
|
2012-09-06 23:59:27 +08:00
|
|
|
os << "{ " << (const void*) CL << " }";
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
|
2011-08-26 08:41:31 +08:00
|
|
|
os << "temp_object{" << getValueType().getAsString() << ','
|
2012-09-06 23:59:27 +08:00
|
|
|
<< (const void*) Ex << '}';
|
2010-11-25 10:07:24 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
|
2013-02-21 11:12:32 +08:00
|
|
|
os << "base{" << superRegion << ',' << getDecl()->getName() << '}';
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXThisRegion::dumpToStream(raw_ostream &os) const {
|
2010-01-05 10:18:06 +08:00
|
|
|
os << "this";
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ElementRegion::dumpToStream(raw_ostream &os) const {
|
2009-08-01 14:17:29 +08:00
|
|
|
os << "element{" << superRegion << ','
|
|
|
|
<< Index << ',' << getElementType().getAsString() << '}';
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void FieldRegion::dumpToStream(raw_ostream &os) const {
|
2011-10-15 02:45:37 +08:00
|
|
|
os << superRegion << "->" << *getDecl();
|
2008-10-18 05:05:44 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
|
2011-10-15 02:45:37 +08:00
|
|
|
os << "ivar{" << superRegion << ',' << *getDecl() << '}';
|
2009-07-20 04:36:24 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void StringRegion::dumpToStream(raw_ostream &os) const {
|
2012-03-11 15:00:24 +08:00
|
|
|
Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOpts()));
|
2008-10-24 14:30:07 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
|
2012-03-11 15:00:24 +08:00
|
|
|
Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOpts()));
|
2012-02-28 08:56:05 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void SymbolicRegion::dumpToStream(raw_ostream &os) const {
|
2009-07-14 07:38:57 +08:00
|
|
|
os << "SymRegion{" << sym << '}';
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void VarRegion::dumpToStream(raw_ostream &os) const {
|
2011-10-15 02:45:37 +08:00
|
|
|
os << *cast<VarDecl>(D);
|
2008-11-10 21:05:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
void RegionRawOffset::dump() const {
|
|
|
|
dumpToStream(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void RegionRawOffset::dumpToStream(raw_ostream &os) const {
|
2011-01-24 09:55:39 +08:00
|
|
|
os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
|
2010-07-07 07:37:21 +08:00
|
|
|
os << "StaticGlobalsMemSpace{" << CR << '}';
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalInternalSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalSystemSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalImmutableSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2012-06-07 04:47:00 +08:00
|
|
|
void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "HeapSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "UnknownSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "StackArgumentsSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "StackLocalsSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool MemRegion::canPrintPretty() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemRegion::printPretty(raw_ostream &os) const {
|
2012-03-22 03:45:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool VarRegion::canPrintPretty() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VarRegion::printPretty(raw_ostream &os) const {
|
2012-03-22 03:45:08 +08:00
|
|
|
os << getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool FieldRegion::canPrintPretty() const {
|
|
|
|
return superRegion->canPrintPretty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FieldRegion::printPretty(raw_ostream &os) const {
|
|
|
|
superRegion->printPretty(os);
|
|
|
|
os << "." << getDecl()->getName();
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegionManager methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
template <typename REG>
|
|
|
|
const REG *MemRegionManager::LazyAllocate(REG*& region) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!region) {
|
2009-12-08 06:05:27 +08:00
|
|
|
region = (REG*) A.Allocate<REG>();
|
|
|
|
new (region) REG(this);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-06-23 08:46:41 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
template <typename REG, typename ARG>
|
|
|
|
const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
|
|
|
|
if (!region) {
|
|
|
|
region = (REG*) A.Allocate<REG>();
|
|
|
|
new (region) REG(this, a);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StackLocalsSpaceRegion*
|
2009-12-11 14:43:27 +08:00
|
|
|
MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
|
|
|
|
assert(STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
|
|
|
|
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StackLocalsSpaceRegion>();
|
|
|
|
new (R) StackLocalsSpaceRegion(this, STC);
|
|
|
|
return R;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const StackArgumentsSpaceRegion *
|
|
|
|
MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
|
2009-12-11 14:43:27 +08:00
|
|
|
assert(STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
|
|
|
|
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StackArgumentsSpaceRegion>();
|
|
|
|
new (R) StackArgumentsSpaceRegion(this, STC);
|
|
|
|
return R;
|
2009-07-03 02:14:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
const GlobalsSpaceRegion
|
2012-01-05 07:54:01 +08:00
|
|
|
*MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
|
|
|
|
const CodeTextRegion *CR) {
|
|
|
|
if (!CR) {
|
|
|
|
if (K == MemRegion::GlobalSystemSpaceRegionKind)
|
|
|
|
return LazyAllocate(SystemGlobals);
|
|
|
|
if (K == MemRegion::GlobalImmutableSpaceRegionKind)
|
|
|
|
return LazyAllocate(ImmutableGlobals);
|
|
|
|
assert(K == MemRegion::GlobalInternalSpaceRegionKind);
|
|
|
|
return LazyAllocate(InternalGlobals);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
assert(K == MemRegion::StaticGlobalSpaceRegionKind);
|
2010-07-02 04:16:50 +08:00
|
|
|
StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StaticGlobalSpaceRegion>();
|
|
|
|
new (R) StaticGlobalSpaceRegion(this, CR);
|
|
|
|
return R;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
|
2008-10-04 13:50:14 +08:00
|
|
|
return LazyAllocate(heap);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
|
2008-10-08 10:50:44 +08:00
|
|
|
return LazyAllocate(unknown);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getCodeRegion() {
|
2009-04-10 16:45:10 +08:00
|
|
|
return LazyAllocate(code);
|
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constructing regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-02 04:16:50 +08:00
|
|
|
const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<StringRegion>(Str, getGlobalsRegion());
|
2008-10-25 22:13:41 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
const ObjCStringRegion *
|
|
|
|
MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){
|
|
|
|
return getSubRegion<ObjCStringRegion>(Str, getGlobalsRegion());
|
|
|
|
}
|
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
/// Look through a chain of LocationContexts to either find the
|
|
|
|
/// StackFrameContext that matches a DeclContext, or find a VarRegion
|
|
|
|
/// for a variable captured by a block.
|
|
|
|
static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
|
|
|
|
getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
|
|
|
|
const DeclContext *DC,
|
|
|
|
const VarDecl *VD) {
|
|
|
|
while (LC) {
|
|
|
|
if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
|
|
|
|
if (cast<DeclContext>(SFC->getDecl()) == DC)
|
|
|
|
return SFC;
|
|
|
|
}
|
|
|
|
if (const BlockInvocationContext *BC =
|
|
|
|
dyn_cast<BlockInvocationContext>(LC)) {
|
|
|
|
const BlockDataRegion *BR =
|
|
|
|
static_cast<const BlockDataRegion*>(BC->getContextData());
|
|
|
|
// FIXME: This can be made more efficient.
|
|
|
|
for (BlockDataRegion::referenced_vars_iterator
|
|
|
|
I = BR->referenced_vars_begin(),
|
|
|
|
E = BR->referenced_vars_end(); I != E; ++I) {
|
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(I.getOriginalRegion()))
|
|
|
|
if (VR->getDecl() == VD)
|
|
|
|
return cast<VarRegion>(I.getCapturedRegion());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LC = LC->getParent();
|
|
|
|
}
|
|
|
|
return (const StackFrameContext*)0;
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
|
|
|
|
const LocationContext *LC) {
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion *sReg = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
if (D->hasGlobalStorage() && !D->isStaticLocal()) {
|
|
|
|
|
|
|
|
// First handle the globals defined in system headers.
|
|
|
|
if (C.getSourceManager().isInSystemHeader(D->getLocation())) {
|
|
|
|
// Whitelist the system globals which often DO GET modified, assume the
|
|
|
|
// rest are immutable.
|
|
|
|
if (D->getName().find("errno") != StringRef::npos)
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
|
|
|
|
else
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
|
|
|
|
|
|
|
// Treat other globals as GlobalInternal unless they are constants.
|
|
|
|
} else {
|
|
|
|
QualType GQT = D->getType();
|
|
|
|
const Type *GT = GQT.getTypePtrOrNull();
|
|
|
|
// TODO: We could walk the complex types here and see if everything is
|
|
|
|
// constified.
|
|
|
|
if (GT && GQT.isConstQualified() && GT->isArithmeticType())
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
|
|
|
else
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally handle static locals.
|
|
|
|
} else {
|
2009-12-08 06:05:27 +08:00
|
|
|
// FIXME: Once we implement scope handling, we will need to properly lookup
|
|
|
|
// 'D' to the proper LocationContext.
|
2009-12-11 14:43:27 +08:00
|
|
|
const DeclContext *DC = D->getDeclContext();
|
2012-06-02 04:04:04 +08:00
|
|
|
llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
|
|
|
|
getStackOrCaptureRegionForDeclContext(LC, DC, D);
|
|
|
|
|
|
|
|
if (V.is<const VarRegion*>())
|
|
|
|
return V.get<const VarRegion*>();
|
|
|
|
|
|
|
|
const StackFrameContext *STC = V.get<const StackFrameContext*>();
|
2009-12-11 14:43:27 +08:00
|
|
|
|
|
|
|
if (!STC)
|
|
|
|
sReg = getUnknownRegion();
|
|
|
|
else {
|
2010-07-02 04:16:50 +08:00
|
|
|
if (D->hasLocalStorage()) {
|
|
|
|
sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
|
|
|
|
? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
|
|
|
|
: static_cast<const MemRegion*>(getStackLocalsRegion(STC));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(D->isStaticLocal());
|
2012-09-18 03:13:56 +08:00
|
|
|
const Decl *STCD = STC->getDecl();
|
|
|
|
if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD))
|
2012-01-05 07:54:01 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
|
2012-09-18 03:13:56 +08:00
|
|
|
getFunctionTextRegion(cast<NamedDecl>(STCD)));
|
|
|
|
else if (const BlockDecl *BD = dyn_cast<BlockDecl>(STCD)) {
|
2010-07-02 04:16:50 +08:00
|
|
|
const BlockTextRegion *BTR =
|
|
|
|
getBlockTextRegion(BD,
|
|
|
|
C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
|
2011-10-24 09:32:45 +08:00
|
|
|
STC->getAnalysisDeclContext());
|
2012-01-05 07:54:01 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
|
|
|
|
BTR);
|
2010-07-02 04:16:50 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
}
|
|
|
|
}
|
2009-12-11 14:43:27 +08:00
|
|
|
}
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<VarRegion>(D, sReg);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
|
|
|
|
const MemRegion *superR) {
|
|
|
|
return getSubRegion<VarRegion>(D, superR);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockDataRegion *
|
|
|
|
MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
|
|
|
|
const LocationContext *LC) {
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion *sReg = 0;
|
2012-02-19 06:41:01 +08:00
|
|
|
const BlockDecl *BD = BC->getDecl();
|
|
|
|
if (!BD->hasCaptures()) {
|
|
|
|
// This handles 'static' blocks.
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-02-19 06:41:01 +08:00
|
|
|
if (LC) {
|
|
|
|
// FIXME: Once we implement scope handling, we want the parent region
|
|
|
|
// to be the scope.
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
sReg = getStackLocalsRegion(STC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We allow 'LC' to be NULL for cases where want BlockDataRegions
|
|
|
|
// without context-sensitivity.
|
|
|
|
sReg = getUnknownRegion();
|
|
|
|
}
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return getSubRegion<BlockDataRegion>(BC, LC, sReg);
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const CompoundLiteralRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC) {
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion *sReg = 0;
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
if (CL->isFileScope())
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
else {
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
sReg = getStackLocalsRegion(STC);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<CompoundLiteralRegion>(CL, sReg);
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ElementRegion*
|
2010-09-15 11:13:30 +08:00
|
|
|
MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
|
2009-07-16 09:33:37 +08:00
|
|
|
const MemRegion* superRegion,
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx){
|
2009-06-16 17:55:50 +08:00
|
|
|
|
2010-05-27 08:29:00 +08:00
|
|
|
QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
|
2008-12-14 03:24:37 +08:00
|
|
|
|
2008-10-21 13:27:10 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-06-16 17:55:50 +08:00
|
|
|
ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-10-21 13:27:10 +08:00
|
|
|
MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
ElementRegion* R = cast_or_null<ElementRegion>(data);
|
|
|
|
|
|
|
|
if (!R) {
|
|
|
|
R = (ElementRegion*) A.Allocate<ElementRegion>();
|
2009-06-16 17:55:50 +08:00
|
|
|
new (R) ElementRegion(T, Idx, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const FunctionTextRegion *
|
2012-09-18 03:13:56 +08:00
|
|
|
MemRegionManager::getFunctionTextRegion(const NamedDecl *FD) {
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockTextRegion *
|
2009-12-08 06:05:27 +08:00
|
|
|
MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC) {
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-18 04:28:54 +08:00
|
|
|
/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
|
2009-12-04 08:26:31 +08:00
|
|
|
const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
|
|
|
|
return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
const FieldRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getFieldRegion(const FieldDecl *d,
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemRegion* superRegion){
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<FieldRegion>(d, superRegion);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ObjCIvarRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
|
2008-10-18 04:28:54 +08:00
|
|
|
const MemRegion* superRegion) {
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<ObjCIvarRegion>(d, superRegion);
|
2008-10-25 04:30:08 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
const CXXTempObjectRegion*
|
|
|
|
MemRegionManager::getCXXTempObjectRegion(Expr const *E,
|
|
|
|
LocationContext const *LC) {
|
2010-01-09 17:16:47 +08:00
|
|
|
const StackFrameContext *SFC = LC->getCurrentStackFrame();
|
|
|
|
assert(SFC);
|
2010-11-26 16:52:48 +08:00
|
|
|
return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
|
2009-12-16 19:27:52 +08:00
|
|
|
}
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
/// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
|
|
|
|
/// class of the type of \p Super.
|
|
|
|
static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
|
|
|
|
const TypedValueRegion *Super,
|
|
|
|
bool IsVirtual) {
|
|
|
|
const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
|
|
|
|
if (!Class)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsVirtual)
|
|
|
|
return Class->isVirtuallyDerivedFrom(BaseClass);
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
|
|
|
|
E = Class->bases_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
|
|
|
|
return true;
|
|
|
|
}
|
2012-08-14 06:11:34 +08:00
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXBaseObjectRegion *
|
|
|
|
MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
|
|
|
|
const MemRegion *Super,
|
|
|
|
bool IsVirtual) {
|
|
|
|
RD = RD->getCanonicalDecl();
|
|
|
|
|
|
|
|
if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(Super)) {
|
|
|
|
assert(isValidBaseClass(RD, TVR, IsVirtual));
|
|
|
|
|
|
|
|
if (IsVirtual) {
|
|
|
|
// Virtual base regions should not be layered, since the layout rules
|
|
|
|
// are different.
|
|
|
|
while (const CXXBaseObjectRegion *Base =
|
|
|
|
dyn_cast<CXXBaseObjectRegion>(Super)) {
|
|
|
|
Super = Base->getSuperRegion();
|
2012-08-14 06:11:34 +08:00
|
|
|
}
|
2013-02-21 11:12:32 +08:00
|
|
|
assert(Super && !isa<MemSpaceRegion>(Super));
|
2012-08-14 06:11:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
const CXXThisRegion*
|
|
|
|
MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
|
|
|
|
const LocationContext *LC) {
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
const PointerType *PT = thisPointerTy->getAs<PointerType>();
|
|
|
|
assert(PT);
|
|
|
|
return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const AllocaRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC) {
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
const MemSpaceRegion *MemRegion::getMemorySpace() const {
|
|
|
|
const MemRegion *R = this;
|
|
|
|
const SubRegion* SR = dyn_cast<SubRegion>(this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 04:28:54 +08:00
|
|
|
while (SR) {
|
2009-06-24 02:17:08 +08:00
|
|
|
R = SR->getSuperRegion();
|
|
|
|
SR = dyn_cast<SubRegion>(R);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
return dyn_cast<MemSpaceRegion>(R);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
bool MemRegion::hasStackStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
return isa<StackSpaceRegion>(getMemorySpace());
|
2009-06-24 02:17:08 +08:00
|
|
|
}
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
bool MemRegion::hasStackNonParametersStorage() const {
|
|
|
|
return isa<StackLocalsSpaceRegion>(getMemorySpace());
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
bool MemRegion::hasStackParametersStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
return isa<StackArgumentsSpaceRegion>(getMemorySpace());
|
2009-07-03 06:02:15 +08:00
|
|
|
}
|
|
|
|
|
2009-07-03 02:25:09 +08:00
|
|
|
bool MemRegion::hasGlobalsOrParametersStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemSpaceRegion *MS = getMemorySpace();
|
|
|
|
return isa<StackArgumentsSpaceRegion>(MS) ||
|
|
|
|
isa<GlobalsSpaceRegion>(MS);
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-11-10 10:37:53 +08:00
|
|
|
// getBaseRegion strips away all elements and fields, and get the base region
|
|
|
|
// of them.
|
|
|
|
const MemRegion *MemRegion::getBaseRegion() const {
|
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
2010-04-07 06:06:03 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-01-13 20:46:31 +08:00
|
|
|
case MemRegion::CXXBaseObjectRegionKind:
|
2010-04-07 06:06:03 +08:00
|
|
|
R = cast<SubRegion>(R)->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
2009-11-10 10:37:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2012-09-13 08:37:12 +08:00
|
|
|
bool MemRegion::isSubRegionOf(const MemRegion *R) const {
|
2012-09-13 06:57:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// View handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-14 06:11:34 +08:00
|
|
|
const MemRegion *MemRegion::StripCasts(bool StripBaseCasts) const {
|
2009-07-30 02:14:27 +08:00
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
2012-07-12 08:16:25 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case ElementRegionKind: {
|
|
|
|
const ElementRegion *ER = cast<ElementRegion>(R);
|
|
|
|
if (!ER->getIndex().isZeroConstant())
|
|
|
|
return R;
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CXXBaseObjectRegionKind:
|
2012-08-14 06:11:34 +08:00
|
|
|
if (!StripBaseCasts)
|
|
|
|
return R;
|
2012-07-12 08:16:25 +08:00
|
|
|
R = cast<CXXBaseObjectRegion>(R)->getSuperRegion();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return R;
|
2009-07-30 02:14:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-01 14:17:29 +08:00
|
|
|
|
|
|
|
// FIXME: Merge with the implementation of the same method in Store.cpp
|
|
|
|
static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
|
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *D = RT->getDecl();
|
2010-02-11 09:04:33 +08:00
|
|
|
if (!D->getDefinition())
|
2009-08-01 14:17:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-02 12:56:14 +08:00
|
|
|
RegionRawOffset ElementRegion::getAsArrayOffset() const {
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits offset = CharUnits::Zero();
|
2009-08-01 14:17:29 +08:00
|
|
|
const ElementRegion *ER = this;
|
|
|
|
const MemRegion *superR = NULL;
|
|
|
|
ASTContext &C = getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Handle multi-dimensional arrays.
|
|
|
|
|
|
|
|
while (ER) {
|
|
|
|
superR = ER->getSuperRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: generalize to symbolic offsets.
|
|
|
|
SVal index = ER->getIndex();
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> CI = index.getAs<nonloc::ConcreteInt>()) {
|
2009-08-01 14:17:29 +08:00
|
|
|
// Update the offset.
|
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (i != 0) {
|
|
|
|
QualType elemType = ER->getElementType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// If we are pointing to an incomplete type, go no further.
|
|
|
|
if (!IsCompleteType(C, elemType)) {
|
|
|
|
superR = ER;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits size = C.getTypeSizeInChars(elemType);
|
2009-08-01 14:17:29 +08:00
|
|
|
offset += (i * size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next ElementRegion (if any).
|
|
|
|
ER = dyn_cast<ElementRegion>(superR);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
assert(superR && "super region cannot be NULL");
|
2011-01-24 09:55:39 +08:00
|
|
|
return RegionRawOffset(superR, offset);
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
RegionOffset MemRegion::getAsOffset() const {
|
|
|
|
const MemRegion *R = this;
|
2012-08-10 06:55:37 +08:00
|
|
|
const MemRegion *SymbolicOffsetBase = 0;
|
2010-08-03 14:34:25 +08:00
|
|
|
int64_t Offset = 0;
|
2010-08-03 12:52:05 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
switch (R->getKind()) {
|
2013-02-15 08:32:03 +08:00
|
|
|
case GenericMemSpaceRegionKind:
|
|
|
|
case StackLocalsSpaceRegionKind:
|
|
|
|
case StackArgumentsSpaceRegionKind:
|
|
|
|
case HeapSpaceRegionKind:
|
|
|
|
case UnknownSpaceRegionKind:
|
|
|
|
case StaticGlobalSpaceRegionKind:
|
|
|
|
case GlobalInternalSpaceRegionKind:
|
|
|
|
case GlobalSystemSpaceRegionKind:
|
|
|
|
case GlobalImmutableSpaceRegionKind:
|
|
|
|
// Stores can bind directly to a region space to set a default value.
|
|
|
|
assert(Offset == 0 && !SymbolicOffsetBase);
|
|
|
|
goto Finish;
|
|
|
|
|
|
|
|
case FunctionTextRegionKind:
|
|
|
|
case BlockTextRegionKind:
|
|
|
|
case BlockDataRegionKind:
|
|
|
|
// These will never have bindings, but may end up having values requested
|
|
|
|
// if the user does some strange casting.
|
|
|
|
if (Offset != 0)
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
goto Finish;
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
case SymbolicRegionKind:
|
|
|
|
case AllocaRegionKind:
|
|
|
|
case CompoundLiteralRegionKind:
|
|
|
|
case CXXThisRegionKind:
|
|
|
|
case StringRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
case ObjCStringRegionKind:
|
2010-08-03 12:52:05 +08:00
|
|
|
case VarRegionKind:
|
2010-11-26 16:52:48 +08:00
|
|
|
case CXXTempObjectRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
// Usual base regions.
|
2010-08-03 12:52:05 +08:00
|
|
|
goto Finish;
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2012-08-10 06:55:51 +08:00
|
|
|
case ObjCIvarRegionKind:
|
|
|
|
// This is a little strange, but it's a compromise between
|
|
|
|
// ObjCIvarRegions having unknown compile-time offsets (when using the
|
|
|
|
// non-fragile runtime) and yet still being distinct, non-overlapping
|
|
|
|
// regions. Thus we treat them as "like" base regions for the purposes
|
|
|
|
// of computing offsets.
|
|
|
|
goto Finish;
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
case CXXBaseObjectRegionKind: {
|
|
|
|
const CXXBaseObjectRegion *BOR = cast<CXXBaseObjectRegion>(R);
|
|
|
|
R = BOR->getSuperRegion();
|
|
|
|
|
|
|
|
QualType Ty;
|
|
|
|
if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) {
|
|
|
|
Ty = TVR->getDesugaredValueType(getContext());
|
|
|
|
} else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
|
|
|
|
// If our base region is symbolic, we don't know what type it really is.
|
|
|
|
// Pretend the type of the symbol is the true dynamic type.
|
|
|
|
// (This will at least be self-consistent for the life of the symbol.)
|
2012-09-26 14:00:14 +08:00
|
|
|
Ty = SR->getSymbol()->getType()->getPointeeType();
|
2012-08-09 02:23:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
|
2012-08-16 01:33:34 +08:00
|
|
|
if (!Child) {
|
2012-08-09 02:23:27 +08:00
|
|
|
// We cannot compute the offset of the base class.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
2012-08-09 02:23:27 +08:00
|
|
|
}
|
2012-08-10 06:55:37 +08:00
|
|
|
|
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
CharUnits BaseOffset;
|
2013-02-21 11:12:32 +08:00
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Child);
|
|
|
|
if (BOR->isVirtual())
|
|
|
|
BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
|
2012-08-09 02:23:27 +08:00
|
|
|
else
|
2013-02-21 11:12:32 +08:00
|
|
|
BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
|
2012-08-09 02:23:27 +08:00
|
|
|
|
|
|
|
// The base offset is in chars, not in bits.
|
|
|
|
Offset += BaseOffset.getQuantity() * getContext().getCharWidth();
|
|
|
|
break;
|
|
|
|
}
|
2010-08-03 12:52:05 +08:00
|
|
|
case ElementRegionKind: {
|
|
|
|
const ElementRegion *ER = cast<ElementRegion>(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
R = ER->getSuperRegion();
|
2010-08-03 12:52:05 +08:00
|
|
|
|
2012-08-10 06:55:37 +08:00
|
|
|
QualType EleTy = ER->getValueType();
|
|
|
|
if (!IsCompleteType(getContext(), EleTy)) {
|
|
|
|
// We cannot compute the offset of the base class.
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-03 12:52:05 +08:00
|
|
|
|
|
|
|
SVal Index = ER->getIndex();
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> CI =
|
2013-02-20 13:52:05 +08:00
|
|
|
Index.getAs<nonloc::ConcreteInt>()) {
|
2012-08-10 06:55:37 +08:00
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
2012-08-09 02:23:27 +08:00
|
|
|
// This type size is in bits.
|
|
|
|
Offset += i * getContext().getTypeSize(EleTy);
|
2010-08-03 12:52:05 +08:00
|
|
|
} else {
|
|
|
|
// We cannot compute offset for non-concrete index.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
2010-08-03 12:52:05 +08:00
|
|
|
}
|
2010-08-02 12:56:14 +08:00
|
|
|
break;
|
2010-08-03 12:52:05 +08:00
|
|
|
}
|
|
|
|
case FieldRegionKind: {
|
|
|
|
const FieldRegion *FR = cast<FieldRegion>(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
R = FR->getSuperRegion();
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
const RecordDecl *RD = FR->getDecl()->getParent();
|
2012-10-11 07:23:21 +08:00
|
|
|
if (RD->isUnion() || !RD->isCompleteDefinition()) {
|
2010-08-03 12:52:05 +08:00
|
|
|
// We cannot compute offset for incomplete type.
|
2012-10-11 07:23:21 +08:00
|
|
|
// For unions, we could treat everything as offset 0, but we'd rather
|
|
|
|
// treat each field as a symbolic offset so they aren't stored on top
|
|
|
|
// of each other, since we depend on things in typed regions actually
|
|
|
|
// matching their types.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
// Get the field number.
|
|
|
|
unsigned idx = 0;
|
|
|
|
for (RecordDecl::field_iterator FI = RD->field_begin(),
|
|
|
|
FE = RD->field_end(); FI != FE; ++FI, ++idx)
|
2012-06-07 04:45:41 +08:00
|
|
|
if (FR->getDecl() == *FI)
|
2010-08-03 12:52:05 +08:00
|
|
|
break;
|
2010-08-02 12:56:14 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
|
|
|
|
// This is offset in bits.
|
|
|
|
Offset += Layout.getFieldOffset(idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 12:56:14 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
Finish:
|
2012-08-10 06:55:37 +08:00
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
|
2010-08-03 12:52:05 +08:00
|
|
|
return RegionOffset(R, Offset);
|
2010-08-02 12:56:14 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BlockDataRegion
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-06 15:17:13 +08:00
|
|
|
std::pair<const VarRegion *, const VarRegion *>
|
|
|
|
BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
|
|
|
|
MemRegionManager &MemMgr = *getMemRegionManager();
|
|
|
|
const VarRegion *VR = 0;
|
|
|
|
const VarRegion *OriginalVR = 0;
|
|
|
|
|
|
|
|
if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage()) {
|
|
|
|
VR = MemMgr.getVarRegion(VD, this);
|
|
|
|
OriginalVR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (LC) {
|
|
|
|
VR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
OriginalVR = VR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
|
|
|
|
OriginalVR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(VR, OriginalVR);
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
void BlockDataRegion::LazyInitializeReferencedVars() {
|
|
|
|
if (ReferencedVars)
|
|
|
|
return;
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
|
|
|
|
AnalysisDeclContext::referenced_decls_iterator I, E;
|
2009-11-26 10:34:36 +08:00
|
|
|
llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
if (I == E) {
|
|
|
|
ReferencedVars = (void*) 0x1;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
MemRegionManager &MemMgr = *getMemRegionManager();
|
|
|
|
llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
|
|
|
|
BumpVectorContext BC(A);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
typedef BumpVector<const MemRegion*> VarVec;
|
|
|
|
VarVec *BV = (VarVec*) A.Allocate<VarVec>();
|
2009-12-02 06:12:34 +08:00
|
|
|
new (BV) VarVec(BC, E - I);
|
2012-05-05 05:48:42 +08:00
|
|
|
VarVec *BVOriginal = (VarVec*) A.Allocate<VarVec>();
|
|
|
|
new (BVOriginal) VarVec(BC, E - I);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
const VarRegion *VR = 0;
|
2012-05-05 05:48:42 +08:00
|
|
|
const VarRegion *OriginalVR = 0;
|
2012-12-06 15:17:13 +08:00
|
|
|
llvm::tie(VR, OriginalVR) = getCaptureRegions(*I);
|
2009-12-08 06:05:27 +08:00
|
|
|
assert(VR);
|
2012-05-05 05:48:42 +08:00
|
|
|
assert(OriginalVR);
|
2009-12-08 06:05:27 +08:00
|
|
|
BV->push_back(VR, BC);
|
2012-05-05 05:48:42 +08:00
|
|
|
BVOriginal->push_back(OriginalVR, BC);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
ReferencedVars = BV;
|
2012-05-05 05:48:42 +08:00
|
|
|
OriginalVars = BVOriginal;
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_begin() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *Vec =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
if (Vec == (void*) 0x1)
|
|
|
|
return BlockDataRegion::referenced_vars_iterator(0, 0);
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *VecOriginal =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
|
|
|
|
|
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
|
|
|
|
VecOriginal->begin());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_end() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *Vec =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
if (Vec == (void*) 0x1)
|
|
|
|
return BlockDataRegion::referenced_vars_iterator(0, 0);
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *VecOriginal =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
|
|
|
|
|
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec->end(),
|
|
|
|
VecOriginal->end());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
2013-02-06 06:00:19 +08:00
|
|
|
|
|
|
|
const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
|
|
|
|
for (referenced_vars_iterator I = referenced_vars_begin(),
|
|
|
|
E = referenced_vars_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I.getCapturedRegion() == R)
|
|
|
|
return I.getOriginalRegion();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|