]> granicus.if.org Git - clang/commitdiff
Use canonical type for building ElementRegion. Otherwise ElementRegions cannot
authorZhongxing Xu <xuzhongxing@gmail.com>
Tue, 16 Jun 2009 09:55:50 +0000 (09:55 +0000)
committerZhongxing Xu <xuzhongxing@gmail.com>
Tue, 16 Jun 2009 09:55:50 +0000 (09:55 +0000)
be unique.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73482 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Analysis/PathSensitive/MemRegion.h
lib/Analysis/BasicStore.cpp
lib/Analysis/MemRegion.cpp
lib/Analysis/RegionStore.cpp
lib/Analysis/Store.cpp
test/Analysis/elementtype.c [new file with mode: 0644]

index 0e8da2aee31826f0ebd13707517449b99c7745f9..8afcc4c2414fe2cf9d957d845f04494bd31e739e 100644 (file)
@@ -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
index 2dd46c385343dbf5c666cf82288667e6e71c9f2e..ba4c02147536cf31349ff7e4a0341d9d09a38d2e 100644 (file)
@@ -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();
 }
index 9f066f44692fb010e3844b254484009a8c8c9fe5..9e11a263536de3024d3476ad9d13170e49d2e9b0 100644 (file)
@@ -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<ElementRegion>();
-    new (R) ElementRegion(elementType, Idx, superRegion);
+    new (R) ElementRegion(T, Idx, superRegion);
     Regions.InsertNode(R, InsertPos);
   }
 
index eae3aefe218fafd92fb4cc9b96a4b279b62b52c1..d20c70a64d5c1e9e7063b89cb44b3f115d7dcfe9 100644 (file)
@@ -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<AllocaRegion>(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<PointerType>(T->getTypePtr())->getPointeeType();
     SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
-    ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR);
+    ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
   } 
   else
     ER = cast<ElementRegion>(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<ArrayType>(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<ArrayType>(T)->getElementType(),
-                             Idx, R);
+                             Idx, R, getContext());
 
     if (CAT->getElementType()->isStructureType())
       St = BindStruct(St, ER, *VI);
index 13326ab31f8230a867460cb8ef01300247014272..5aa756e14be375bed8cbc66c040c02a28239ad0d 100644 (file)
@@ -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<ElementRegion>(TR) ?
                                   cast<TypedRegion>(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 (file)
index 0000000..10328e6
--- /dev/null
@@ -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;
+  }
+}