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