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