StoreManager::NewCastRegion:

- Refactor logic that creates ElementRegions into a help method 'MakeElementRegion'.
- Fix crash due to not handling StringRegions.  Casts of StringRegions now
  result in a new ElementRegion layered on the original StringRegion.

llvm-svn: 74867
This commit is contained in:
Ted Kremenek 2009-07-06 22:23:45 +00:00
parent e63b0e6f79
commit c5ab3a0eab
2 changed files with 31 additions and 10 deletions

View File

@ -22,6 +22,19 @@ StoreManager::StoreManager(GRStateManager &stateMgr, bool useNewCastRegion)
UseNewCastRegion(useNewCastRegion),
MRMgr(ValMgr.getRegionManager()) {}
StoreManager::CastResult
StoreManager::MakeElementRegion(const GRState *state, const MemRegion *region,
QualType pointeeTy, QualType castToTy) {
// Record the cast type of the region.
state = setCastType(state, region, castToTy);
// Create a new ElementRegion at offset 0.
SVal idx = ValMgr.makeZeroArrayIndex();
return CastResult(state, MRMgr.getElementRegion(pointeeTy, idx, region,
ValMgr.getContext()));
}
StoreManager::CastResult
StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
QualType CastToTy) {
@ -51,6 +64,10 @@ StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
// Process region cast according to the kind of the region being cast.
// Handle casts of string literals.
if (isa<StringRegion>(R))
return MakeElementRegion(state, R, PointeeTy, ToTy);
// FIXME: Need to handle arbitrary downcasts.
if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
state = setCastType(state, R, ToTy);
@ -78,12 +95,7 @@ StoreManager::NewCastRegion(const GRState *state, const MemRegion* R,
if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
(ObjTy->isAggregateType() && PointeeTy->isScalarType()) ||
ObjTySize == 0 /* R has 'void*' type. */) {
// Record the cast type of the region.
state = setCastType(state, R, ToTy);
SVal Idx = ValMgr.makeZeroArrayIndex();
ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R, Ctx);
return CastResult(state, ER);
return MakeElementRegion(state, R, PointeeTy, ToTy);
} else {
state = setCastType(state, R, ToTy);
return CastResult(state, R);

View File

@ -297,4 +297,13 @@ void rdar_7027684(int x, int y) {
(rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);
}
// Test that we handle casts of string literals to arbitrary types.
unsigned const char *string_literal_test1() {
return (const unsigned char*) "hello";
}
const float *string_literal_test2() {
return (const float*) "hello";
}