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