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