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