]> granicus.if.org Git - clang/commitdiff
Instead of setting the default value of the array region, bind the rest of the
authorZhongxing Xu <xuzhongxing@gmail.com>
Tue, 23 Jun 2009 05:23:38 +0000 (05:23 +0000)
committerZhongxing Xu <xuzhongxing@gmail.com>
Tue, 23 Jun 2009 05:23:38 +0000 (05:23 +0000)
array elements to 0 explicitly. Create 0 values with the element type.

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

include/clang/Analysis/PathSensitive/ValueManager.h
lib/Analysis/RegionStore.cpp
lib/Analysis/SVals.cpp

index c28f0ea85511d1854c0629328a2ee4352027d6d5..e6279a3df9e7cced80298cdd65e754dbcc297616 100644 (file)
@@ -89,6 +89,8 @@ public:
   SVal getFunctionPointer(const FunctionDecl* FD);
   
   NonLoc makeNonLoc(SymbolRef sym);
+
+  NonLoc makeNonLoc(const llvm::APSInt& V);
   
   NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
                     const llvm::APSInt& rhs, QualType T);
index 564ffec6cf90332a932bb1d9a999f7302d980ff9..ff2145e26e0ef5701a021f49ff4b85fe15f3845b 100644 (file)
@@ -1079,17 +1079,11 @@ const GRState *RegionStoreManager::BindArray(const GRState *state,
                                              SVal Init) {
 
   QualType T = R->getValueType(getContext());
-  assert(T->isArrayType());
-
-  // When we are binding the whole array, it always has default value 0.
-  state = state->set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(),
-                                                               0, false));
-
   ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
+  QualType ElementTy = CAT->getElementType();
 
   llvm::APSInt Size(CAT->getSize(), false);
-  llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
-                                           Size.isUnsigned());
+  llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false);
 
   // Check if the init expr is a StringLiteral.
   if (isa<loc::MemRegionVal>(Init)) {
@@ -1106,10 +1100,8 @@ const GRState *RegionStoreManager::BindArray(const GRState *state,
       if (j >= len)
         break;
 
-      SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
-      ElementRegion* ER =
-        MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
-                               Idx, R, getContext());
+      SVal Idx = ValMgr.makeNonLoc(i);
+      ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
 
       SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
       state = Bind(state, loc::MemRegionVal(ER), V);
@@ -1122,14 +1114,12 @@ const GRState *RegionStoreManager::BindArray(const GRState *state,
   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
 
   for (; i < Size; ++i, ++VI) {
-    // The init list might be shorter than the array decl.
+    // The init list might be shorter than the array length.
     if (VI == VE)
       break;
 
-    SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
-    ElementRegion* ER =
-      MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
-                             Idx, R, getContext());
+    SVal Idx = ValMgr.makeNonLoc(i);
+    ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
 
     if (CAT->getElementType()->isStructureType())
       state = BindStruct(state, ER, *VI);
@@ -1137,6 +1127,18 @@ const GRState *RegionStoreManager::BindArray(const GRState *state,
       state = Bind(state, Loc::MakeVal(ER), *VI);
   }
 
+  // If the init list is shorter than the array length, bind the rest elements
+  // to 0.
+  if (ElementTy->isIntegerType()) {
+    while (i < Size) {
+      SVal Idx = ValMgr.makeNonLoc(i);
+      ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
+      SVal V = ValMgr.makeZeroVal(ElementTy);
+      state = Bind(state, Loc::MakeVal(ER), V);
+      ++i;
+    }
+  }
+
   return state;
 }
 
index 77c3c8f7722a21233efa8dc47e1736da09e8e703..e33275199ade85db448346af6ab0cd06f3623878 100644 (file)
@@ -264,6 +264,10 @@ NonLoc ValueManager::makeNonLoc(SymbolRef sym) {
   return nonloc::SymbolVal(sym);
 }
 
+NonLoc ValueManager::makeNonLoc(const APSInt& V) {
+  return nonloc::ConcreteInt(BasicVals.getValue(V));
+}
+
 NonLoc ValueManager::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
                                 const APSInt& v, QualType T) {
   // The Environment ensures we always get a persistent APSInt in