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