Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1 | //== BasicStore.cpp - Basic map from Locations to Values --------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defined the BasicStore and BasicStoreManager classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ImmutableMap.h" |
| 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 22 | typedef llvm::ImmutableMap<const VarDecl*,SVal> VarBindingsTy; |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 27 | VarBindingsTy::Factory VBFactory; |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 28 | GRStateManager& StateMgr; |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 29 | MemRegionManager MRMgr; |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 30 | const MemRegion* SelfRegion; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 33 | BasicStoreManager(GRStateManager& mgr) |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 34 | : StateMgr(mgr), MRMgr(StateMgr.getAllocator()), SelfRegion(0) {} |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 36 | ~BasicStoreManager() {} |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 38 | SVal Retrieve(Store St, Loc LV, QualType T); |
| 39 | Store Bind(Store St, Loc LV, SVal V); |
| 40 | Store Remove(Store St, Loc LV); |
| 41 | Store getInitialStore(); |
| 42 | MemRegionManager& getRegionManager() { return MRMgr; } |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 43 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 44 | // FIXME: Investigate what is using this. This method should be removed. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 45 | virtual Loc getLoc(const VarDecl* VD) { |
| 46 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 47 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 48 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 49 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 50 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 51 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 52 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 54 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 55 | /// conversions between arrays and pointers. |
Zhongxing Xu | e1911af | 2008-10-23 03:10:39 +0000 | [diff] [blame] | 56 | SVal ArrayToPointer(SVal Array) { return Array; } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 58 | /// getSelfRegion - Returns the region for the 'self' (Objective-C) or |
| 59 | /// 'this' object (C++). When used when analyzing a normal function this |
| 60 | /// method returns NULL. |
| 61 | const MemRegion* getSelfRegion(Store) { |
| 62 | return SelfRegion; |
| 63 | } |
| 64 | |
| 65 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 66 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 67 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 69 | void iterBindings(Store store, BindingsHandler& f); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 71 | Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, |
| 72 | SVal InitVal = UndefinedVal(), unsigned Count = 0); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 74 | static inline VarBindingsTy GetVarBindings(Store store) { |
| 75 | return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 78 | void print(Store store, std::ostream& Out, const char* nl, const char *sep); |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 79 | }; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 81 | } // end anonymous namespace |
| 82 | |
| 83 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 84 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 85 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 86 | } |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 87 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 88 | SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 89 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 92 | SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 93 | SVal Base) { |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 94 | return UnknownVal(); |
| 95 | } |
| 96 | |
| 97 | |
Zhongxing Xu | c92e5fe | 2008-10-22 09:00:19 +0000 | [diff] [blame] | 98 | SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base, |
| 99 | const FieldDecl* D) { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 100 | |
| 101 | if (Base.isUnknownOrUndef()) |
| 102 | return Base; |
| 103 | |
| 104 | Loc BaseL = cast<Loc>(Base); |
| 105 | const MemRegion* BaseR = 0; |
| 106 | |
| 107 | switch(BaseL.getSubKind()) { |
| 108 | case loc::SymbolValKind: |
| 109 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 110 | break; |
| 111 | |
| 112 | case loc::GotoLabelKind: |
| 113 | case loc::FuncValKind: |
| 114 | // Technically we can get here if people do funny things with casts. |
| 115 | return UndefinedVal(); |
| 116 | |
| 117 | case loc::MemRegionKind: |
| 118 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 119 | break; |
| 120 | |
| 121 | case loc::ConcreteIntKind: |
| 122 | case loc::StringLiteralValKind: |
| 123 | // While these seem funny, this can happen through casts. |
| 124 | // FIXME: What we should return is the field offset. For example, |
| 125 | // add the field offset to the integer value. That way funny things |
| 126 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 127 | return Base; |
| 128 | |
| 129 | default: |
| 130 | assert ("Unhandled Base."); |
| 131 | return Base; |
| 132 | } |
| 133 | |
| 134 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 135 | } |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 136 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 137 | SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base, |
| 138 | SVal Offset) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 139 | // Total hack: Just return "Base" for now. |
| 140 | return Base; |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 143 | SVal BasicStoreManager::Retrieve(Store St, Loc LV, QualType T) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 144 | |
| 145 | if (isa<UnknownVal>(LV)) |
| 146 | return UnknownVal(); |
| 147 | |
| 148 | assert (!isa<UndefinedVal>(LV)); |
| 149 | |
| 150 | switch (LV.getSubKind()) { |
| 151 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 152 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 153 | const VarRegion* R = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 154 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 155 | |
| 156 | if (!R) |
| 157 | return UnknownVal(); |
| 158 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 159 | VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 160 | VarBindingsTy::data_type* T = B.lookup(R->getDecl()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 161 | return T ? *T : UnknownVal(); |
| 162 | } |
| 163 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 164 | case loc::SymbolValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 165 | return UnknownVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 166 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 167 | case loc::ConcreteIntKind: |
| 168 | // Some clients may call GetSVal with such an option simply because |
| 169 | // they are doing a quick scan through their Locs (potentially to |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 170 | // invalidate their bindings). Just return Undefined. |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 171 | return UndefinedVal(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 172 | case loc::FuncValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 173 | return LV; |
| 174 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 175 | case loc::StringLiteralValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 176 | // FIXME: Implement better support for fetching characters from strings. |
| 177 | return UnknownVal(); |
| 178 | |
| 179 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 180 | assert (false && "Invalid Loc."); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | |
| 184 | return UnknownVal(); |
| 185 | } |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 186 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 187 | Store BasicStoreManager::Bind(Store store, Loc LV, SVal V) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 188 | switch (LV.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 189 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 190 | const VarRegion* R = |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 191 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 192 | |
| 193 | if (!R) |
| 194 | return store; |
| 195 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 196 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 197 | return V.isUnknown() |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 198 | ? VBFactory.Remove(B, R->getDecl()).getRoot() |
| 199 | : VBFactory.Add(B, R->getDecl(), V).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 200 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 201 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 202 | assert ("SetSVal for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 203 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 207 | Store BasicStoreManager::Remove(Store store, Loc LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 208 | switch (LV.getSubKind()) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 209 | case loc::MemRegionKind: { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 210 | const VarRegion* R = |
| 211 | dyn_cast<VarRegion>(cast<loc::MemRegionVal>(LV).getRegion()); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 212 | |
| 213 | if (!R) |
| 214 | return store; |
| 215 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 216 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 217 | return VBFactory.Remove(B,R->getDecl()).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 218 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 219 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 220 | assert ("Remove for given Loc type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 221 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 225 | Store |
| 226 | BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 227 | const LiveVariables& Liveness, |
| 228 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 229 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 230 | |
| 231 | VarBindingsTy B = GetVarBindings(store); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 232 | typedef SVal::symbol_iterator symbol_iterator; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 233 | |
| 234 | // Iterate over the variable bindings. |
| 235 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) |
| 236 | if (Liveness.isLive(Loc, I.getKey())) { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 237 | RegionRoots.push_back(MRMgr.getVarRegion(I.getKey())); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 238 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 239 | |
| 240 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 241 | LSymbols.insert(*SI); |
| 242 | } |
| 243 | |
| 244 | // Scan for live variables and live symbols. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 245 | llvm::SmallPtrSet<const VarRegion*, 10> Marked; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 247 | while (!RegionRoots.empty()) { |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 248 | const MemRegion* MR = RegionRoots.back(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 249 | RegionRoots.pop_back(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 251 | while (MR) { |
| 252 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
| 253 | LSymbols.insert(SymR->getSymbol()); |
| 254 | break; |
| 255 | } |
| 256 | else if (const VarRegion* R = dyn_cast<VarRegion>(MR)) { |
| 257 | if (Marked.count(R)) |
| 258 | break; |
| 259 | |
| 260 | Marked.insert(R); |
| 261 | SVal X = GetRegionSVal(store, R); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 263 | // FIXME: We need to handle symbols nested in region definitions. |
| 264 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 265 | LSymbols.insert(*SI); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 267 | if (!isa<loc::MemRegionVal>(X)) |
| 268 | break; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 134e749 | 2008-10-17 22:52:40 +0000 | [diff] [blame] | 270 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 271 | RegionRoots.push_back(LVD.getRegion()); |
| 272 | break; |
| 273 | } |
| 274 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 275 | MR = R->getSuperRegion(); |
| 276 | else |
| 277 | break; |
| 278 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Remove dead variable bindings. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 282 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 283 | const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey())); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 284 | |
| 285 | if (!Marked.count(R)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 286 | store = Remove(store, loc::MemRegionVal(R)); |
| 287 | SVal X = I.getData(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 288 | |
| 289 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 290 | if (!LSymbols.count(*SI)) DSymbols.insert(*SI); |
| 291 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 294 | return store; |
| 295 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 296 | |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 297 | Store BasicStoreManager::getInitialStore() { |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 298 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 299 | // The LiveVariables information already has a compilation of all VarDecls |
| 300 | // used in the function. Iterate through this set, and "symbolicate" |
| 301 | // any VarDecl whose value originally comes from outside the function. |
| 302 | |
| 303 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 304 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 305 | |
| 306 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 307 | |
| 308 | 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] | 309 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 9deb0e3 | 2008-10-24 20:32:16 +0000 | [diff] [blame^] | 311 | // Handle implicit parameters. |
| 312 | if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) { |
| 313 | const Decl& CD = StateMgr.getCodeDecl(); |
| 314 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 315 | if (MD->getSelfDecl() == PD) { |
| 316 | // Create a region for "self". |
| 317 | assert (SelfRegion == 0); |
| 318 | SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(), |
| 319 | MRMgr.getHeapRegion()); |
| 320 | |
| 321 | St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(PD)), |
| 322 | loc::MemRegionVal(SelfRegion)); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 327 | // Punt on static variables for now. |
| 328 | if (VD->getStorageClass() == VarDecl::Static) |
| 329 | continue; |
| 330 | |
| 331 | // Only handle pointers and integers for now. |
| 332 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 333 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 334 | // Initialize globals and parameters to symbolic values. |
| 335 | // Initialize local variables to undefined. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 336 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 337 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 338 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 339 | : UndefinedVal(); |
| 340 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 341 | St = Bind(St, loc::MemRegionVal(MRMgr.getVarRegion(VD)), X); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
| 345 | return St; |
| 346 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 347 | |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 348 | Store BasicStoreManager::AddDecl(Store store, |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 349 | const VarDecl* VD, Expr* Ex, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 350 | SVal InitVal, unsigned Count) { |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 351 | |
| 352 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 353 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 354 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 355 | // BasicStore does not model arrays and structs. |
| 356 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 357 | return store; |
| 358 | |
| 359 | if (VD->hasGlobalStorage()) { |
| 360 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 361 | |
| 362 | // FIXME:: static variables may have an initializer, but the second time a |
| 363 | // function is called those values may not be current. Currently, a function |
| 364 | // will not be called more than once. |
| 365 | |
| 366 | // Static global variables should not be visited here. |
| 367 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 368 | VD->isFileVarDecl())); |
| 369 | |
| 370 | // Process static variables. |
| 371 | if (VD->getStorageClass() == VarDecl::Static) { |
| 372 | // C99: 6.7.8 Initialization |
| 373 | // If an object that has static storage duration is not initialized |
| 374 | // explicitly, then: |
| 375 | // —if it has pointer type, it is initialized to a null pointer; |
| 376 | // —if it has arithmetic type, it is initialized to (positive or |
| 377 | // unsigned) zero; |
| 378 | if (!Ex) { |
| 379 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 380 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 381 | store = Bind(store, getLoc(VD), |
| 382 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 383 | else if (T->isIntegerType()) |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 384 | store = Bind(store, getLoc(VD), |
| 385 | nonloc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 386 | else { |
| 387 | // assert(0 && "ignore other types of variables"); |
| 388 | } |
| 389 | } else { |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 390 | store = Bind(store, getLoc(VD), InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } else { |
| 394 | // Process local scalar variables. |
| 395 | QualType T = VD->getType(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 396 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 397 | SVal V = Ex ? InitVal : UndefinedVal(); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 398 | |
| 399 | if (Ex && InitVal.isUnknown()) { |
| 400 | // EXPERIMENTAL: "Conjured" symbols. |
| 401 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 402 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 403 | V = Loc::IsLocType(Ex->getType()) |
| 404 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 405 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Zhongxing Xu | 8485ec6 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 408 | store = Bind(store, getLoc(VD), V); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | return store; |
| 413 | } |
| 414 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 415 | void BasicStoreManager::print(Store store, std::ostream& Out, |
| 416 | const char* nl, const char *sep) { |
| 417 | |
| 418 | VarBindingsTy B = GetVarBindings(store); |
| 419 | Out << "Variables:" << nl; |
| 420 | |
| 421 | bool isFirst = true; |
| 422 | |
| 423 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
| 424 | if (isFirst) isFirst = false; |
| 425 | else Out << nl; |
| 426 | |
| 427 | Out << ' ' << I.getKey()->getName() << " : "; |
| 428 | I.getData().print(Out); |
| 429 | } |
| 430 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 431 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 432 | |
| 433 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
| 434 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 436 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 437 | |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 438 | f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData()); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 442 | StoreManager::BindingsHandler::~BindingsHandler() {} |