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