Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1 | //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a basic region store model. In this model, we do have field |
| 11 | // sensitivity. But we assume nothing about the heap shape. So recursive data |
| 12 | // structures are largely ignored. Basically we do 1-limiting analysis. |
| 13 | // Parameter pointers are assumed with no aliasing. Pointee objects of |
| 14 | // parameters are created lazily. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
| 19 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 20 | |
| 21 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | ca892b8 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 27 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 32 | RegionBindingsTy::Factory RBFactory; |
| 33 | GRStateManager& StateMgr; |
| 34 | MemRegionManager MRMgr; |
| 35 | |
| 36 | public: |
| 37 | RegionStoreManager(GRStateManager& mgr) |
| 38 | : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {} |
| 39 | |
| 40 | virtual ~RegionStoreManager() {} |
| 41 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 42 | MemRegionManager& getRegionManager() { return MRMgr; } |
| 43 | |
| 44 | // FIXME: Is this function necessary? |
| 45 | SVal GetRegionSVal(Store St, const MemRegion* R) { |
| 46 | return Retrieve(St, loc::MemRegionVal(R)); |
| 47 | } |
Ted Kremenek | d83daa5 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 48 | |
| 49 | Store BindCompoundLiteral(Store store, const CompoundLiteralRegion* R, |
| 50 | const SVal* BegInit, const SVal* EndInit) { |
| 51 | |
| 52 | // FIXME: Let's discuss how we want to do the mapping in RegionStore |
| 53 | // from CompoundLiteralRegion to values. |
| 54 | assert (false && "Not yet implemented."); |
| 55 | return store; |
| 56 | } |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 57 | |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 58 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 59 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 60 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 61 | |
| 62 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 63 | |
| 64 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 65 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 66 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 67 | |
| 68 | SVal ArrayToPointer(SVal Array); |
| 69 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 70 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 71 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 72 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 73 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 74 | Store Remove(Store store, Loc LV) { |
| 75 | // FIXME: Implement. |
| 76 | return store; |
| 77 | } |
| 78 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 79 | Store getInitialStore(); |
Ted Kremenek | 73a36c9 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 80 | |
| 81 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 82 | /// 'this' object (C++). When used when analyzing a normal function this |
| 83 | /// method returns NULL. |
| 84 | const MemRegion* getSelfRegion(Store) { |
| 85 | assert (false && "Not implemented."); |
| 86 | return 0; |
| 87 | } |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 88 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 89 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 90 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 91 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 92 | // FIXME: Implement this. |
| 93 | return store; |
| 94 | } |
| 95 | |
Zhongxing Xu | f05e4ed | 2008-10-29 02:34:02 +0000 | [diff] [blame] | 96 | Store BindDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal, |
| 97 | unsigned Count); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 98 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 99 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 100 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 101 | } |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 102 | |
Zhongxing Xu | 6149e88 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 103 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 104 | |
| 105 | void iterBindings(Store store, BindingsHandler& f) { |
| 106 | // FIXME: Implement. |
| 107 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | Loc getVarLoc(const VarDecl* VD) { |
| 111 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 112 | } |
| 113 | |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame^] | 114 | Store InitializeArray(Store store, TypedRegion* R, SVal Init); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 115 | Store InitializeArrayToUndefined(Store store, QualType T, MemRegion* BaseR); |
| 116 | Store InitializeStructToUndefined(Store store, QualType T, MemRegion* BaseR); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 117 | |
| 118 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 119 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 120 | // Utility methods. |
| 121 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 122 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // end anonymous namespace |
| 126 | |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 127 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 128 | return new RegionStoreManager(StMgr); |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 131 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 132 | const StringLiteral* S) { |
| 133 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 134 | } |
| 135 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 136 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 137 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 138 | } |
| 139 | |
| 140 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 141 | SVal Base) { |
| 142 | return UnknownVal(); |
| 143 | } |
| 144 | |
| 145 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 146 | const FieldDecl* D) { |
| 147 | if (Base.isUnknownOrUndef()) |
| 148 | return Base; |
| 149 | |
| 150 | Loc BaseL = cast<Loc>(Base); |
| 151 | const MemRegion* BaseR = 0; |
| 152 | |
| 153 | switch (BaseL.getSubKind()) { |
| 154 | case loc::MemRegionKind: |
| 155 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 156 | break; |
| 157 | |
| 158 | case loc::SymbolValKind: |
| 159 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 160 | break; |
| 161 | |
| 162 | case loc::GotoLabelKind: |
| 163 | case loc::FuncValKind: |
| 164 | // These are anormal cases. Flag an undefined value. |
| 165 | return UndefinedVal(); |
| 166 | |
| 167 | case loc::ConcreteIntKind: |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 168 | // While these seem funny, this can happen through casts. |
| 169 | // FIXME: What we should return is the field offset. For example, |
| 170 | // add the field offset to the integer value. That way funny things |
| 171 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 172 | return Base; |
| 173 | |
| 174 | default: |
| 175 | assert("Unhandled Base."); |
| 176 | return Base; |
| 177 | } |
| 178 | |
| 179 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 180 | } |
| 181 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 182 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 183 | SVal Base, SVal Offset) { |
| 184 | if (Base.isUnknownOrUndef()) |
| 185 | return Base; |
| 186 | |
Zhongxing Xu | 13a05fa | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 187 | if (isa<loc::SymbolVal>(Base)) |
| 188 | return Base; |
| 189 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 190 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 191 | |
| 192 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 193 | |
| 194 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 195 | |
| 196 | SVal Idx = ElemR->getIndex(); |
| 197 | |
| 198 | nonloc::ConcreteInt *CI1, *CI2; |
| 199 | |
| 200 | // Only handle integer indices for now. |
| 201 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 202 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
| 203 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 204 | *CI2); |
| 205 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 206 | ElemR->getSuperRegion())); |
| 207 | } |
| 208 | |
| 209 | return UnknownVal(); |
| 210 | } |
| 211 | |
| 212 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 213 | |
| 214 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 215 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 216 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 217 | |
Zhongxing Xu | 5ac8bf1 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 218 | // FIXME: Find a better way to get bit width. |
| 219 | nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false)); |
| 220 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 221 | |
| 222 | return loc::MemRegionVal(ER); |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 225 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 226 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 227 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 228 | |
| 229 | switch (L.getSubKind()) { |
| 230 | case loc::MemRegionKind: { |
| 231 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 232 | assert(R && "bad region"); |
| 233 | |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 234 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 235 | if (TR->getType(getContext())->isStructureType()) |
| 236 | return RetrieveStruct(S, TR); |
| 237 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 238 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 239 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 240 | return V ? *V : UnknownVal(); |
| 241 | } |
| 242 | |
| 243 | case loc::SymbolValKind: |
| 244 | return UnknownVal(); |
| 245 | |
| 246 | case loc::ConcreteIntKind: |
| 247 | return UndefinedVal(); // As in BasicStoreManager. |
| 248 | |
| 249 | case loc::FuncValKind: |
| 250 | return L; |
| 251 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 252 | default: |
| 253 | assert(false && "Invalid Location"); |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 258 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 259 | QualType T = R->getType(getContext()); |
| 260 | assert(T->isStructureType()); |
| 261 | |
| 262 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 263 | RecordDecl* RD = RT->getDecl(); |
| 264 | assert(RD->isDefinition()); |
| 265 | |
| 266 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 267 | |
| 268 | for (int i = RD->getNumMembers() - 1; i >= 0; --i) { |
| 269 | FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R); |
| 270 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 271 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 272 | |
| 273 | SVal FieldValue = data ? *data : UnknownVal(); |
| 274 | |
| 275 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 276 | } |
| 277 | |
| 278 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 279 | } |
| 280 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 281 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | e7c8a13 | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 282 | if (LV.getSubKind() == loc::SymbolValKind) |
| 283 | return store; |
| 284 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 285 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 287 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 288 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 289 | assert(R); |
| 290 | |
| 291 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 292 | if (TR->getType(getContext())->isStructureType()) |
| 293 | return BindStruct(store, TR, V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 294 | |
| 295 | RegionBindingsTy B = GetRegionBindings(store); |
| 296 | return V.isUnknown() |
| 297 | ? RBFactory.Remove(B, R).getRoot() |
| 298 | : RBFactory.Add(B, R, V).getRoot(); |
| 299 | } |
| 300 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 301 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 302 | QualType T = R->getType(getContext()); |
| 303 | assert(T->isStructureType()); |
| 304 | |
| 305 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 306 | RecordDecl* RD = RT->getDecl(); |
| 307 | assert(RD->isDefinition()); |
| 308 | |
| 309 | RegionBindingsTy B = GetRegionBindings(store); |
| 310 | |
| 311 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
| 312 | |
| 313 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 314 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 315 | |
| 316 | for (; FI != FE; ++FI, ++VI) { |
| 317 | assert(VI != VE); |
| 318 | |
| 319 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 320 | |
| 321 | B = RBFactory.Add(B, FR, *VI); |
| 322 | } |
| 323 | |
| 324 | return B.getRoot(); |
| 325 | } |
| 326 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 327 | Store RegionStoreManager::getInitialStore() { |
| 328 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 329 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 330 | |
| 331 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 332 | |
| 333 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 334 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 336 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 337 | // Punt on static variables for now. |
| 338 | if (VD->getStorageClass() == VarDecl::Static) |
| 339 | continue; |
| 340 | |
| 341 | QualType T = VD->getType(); |
| 342 | // Only handle pointers and integers for now. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 343 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 344 | // Initialize globals and parameters to symbolic values. |
| 345 | // Initialize local variables to undefined. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 346 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 347 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 348 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 349 | : UndefinedVal(); |
| 350 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 351 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | return St; |
| 356 | } |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 357 | |
Zhongxing Xu | f05e4ed | 2008-10-29 02:34:02 +0000 | [diff] [blame] | 358 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex, |
| 359 | SVal InitVal, unsigned Count) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 360 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 361 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 362 | |
| 363 | if (VD->hasGlobalStorage()) { |
| 364 | // Static global variables should not be visited here. |
| 365 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 366 | VD->isFileVarDecl())); |
| 367 | // Process static variables. |
| 368 | if (VD->getStorageClass() == VarDecl::Static) { |
| 369 | if (!Ex) { |
| 370 | // Only handle pointer and integer static variables. |
| 371 | |
| 372 | QualType T = VD->getType(); |
| 373 | |
| 374 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 375 | store = Bind(store, getVarLoc(VD), |
| 376 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 377 | |
| 378 | else if (T->isIntegerType()) |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 379 | store = Bind(store, getVarLoc(VD), |
| 380 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame^] | 381 | |
| 382 | // Other types of static local variables are not handled yet. |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 383 | } else { |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 384 | store = Bind(store, getVarLoc(VD), InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } else { |
| 388 | // Process local variables. |
| 389 | |
| 390 | QualType T = VD->getType(); |
| 391 | |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 392 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 393 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 394 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 395 | SVal V = Ex ? InitVal : UndefinedVal(); |
| 396 | if (Ex && InitVal.isUnknown()) { |
| 397 | // "Conjured" symbols. |
| 398 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 399 | V = Loc::IsLocType(Ex->getType()) |
| 400 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 401 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
| 402 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 403 | store = Bind(store, loc::MemRegionVal(VR), V); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 404 | |
| 405 | } else if (T->isArrayType()) { |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame^] | 406 | if (!Ex) |
| 407 | store = InitializeArrayToUndefined(store, T, VR); |
| 408 | else |
| 409 | store = InitializeArray(store, VR, InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 410 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 411 | } else if (T->isStructureType()) { |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 412 | store = InitializeStructToUndefined(store, T, VR); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 413 | } |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame^] | 414 | |
| 415 | // Other types of local variables are not handled yet. |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 416 | } |
| 417 | return store; |
| 418 | } |
| 419 | |
Zhongxing Xu | ca892b8 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 420 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 421 | const char* nl, const char *sep) { |
| 422 | llvm::raw_os_ostream OS(Out); |
| 423 | RegionBindingsTy B = GetRegionBindings(store); |
| 424 | OS << "Store:" << nl; |
| 425 | |
| 426 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 427 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 428 | I.getData().print(OS); OS << nl; |
| 429 | } |
Zhongxing Xu | 6149e88 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 430 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 431 | |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame^] | 432 | Store RegionStoreManager::InitializeArray(Store store, TypedRegion* R, |
| 433 | SVal Init) { |
| 434 | QualType T = R->getType(getContext()); |
| 435 | assert(T->isArrayType()); |
| 436 | |
| 437 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 438 | |
| 439 | llvm::APInt Size = CAT->getSize(); |
| 440 | |
| 441 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 442 | |
| 443 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 444 | |
| 445 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 446 | |
| 447 | for (; i != Size; ++i) { |
| 448 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 449 | |
| 450 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 451 | |
| 452 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 453 | // The init list might be shorter than the array decl. |
| 454 | if (VI != VE) ++VI; |
| 455 | } |
| 456 | |
| 457 | return store; |
| 458 | } |
| 459 | |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 460 | Store RegionStoreManager::InitializeArrayToUndefined(Store store, QualType T, |
| 461 | MemRegion* BaseR) { |
| 462 | assert(T->isArrayType()); |
| 463 | |
| 464 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 465 | |
| 466 | // Only handle constant size array for now. |
| 467 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 468 | |
| 469 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame^] | 470 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 471 | for (; i != Size; ++i) { |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 472 | nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i))); |
| 473 | |
| 474 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 475 | |
| 476 | store = Bind(store, loc::MemRegionVal(ER), UndefinedVal()); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return store; |
| 481 | } |
| 482 | |
| 483 | Store RegionStoreManager::InitializeStructToUndefined(Store store, QualType T, |
| 484 | MemRegion* BaseR) { |
Zhongxing Xu | d766346 | 2008-10-27 13:35:03 +0000 | [diff] [blame] | 485 | QualType CT = StateMgr.getContext().getCanonicalType(T); |
Zhongxing Xu | e7c8a13 | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 486 | const RecordType* RT = cast<RecordType>(CT.getTypePtr()); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 487 | RecordDecl* RD = RT->getDecl(); |
| 488 | assert(RD->isDefinition()); |
| 489 | |
| 490 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 491 | I != E; ++I) { |
| 492 | |
| 493 | QualType FTy = (*I)->getType(); |
| 494 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 495 | |
| 496 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 497 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 498 | |
| 499 | } else if (FTy->isArrayType()) { |
| 500 | store = InitializeArrayToUndefined(store, FTy, FR); |
| 501 | |
| 502 | } else if (FTy->isStructureType()) { |
| 503 | store = InitializeStructToUndefined(store, FTy, FR); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return store; |
| 508 | } |