Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1 | //== BasicStore.cpp - Basic map from Locations to Values --------*- 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 defined the BasicStore and BasicStoreManager classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ImmutableMap.h" |
| 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 22 | typedef llvm::ImmutableMap<const MemRegion*,SVal> VarBindingsTy; |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 26 | class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap { |
| 27 | public: |
| 28 | BasicStoreSubRegionMap() {} |
| 29 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 30 | bool iterSubRegions(const MemRegion* R, Visitor& V) const { |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 31 | // Do nothing. No subregions. |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 32 | return true; |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 33 | } |
| 34 | }; |
| 35 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 36 | class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 37 | VarBindingsTy::Factory VBFactory; |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 38 | GRStateManager& StateMgr; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 39 | const MemRegion* SelfRegion; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 42 | BasicStoreManager(GRStateManager& mgr) |
Ted Kremenek | d6cfbe4 | 2009-01-07 22:18:50 +0000 | [diff] [blame] | 43 | : StoreManager(mgr.getAllocator()), |
| 44 | VBFactory(mgr.getAllocator()), |
Zhongxing Xu | 0adfbf6 | 2008-11-15 08:19:58 +0000 | [diff] [blame] | 45 | StateMgr(mgr), |
Zhongxing Xu | 0adfbf6 | 2008-11-15 08:19:58 +0000 | [diff] [blame] | 46 | SelfRegion(0) {} |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 48 | ~BasicStoreManager() {} |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 14453bf | 2009-03-03 19:02:42 +0000 | [diff] [blame] | 50 | SubRegionMap* getSubRegionMap(const GRState *state) { |
| 51 | return new BasicStoreSubRegionMap(); |
Ted Kremenek | 59e8f11 | 2009-03-03 01:35:36 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 54 | SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType()); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 55 | |
| 56 | const GRState* Bind(const GRState* St, Loc L, SVal V) { |
| 57 | Store store = St->getStore(); |
| 58 | store = BindInternal(store, L, V); |
| 59 | return StateMgr.MakeStateWithStore(St, store); |
| 60 | } |
| 61 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 62 | Store BindInternal(Store St, Loc loc, SVal V); |
| 63 | Store Remove(Store St, Loc loc); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 64 | Store getInitialStore(); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 65 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 66 | // FIXME: Investigate what is using this. This method should be removed. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 67 | virtual Loc getLoc(const VarDecl* VD) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 68 | return Loc::MakeVal(MRMgr.getVarRegion(VD)); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 69 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 70 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 71 | const GRState* BindCompoundLiteral(const GRState* St, |
| 72 | const CompoundLiteralExpr* CL, |
| 73 | SVal V) { |
| 74 | return St; |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 77 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 78 | SVal getLValueString(const GRState* St, const StringLiteral* S); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 79 | SVal getLValueCompoundLiteral(const GRState* St, |
| 80 | const CompoundLiteralExpr* CL); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 81 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 82 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 83 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 85 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 86 | /// conversions between arrays and pointers. |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 87 | SVal ArrayToPointer(SVal Array) { return Array; } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 89 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 90 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 91 | /// casted and 'CastToTy' the result type of the cast. |
| 92 | CastResult CastRegion(const GRState* state, const MemRegion* R, |
| 93 | QualType CastToTy); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 95 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 96 | /// 'this' object (C++). When used when analyzing a normal function this |
| 97 | /// method returns NULL. |
| 98 | const MemRegion* getSelfRegion(Store) { |
| 99 | return SelfRegion; |
| 100 | } |
| 101 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 102 | /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values. |
| 103 | /// It returns a new Store with these values removed, and populates LSymbols |
| 104 | /// and DSymbols with the known set of live and dead symbols respectively. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 105 | Store |
| 106 | RemoveDeadBindings(const GRState* state, Stmt* Loc, |
| 107 | SymbolReaper& SymReaper, |
| 108 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 110 | void iterBindings(Store store, BindingsHandler& f); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 111 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 112 | const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) { |
| 113 | Store store = St->getStore(); |
| 114 | store = BindDeclInternal(store, VD, &InitVal); |
| 115 | return StateMgr.MakeStateWithStore(St, store); |
| 116 | } |
| 117 | |
| 118 | const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) { |
| 119 | Store store = St->getStore(); |
| 120 | store = BindDeclInternal(store, VD, 0); |
| 121 | return StateMgr.MakeStateWithStore(St, store); |
| 122 | } |
| 123 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 124 | Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 126 | static inline VarBindingsTy GetVarBindings(Store store) { |
| 127 | return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 130 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 131 | }; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 133 | } // end anonymous namespace |
| 134 | |
| 135 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 136 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 137 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 138 | } |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 139 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 140 | SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 141 | return Loc::MakeVal(MRMgr.getVarRegion(VD)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 142 | } |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 143 | |
| 144 | SVal BasicStoreManager::getLValueString(const GRState* St, |
| 145 | const StringLiteral* S) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 146 | return Loc::MakeVal(MRMgr.getStringRegion(S)); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 147 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 148 | |
| 149 | SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St, |
| 150 | const CompoundLiteralExpr* CL){ |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 151 | return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL)); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 154 | SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 155 | SVal Base) { |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 156 | return UnknownVal(); |
| 157 | } |
| 158 | |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 159 | /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from |
| 160 | /// a MemRegion* to a specific location type. 'R' is the region being |
| 161 | /// casted and 'CastToTy' the result type of the cast. |
| 162 | StoreManager::CastResult |
| 163 | BasicStoreManager::CastRegion(const GRState* state, const MemRegion* R, |
| 164 | QualType CastToTy) { |
| 165 | |
| 166 | // Return the same region if the region types are compatible. |
| 167 | if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) { |
| 168 | ASTContext& Ctx = StateMgr.getContext(); |
| 169 | QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx)); |
| 170 | QualType Tb = Ctx.getCanonicalType(CastToTy); |
| 171 | |
| 172 | if (Ta == Tb) |
| 173 | return CastResult(state, R); |
| 174 | } |
| 175 | |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 176 | return CastResult(state, MRMgr.getTypedViewRegion(CastToTy, R)); |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 177 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 178 | |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 179 | SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base, |
| 180 | const FieldDecl* D) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 181 | |
| 182 | if (Base.isUnknownOrUndef()) |
| 183 | return Base; |
| 184 | |
| 185 | Loc BaseL = cast<Loc>(Base); |
| 186 | const MemRegion* BaseR = 0; |
| 187 | |
| 188 | switch(BaseL.getSubKind()) { |
| 189 | case loc::SymbolValKind: |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 190 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(), |
| 191 | StateMgr.getSymbolManager()); |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 192 | break; |
| 193 | |
| 194 | case loc::GotoLabelKind: |
| 195 | case loc::FuncValKind: |
| 196 | // Technically we can get here if people do funny things with casts. |
| 197 | return UndefinedVal(); |
| 198 | |
| 199 | case loc::MemRegionKind: |
| 200 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 201 | break; |
| 202 | |
| 203 | case loc::ConcreteIntKind: |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 204 | // While these seem funny, this can happen through casts. |
| 205 | // FIXME: What we should return is the field offset. For example, |
| 206 | // add the field offset to the integer value. That way funny things |
| 207 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 208 | return Base; |
| 209 | |
| 210 | default: |
| 211 | assert ("Unhandled Base."); |
| 212 | return Base; |
| 213 | } |
| 214 | |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 215 | return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 216 | } |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 217 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 218 | SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base, |
| 219 | SVal Offset) { |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 221 | if (Base.isUnknownOrUndef()) |
| 222 | return Base; |
| 223 | |
| 224 | Loc BaseL = cast<Loc>(Base); |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 225 | const TypedRegion* BaseR = 0; |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 226 | |
| 227 | switch(BaseL.getSubKind()) { |
Ted Kremenek | 1d6c14b | 2008-12-09 23:50:57 +0000 | [diff] [blame] | 228 | case loc::SymbolValKind: { |
| 229 | // FIXME: Should we have symbolic regions be typed or typeless? |
| 230 | // Here we assume that these regions are typeless, even though the |
| 231 | // symbol is typed. |
| 232 | SymbolRef Sym = cast<loc::SymbolVal>(&BaseL)->getSymbol(); |
| 233 | // Create a region to represent this symbol. |
| 234 | // FIXME: In the future we may just use symbolic regions instead of |
| 235 | // SymbolVals to reason about symbolic memory chunks. |
Zhongxing Xu | 026c663 | 2009-02-05 06:57:29 +0000 | [diff] [blame] | 236 | const MemRegion* SymR = MRMgr.getSymbolicRegion(Sym, |
| 237 | StateMgr.getSymbolManager()); |
Ted Kremenek | 1d6c14b | 2008-12-09 23:50:57 +0000 | [diff] [blame] | 238 | // Layered a typed region on top of this. |
| 239 | QualType T = StateMgr.getSymbolManager().getType(Sym); |
Ted Kremenek | 0312c0e | 2009-03-01 05:44:08 +0000 | [diff] [blame] | 240 | BaseR = MRMgr.getTypedViewRegion(T, SymR); |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 241 | break; |
Ted Kremenek | 1d6c14b | 2008-12-09 23:50:57 +0000 | [diff] [blame] | 242 | } |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 243 | |
| 244 | case loc::GotoLabelKind: |
| 245 | case loc::FuncValKind: |
| 246 | // Technically we can get here if people do funny things with casts. |
| 247 | return UndefinedVal(); |
| 248 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 249 | case loc::MemRegionKind: { |
| 250 | const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion(); |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 251 | |
| 252 | if (isa<ElementRegion>(R)) { |
| 253 | // Basic example: |
| 254 | // char buf[100]; |
| 255 | // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown) |
| 256 | // &q[10] |
| 257 | assert(cast<ElementRegion>(R)->getIndex().isUnknown()); |
| 258 | return Base; |
| 259 | } |
| 260 | |
| 261 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 262 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 263 | BaseR = TR; |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | // FIXME: Handle SymbolRegions? Shouldn't be possible in |
| 268 | // BasicStoreManager. |
| 269 | assert(!isa<SymbolicRegion>(R)); |
| 270 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 271 | break; |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 272 | } |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 274 | case loc::ConcreteIntKind: |
| 275 | // While these seem funny, this can happen through casts. |
| 276 | // FIXME: What we should return is the field offset. For example, |
| 277 | // add the field offset to the integer value. That way funny things |
| 278 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 279 | return Base; |
| 280 | |
| 281 | default: |
| 282 | assert ("Unhandled Base."); |
| 283 | return Base; |
| 284 | } |
| 285 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 286 | if (BaseR) |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 287 | return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR)); |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 288 | else |
| 289 | return UnknownVal(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 292 | SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 294 | if (isa<UnknownVal>(loc)) |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 295 | return UnknownVal(); |
| 296 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 297 | assert (!isa<UndefinedVal>(loc)); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 299 | switch (loc.getSubKind()) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 300 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 301 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 302 | const VarRegion* R = |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 303 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 304 | |
| 305 | if (!R) |
| 306 | return UnknownVal(); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 307 | |
| 308 | Store store = state->getStore(); |
Zhongxing Xu | 47b3240 | 2008-12-21 03:46:11 +0000 | [diff] [blame] | 309 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 310 | VarBindingsTy::data_type* T = B.lookup(R); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 311 | return T ? *T : UnknownVal(); |
| 312 | } |
| 313 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 314 | case loc::SymbolValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 315 | return UnknownVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 316 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 317 | case loc::ConcreteIntKind: |
| 318 | // Some clients may call GetSVal with such an option simply because |
| 319 | // they are doing a quick scan through their Locs (potentially to |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 320 | // invalidate their bindings). Just return Undefined. |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 321 | return UndefinedVal(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 322 | case loc::FuncValKind: |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 323 | return loc; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 325 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 326 | assert (false && "Invalid Loc."); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | |
| 330 | return UnknownVal(); |
| 331 | } |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 333 | Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) { |
| 334 | switch (loc.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 335 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 336 | const VarRegion* R = |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 337 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 338 | |
| 339 | if (!R) |
| 340 | return store; |
| 341 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 342 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 343 | return V.isUnknown() |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 344 | ? VBFactory.Remove(B, R).getRoot() |
| 345 | : VBFactory.Add(B, R, V).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 346 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 347 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 348 | assert ("SetSVal for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 349 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 353 | Store BasicStoreManager::Remove(Store store, Loc loc) { |
| 354 | switch (loc.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 355 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 356 | const VarRegion* R = |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 357 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(loc).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 358 | |
| 359 | if (!R) |
| 360 | return store; |
| 361 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 362 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 363 | return VBFactory.Remove(B, R).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 365 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 366 | assert ("Remove for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 367 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 371 | Store |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 372 | BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 373 | SymbolReaper& SymReaper, |
| 374 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 375 | { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 377 | Store store = state->getStore(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 378 | VarBindingsTy B = GetVarBindings(store); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 379 | typedef SVal::symbol_iterator symbol_iterator; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 380 | |
| 381 | // Iterate over the variable bindings. |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 382 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
| 383 | const VarRegion *VR = cast<VarRegion>(I.getKey()); |
| 384 | if (SymReaper.isLive(Loc, VR->getDecl())) { |
| 385 | RegionRoots.push_back(VR); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 386 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 387 | |
| 388 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 389 | SymReaper.markLive(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 390 | } |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 391 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 392 | |
| 393 | // Scan for live variables and live symbols. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 394 | llvm::SmallPtrSet<const VarRegion*, 10> Marked; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 396 | while (!RegionRoots.empty()) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 397 | const MemRegion* MR = RegionRoots.back(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 398 | RegionRoots.pop_back(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 400 | while (MR) { |
| 401 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 402 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 403 | break; |
| 404 | } |
| 405 | else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) { |
| 406 | if (Marked.count(R)) |
| 407 | break; |
| 408 | |
| 409 | Marked.insert(R); |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 410 | SVal X = Retrieve(state, loc::MemRegionVal(R)); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 412 | // FIXME: We need to handle symbols nested in region definitions. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 413 | for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 414 | SymReaper.markLive(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 416 | if (!isa<loc::MemRegionVal>(X)) |
| 417 | break; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 419 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 420 | RegionRoots.push_back(LVD.getRegion()); |
| 421 | break; |
| 422 | } |
| 423 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 424 | MR = R->getSuperRegion(); |
| 425 | else |
| 426 | break; |
| 427 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // Remove dead variable bindings. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 431 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 432 | const VarRegion* R = cast<VarRegion>(I.getKey()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 433 | |
| 434 | if (!Marked.count(R)) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 435 | store = Remove(store, Loc::MakeVal(R)); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 436 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 437 | |
| 438 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 439 | SymReaper.maybeDead(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 440 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 443 | return store; |
| 444 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 445 | |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 446 | Store BasicStoreManager::getInitialStore() { |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 448 | // The LiveVariables information already has a compilation of all VarDecls |
| 449 | // used in the function. Iterate through this set, and "symbolicate" |
| 450 | // any VarDecl whose value originally comes from outside the function. |
| 451 | |
| 452 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 453 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 454 | |
| 455 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 456 | |
| 457 | 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] | 458 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 460 | // Handle implicit parameters. |
| 461 | if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) { |
| 462 | const Decl& CD = StateMgr.getCodeDecl(); |
| 463 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 464 | if (MD->getSelfDecl() == PD) { |
| 465 | // Create a region for "self". |
| 466 | assert (SelfRegion == 0); |
| 467 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 468 | MRMgr.getHeapRegion()); |
| 469 | |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 470 | St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)), |
| 471 | Loc::MakeVal(SelfRegion)); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 476 | // Punt on static variables for now. |
| 477 | if (VD->getStorageClass() == VarDecl::Static) |
| 478 | continue; |
| 479 | |
| 480 | // Only handle pointers and integers for now. |
| 481 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 482 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 483 | // Initialize globals and parameters to symbolic values. |
| 484 | // Initialize local variables to undefined. |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 485 | const MemRegion *R = StateMgr.getRegion(VD); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 486 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 487 | isa<ImplicitParamDecl>(VD)) |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 488 | ? SVal::GetRValueSymbolVal(StateMgr.getSymbolManager(), R) |
| 489 | : UndefinedVal(); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 490 | |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 491 | St = BindInternal(St, Loc::MakeVal(R), X); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | return St; |
| 496 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 497 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 498 | Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD, |
| 499 | SVal* InitVal) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 501 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 502 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 503 | // BasicStore does not model arrays and structs. |
| 504 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 505 | return store; |
| 506 | |
| 507 | if (VD->hasGlobalStorage()) { |
| 508 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 509 | |
| 510 | // FIXME:: static variables may have an initializer, but the second time a |
| 511 | // function is called those values may not be current. Currently, a function |
| 512 | // will not be called more than once. |
| 513 | |
| 514 | // Static global variables should not be visited here. |
| 515 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 516 | VD->isFileVarDecl())); |
| 517 | |
| 518 | // Process static variables. |
| 519 | if (VD->getStorageClass() == VarDecl::Static) { |
| 520 | // C99: 6.7.8 Initialization |
| 521 | // If an object that has static storage duration is not initialized |
| 522 | // explicitly, then: |
| 523 | // —if it has pointer type, it is initialized to a null pointer; |
| 524 | // —if it has arithmetic type, it is initialized to (positive or |
| 525 | // unsigned) zero; |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 526 | if (!InitVal) { |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 527 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 528 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 529 | store = BindInternal(store, getLoc(VD), |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 530 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 531 | else if (T->isIntegerType()) |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 532 | store = BindInternal(store, getLoc(VD), |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 533 | nonloc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 534 | else { |
| 535 | // assert(0 && "ignore other types of variables"); |
| 536 | } |
| 537 | } else { |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 538 | store = BindInternal(store, getLoc(VD), *InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | } else { |
| 542 | // Process local scalar variables. |
| 543 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 544 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | 42577d1 | 2008-11-12 19:18:35 +0000 | [diff] [blame] | 545 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 546 | store = BindInternal(store, getLoc(VD), V); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | return store; |
| 551 | } |
| 552 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 553 | void BasicStoreManager::print(Store store, std::ostream& O, |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 554 | const char* nl, const char *sep) { |
| 555 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 556 | llvm::raw_os_ostream Out(O); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 557 | VarBindingsTy B = GetVarBindings(store); |
| 558 | Out << "Variables:" << nl; |
| 559 | |
| 560 | bool isFirst = true; |
| 561 | |
| 562 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
| 563 | if (isFirst) isFirst = false; |
| 564 | else Out << nl; |
| 565 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 566 | Out << ' ' << I.getKey() << " : "; |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 567 | I.getData().print(Out); |
| 568 | } |
| 569 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 570 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 571 | |
| 572 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
| 573 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 574 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame^] | 575 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) |
| 576 | f.HandleBinding(*this, store, I.getKey(), I.getData()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 580 | StoreManager::BindingsHandler::~BindingsHandler() {} |