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 | |
Zhongxing Xu | c88ca9d | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 49 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 50 | |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 51 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 52 | |
Zhongxing Xu | c88ca9d | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 53 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 54 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 55 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 56 | |
| 57 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 58 | |
| 59 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 60 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 61 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 62 | |
| 63 | SVal ArrayToPointer(SVal Array); |
| 64 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 65 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 66 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 67 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 68 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 69 | Store Remove(Store store, Loc LV) { |
| 70 | // FIXME: Implement. |
| 71 | return store; |
| 72 | } |
| 73 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 74 | Store getInitialStore(); |
Ted Kremenek | 73a36c9 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 75 | |
| 76 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 77 | /// 'this' object (C++). When used when analyzing a normal function this |
| 78 | /// method returns NULL. |
| 79 | const MemRegion* getSelfRegion(Store) { |
| 80 | assert (false && "Not implemented."); |
| 81 | return 0; |
| 82 | } |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 83 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 84 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 85 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
Zhongxing Xu | ab42da3 | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 86 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 88 | Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 89 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 90 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 91 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 92 | } |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 93 | |
Zhongxing Xu | 6149e88 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 94 | 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] | 95 | |
| 96 | void iterBindings(Store store, BindingsHandler& f) { |
| 97 | // FIXME: Implement. |
| 98 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | Loc getVarLoc(const VarDecl* VD) { |
| 102 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 103 | } |
| 104 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 105 | Store InitializeArray(Store store, const TypedRegion* R, SVal Init); |
| 106 | Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V); |
| 107 | Store InitializeStruct(Store store, const TypedRegion* R, SVal Init); |
| 108 | Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 109 | |
| 110 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 111 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 112 | // Utility methods. |
| 113 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 114 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // end anonymous namespace |
| 118 | |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 119 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 120 | return new RegionStoreManager(StMgr); |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 123 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 124 | const StringLiteral* S) { |
| 125 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 126 | } |
| 127 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 128 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 129 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 130 | } |
Zhongxing Xu | c88ca9d | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 131 | |
| 132 | SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 133 | const CompoundLiteralExpr* CL) { |
| 134 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 135 | } |
| 136 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 137 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 138 | SVal Base) { |
| 139 | return UnknownVal(); |
| 140 | } |
| 141 | |
| 142 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 143 | const FieldDecl* D) { |
| 144 | if (Base.isUnknownOrUndef()) |
| 145 | return Base; |
| 146 | |
| 147 | Loc BaseL = cast<Loc>(Base); |
| 148 | const MemRegion* BaseR = 0; |
| 149 | |
| 150 | switch (BaseL.getSubKind()) { |
| 151 | case loc::MemRegionKind: |
| 152 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 153 | break; |
| 154 | |
| 155 | case loc::SymbolValKind: |
| 156 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 157 | break; |
| 158 | |
| 159 | case loc::GotoLabelKind: |
| 160 | case loc::FuncValKind: |
| 161 | // These are anormal cases. Flag an undefined value. |
| 162 | return UndefinedVal(); |
| 163 | |
| 164 | case loc::ConcreteIntKind: |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 165 | // While these seem funny, this can happen through casts. |
| 166 | // FIXME: What we should return is the field offset. For example, |
| 167 | // add the field offset to the integer value. That way funny things |
| 168 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 169 | return Base; |
| 170 | |
| 171 | default: |
Zhongxing Xu | edfbcd9 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 172 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 173 | return Base; |
| 174 | } |
| 175 | |
| 176 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 177 | } |
| 178 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 179 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 180 | SVal Base, SVal Offset) { |
| 181 | if (Base.isUnknownOrUndef()) |
| 182 | return Base; |
| 183 | |
Zhongxing Xu | 13a05fa | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 184 | if (isa<loc::SymbolVal>(Base)) |
| 185 | return Base; |
| 186 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 187 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 188 | |
| 189 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 190 | |
| 191 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 192 | |
| 193 | SVal Idx = ElemR->getIndex(); |
| 194 | |
| 195 | nonloc::ConcreteInt *CI1, *CI2; |
| 196 | |
| 197 | // Only handle integer indices for now. |
| 198 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 199 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
| 200 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 201 | *CI2); |
| 202 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 203 | ElemR->getSuperRegion())); |
| 204 | } |
| 205 | |
| 206 | return UnknownVal(); |
| 207 | } |
| 208 | |
| 209 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 210 | |
| 211 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 212 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 2abba44 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 213 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 214 | |
Zhongxing Xu | 5ac8bf1 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 215 | // FIXME: Find a better way to get bit width. |
| 216 | nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false)); |
| 217 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 218 | |
| 219 | return loc::MemRegionVal(ER); |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 222 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 223 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 224 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 225 | |
| 226 | switch (L.getSubKind()) { |
| 227 | case loc::MemRegionKind: { |
| 228 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 229 | assert(R && "bad region"); |
| 230 | |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 231 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 232 | if (TR->getType(getContext())->isStructureType()) |
| 233 | return RetrieveStruct(S, TR); |
| 234 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 235 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 236 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 237 | return V ? *V : UnknownVal(); |
| 238 | } |
| 239 | |
| 240 | case loc::SymbolValKind: |
| 241 | return UnknownVal(); |
| 242 | |
| 243 | case loc::ConcreteIntKind: |
| 244 | return UndefinedVal(); // As in BasicStoreManager. |
| 245 | |
| 246 | case loc::FuncValKind: |
| 247 | return L; |
| 248 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 249 | default: |
| 250 | assert(false && "Invalid Location"); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 255 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 256 | QualType T = R->getType(getContext()); |
| 257 | assert(T->isStructureType()); |
| 258 | |
| 259 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 260 | RecordDecl* RD = RT->getDecl(); |
| 261 | assert(RD->isDefinition()); |
| 262 | |
| 263 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 264 | |
| 265 | for (int i = RD->getNumMembers() - 1; i >= 0; --i) { |
| 266 | FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R); |
| 267 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 268 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6f267e5 | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 269 | |
| 270 | SVal FieldValue = data ? *data : UnknownVal(); |
| 271 | |
| 272 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 273 | } |
| 274 | |
| 275 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 276 | } |
| 277 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 278 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | e7c8a13 | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 279 | if (LV.getSubKind() == loc::SymbolValKind) |
| 280 | return store; |
| 281 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 282 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 284 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 285 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 286 | assert(R); |
| 287 | |
| 288 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 289 | if (TR->getType(getContext())->isStructureType()) |
| 290 | return BindStruct(store, TR, V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 291 | |
| 292 | RegionBindingsTy B = GetRegionBindings(store); |
| 293 | return V.isUnknown() |
| 294 | ? RBFactory.Remove(B, R).getRoot() |
| 295 | : RBFactory.Add(B, R, V).getRoot(); |
| 296 | } |
| 297 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 298 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 299 | QualType T = R->getType(getContext()); |
| 300 | assert(T->isStructureType()); |
| 301 | |
| 302 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 303 | RecordDecl* RD = RT->getDecl(); |
| 304 | assert(RD->isDefinition()); |
| 305 | |
| 306 | RegionBindingsTy B = GetRegionBindings(store); |
| 307 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 308 | if (isa<UnknownVal>(V)) |
| 309 | return BindStructToVal(store, R, UnknownVal()); |
| 310 | |
Zhongxing Xu | 29454f4 | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 358 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, |
| 359 | SVal* InitVal, unsigned Count) { |
| 360 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 361 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 369 | if (!InitVal) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 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 { |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +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()) { |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 395 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 396 | store = Bind(store, loc::MemRegionVal(VR), V); |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 397 | } |
| 398 | else if (T->isArrayType()) { |
| 399 | if (!InitVal) |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 400 | store = BindArrayToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 401 | else |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 402 | store = InitializeArray(store, VR, *InitVal); |
| 403 | } |
| 404 | else if (T->isStructureType()) { |
| 405 | if (!InitVal) |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 406 | store = BindStructToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 407 | else |
Ted Kremenek | 37b78a1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 408 | store = InitializeStruct(store, VR, *InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 409 | } |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 410 | |
| 411 | // Other types of local variables are not handled yet. |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 412 | } |
| 413 | return store; |
| 414 | } |
| 415 | |
Zhongxing Xu | c88ca9d | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 416 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 417 | const CompoundLiteralExpr* CL, |
| 418 | SVal V) { |
| 419 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
| 420 | store = Bind(store, loc::MemRegionVal(R), V); |
| 421 | return store; |
| 422 | } |
| 423 | |
Zhongxing Xu | ab42da3 | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 424 | Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 425 | const LiveVariables& Live, |
| 426 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 427 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 428 | |
| 429 | RegionBindingsTy B = GetRegionBindings(store); |
| 430 | typedef SVal::symbol_iterator symbol_iterator; |
| 431 | |
| 432 | // FIXME: Mark all region binding value's symbol as live. We also omit symbols |
| 433 | // in SymbolicRegions. |
| 434 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 435 | SVal X = I.getData(); |
| 436 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 437 | LSymbols.insert(*SI); |
| 438 | } |
| 439 | |
| 440 | return store; |
| 441 | } |
| 442 | |
Zhongxing Xu | ca892b8 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 443 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 444 | const char* nl, const char *sep) { |
| 445 | llvm::raw_os_ostream OS(Out); |
| 446 | RegionBindingsTy B = GetRegionBindings(store); |
| 447 | OS << "Store:" << nl; |
| 448 | |
| 449 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 450 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 451 | I.getData().print(OS); OS << nl; |
| 452 | } |
Zhongxing Xu | 6149e88 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 453 | } |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 454 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 455 | Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R, |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 456 | SVal Init) { |
| 457 | QualType T = R->getType(getContext()); |
| 458 | assert(T->isArrayType()); |
| 459 | |
| 460 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 461 | |
| 462 | llvm::APInt Size = CAT->getSize(); |
| 463 | |
| 464 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 465 | |
| 466 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 467 | |
| 468 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 469 | |
| 470 | for (; i != Size; ++i) { |
| 471 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 472 | |
| 473 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 474 | |
| 475 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 476 | // The init list might be shorter than the array decl. |
| 477 | if (VI != VE) ++VI; |
| 478 | } |
| 479 | |
| 480 | return store; |
| 481 | } |
| 482 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 483 | // Bind all elements of the array to some value. |
| 484 | Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR, |
| 485 | SVal V){ |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 486 | QualType T = BaseR->getType(getContext()); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 487 | assert(T->isArrayType()); |
| 488 | |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 489 | // Only handle constant size array for now. |
| 490 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 491 | |
| 492 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | b30a073 | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 493 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 494 | for (; i != Size; ++i) { |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 495 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 496 | |
| 497 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 498 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 499 | store = Bind(store, loc::MemRegionVal(ER), V); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | return store; |
| 504 | } |
| 505 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 506 | Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R, |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 507 | SVal Init) { |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 508 | QualType T = R->getType(getContext()); |
| 509 | assert(T->isStructureType()); |
| 510 | |
| 511 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 512 | RecordDecl* RD = RT->getDecl(); |
| 513 | assert(RD->isDefinition()); |
| 514 | |
| 515 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 516 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 517 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 518 | |
| 519 | for (; FI != FE; ++FI) { |
| 520 | QualType FTy = (*FI)->getType(); |
| 521 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 522 | |
| 523 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 524 | if (VI != VE) { |
| 525 | store = Bind(store, loc::MemRegionVal(FR), *VI); |
| 526 | ++VI; |
| 527 | } else |
| 528 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 529 | } |
| 530 | else if (FTy->isArrayType()) { |
| 531 | if (VI != VE) { |
| 532 | store = InitializeArray(store, FR, *VI); |
| 533 | ++VI; |
| 534 | } else |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 535 | store = BindArrayToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 536 | } |
| 537 | else if (FTy->isStructureType()) { |
| 538 | if (VI != VE) { |
| 539 | store = InitializeStruct(store, FR, *VI); |
| 540 | ++VI; |
| 541 | } else |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 542 | store = BindStructToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | c00c55a | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | return store; |
| 546 | } |
| 547 | |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 548 | // Bind all fields of the struct to some value. |
| 549 | Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR, |
| 550 | SVal V) { |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 551 | QualType T = BaseR->getType(getContext()); |
| 552 | assert(T->isStructureType()); |
| 553 | |
| 554 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 555 | RecordDecl* RD = RT->getDecl(); |
| 556 | assert(RD->isDefinition()); |
Zhongxing Xu | 3dd9ec8 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 557 | |
| 558 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 559 | |
| 560 | for (; I != E; ++I) { |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 561 | |
| 562 | QualType FTy = (*I)->getType(); |
| 563 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 564 | |
| 565 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 566 | store = Bind(store, loc::MemRegionVal(FR), V); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 567 | |
| 568 | } else if (FTy->isArrayType()) { |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 569 | store = BindArrayToVal(store, FR, V); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 570 | |
| 571 | } else if (FTy->isStructureType()) { |
Zhongxing Xu | 9ef12d3 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 572 | store = BindStructToVal(store, FR, V); |
Zhongxing Xu | 702d470 | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | return store; |
| 577 | } |