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