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 | 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 | 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 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 54 | const GRState *Bind(const GRState *state, Loc L, SVal V) { |
| 55 | return state->makeWithStore(BindInternal(state->getStore(), L, V)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 58 | Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St); |
| 59 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 60 | Store BindInternal(Store St, Loc loc, SVal V); |
| 61 | Store Remove(Store St, Loc loc); |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 62 | Store getInitialStore(); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 63 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 64 | // FIXME: Investigate what is using this. This method should be removed. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 65 | virtual Loc getLoc(const VarDecl* VD) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 66 | return Loc::MakeVal(MRMgr.getVarRegion(VD)); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 67 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 69 | const GRState *BindCompoundLiteral(const GRState *state, |
| 70 | const CompoundLiteralExpr* cl, |
| 71 | SVal val) { |
| 72 | return state; |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 75 | SVal getLValueVar(const GRState *state, const VarDecl* VD); |
| 76 | SVal getLValueString(const GRState *state, const StringLiteral* S); |
| 77 | SVal getLValueCompoundLiteral(const GRState *state, |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 78 | const CompoundLiteralExpr* CL); |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 79 | SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base); |
| 80 | SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D); |
| 81 | SVal getLValueElement(const GRState *state, QualType elementType, |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 82 | SVal Base, SVal Offset); |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 84 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 85 | /// conversions between arrays and pointers. |
Zhongxing Xu | f1d537f | 2009-03-30 05:55:46 +0000 | [diff] [blame] | 86 | SVal ArrayToPointer(Loc Array) { return Array; } |
Zhongxing Xu | dc0a25d | 2008-11-16 04:07:26 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 88 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 89 | /// 'this' object (C++). When used when analyzing a normal function this |
| 90 | /// method returns NULL. |
Ted Kremenek | 39fc715 | 2009-03-05 16:41:21 +0000 | [diff] [blame] | 91 | const MemRegion* getSelfRegion(Store) { return SelfRegion; } |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 93 | /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values. |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 94 | /// It returns a new Store with these values removed. |
| 95 | Store RemoveDeadBindings(const GRState *state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 96 | SymbolReaper& SymReaper, |
| 97 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 99 | void iterBindings(Store store, BindingsHandler& f); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 101 | const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal) { |
| 102 | return state->makeWithStore(BindDeclInternal(state->getStore(),VD, &InitVal)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 105 | const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) { |
| 106 | return state->makeWithStore(BindDeclInternal(state->getStore(), VD, 0)); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 109 | Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 111 | static inline BindingsTy GetBindings(Store store) { |
| 112 | return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame] | 115 | 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] | 116 | |
| 117 | private: |
| 118 | ASTContext& getContext() { return StateMgr.getContext(); } |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 119 | }; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 121 | } // end anonymous namespace |
| 122 | |
| 123 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 124 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 125 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 126 | } |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 128 | SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 129 | return Loc::MakeVal(MRMgr.getVarRegion(VD)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 130 | } |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 132 | SVal BasicStoreManager::getLValueString(const GRState *state, |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 133 | const StringLiteral* S) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 134 | return Loc::MakeVal(MRMgr.getStringRegion(S)); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 135 | } |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 137 | SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state, |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 138 | const CompoundLiteralExpr* CL){ |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 139 | return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL)); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 142 | SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 143 | SVal Base) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 144 | |
| 145 | if (Base.isUnknownOrUndef()) |
| 146 | return Base; |
| 147 | |
| 148 | Loc BaseL = cast<Loc>(Base); |
| 149 | |
| 150 | if (isa<loc::MemRegionVal>(BaseL)) { |
| 151 | const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 152 | |
| 153 | if (BaseR == SelfRegion) |
| 154 | return loc::MemRegionVal(MRMgr.getObjCIvarRegion(D, BaseR)); |
| 155 | } |
| 156 | |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 157 | return UnknownVal(); |
| 158 | } |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 160 | SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base, |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 161 | const FieldDecl* D) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 162 | |
| 163 | if (Base.isUnknownOrUndef()) |
| 164 | return Base; |
| 165 | |
| 166 | Loc BaseL = cast<Loc>(Base); |
| 167 | const MemRegion* BaseR = 0; |
| 168 | |
| 169 | switch(BaseL.getSubKind()) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 170 | case loc::GotoLabelKind: |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 171 | return UndefinedVal(); |
| 172 | |
| 173 | case loc::MemRegionKind: |
| 174 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 175 | break; |
| 176 | |
| 177 | case loc::ConcreteIntKind: |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 178 | // While these seem funny, this can happen through casts. |
| 179 | // FIXME: What we should return is the field offset. For example, |
| 180 | // add the field offset to the integer value. That way funny things |
| 181 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 182 | return Base; |
| 183 | |
| 184 | default: |
| 185 | assert ("Unhandled Base."); |
| 186 | return Base; |
| 187 | } |
| 188 | |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 189 | return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 190 | } |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 192 | SVal BasicStoreManager::getLValueElement(const GRState *state, |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 193 | QualType elementType, |
| 194 | SVal Base, SVal Offset) { |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 196 | if (Base.isUnknownOrUndef()) |
| 197 | return Base; |
| 198 | |
| 199 | Loc BaseL = cast<Loc>(Base); |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 200 | const TypedRegion* BaseR = 0; |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 201 | |
| 202 | switch(BaseL.getSubKind()) { |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 203 | case loc::GotoLabelKind: |
Ted Kremenek | 7d71b29 | 2008-12-09 21:20:27 +0000 | [diff] [blame] | 204 | // Technically we can get here if people do funny things with casts. |
| 205 | return UndefinedVal(); |
| 206 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 207 | case loc::MemRegionKind: { |
| 208 | const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion(); |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 921b0b5 | 2009-05-05 00:06:16 +0000 | [diff] [blame] | 210 | if (isa<ElementRegion>(R)) { |
Zhongxing Xu | b5b848e | 2009-05-04 08:52:47 +0000 | [diff] [blame] | 211 | // int x; |
| 212 | // char* y = (char*) &x; |
| 213 | // 'y' => ElementRegion(0, VarRegion('x')) |
| 214 | // y[0] = 'a'; |
Ted Kremenek | d76d47e | 2009-01-27 18:29:03 +0000 | [diff] [blame] | 215 | return Base; |
| 216 | } |
| 217 | |
| 218 | |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 219 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 220 | BaseR = TR; |
| 221 | break; |
| 222 | } |
| 223 | |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 224 | if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) { |
| 225 | SymbolRef Sym = SR->getSymbol(); |
| 226 | BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR); |
| 227 | } |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 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 | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 244 | if (BaseR) |
Ted Kremenek | f936f45 | 2009-05-04 06:18:28 +0000 | [diff] [blame] | 245 | return Loc::MakeVal(MRMgr.getElementRegion(elementType, UnknownVal(), |
Zhongxing Xu | 143b2fc | 2009-06-16 09:55:50 +0000 | [diff] [blame] | 246 | BaseR, getContext())); |
Ted Kremenek | abb042f | 2008-12-13 19:24:37 +0000 | [diff] [blame] | 247 | else |
| 248 | return UnknownVal(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 251 | static bool isHigherOrderRawPtr(QualType T, ASTContext &C) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 252 | bool foundPointer = false; |
| 253 | while (1) { |
| 254 | const PointerType *PT = T->getAsPointerType(); |
| 255 | if (!PT) { |
| 256 | if (!foundPointer) |
| 257 | return false; |
| 258 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 259 | // intptr_t* or intptr_t**, etc? |
| 260 | if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy)) |
| 261 | return true; |
| 262 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 263 | QualType X = C.getCanonicalType(T).getUnqualifiedType(); |
| 264 | return X == C.VoidTy; |
| 265 | } |
| 266 | |
| 267 | foundPointer = true; |
| 268 | T = PT->getPointeeType(); |
| 269 | } |
| 270 | } |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 272 | SVal BasicStoreManager::Retrieve(const GRState *state, Loc loc, QualType T) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 274 | if (isa<UnknownVal>(loc)) |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 275 | return UnknownVal(); |
| 276 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 277 | assert (!isa<UndefinedVal>(loc)); |
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 | switch (loc.getSubKind()) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 280 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 281 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 282 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 284 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 285 | // Just support void**, void***, intptr_t*, intptr_t**, etc., for now. |
| 286 | // This is needed to handle OSCompareAndSwapPtr() and friends. |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 287 | ASTContext &Ctx = StateMgr.getContext(); |
Zhongxing Xu | ff69782 | 2009-05-09 00:50:33 +0000 | [diff] [blame] | 288 | QualType T = ER->getLocationType(Ctx); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 290 | if (!isHigherOrderRawPtr(T, Ctx)) |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 291 | return UnknownVal(); |
| 292 | |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 293 | // FIXME: Should check for element 0. |
| 294 | // Otherwise, strip the element region. |
| 295 | R = ER->getSuperRegion(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 298 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 299 | return UnknownVal(); |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 301 | BindingsTy B = GetBindings(state->getStore()); |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 302 | BindingsTy::data_type* T = B.lookup(R); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 303 | return T ? *T : UnknownVal(); |
| 304 | } |
| 305 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 306 | case loc::ConcreteIntKind: |
| 307 | // Some clients may call GetSVal with such an option simply because |
| 308 | // they are doing a quick scan through their Locs (potentially to |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 309 | // invalidate their bindings). Just return Undefined. |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 310 | return UndefinedVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 311 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 312 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 313 | assert (false && "Invalid Loc."); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | |
| 317 | return UnknownVal(); |
| 318 | } |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 320 | Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) { |
| 321 | switch (loc.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 322 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 323 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Ted Kremenek | d91719a | 2009-05-01 19:04:28 +0000 | [diff] [blame] | 324 | ASTContext &C = StateMgr.getContext(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 326 | // Special case: handle store of pointer values (Loc) to pointers via |
| 327 | // a cast to intXX_t*, void*, etc. This is needed to handle |
| 328 | // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier. |
| 329 | if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V)) |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 330 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 331 | // FIXME: Should check for index 0. |
Zhongxing Xu | ff69782 | 2009-05-09 00:50:33 +0000 | [diff] [blame] | 332 | QualType T = ER->getLocationType(C); |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | d91719a | 2009-05-01 19:04:28 +0000 | [diff] [blame] | 334 | if (isHigherOrderRawPtr(T, C)) |
Ted Kremenek | 20bd746 | 2009-05-04 07:04:36 +0000 | [diff] [blame] | 335 | R = ER->getSuperRegion(); |
Ted Kremenek | 5fa93d5 | 2009-04-29 16:03:27 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 338 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 339 | return store; |
| 340 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 341 | // We only track bindings to self.ivar. |
| 342 | if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) |
| 343 | if (IVR->getSuperRegion() != SelfRegion) |
| 344 | return store; |
Ted Kremenek | d91719a | 2009-05-01 19:04:28 +0000 | [diff] [blame] | 345 | |
| 346 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) { |
| 347 | // Only convert 'V' to a location iff the underlying region type |
| 348 | // is a location as well. |
| 349 | // FIXME: We are allowing a store of an arbitrary location to |
| 350 | // a pointer. We may wish to flag a type error here if the types |
| 351 | // are incompatible. This may also cause lots of breakage |
| 352 | // elsewhere. Food for thought. |
| 353 | if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) { |
| 354 | if (TyR->isBoundable(C) && |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 355 | Loc::IsLocType(TyR->getValueType(C))) |
Ted Kremenek | d91719a | 2009-05-01 19:04:28 +0000 | [diff] [blame] | 356 | V = X->getLoc(); |
| 357 | } |
| 358 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 360 | BindingsTy B = GetBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 361 | return V.isUnknown() |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 362 | ? VBFactory.Remove(B, R).getRoot() |
| 363 | : VBFactory.Add(B, R, V).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 365 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 366 | assert ("SetSVal for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 367 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Ted Kremenek | c6ed384 | 2009-01-07 22:56:17 +0000 | [diff] [blame] | 371 | Store BasicStoreManager::Remove(Store store, Loc loc) { |
| 372 | switch (loc.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 373 | case loc::MemRegionKind: { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 374 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 375 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 376 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 377 | return store; |
| 378 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 379 | return VBFactory.Remove(GetBindings(store), R).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 380 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 381 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 382 | assert ("Remove for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 383 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 387 | Store |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame^] | 388 | BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 389 | SymbolReaper& SymReaper, |
| 390 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 391 | { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 393 | Store store = state->getStore(); |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 394 | BindingsTy B = GetBindings(store); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 395 | typedef SVal::symbol_iterator symbol_iterator; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 396 | |
| 397 | // Iterate over the variable bindings. |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 398 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 399 | if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) { |
| 400 | if (SymReaper.isLive(Loc, VR->getDecl())) |
| 401 | RegionRoots.push_back(VR); |
| 402 | else |
| 403 | continue; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 404 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 405 | else if (isa<ObjCIvarRegion>(I.getKey())) { |
| 406 | RegionRoots.push_back(I.getKey()); |
| 407 | } |
| 408 | else |
| 409 | continue; |
| 410 | |
| 411 | // Mark the bindings in the data as live. |
| 412 | SVal X = I.getData(); |
| 413 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 414 | SymReaper.markLive(*SI); |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 415 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 416 | |
| 417 | // Scan for live variables and live symbols. |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 418 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 420 | while (!RegionRoots.empty()) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 421 | const MemRegion* MR = RegionRoots.back(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 422 | RegionRoots.pop_back(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 424 | while (MR) { |
| 425 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 426 | SymReaper.markLive(SymR->getSymbol()); |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 429 | else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) { |
| 430 | if (Marked.count(MR)) |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 431 | break; |
| 432 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 433 | Marked.insert(MR); |
| 434 | SVal X = Retrieve(state, loc::MemRegionVal(MR)); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 436 | // FIXME: We need to handle symbols nested in region definitions. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 437 | for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 438 | SymReaper.markLive(*SI); |
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 | if (!isa<loc::MemRegionVal>(X)) |
| 441 | break; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 443 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 444 | RegionRoots.push_back(LVD.getRegion()); |
| 445 | break; |
| 446 | } |
| 447 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 448 | MR = R->getSuperRegion(); |
| 449 | else |
| 450 | break; |
| 451 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Remove dead variable bindings. |
Ted Kremenek | 2c8e788 | 2009-03-05 16:32:59 +0000 | [diff] [blame] | 455 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 456 | const MemRegion* R = I.getKey(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 457 | |
| 458 | if (!Marked.count(R)) { |
Zhongxing Xu | 56a3460 | 2008-12-21 03:31:01 +0000 | [diff] [blame] | 459 | store = Remove(store, Loc::MakeVal(R)); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 460 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 461 | |
| 462 | 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] | 463 | SymReaper.maybeDead(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 464 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 465 | } |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 467 | return store; |
| 468 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 470 | Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) { |
| 471 | for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end(); |
| 472 | CI != CE; ++CI) { |
| 473 | |
| 474 | if (!*CI) |
| 475 | continue; |
| 476 | |
| 477 | // Check if the statement is an ivar reference. We only |
| 478 | // care about self.ivar. |
| 479 | if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) { |
| 480 | const Expr *Base = IV->getBase()->IgnoreParenCasts(); |
| 481 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) { |
| 482 | if (DR->getDecl() == SelfDecl) { |
| 483 | const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(), |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 484 | SelfRegion); |
Zhongxing Xu | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 485 | SVal X = ValMgr.getRegionValueSymbolVal(IVR); |
Ted Kremenek | f684d56 | 2009-03-05 18:08:28 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 523 | St = scanForIvars(MD->getBody(getContext()), 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 | d9b6ad6 | 2009-05-09 04:08:27 +0000 | [diff] [blame] | 541 | ? ValMgr.getRegionValueSymbolVal(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() {} |