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 | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 115 | Store InitializeArrayToUndefined(Store store, TypedRegion* BaseR); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 116 | Store InitializeStruct(Store store, TypedRegion* R, SVal Init); |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 117 | Store InitializeStructToUndefined(Store store, TypedRegion* BaseR); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 118 | |
| 119 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 120 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 121 | // Utility methods. |
| 122 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 123 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | } // end anonymous namespace |
| 127 | |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 128 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 129 | return new RegionStoreManager(StMgr); |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 132 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 133 | const StringLiteral* S) { |
| 134 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 135 | } |
| 136 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 137 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 138 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 139 | } |
| 140 | |
| 141 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 142 | SVal Base) { |
| 143 | return UnknownVal(); |
| 144 | } |
| 145 | |
| 146 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 147 | const FieldDecl* D) { |
| 148 | if (Base.isUnknownOrUndef()) |
| 149 | return Base; |
| 150 | |
| 151 | Loc BaseL = cast<Loc>(Base); |
| 152 | const MemRegion* BaseR = 0; |
| 153 | |
| 154 | switch (BaseL.getSubKind()) { |
| 155 | case loc::MemRegionKind: |
| 156 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 157 | break; |
| 158 | |
| 159 | case loc::SymbolValKind: |
| 160 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 161 | break; |
| 162 | |
| 163 | case loc::GotoLabelKind: |
| 164 | case loc::FuncValKind: |
| 165 | // These are anormal cases. Flag an undefined value. |
| 166 | return UndefinedVal(); |
| 167 | |
| 168 | case loc::ConcreteIntKind: |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 169 | // While these seem funny, this can happen through casts. |
| 170 | // FIXME: What we should return is the field offset. For example, |
| 171 | // add the field offset to the integer value. That way funny things |
| 172 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 173 | return Base; |
| 174 | |
| 175 | default: |
| 176 | assert("Unhandled Base."); |
| 177 | return Base; |
| 178 | } |
| 179 | |
| 180 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 181 | } |
| 182 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 183 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 184 | SVal Base, SVal Offset) { |
| 185 | if (Base.isUnknownOrUndef()) |
| 186 | return Base; |
| 187 | |
Zhongxing Xu | 13a05fa | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 188 | if (isa<loc::SymbolVal>(Base)) |
| 189 | return Base; |
| 190 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 191 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 192 | |
| 193 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 194 | |
| 195 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 196 | |
| 197 | SVal Idx = ElemR->getIndex(); |
| 198 | |
| 199 | nonloc::ConcreteInt *CI1, *CI2; |
| 200 | |
| 201 | // Only handle integer indices for now. |
| 202 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 203 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
| 204 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 205 | *CI2); |
| 206 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 207 | ElemR->getSuperRegion())); |
| 208 | } |
| 209 | |
| 210 | return UnknownVal(); |
| 211 | } |
| 212 | |
| 213 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 214 | |
| 215 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 216 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 217 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 218 | |
Zhongxing Xu | 5ac8bf1 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 219 | // FIXME: Find a better way to get bit width. |
| 220 | nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false)); |
| 221 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 222 | |
| 223 | return loc::MemRegionVal(ER); |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 226 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 227 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 228 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 229 | |
| 230 | switch (L.getSubKind()) { |
| 231 | case loc::MemRegionKind: { |
| 232 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 233 | assert(R && "bad region"); |
| 234 | |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 235 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 236 | if (TR->getType(getContext())->isStructureType()) |
| 237 | return RetrieveStruct(S, TR); |
| 238 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 239 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 240 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 241 | return V ? *V : UnknownVal(); |
| 242 | } |
| 243 | |
| 244 | case loc::SymbolValKind: |
| 245 | return UnknownVal(); |
| 246 | |
| 247 | case loc::ConcreteIntKind: |
| 248 | return UndefinedVal(); // As in BasicStoreManager. |
| 249 | |
| 250 | case loc::FuncValKind: |
| 251 | return L; |
| 252 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 253 | default: |
| 254 | assert(false && "Invalid Location"); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 259 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 260 | QualType T = R->getType(getContext()); |
| 261 | assert(T->isStructureType()); |
| 262 | |
| 263 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 264 | RecordDecl* RD = RT->getDecl(); |
| 265 | assert(RD->isDefinition()); |
| 266 | |
| 267 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 268 | |
| 269 | for (int i = RD->getNumMembers() - 1; i >= 0; --i) { |
| 270 | FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R); |
| 271 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 272 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 273 | |
| 274 | SVal FieldValue = data ? *data : UnknownVal(); |
| 275 | |
| 276 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 277 | } |
| 278 | |
| 279 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 280 | } |
| 281 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 282 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | e7c8a13 | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 283 | if (LV.getSubKind() == loc::SymbolValKind) |
| 284 | return store; |
| 285 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 286 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 288 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 289 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 290 | assert(R); |
| 291 | |
| 292 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 293 | if (TR->getType(getContext())->isStructureType()) |
| 294 | return BindStruct(store, TR, V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 295 | |
| 296 | RegionBindingsTy B = GetRegionBindings(store); |
| 297 | return V.isUnknown() |
| 298 | ? RBFactory.Remove(B, R).getRoot() |
| 299 | : RBFactory.Add(B, R, V).getRoot(); |
| 300 | } |
| 301 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 302 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 303 | QualType T = R->getType(getContext()); |
| 304 | assert(T->isStructureType()); |
| 305 | |
| 306 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 307 | RecordDecl* RD = RT->getDecl(); |
| 308 | assert(RD->isDefinition()); |
| 309 | |
| 310 | RegionBindingsTy B = GetRegionBindings(store); |
| 311 | |
| 312 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
| 313 | |
| 314 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 315 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 316 | |
| 317 | for (; FI != FE; ++FI, ++VI) { |
| 318 | assert(VI != VE); |
| 319 | |
| 320 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 321 | |
| 322 | B = RBFactory.Add(B, FR, *VI); |
| 323 | } |
| 324 | |
| 325 | return B.getRoot(); |
| 326 | } |
| 327 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 328 | Store RegionStoreManager::getInitialStore() { |
| 329 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 330 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 331 | |
| 332 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 333 | |
| 334 | 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] | 335 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 336 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 337 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 338 | // Punt on static variables for now. |
| 339 | if (VD->getStorageClass() == VarDecl::Static) |
| 340 | continue; |
| 341 | |
| 342 | QualType T = VD->getType(); |
| 343 | // Only handle pointers and integers for now. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 344 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 345 | // Initialize globals and parameters to symbolic values. |
| 346 | // Initialize local variables to undefined. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 347 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 348 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 349 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 350 | : UndefinedVal(); |
| 351 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 352 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | return St; |
| 357 | } |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 358 | |
Zhongxing Xu | f05e4ed | 2008-10-29 02:34:02 +0000 | [diff] [blame] | 359 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, Expr* Ex, |
| 360 | SVal InitVal, unsigned Count) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 361 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 362 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 363 | |
| 364 | if (VD->hasGlobalStorage()) { |
| 365 | // Static global variables should not be visited here. |
| 366 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 367 | VD->isFileVarDecl())); |
| 368 | // Process static variables. |
| 369 | if (VD->getStorageClass() == VarDecl::Static) { |
| 370 | if (!Ex) { |
| 371 | // Only handle pointer and integer static variables. |
| 372 | |
| 373 | QualType T = VD->getType(); |
| 374 | |
| 375 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 376 | store = Bind(store, getVarLoc(VD), |
| 377 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 378 | |
| 379 | else if (T->isIntegerType()) |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 380 | store = Bind(store, getVarLoc(VD), |
| 381 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 382 | |
| 383 | // Other types of static local variables are not handled yet. |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 384 | } else { |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 385 | store = Bind(store, getVarLoc(VD), InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } else { |
| 389 | // Process local variables. |
| 390 | |
| 391 | QualType T = VD->getType(); |
| 392 | |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 393 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 394 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 395 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 396 | SVal V = Ex ? InitVal : UndefinedVal(); |
| 397 | if (Ex && InitVal.isUnknown()) { |
| 398 | // "Conjured" symbols. |
| 399 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 400 | V = Loc::IsLocType(Ex->getType()) |
| 401 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 402 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
| 403 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 404 | store = Bind(store, loc::MemRegionVal(VR), V); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 405 | |
| 406 | } else if (T->isArrayType()) { |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 407 | if (!Ex) |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 408 | store = InitializeArrayToUndefined(store, VR); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 409 | else |
| 410 | store = InitializeArray(store, VR, InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 411 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 412 | } else if (T->isStructureType()) { |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 413 | if (!Ex) |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 414 | store = InitializeStructToUndefined(store, VR); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 415 | else |
| 416 | store = InitializeStruct(store, VR, InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 417 | } |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 418 | |
| 419 | // Other types of local variables are not handled yet. |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 420 | } |
| 421 | return store; |
| 422 | } |
| 423 | |
Zhongxing Xu | ca892b8 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 424 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 425 | const char* nl, const char *sep) { |
| 426 | llvm::raw_os_ostream OS(Out); |
| 427 | RegionBindingsTy B = GetRegionBindings(store); |
| 428 | OS << "Store:" << nl; |
| 429 | |
| 430 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 431 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 432 | I.getData().print(OS); OS << nl; |
| 433 | } |
Zhongxing Xu | 6149e88 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 434 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 435 | |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 436 | Store RegionStoreManager::InitializeArray(Store store, TypedRegion* R, |
| 437 | SVal Init) { |
| 438 | QualType T = R->getType(getContext()); |
| 439 | assert(T->isArrayType()); |
| 440 | |
| 441 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 442 | |
| 443 | llvm::APInt Size = CAT->getSize(); |
| 444 | |
| 445 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 446 | |
| 447 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 448 | |
| 449 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 450 | |
| 451 | for (; i != Size; ++i) { |
| 452 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 453 | |
| 454 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 455 | |
| 456 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 457 | // The init list might be shorter than the array decl. |
| 458 | if (VI != VE) ++VI; |
| 459 | } |
| 460 | |
| 461 | return store; |
| 462 | } |
| 463 | |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 464 | Store RegionStoreManager::InitializeArrayToUndefined(Store store, |
| 465 | TypedRegion* BaseR) { |
| 466 | QualType T = BaseR->getType(getContext()); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 467 | assert(T->isArrayType()); |
| 468 | |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 469 | // Only handle constant size array for now. |
| 470 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 471 | |
| 472 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 473 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 474 | for (; i != Size; ++i) { |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 475 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 476 | |
| 477 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 478 | |
| 479 | store = Bind(store, loc::MemRegionVal(ER), UndefinedVal()); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return store; |
| 484 | } |
| 485 | |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 486 | Store RegionStoreManager::InitializeStruct(Store store, TypedRegion* R, |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 487 | SVal Init) { |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 488 | QualType T = R->getType(getContext()); |
| 489 | assert(T->isStructureType()); |
| 490 | |
| 491 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 492 | RecordDecl* RD = RT->getDecl(); |
| 493 | assert(RD->isDefinition()); |
| 494 | |
| 495 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 496 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 497 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 498 | |
| 499 | for (; FI != FE; ++FI) { |
| 500 | QualType FTy = (*FI)->getType(); |
| 501 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 502 | |
| 503 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 504 | if (VI != VE) { |
| 505 | store = Bind(store, loc::MemRegionVal(FR), *VI); |
| 506 | ++VI; |
| 507 | } else |
| 508 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 509 | } |
| 510 | else if (FTy->isArrayType()) { |
| 511 | if (VI != VE) { |
| 512 | store = InitializeArray(store, FR, *VI); |
| 513 | ++VI; |
| 514 | } else |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 515 | store = InitializeArrayToUndefined(store, FR); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 516 | } |
| 517 | else if (FTy->isStructureType()) { |
| 518 | if (VI != VE) { |
| 519 | store = InitializeStruct(store, FR, *VI); |
| 520 | ++VI; |
| 521 | } else |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 522 | store = InitializeStructToUndefined(store, FR); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | return store; |
| 526 | } |
| 527 | |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 528 | Store RegionStoreManager::InitializeStructToUndefined(Store store, |
| 529 | TypedRegion* BaseR) { |
| 530 | QualType T = BaseR->getType(getContext()); |
| 531 | assert(T->isStructureType()); |
| 532 | |
| 533 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 534 | RecordDecl* RD = RT->getDecl(); |
| 535 | assert(RD->isDefinition()); |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 536 | |
| 537 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 538 | |
| 539 | for (; I != E; ++I) { |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 540 | |
| 541 | QualType FTy = (*I)->getType(); |
| 542 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 543 | |
| 544 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 545 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 546 | |
| 547 | } else if (FTy->isArrayType()) { |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 548 | store = InitializeArrayToUndefined(store, FR); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 549 | |
| 550 | } else if (FTy->isStructureType()) { |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 551 | store = InitializeStructToUndefined(store, FR); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | return store; |
| 556 | } |