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" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | |
| 22 | #include "llvm/ADT/ImmutableMap.h" |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ImmutableList.h" |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 29 | // Actual Store type. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 30 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 31 | |
| 32 | // RegionView GDM stuff. |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 33 | typedef llvm::ImmutableList<const MemRegion*> RegionViewTy; |
| 34 | typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 35 | static int RegionViewMapTyIndex = 0; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 36 | namespace clang { |
| 37 | template<> struct GRStateTrait<RegionViewMapTy> |
| 38 | : public GRStatePartialTrait<RegionViewMapTy> { |
| 39 | static void* GDMIndex() { return &RegionViewMapTyIndex; } |
| 40 | }; |
| 41 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 42 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 43 | // RegionExtents GDM stuff. |
| 44 | // Currently RegionExtents are in bytes. We can change this representation when |
| 45 | // there are real requirements. |
| 46 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy; |
| 47 | static int RegionExtentsTyIndex = 0; |
| 48 | namespace clang { |
| 49 | template<> struct GRStateTrait<RegionExtentsTy> |
| 50 | : public GRStatePartialTrait<RegionExtentsTy> { |
| 51 | static void* GDMIndex() { return &RegionExtentsTyIndex; } |
| 52 | }; |
| 53 | } |
| 54 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | |
| 57 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 58 | RegionBindingsTy::Factory RBFactory; |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 59 | RegionViewTy::Factory RVFactory; |
| 60 | RegionViewMapTy::Factory RVMFactory; |
| 61 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 62 | GRStateManager& StateMgr; |
| 63 | MemRegionManager MRMgr; |
| 64 | |
| 65 | public: |
| 66 | RegionStoreManager(GRStateManager& mgr) |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 67 | : RBFactory(mgr.getAllocator()), |
| 68 | RVFactory(mgr.getAllocator()), |
| 69 | RVMFactory(mgr.getAllocator()), |
| 70 | StateMgr(mgr), |
| 71 | MRMgr(StateMgr.getAllocator()) {} |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 72 | |
| 73 | virtual ~RegionStoreManager() {} |
| 74 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 75 | MemRegionManager& getRegionManager() { return MRMgr; } |
| 76 | |
| 77 | // FIXME: Is this function necessary? |
| 78 | SVal GetRegionSVal(Store St, const MemRegion* R) { |
| 79 | return Retrieve(St, loc::MemRegionVal(R)); |
| 80 | } |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 81 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 82 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 83 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 84 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
| 85 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 86 | SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*); |
| 87 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 88 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 89 | |
| 90 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 91 | |
| 92 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 93 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 94 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 95 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 96 | SVal getSizeInElements(const GRState* St, const MemRegion* R); |
| 97 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 98 | SVal ArrayToPointer(SVal Array); |
| 99 | |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 100 | std::pair<const GRState*, SVal> |
| 101 | CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 102 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 103 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 104 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 105 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 106 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 107 | Store Remove(Store store, Loc LV) { |
| 108 | // FIXME: Implement. |
| 109 | return store; |
| 110 | } |
| 111 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 112 | Store getInitialStore(); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 113 | |
| 114 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 115 | /// 'this' object (C++). When used when analyzing a normal function this |
| 116 | /// method returns NULL. |
| 117 | const MemRegion* getSelfRegion(Store) { |
| 118 | assert (false && "Not implemented."); |
| 119 | return 0; |
| 120 | } |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 121 | |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 122 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 123 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 124 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 126 | Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 127 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 128 | const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent); |
| 129 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 130 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 131 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 132 | } |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 133 | |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 134 | 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] | 135 | |
| 136 | void iterBindings(Store store, BindingsHandler& f) { |
| 137 | // FIXME: Implement. |
| 138 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | Loc getVarLoc(const VarDecl* VD) { |
| 142 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 143 | } |
| 144 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 145 | Store InitializeArray(Store store, const TypedRegion* R, SVal Init); |
| 146 | Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 147 | Store BindArrayToSymVal(Store store, const TypedRegion* BaseR); |
| 148 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 149 | Store InitializeStruct(Store store, const TypedRegion* R, SVal Init); |
| 150 | Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 151 | Store BindStructToSymVal(Store store, const TypedRegion* BaseR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 152 | |
| 153 | SVal RetrieveStruct(Store store, const TypedRegion* R); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 154 | Store BindStruct(Store store, const TypedRegion* R, SVal V); |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 155 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 156 | // Utility methods. |
| 157 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
| 158 | ASTContext& getContext() { return StateMgr.getContext(); } |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 159 | SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 160 | |
| 161 | const GRState* AddRegionView(const GRState* St, |
| 162 | const MemRegion* View, const MemRegion* Base); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // end anonymous namespace |
| 166 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 167 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | 24194ef | 2008-10-24 01:38:55 +0000 | [diff] [blame] | 168 | return new RegionStoreManager(StMgr); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 171 | SVal RegionStoreManager::getLValueString(const GRState* St, |
| 172 | const StringLiteral* S) { |
| 173 | return loc::MemRegionVal(MRMgr.getStringRegion(S)); |
| 174 | } |
| 175 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 176 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 177 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 178 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 179 | |
| 180 | SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 181 | const CompoundLiteralExpr* CL) { |
| 182 | return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)); |
| 183 | } |
| 184 | |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 185 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 186 | SVal Base) { |
| 187 | return UnknownVal(); |
| 188 | } |
| 189 | |
| 190 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 191 | const FieldDecl* D) { |
| 192 | if (Base.isUnknownOrUndef()) |
| 193 | return Base; |
| 194 | |
| 195 | Loc BaseL = cast<Loc>(Base); |
| 196 | const MemRegion* BaseR = 0; |
| 197 | |
| 198 | switch (BaseL.getSubKind()) { |
| 199 | case loc::MemRegionKind: |
| 200 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 201 | break; |
| 202 | |
| 203 | case loc::SymbolValKind: |
| 204 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 205 | break; |
| 206 | |
| 207 | case loc::GotoLabelKind: |
| 208 | case loc::FuncValKind: |
| 209 | // These are anormal cases. Flag an undefined value. |
| 210 | return UndefinedVal(); |
| 211 | |
| 212 | case loc::ConcreteIntKind: |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 213 | // While these seem funny, this can happen through casts. |
| 214 | // FIXME: What we should return is the field offset. For example, |
| 215 | // add the field offset to the integer value. That way funny things |
| 216 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 217 | return Base; |
| 218 | |
| 219 | default: |
Zhongxing Xu | 13d1ee2 | 2008-11-07 08:57:30 +0000 | [diff] [blame] | 220 | assert(0 && "Unhandled Base."); |
Zhongxing Xu | c4bf72c | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 221 | return Base; |
| 222 | } |
| 223 | |
| 224 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 225 | } |
| 226 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 227 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 228 | SVal Base, SVal Offset) { |
| 229 | if (Base.isUnknownOrUndef()) |
| 230 | return Base; |
| 231 | |
Zhongxing Xu | 4a1513e | 2008-10-27 12:23:17 +0000 | [diff] [blame] | 232 | if (isa<loc::SymbolVal>(Base)) |
| 233 | return Base; |
| 234 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 235 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 236 | |
Zhongxing Xu | e4d1393 | 2008-11-13 09:48:44 +0000 | [diff] [blame] | 237 | // Pointer of any type can be cast and used as array base. We do not support |
| 238 | // that case yet. |
| 239 | if (!isa<ElementRegion>(BaseL.getRegion())) { |
| 240 | // Record what we have seen in real code. |
| 241 | assert(isa<FieldRegion>(BaseL.getRegion())); |
| 242 | return UnknownVal(); |
| 243 | } |
| 244 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 245 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 246 | |
| 247 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 248 | |
| 249 | SVal Idx = ElemR->getIndex(); |
| 250 | |
| 251 | nonloc::ConcreteInt *CI1, *CI2; |
| 252 | |
| 253 | // Only handle integer indices for now. |
| 254 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 255 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 256 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame^] | 257 | // Temporary SVal to hold a potential signed and extended APSInt. |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 258 | SVal SignedInt; |
| 259 | |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame^] | 260 | // Index might be unsigned. We have to convert it to signed. It might also |
| 261 | // be less wide than the size. We have to extend it. |
| 262 | if (CI2->getValue().isUnsigned() || |
| 263 | CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) { |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 264 | llvm::APSInt SI = CI2->getValue(); |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame^] | 265 | SI.extend(CI1->getValue().getBitWidth()); |
Zhongxing Xu | cc0d0ec | 2008-11-13 09:15:14 +0000 | [diff] [blame] | 266 | SI.setIsSigned(true); |
| 267 | SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI)); |
| 268 | CI2 = cast<nonloc::ConcreteInt>(&SignedInt); |
| 269 | } |
| 270 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 271 | SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 272 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 273 | ElemR->getSuperRegion())); |
| 274 | } |
| 275 | |
| 276 | return UnknownVal(); |
| 277 | } |
| 278 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 279 | SVal RegionStoreManager::getSizeInElements(const GRState* St, |
| 280 | const MemRegion* R) { |
| 281 | if (const VarRegion* VR = dyn_cast<VarRegion>(R)) { |
| 282 | // Get the type of the variable. |
| 283 | QualType T = VR->getType(getContext()); |
| 284 | |
| 285 | // It must be of array type. |
| 286 | const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 287 | |
| 288 | // return the size as signed integer. |
| 289 | return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false); |
| 290 | } |
| 291 | |
| 292 | if (const StringRegion* SR = dyn_cast<StringRegion>(R)) { |
Zhongxing Xu | 6613d08 | 2008-11-24 02:18:56 +0000 | [diff] [blame] | 293 | const StringLiteral* Str = SR->getStringLiteral(); |
Zhongxing Xu | d0fd3b7 | 2008-11-24 02:30:48 +0000 | [diff] [blame] | 294 | // We intentionally made the size value signed because it participates in |
| 295 | // operations with signed indices. |
Zhongxing Xu | 4b89e03 | 2008-11-24 05:16:01 +0000 | [diff] [blame] | 296 | return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) { |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 300 | GRStateRef state(St, StateMgr); |
| 301 | |
| 302 | // Get the size of the super region in bytes. |
| 303 | RegionExtentsTy::data_type* T |
| 304 | = state.get<RegionExtentsTy>(ATR->getSuperRegion()); |
| 305 | |
| 306 | assert(T && "region extent not exist"); |
| 307 | |
| 308 | // Assume it's ConcreteInt for now. |
| 309 | llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue(); |
| 310 | |
| 311 | // Get the size of the element in bits. |
| 312 | QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr()) |
| 313 | ->getPointeeType(); |
| 314 | |
| 315 | uint64_t X = getContext().getTypeSize(ElemTy); |
| 316 | |
| 317 | const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(), |
| 318 | false); |
| 319 | |
| 320 | // Calculate the number of elements. |
| 321 | |
| 322 | // FIXME: What do we do with signed-ness problem? Shall we make all APSInts |
| 323 | // signed? |
| 324 | if (SSize.isUnsigned()) |
| 325 | SSize.setIsSigned(true); |
| 326 | |
| 327 | // FIXME: move this operation into BasicVals. |
| 328 | const llvm::APSInt S = |
| 329 | (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize; |
| 330 | |
| 331 | return NonLoc::MakeVal(getBasicVals(), S); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) { |
| 335 | // FIXME: Unsupported yet. |
| 336 | FR = 0; |
| 337 | return UnknownVal(); |
| 338 | } |
Zhongxing Xu | 369f429 | 2008-11-22 13:23:00 +0000 | [diff] [blame] | 339 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 340 | assert(0 && "Other regions are not supported yet."); |
| 341 | } |
| 342 | |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 343 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 344 | |
| 345 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 346 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 347 | |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 348 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
Zhongxing Xu | 0b7e642 | 2008-10-26 02:23:57 +0000 | [diff] [blame] | 349 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 350 | |
| 351 | return loc::MemRegionVal(ER); |
Zhongxing Xu | b1d542a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 354 | std::pair<const GRState*, SVal> |
| 355 | RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr, |
| 356 | QualType CastToTy, Stmt* CastE) { |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 357 | if (const AllocaRegion* AR = |
| 358 | dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) { |
| 359 | |
| 360 | // Create a new region to attach type information to it. |
| 361 | const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR); |
| 362 | |
| 363 | // Get the pointer to the first element. |
| 364 | nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false)); |
| 365 | const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR); |
| 366 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 367 | // Add a RegionView to base region. |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 368 | return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR), |
| 369 | loc::MemRegionVal(ER)); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Default case. |
Zhongxing Xu | cb529b5 | 2008-11-16 07:06:26 +0000 | [diff] [blame] | 373 | return std::pair<const GRState*, SVal>(St, UnknownVal()); |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 376 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 377 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 378 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 379 | |
| 380 | switch (L.getSubKind()) { |
| 381 | case loc::MemRegionKind: { |
| 382 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 383 | assert(R && "bad region"); |
| 384 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 385 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 386 | if (TR->getType(getContext())->isStructureType()) |
| 387 | return RetrieveStruct(S, TR); |
| 388 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 389 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 390 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 391 | return V ? *V : UnknownVal(); |
| 392 | } |
| 393 | |
| 394 | case loc::SymbolValKind: |
| 395 | return UnknownVal(); |
| 396 | |
| 397 | case loc::ConcreteIntKind: |
| 398 | return UndefinedVal(); // As in BasicStoreManager. |
| 399 | |
| 400 | case loc::FuncValKind: |
| 401 | return L; |
| 402 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 403 | default: |
| 404 | assert(false && "Invalid Location"); |
Ted Kremenek | ab7b32b | 2008-11-19 00:27:37 +0000 | [diff] [blame] | 405 | return L; |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 409 | SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) { |
| 410 | QualType T = R->getType(getContext()); |
| 411 | assert(T->isStructureType()); |
| 412 | |
| 413 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 414 | RecordDecl* RD = RT->getDecl(); |
| 415 | assert(RD->isDefinition()); |
| 416 | |
| 417 | llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList(); |
| 418 | |
| 419 | for (int i = RD->getNumMembers() - 1; i >= 0; --i) { |
| 420 | FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R); |
| 421 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 422 | RegionBindingsTy::data_type* data = B.lookup(FR); |
Zhongxing Xu | 6e3f01c | 2008-10-31 07:16:08 +0000 | [diff] [blame] | 423 | |
| 424 | SVal FieldValue = data ? *data : UnknownVal(); |
| 425 | |
| 426 | StructVal = getBasicVals().consVals(FieldValue, StructVal); |
| 427 | } |
| 428 | |
| 429 | return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals()); |
| 430 | } |
| 431 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 432 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | 8fe63af | 2008-10-27 09:24:07 +0000 | [diff] [blame] | 433 | if (LV.getSubKind() == loc::SymbolValKind) |
| 434 | return store; |
| 435 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 436 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 438 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 439 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 440 | assert(R); |
| 441 | |
| 442 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) |
| 443 | if (TR->getType(getContext())->isStructureType()) |
| 444 | return BindStruct(store, TR, V); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 445 | |
| 446 | RegionBindingsTy B = GetRegionBindings(store); |
| 447 | return V.isUnknown() |
| 448 | ? RBFactory.Remove(B, R).getRoot() |
| 449 | : RBFactory.Add(B, R, V).getRoot(); |
| 450 | } |
| 451 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 452 | Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){ |
| 453 | QualType T = R->getType(getContext()); |
| 454 | assert(T->isStructureType()); |
| 455 | |
| 456 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 457 | RecordDecl* RD = RT->getDecl(); |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 458 | |
| 459 | if (!RD->isDefinition()) { |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 460 | // This can only occur when a pointer of incomplete struct type is used as a |
Zhongxing Xu | a4f28ff | 2008-11-13 08:41:36 +0000 | [diff] [blame] | 461 | // function argument. |
| 462 | assert(V.isUnknown()); |
| 463 | return store; |
| 464 | } |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 465 | |
| 466 | RegionBindingsTy B = GetRegionBindings(store); |
| 467 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 468 | if (isa<UnknownVal>(V)) |
| 469 | return BindStructToVal(store, R, UnknownVal()); |
| 470 | |
Zhongxing Xu | f0dfa8d | 2008-10-31 08:10:01 +0000 | [diff] [blame] | 471 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); |
| 472 | |
| 473 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 474 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 475 | |
| 476 | for (; FI != FE; ++FI, ++VI) { |
| 477 | assert(VI != VE); |
| 478 | |
| 479 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 480 | |
| 481 | B = RBFactory.Add(B, FR, *VI); |
| 482 | } |
| 483 | |
| 484 | return B.getRoot(); |
| 485 | } |
| 486 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 487 | Store RegionStoreManager::getInitialStore() { |
| 488 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 489 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 490 | |
| 491 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 492 | |
| 493 | 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] | 494 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 496 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 497 | // Punt on static variables for now. |
| 498 | if (VD->getStorageClass() == VarDecl::Static) |
| 499 | continue; |
| 500 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 501 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 502 | |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 503 | QualType T = VD->getType(); |
| 504 | // Only handle pointers and integers for now. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 505 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 506 | // Initialize globals and parameters to symbolic values. |
| 507 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 508 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 509 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 510 | ? SVal::GetSymbolValue(getSymbolManager(), VD) |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 511 | : UndefinedVal(); |
| 512 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 513 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 514 | } |
| 515 | else if (T->isArrayType()) { |
| 516 | if (VD->hasGlobalStorage()) // Params cannot have array type. |
| 517 | St = BindArrayToSymVal(St, VR); |
| 518 | else |
| 519 | St = BindArrayToVal(St, VR, UndefinedVal()); |
| 520 | } |
| 521 | else if (T->isStructureType()) { |
| 522 | if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
| 523 | isa<ImplicitParamDecl>(VD)) |
| 524 | St = BindStructToSymVal(St, VR); |
| 525 | else |
| 526 | St = BindStructToVal(St, VR, UndefinedVal()); |
Zhongxing Xu | 1789275 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
| 530 | return St; |
| 531 | } |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 533 | Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD, |
| 534 | SVal* InitVal, unsigned Count) { |
| 535 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 536 | if (VD->hasGlobalStorage()) { |
| 537 | // Static global variables should not be visited here. |
| 538 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 539 | VD->isFileVarDecl())); |
| 540 | // Process static variables. |
| 541 | if (VD->getStorageClass() == VarDecl::Static) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 542 | if (!InitVal) { |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 543 | // Only handle pointer and integer static variables. |
| 544 | |
| 545 | QualType T = VD->getType(); |
| 546 | |
| 547 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 548 | store = Bind(store, getVarLoc(VD), |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 549 | loc::ConcreteInt(getBasicVals().getValue(0, T))); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 550 | |
| 551 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 552 | store = Bind(store, getVarLoc(VD), |
Zhongxing Xu | 63123d8 | 2008-11-23 04:30:35 +0000 | [diff] [blame] | 553 | loc::ConcreteInt(getBasicVals().getValue(0, T))); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 554 | |
| 555 | // Other types of static local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 556 | } else { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 557 | store = Bind(store, getVarLoc(VD), *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } else { |
| 561 | // Process local variables. |
| 562 | |
| 563 | QualType T = VD->getType(); |
| 564 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 565 | VarRegion* VR = MRMgr.getVarRegion(VD); |
| 566 | |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 567 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 568 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 569 | store = Bind(store, loc::MemRegionVal(VR), V); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 570 | } |
| 571 | else if (T->isArrayType()) { |
| 572 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 573 | store = BindArrayToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 574 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 575 | store = InitializeArray(store, VR, *InitVal); |
| 576 | } |
| 577 | else if (T->isStructureType()) { |
| 578 | if (!InitVal) |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 579 | store = BindStructToVal(store, VR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 580 | else |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 581 | store = InitializeStruct(store, VR, *InitVal); |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 582 | } |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 583 | |
| 584 | // Other types of local variables are not handled yet. |
Zhongxing Xu | 53bcdd4 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 585 | } |
| 586 | return store; |
| 587 | } |
| 588 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 589 | Store RegionStoreManager::BindCompoundLiteral(Store store, |
| 590 | const CompoundLiteralExpr* CL, |
| 591 | SVal V) { |
| 592 | CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL); |
| 593 | store = Bind(store, loc::MemRegionVal(R), V); |
| 594 | return store; |
| 595 | } |
| 596 | |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 597 | const GRState* RegionStoreManager::setExtent(const GRState* St, |
| 598 | const MemRegion* R, SVal Extent) { |
| 599 | GRStateRef state(St, StateMgr); |
| 600 | return state.set<RegionExtentsTy>(R, Extent); |
| 601 | } |
| 602 | |
| 603 | |
Zhongxing Xu | 8916d5b | 2008-11-10 09:39:04 +0000 | [diff] [blame] | 604 | Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 605 | const LiveVariables& Live, |
| 606 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 607 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 608 | |
| 609 | RegionBindingsTy B = GetRegionBindings(store); |
| 610 | typedef SVal::symbol_iterator symbol_iterator; |
| 611 | |
| 612 | // FIXME: Mark all region binding value's symbol as live. We also omit symbols |
| 613 | // in SymbolicRegions. |
| 614 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 615 | SVal X = I.getData(); |
| 616 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 617 | LSymbols.insert(*SI); |
| 618 | } |
| 619 | |
| 620 | return store; |
| 621 | } |
| 622 | |
Zhongxing Xu | a071eb0 | 2008-10-24 06:01:33 +0000 | [diff] [blame] | 623 | void RegionStoreManager::print(Store store, std::ostream& Out, |
| 624 | const char* nl, const char *sep) { |
| 625 | llvm::raw_os_ostream OS(Out); |
| 626 | RegionBindingsTy B = GetRegionBindings(store); |
| 627 | OS << "Store:" << nl; |
| 628 | |
| 629 | for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 630 | OS << ' '; I.getKey()->print(OS); OS << " : "; |
| 631 | I.getData().print(OS); OS << nl; |
| 632 | } |
Zhongxing Xu | 5b8b6f2 | 2008-10-24 04:33:15 +0000 | [diff] [blame] | 633 | } |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 634 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 635 | Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R, |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 636 | SVal Init) { |
| 637 | QualType T = R->getType(getContext()); |
| 638 | assert(T->isArrayType()); |
| 639 | |
| 640 | ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr()); |
| 641 | |
| 642 | llvm::APInt Size = CAT->getSize(); |
| 643 | |
| 644 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 645 | |
| 646 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 647 | |
| 648 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 649 | |
| 650 | for (; i != Size; ++i) { |
| 651 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 652 | |
| 653 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 654 | |
| 655 | store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal()); |
| 656 | // The init list might be shorter than the array decl. |
| 657 | if (VI != VE) ++VI; |
| 658 | } |
| 659 | |
| 660 | return store; |
| 661 | } |
| 662 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 663 | // Bind all elements of the array to some value. |
| 664 | Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR, |
| 665 | SVal V){ |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 666 | QualType T = BaseR->getType(getContext()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 667 | assert(T->isArrayType()); |
| 668 | |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 669 | // Only handle constant size array for now. |
| 670 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 671 | |
| 672 | llvm::APInt Size = CAT->getSize(); |
Zhongxing Xu | 1a12a0e | 2008-10-31 10:24:47 +0000 | [diff] [blame] | 673 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 674 | for (; i != Size; ++i) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 675 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 676 | |
| 677 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 678 | |
Zhongxing Xu | 9b6ceb1 | 2008-11-18 13:11:04 +0000 | [diff] [blame] | 679 | if (CAT->getElementType()->isStructureType()) |
| 680 | store = BindStructToVal(store, ER, V); |
| 681 | else |
| 682 | store = Bind(store, loc::MemRegionVal(ER), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | return store; |
| 687 | } |
| 688 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 689 | Store RegionStoreManager::BindArrayToSymVal(Store store, |
| 690 | const TypedRegion* BaseR) { |
| 691 | QualType T = BaseR->getType(getContext()); |
| 692 | assert(T->isArrayType()); |
| 693 | |
| 694 | if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 695 | llvm::APInt Size = CAT->getSize(); |
| 696 | llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 697 | for (; i != Size; ++i) { |
| 698 | nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i))); |
| 699 | |
| 700 | ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR); |
| 701 | |
| 702 | if (CAT->getElementType()->isStructureType()) { |
| 703 | store = BindStructToSymVal(store, ER); |
| 704 | } |
| 705 | else { |
| 706 | SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR, |
| 707 | &Idx.getValue(), CAT->getElementType()); |
| 708 | store = Bind(store, loc::MemRegionVal(ER), V); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | return store; |
| 714 | } |
| 715 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 716 | Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R, |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 717 | SVal Init) { |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 718 | QualType T = R->getType(getContext()); |
| 719 | assert(T->isStructureType()); |
| 720 | |
| 721 | RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 722 | RecordDecl* RD = RT->getDecl(); |
| 723 | assert(RD->isDefinition()); |
| 724 | |
| 725 | nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); |
| 726 | nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); |
| 727 | RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end(); |
| 728 | |
| 729 | for (; FI != FE; ++FI) { |
| 730 | QualType FTy = (*FI)->getType(); |
| 731 | FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); |
| 732 | |
| 733 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 734 | if (VI != VE) { |
| 735 | store = Bind(store, loc::MemRegionVal(FR), *VI); |
| 736 | ++VI; |
| 737 | } else |
| 738 | store = Bind(store, loc::MemRegionVal(FR), UndefinedVal()); |
| 739 | } |
| 740 | else if (FTy->isArrayType()) { |
| 741 | if (VI != VE) { |
| 742 | store = InitializeArray(store, FR, *VI); |
| 743 | ++VI; |
| 744 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 745 | store = BindArrayToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 746 | } |
| 747 | else if (FTy->isStructureType()) { |
| 748 | if (VI != VE) { |
| 749 | store = InitializeStruct(store, FR, *VI); |
| 750 | ++VI; |
| 751 | } else |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 752 | store = BindStructToVal(store, FR, UndefinedVal()); |
Zhongxing Xu | af0a844 | 2008-10-31 10:53:01 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | return store; |
| 756 | } |
| 757 | |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 758 | // Bind all fields of the struct to some value. |
| 759 | Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR, |
| 760 | SVal V) { |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 761 | QualType T = BaseR->getType(getContext()); |
| 762 | assert(T->isStructureType()); |
| 763 | |
| 764 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 765 | RecordDecl* RD = RT->getDecl(); |
| 766 | assert(RD->isDefinition()); |
Zhongxing Xu | ea8a185 | 2008-10-31 11:02:48 +0000 | [diff] [blame] | 767 | |
| 768 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 769 | |
| 770 | for (; I != E; ++I) { |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 771 | |
| 772 | QualType FTy = (*I)->getType(); |
| 773 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 774 | |
| 775 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 776 | store = Bind(store, loc::MemRegionVal(FR), V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 777 | |
| 778 | } else if (FTy->isArrayType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 779 | store = BindArrayToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 780 | |
| 781 | } else if (FTy->isStructureType()) { |
Zhongxing Xu | d463d44 | 2008-11-02 12:13:30 +0000 | [diff] [blame] | 782 | store = BindStructToVal(store, FR, V); |
Zhongxing Xu | a82512a | 2008-10-24 08:42:28 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
| 786 | return store; |
| 787 | } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 788 | |
Zhongxing Xu | c3a0599 | 2008-11-19 11:06:24 +0000 | [diff] [blame] | 789 | Store RegionStoreManager::BindStructToSymVal(Store store, |
| 790 | const TypedRegion* BaseR) { |
| 791 | QualType T = BaseR->getType(getContext()); |
| 792 | assert(T->isStructureType()); |
| 793 | |
| 794 | const RecordType* RT = cast<RecordType>(T.getTypePtr()); |
| 795 | RecordDecl* RD = RT->getDecl(); |
| 796 | assert(RD->isDefinition()); |
| 797 | |
| 798 | RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 799 | |
| 800 | for (; I != E; ++I) { |
| 801 | QualType FTy = (*I)->getType(); |
| 802 | FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR); |
| 803 | |
| 804 | if (Loc::IsLocType(FTy) || FTy->isIntegerType()) { |
| 805 | store = Bind(store, loc::MemRegionVal(FR), |
| 806 | SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy)); |
| 807 | } |
| 808 | else if (FTy->isArrayType()) { |
| 809 | store = BindArrayToSymVal(store, FR); |
| 810 | } |
| 811 | else if (FTy->isStructureType()) { |
| 812 | store = BindStructToSymVal(store, FR); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | return store; |
| 817 | } |
| 818 | |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 819 | const GRState* RegionStoreManager::AddRegionView(const GRState* St, |
| 820 | const MemRegion* View, |
| 821 | const MemRegion* Base) { |
| 822 | GRStateRef state(St, StateMgr); |
| 823 | |
| 824 | // First, retrieve the region view of the base region. |
| 825 | RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base); |
| 826 | RegionViewTy L = d ? *d : RVFactory.GetEmptyList(); |
| 827 | |
| 828 | // Now add View to the region view. |
| 829 | L = RVFactory.Add(View, L); |
| 830 | |
| 831 | // Create a new state with the new region view. |
| 832 | return state.set<RegionViewMapTy>(Base, L); |
| 833 | } |