From 143b2fc6fd3945c250b333383749010c2c8e3a4c Mon Sep 17 00:00:00 2001 From: Zhongxing Xu Date: Tue, 16 Jun 2009 09:55:50 +0000 Subject: [PATCH] Use canonical type for building ElementRegion. Otherwise ElementRegions cannot be unique. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73482 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../clang/Analysis/PathSensitive/MemRegion.h | 2 +- lib/Analysis/BasicStore.cpp | 2 +- lib/Analysis/MemRegion.cpp | 8 ++++--- lib/Analysis/RegionStore.cpp | 23 +++++++++++-------- lib/Analysis/Store.cpp | 3 ++- test/Analysis/elementtype.c | 13 +++++++++++ 6 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 test/Analysis/elementtype.c diff --git a/include/clang/Analysis/PathSensitive/MemRegion.h b/include/clang/Analysis/PathSensitive/MemRegion.h index 0e8da2aee3..8afcc4c241 100644 --- a/include/clang/Analysis/PathSensitive/MemRegion.h +++ b/include/clang/Analysis/PathSensitive/MemRegion.h @@ -620,7 +620,7 @@ public: /// getElementRegion - Retrieve the memory region associated with the /// associated element type, index, and super region. ElementRegion* getElementRegion(QualType elementType, SVal Idx, - const MemRegion* superRegion); + const MemRegion* superRegion,ASTContext &Ctx); /// getFieldRegion - Retrieve or create the memory region associated with /// a specified FieldDecl. 'superRegion' corresponds to the containing diff --git a/lib/Analysis/BasicStore.cpp b/lib/Analysis/BasicStore.cpp index 2dd46c3853..ba4c021475 100644 --- a/lib/Analysis/BasicStore.cpp +++ b/lib/Analysis/BasicStore.cpp @@ -248,7 +248,7 @@ SVal BasicStoreManager::getLValueElement(const GRState* St, if (BaseR) return Loc::MakeVal(MRMgr.getElementRegion(elementType, UnknownVal(), - BaseR)); + BaseR, getContext())); else return UnknownVal(); } diff --git a/lib/Analysis/MemRegion.cpp b/lib/Analysis/MemRegion.cpp index 9f066f4469..9e11a26353 100644 --- a/lib/Analysis/MemRegion.cpp +++ b/lib/Analysis/MemRegion.cpp @@ -296,10 +296,12 @@ MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) { ElementRegion* MemRegionManager::getElementRegion(QualType elementType, SVal Idx, - const MemRegion* superRegion){ + const MemRegion* superRegion, ASTContext& Ctx){ + + QualType T = Ctx.getCanonicalType(elementType); llvm::FoldingSetNodeID ID; - ElementRegion::ProfileRegion(ID, elementType, Idx, superRegion); + ElementRegion::ProfileRegion(ID, T, Idx, superRegion); void* InsertPos; MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); @@ -307,7 +309,7 @@ MemRegionManager::getElementRegion(QualType elementType, SVal Idx, if (!R) { R = (ElementRegion*) A.Allocate(); - new (R) ElementRegion(elementType, Idx, superRegion); + new (R) ElementRegion(T, Idx, superRegion); Regions.InsertNode(R, InsertPos); } diff --git a/lib/Analysis/RegionStore.cpp b/lib/Analysis/RegionStore.cpp index eae3aefe21..d20c70a64d 100644 --- a/lib/Analysis/RegionStore.cpp +++ b/lib/Analysis/RegionStore.cpp @@ -438,7 +438,7 @@ SVal RegionStoreManager::getLValueElement(const GRState* St, } } return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset, - BaseRegion)); + BaseRegion, getContext())); } SVal BaseIdx = ElemR->getIndex(); @@ -473,7 +473,8 @@ SVal RegionStoreManager::getLValueElement(const GRState* St, else NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI)); - return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR)); + return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR, + getContext())); } SVal RegionStoreManager::getSizeInElements(const GRState* St, @@ -560,7 +561,7 @@ SVal RegionStoreManager::ArrayToPointer(Loc Array) { T = AT->getElementType(); nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); - ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR); + ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext()); return loc::MemRegionVal(ER); } @@ -622,7 +623,7 @@ RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R, state = setCastType(state, R, ToTy); SVal Idx = ValMgr.makeZeroArrayIndex(); - ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx, R); + ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R,getContext()); return CastResult(state, ER); } else return CastResult(state, R); @@ -654,7 +655,7 @@ SVal RegionStoreManager::EvalBinOp(const GRState *state, QualType EleTy = T->getAsPointerType()->getPointeeType(); SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); - ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR); + ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext()); } else if (const AllocaRegion *AR = dyn_cast(MR)) { // Get the alloca region's current cast type. @@ -664,7 +665,7 @@ SVal RegionStoreManager::EvalBinOp(const GRState *state, assert(T && "alloca region has no type."); QualType EleTy = cast(T->getTypePtr())->getPointeeType(); SVal ZeroIdx = ValMgr.makeZeroArrayIndex(); - ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR); + ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext()); } else ER = cast(MR); @@ -686,7 +687,8 @@ SVal RegionStoreManager::EvalBinOp(const GRState *state, Offset->getValue())); SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted); const MemRegion* NewER = - MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion()); + MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(), + getContext()); return Loc::MakeVal(NewER); } @@ -871,7 +873,8 @@ SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){ for (; i < Size; ++i) { SVal Idx = NonLoc::MakeVal(getBasicVals(), i); - ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R); + ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R, + getContext()); QualType ETy = ER->getElementType(); SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy); ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal); @@ -1153,7 +1156,7 @@ const GRState* RegionStoreManager::BindArray(const GRState* St, SVal Idx = NonLoc::MakeVal(getBasicVals(), i); ElementRegion* ER = MRMgr.getElementRegion(cast(T)->getElementType(), - Idx, R); + Idx, R, getContext()); SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true); St = Bind(St, loc::MemRegionVal(ER), V); @@ -1173,7 +1176,7 @@ const GRState* RegionStoreManager::BindArray(const GRState* St, SVal Idx = NonLoc::MakeVal(getBasicVals(), i); ElementRegion* ER = MRMgr.getElementRegion(cast(T)->getElementType(), - Idx, R); + Idx, R, getContext()); if (CAT->getElementType()->isStructureType()) St = BindStruct(St, ER, *VI); diff --git a/lib/Analysis/Store.cpp b/lib/Analysis/Store.cpp index 13326ab31f..5aa756e14b 100644 --- a/lib/Analysis/Store.cpp +++ b/lib/Analysis/Store.cpp @@ -90,7 +90,8 @@ StoreManager::CastRegion(const GRState* state, const MemRegion* R, // FIXME: Is this the right thing to do in all cases? const TypedRegion *Base = isa(TR) ? cast(TR->getSuperRegion()) : TR; - ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base); + ElementRegion* ER = MRMgr.getElementRegion(Pointee, Idx, Base, + StateMgr.getContext()); return CastResult(state, ER); } } diff --git a/test/Analysis/elementtype.c b/test/Analysis/elementtype.c new file mode 100644 index 0000000000..10328e69ef --- /dev/null +++ b/test/Analysis/elementtype.c @@ -0,0 +1,13 @@ +// RUN: clang-cc -analyze -checker-simple -analyzer-store=region %s + +typedef struct added_obj_st { + int type; +} ADDED_OBJ; + +// Test if we are using the canonical type for ElementRegion. +void f() { + ADDED_OBJ *ao[4]={((void*)0),((void*)0),((void*)0),((void*)0)}; + if (ao[0] != ((void*)0)) { + ao[0]->type=0; + } +} -- 2.40.0