Zhongxing Xu | 1789275 | 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 | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 27 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | 1789275 | 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 | 24194ef | 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 | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 48 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 49 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 50 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 51 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 52 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 53 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 54 | |
Zhongxing Xu | c4bf72c | 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 | b1d542a | 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 | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 65 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 66 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 67 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 68 | |
Zhongxing Xu | 24194ef | 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 | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 74 | Store getInitialStore(); |
Ted Kremenek | 9deb0e3 | 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 | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 83 | |
Zhongxing Xu | 24194ef | 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 | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 86 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 88 | Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 89 | |
Zhongxing Xu | 1789275 | 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 | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 93 | |
Zhongxing Xu | 5b8b6f2 | 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 | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 95 | |
| 96 | void iterBindings(Store store, BindingsHandler& f) { |
| 97 | // FIXME: Implement. |
| 98 | } |
Zhongxing Xu | a82512a | 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 | d463d44 | 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 | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 109 | |
| 110 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 111 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 6e3f01c | 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 | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // end anonymous namespace |
| 118 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 119 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 120 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Zhongxing Xu | 143bf82 | 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 | c4bf72c | 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 | f22679e | 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 | c4bf72c | 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 | c4bf72c | 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 | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 172 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 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 | b1d542a | 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 | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 184 | if (isa<loc::SymbolVal>(Base)) |
| 185 | return Base; |
| 186 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 187 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 188 | |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 189 | // Pointer of any type can be cast and used as array base. We do not support |
| 190 | // that case yet. |
| 191 | if (!isa<ElementRegion>(BaseL.getRegion())) { |
| 192 | // Record what we have seen in real code. |
| 193 | assert(isa<FieldRegion>(BaseL.getRegion())); |
| 194 | return UnknownVal(); |
| 195 | } |
| 196 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 197 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 198 | |
| 199 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 200 | |
| 201 | SVal Idx = ElemR->getIndex(); |
| 202 | |
| 203 | nonloc::ConcreteInt *CI1, *CI2; |
| 204 | |
| 205 | // Only handle integer indices for now. |
| 206 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 207 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 208 | |
| 209 | // Temporary SVal to hold a potential signed APSInt. |
| 210 | SVal SignedInt; |
| 211 | |
| 212 | // Index might be unsigned. We have to convert it to signed. |
| 213 | if (CI2->getValue().isUnsigned()) { |
| 214 | llvm::APSInt SI = CI2->getValue(); |
| 215 | SI.setIsSigned(true); |
| 216 | SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI)); |
| 217 | CI2 = cast<nonloc::ConcreteInt>(&SignedInt); |
| 218 | } |
| 219 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 220 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 221 | *CI2); |
| 222 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 223 | ElemR->getSuperRegion())); |
| 224 | } |
| 225 | |
| 226 | return UnknownVal(); |
| 227 | } |
| 228 | |
| 229 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 230 | |
| 231 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 232 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 233 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 234 | |
Zhongxing Xu | a09300a | 2008-11-15 05:18:50 +0000 | [diff] [blame^] | 235 | nonloc::ConcreteInt Idx(BasicVals.getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 236 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 237 | |
| 238 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 241 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 242 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 243 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 244 | |
| 245 | switch (L.getSubKind()) { |
| 246 | case loc::MemRegionKind: { |
| 247 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 248 | assert(R && "bad region"); |
| 249 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 250 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 251 | if (TR->getType(getContext())->isStructureType()) |
| 252 | return RetrieveStruct(S, TR); |
| 253 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 254 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 255 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 256 | return V ? *V : UnknownVal(); |
| 257 | } |
| 258 | |
| 259 | case loc::SymbolValKind: |
| 260 | return UnknownVal(); |
| 261 | |
| 262 | case loc::ConcreteIntKind: |
| 263 | return UndefinedVal(); // As in BasicStoreManager. |
| 264 | |
| 265 | case loc::FuncValKind: |
| 266 | return L; |
| 267 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 268 | default: |
| 269 | assert(false && "Invalid Location"); |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 274 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 275 | QualType T = R->getType(getContext()); |
| 276 | assert(T->isStructureType()); |
| 277 | |
| 278 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 279 | RecordDecl* RD = RT->getDecl(); |
| 280 | assert(RD->isDefinition()); |
| 281 | |
| 282 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 283 | |
| 284 | for (int i = RD->getNumMembers() - 1; i >= 0; --i) { |
| 285 | FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R); |
| 286 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 287 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 288 | |
| 289 | SVal FieldValue = data ? *data : UnknownVal(); |
| 290 | |
| 291 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 292 | } |
| 293 | |
| 294 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 295 | } |
| 296 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 297 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 298 | if (LV.getSubKind() == loc::SymbolValKind) |
| 299 | return store; |
| 300 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 301 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 303 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 304 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 305 | assert(R); |
| 306 | |
| 307 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 308 | if (TR->getType(getContext())->isStructureType()) |
| 309 | return BindStruct(store, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 310 | |
| 311 | RegionBindingsTy B = GetRegionBindings(store); |
| 312 | return V.isUnknown() |
| 313 | ? RBFactory.Remove(B, R).getRoot() |
| 314 | : RBFactory.Add(B, R, V).getRoot(); |
| 315 | } |
| 316 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 317 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 318 | QualType T = R->getType(getContext()); |
| 319 | assert(T->isStructureType()); |
| 320 | |
| 321 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 322 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 323 | |
| 324 | if (!RD->isDefinition()) { |
| 325 | // This can only occur when a pointer of imcomplete struct type is used as a |
| 326 | // function argument. |
| 327 | assert(V.isUnknown()); |
| 328 | return store; |
| 329 | } |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 330 | |
| 331 | RegionBindingsTy B = GetRegionBindings(store); |
| 332 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 333 | if (isa<UnknownVal>(V)) |
| 334 | return BindStructToVal(store, R, UnknownVal()); |
| 335 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 336 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
| 337 | |
| 338 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 339 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 340 | |
| 341 | for (; FI != FE; ++FI, ++VI) { |
| 342 | assert(VI != VE); |
| 343 | |
| 344 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 345 | |
| 346 | B = RBFactory.Add(B, FR, *VI); |
| 347 | } |
| 348 | |
| 349 | return B.getRoot(); |
| 350 | } |
| 351 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 352 | Store RegionStoreManager::getInitialStore() { |
| 353 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 354 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 355 | |
| 356 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 357 | |
| 358 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 359 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 361 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 362 | // Punt on static variables for now. |
| 363 | if (VD->getStorageClass() == VarDecl::Static) |
| 364 | continue; |
| 365 | |
| 366 | QualType T = VD->getType(); |
| 367 | // Only handle pointers and integers for now. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 368 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 369 | // Initialize globals and parameters to symbolic values. |
| 370 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 371 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 372 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 373 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 374 | : UndefinedVal(); |
| 375 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 376 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | return St; |
| 381 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 383 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, |
| 384 | SVal* InitVal, unsigned Count) { |
| 385 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 386 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 387 | |
| 388 | if (VD->hasGlobalStorage()) { |
| 389 | // Static global variables should not be visited here. |
| 390 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 391 | VD->isFileVarDecl())); |
| 392 | // Process static variables. |
| 393 | if (VD->getStorageClass() == VarDecl::Static) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 394 | if (!InitVal) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 395 | // Only handle pointer and integer static variables. |
| 396 | |
| 397 | QualType T = VD->getType(); |
| 398 | |
| 399 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 400 | store = Bind(store, getVarLoc(VD), |
| 401 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 402 | |
| 403 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 404 | store = Bind(store, getVarLoc(VD), |
| 405 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 406 | |
| 407 | // Other types of static local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 408 | } else { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 409 | store = Bind(store, getVarLoc(VD), *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } else { |
| 413 | // Process local variables. |
| 414 | |
| 415 | QualType T = VD->getType(); |
| 416 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 417 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 418 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 419 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 420 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 421 | store = Bind(store, loc::MemRegionVal(VR), V); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 422 | } |
| 423 | else if (T->isArrayType()) { |
| 424 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 425 | store = BindArrayToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 426 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 427 | store = InitializeArray(store, VR, *InitVal); |
| 428 | } |
| 429 | else if (T->isStructureType()) { |
| 430 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 431 | store = BindStructToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 432 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 433 | store = InitializeStruct(store, VR, *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 434 | } |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 435 | |
| 436 | // Other types of local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 437 | } |
| 438 | return store; |
| 439 | } |
| 440 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 441 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 442 | const CompoundLiteralExpr* CL, |
| 443 | SVal V) { |
| 444 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
| 445 | store = Bind(store, loc::MemRegionVal(R), V); |
| 446 | return store; |
| 447 | } |
| 448 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 449 | Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 450 | const LiveVariables& Live, |
| 451 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 452 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 453 | |
| 454 | RegionBindingsTy B = GetRegionBindings(store); |
| 455 | typedef SVal::symbol_iterator symbol_iterator; |
| 456 | |
| 457 | // FIXME: Mark all region binding value's symbol as live. We also omit symbols |
| 458 | // in SymbolicRegions. |
| 459 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 460 | SVal X = I.getData(); |
| 461 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 462 | LSymbols.insert(*SI); |
| 463 | } |
| 464 | |
| 465 | return store; |
| 466 | } |
| 467 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 468 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 469 | const char* nl, const char *sep) { |
| 470 | llvm::raw_os_ostream OS(Out); |
| 471 | RegionBindingsTy B = GetRegionBindings(store); |
| 472 | OS << "Store:" << nl; |
| 473 | |
| 474 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 475 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 476 | I.getData().print(OS); OS << nl; |
| 477 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 478 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 479 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 480 | Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R, |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 481 | SVal Init) { |
| 482 | QualType T = R->getType(getContext()); |
| 483 | assert(T->isArrayType()); |
| 484 | |
| 485 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 486 | |
| 487 | llvm::APInt Size = CAT->getSize(); |
| 488 | |
| 489 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 490 | |
| 491 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 492 | |
| 493 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 494 | |
| 495 | for (; i != Size; ++i) { |
| 496 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 497 | |
| 498 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 499 | |
| 500 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 501 | // The init list might be shorter than the array decl. |
| 502 | if (VI != VE) ++VI; |
| 503 | } |
| 504 | |
| 505 | return store; |
| 506 | } |
| 507 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 508 | // Bind all elements of the array to some value. |
| 509 | Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR, |
| 510 | SVal V){ |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 511 | QualType T = BaseR->getType(getContext()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 512 | assert(T->isArrayType()); |
| 513 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 514 | // Only handle constant size array for now. |
| 515 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 516 | |
| 517 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 518 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 519 | for (; i != Size; ++i) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 520 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 521 | |
| 522 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 523 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 524 | store = Bind(store, loc::MemRegionVal(ER), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | return store; |
| 529 | } |
| 530 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 531 | Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R, |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 532 | SVal Init) { |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 533 | QualType T = R->getType(getContext()); |
| 534 | assert(T->isStructureType()); |
| 535 | |
| 536 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 537 | RecordDecl* RD = RT->getDecl(); |
| 538 | assert(RD->isDefinition()); |
| 539 | |
| 540 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 541 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 542 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 543 | |
| 544 | for (; FI != FE; ++FI) { |
| 545 | QualType FTy = (*FI)->getType(); |
| 546 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 547 | |
| 548 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 549 | if (VI != VE) { |
| 550 | store = Bind(store, loc::MemRegionVal(FR), *VI); |
| 551 | ++VI; |
| 552 | } else |
| 553 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 554 | } |
| 555 | else if (FTy->isArrayType()) { |
| 556 | if (VI != VE) { |
| 557 | store = InitializeArray(store, FR, *VI); |
| 558 | ++VI; |
| 559 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 560 | store = BindArrayToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 561 | } |
| 562 | else if (FTy->isStructureType()) { |
| 563 | if (VI != VE) { |
| 564 | store = InitializeStruct(store, FR, *VI); |
| 565 | ++VI; |
| 566 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 567 | store = BindStructToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | return store; |
| 571 | } |
| 572 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 573 | // Bind all fields of the struct to some value. |
| 574 | Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR, |
| 575 | SVal V) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 576 | QualType T = BaseR->getType(getContext()); |
| 577 | assert(T->isStructureType()); |
| 578 | |
| 579 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 580 | RecordDecl* RD = RT->getDecl(); |
| 581 | assert(RD->isDefinition()); |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 582 | |
| 583 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 584 | |
| 585 | for (; I != E; ++I) { |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 586 | |
| 587 | QualType FTy = (*I)->getType(); |
| 588 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 589 | |
| 590 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 591 | store = Bind(store, loc::MemRegionVal(FR), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 592 | |
| 593 | } else if (FTy->isArrayType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 594 | store = BindArrayToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 595 | |
| 596 | } else if (FTy->isStructureType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 597 | store = BindStructToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | return store; |
| 602 | } |