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 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 22 | typedef llvm::ImmutableMap<const VarDecl*,RVal> 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 | |
| 37 | virtual RVal GetRVal(Store St, LVal LV, QualType T); |
| 38 | virtual Store SetRVal(Store St, LVal LV, RVal V); |
| 39 | virtual Store Remove(Store St, LVal LV); |
| 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 | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 46 | virtual LVal getLVal(const VarDecl* VD) { |
| 47 | return lval::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 48 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame^] | 49 | |
| 50 | RVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 51 | RVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, RVal Base); |
| 52 | RVal getLValueField(const GRState* St, const FieldDecl* D, RVal Base); |
| 53 | RVal getLValueElement(const GRState* St, RVal Base, RVal 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 | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 64 | RVal InitVal = UndefinedVal(), unsigned Count = 0); |
| 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 | } |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame^] | 81 | RVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 82 | QualType T = VD->getType(); |
| 83 | assert(!T->isArrayType() && "Array and struct variable have no lvalue."); |
| 84 | return lval::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 85 | } |
| 86 | |
| 87 | RVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 88 | RVal Base) { |
| 89 | return UnknownVal(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | RVal BasicStoreManager::getLValueField(const GRState* St, const FieldDecl* D, |
| 94 | RVal Base) { |
| 95 | return UnknownVal(); |
| 96 | } |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame^] | 98 | RVal BasicStoreManager::getLValueElement(const GRState* St, RVal Base, |
| 99 | RVal Offset) { |
| 100 | return UnknownVal(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 103 | RVal BasicStoreManager::GetRVal(Store St, LVal LV, QualType T) { |
| 104 | |
| 105 | if (isa<UnknownVal>(LV)) |
| 106 | return UnknownVal(); |
| 107 | |
| 108 | assert (!isa<UndefinedVal>(LV)); |
| 109 | |
| 110 | switch (LV.getSubKind()) { |
| 111 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 112 | case lval::MemRegionKind: { |
| 113 | VarRegion* R = |
| 114 | dyn_cast<VarRegion>(cast<lval::MemRegionVal>(LV).getRegion()); |
| 115 | |
| 116 | if (!R) |
| 117 | return UnknownVal(); |
| 118 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 119 | VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 120 | VarBindingsTy::data_type* T = B.lookup(R->getDecl()); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 121 | return T ? *T : UnknownVal(); |
| 122 | } |
| 123 | |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 124 | case lval::SymbolValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 125 | return UnknownVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 126 | |
| 127 | case lval::ConcreteIntKind: |
| 128 | // Some clients may call GetRVal with such an option simply because |
| 129 | // they are doing a quick scan through their LVals (potentially to |
| 130 | // invalidate their bindings). Just return Undefined. |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame^] | 131 | return UndefinedVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 132 | case lval::FuncValKind: |
| 133 | return LV; |
| 134 | |
| 135 | case lval::StringLiteralValKind: |
| 136 | // FIXME: Implement better support for fetching characters from strings. |
| 137 | return UnknownVal(); |
| 138 | |
| 139 | default: |
| 140 | assert (false && "Invalid LVal."); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | return UnknownVal(); |
| 145 | } |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 147 | Store BasicStoreManager::SetRVal(Store store, LVal LV, RVal V) { |
| 148 | switch (LV.getSubKind()) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 149 | case lval::MemRegionKind: { |
| 150 | VarRegion* R = |
| 151 | dyn_cast<VarRegion>(cast<lval::MemRegionVal>(LV).getRegion()); |
| 152 | |
| 153 | if (!R) |
| 154 | return store; |
| 155 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 156 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 157 | return V.isUnknown() |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 158 | ? VBFactory.Remove(B, R->getDecl()).getRoot() |
| 159 | : VBFactory.Add(B, R->getDecl(), V).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 160 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 161 | default: |
| 162 | assert ("SetRVal for given LVal type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 163 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 167 | Store BasicStoreManager::Remove(Store store, LVal LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 168 | switch (LV.getSubKind()) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 169 | case lval::MemRegionKind: { |
| 170 | VarRegion* R = |
| 171 | dyn_cast<VarRegion>(cast<lval::MemRegionVal>(LV).getRegion()); |
| 172 | |
| 173 | if (!R) |
| 174 | return store; |
| 175 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 176 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 177 | return VBFactory.Remove(B,R->getDecl()).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 178 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 179 | default: |
| 180 | assert ("Remove for given LVal type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 181 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 185 | Store |
| 186 | BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 187 | const LiveVariables& Liveness, |
| 188 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 189 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 190 | |
| 191 | VarBindingsTy B = GetVarBindings(store); |
| 192 | typedef RVal::symbol_iterator symbol_iterator; |
| 193 | |
| 194 | // Iterate over the variable bindings. |
| 195 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) |
| 196 | if (Liveness.isLive(Loc, I.getKey())) { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 197 | RegionRoots.push_back(MRMgr.getVarRegion(I.getKey())); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 198 | RVal X = I.getData(); |
| 199 | |
| 200 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 201 | LSymbols.insert(*SI); |
| 202 | } |
| 203 | |
| 204 | // Scan for live variables and live symbols. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 205 | llvm::SmallPtrSet<const VarRegion*, 10> Marked; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 207 | while (!RegionRoots.empty()) { |
| 208 | const VarRegion* R = cast<VarRegion>(RegionRoots.back()); |
| 209 | RegionRoots.pop_back(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 211 | if (Marked.count(R)) |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 212 | continue; |
| 213 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 214 | Marked.insert(R); |
| 215 | // FIXME: Do we need the QualType here, since regions are partially |
| 216 | // typed? |
| 217 | RVal X = GetRVal(store, lval::MemRegionVal(R), QualType()); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 218 | |
| 219 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 220 | LSymbols.insert(*SI); |
| 221 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 222 | if (!isa<lval::MemRegionVal>(X)) |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 223 | continue; |
| 224 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 225 | const lval::MemRegionVal& LVD = cast<lval::MemRegionVal>(X); |
| 226 | RegionRoots.push_back(cast<VarRegion>(LVD.getRegion())); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Remove dead variable bindings. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 230 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 231 | const VarRegion* R = cast<VarRegion>(MRMgr.getVarRegion(I.getKey())); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 232 | |
| 233 | if (!Marked.count(R)) { |
| 234 | store = Remove(store, lval::MemRegionVal(R)); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 235 | RVal X = I.getData(); |
| 236 | |
| 237 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 238 | if (!LSymbols.count(*SI)) DSymbols.insert(*SI); |
| 239 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 242 | return store; |
| 243 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 244 | |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 245 | Store BasicStoreManager::getInitialStore() { |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 246 | // The LiveVariables information already has a compilation of all VarDecls |
| 247 | // used in the function. Iterate through this set, and "symbolicate" |
| 248 | // any VarDecl whose value originally comes from outside the function. |
| 249 | |
| 250 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 251 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 252 | |
| 253 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 254 | |
| 255 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
| 256 | ScopedDecl* SD = const_cast<ScopedDecl*>(I->first); |
| 257 | |
| 258 | if (VarDecl* VD = dyn_cast<VarDecl>(SD)) { |
| 259 | // Punt on static variables for now. |
| 260 | if (VD->getStorageClass() == VarDecl::Static) |
| 261 | continue; |
| 262 | |
| 263 | // Only handle pointers and integers for now. |
| 264 | QualType T = VD->getType(); |
| 265 | if (LVal::IsLValType(T) || T->isIntegerType()) { |
| 266 | // Initialize globals and parameters to symbolic values. |
| 267 | // Initialize local variables to undefined. |
| 268 | RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
| 269 | isa<ImplicitParamDecl>(VD)) |
| 270 | ? RVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
| 271 | : UndefinedVal(); |
| 272 | |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 273 | St = SetRVal(St, lval::MemRegionVal(MRMgr.getVarRegion(VD)), X); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | return St; |
| 278 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 279 | |
Zhongxing Xu | c1d1bbf | 2008-10-05 12:12:48 +0000 | [diff] [blame] | 280 | Store BasicStoreManager::AddDecl(Store store, |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 281 | const VarDecl* VD, Expr* Ex, |
| 282 | RVal InitVal, unsigned Count) { |
| 283 | |
| 284 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 285 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 286 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 287 | // BasicStore does not model arrays and structs. |
| 288 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 289 | return store; |
| 290 | |
| 291 | if (VD->hasGlobalStorage()) { |
| 292 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 293 | |
| 294 | // FIXME:: static variables may have an initializer, but the second time a |
| 295 | // function is called those values may not be current. Currently, a function |
| 296 | // will not be called more than once. |
| 297 | |
| 298 | // Static global variables should not be visited here. |
| 299 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 300 | VD->isFileVarDecl())); |
| 301 | |
| 302 | // Process static variables. |
| 303 | if (VD->getStorageClass() == VarDecl::Static) { |
| 304 | // C99: 6.7.8 Initialization |
| 305 | // If an object that has static storage duration is not initialized |
| 306 | // explicitly, then: |
| 307 | // —if it has pointer type, it is initialized to a null pointer; |
| 308 | // —if it has arithmetic type, it is initialized to (positive or |
| 309 | // unsigned) zero; |
| 310 | if (!Ex) { |
| 311 | QualType T = VD->getType(); |
| 312 | if (LVal::IsLValType(T)) |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 313 | store = SetRVal(store, getLVal(VD), |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 314 | lval::ConcreteInt(BasicVals.getValue(0, T))); |
| 315 | else if (T->isIntegerType()) |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 316 | store = SetRVal(store, getLVal(VD), |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 317 | nonlval::ConcreteInt(BasicVals.getValue(0, T))); |
| 318 | else { |
| 319 | // assert(0 && "ignore other types of variables"); |
| 320 | } |
| 321 | } else { |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 322 | store = SetRVal(store, getLVal(VD), InitVal); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } else { |
| 326 | // Process local scalar variables. |
| 327 | QualType T = VD->getType(); |
| 328 | if (LVal::IsLValType(T) || T->isIntegerType()) { |
| 329 | RVal V = Ex ? InitVal : UndefinedVal(); |
| 330 | |
| 331 | if (Ex && InitVal.isUnknown()) { |
| 332 | // EXPERIMENTAL: "Conjured" symbols. |
| 333 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 334 | |
| 335 | V = LVal::IsLValType(Ex->getType()) |
| 336 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 337 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 338 | } |
| 339 | |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 340 | store = SetRVal(store, getLVal(VD), V); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | return store; |
| 345 | } |
| 346 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 347 | void BasicStoreManager::print(Store store, std::ostream& Out, |
| 348 | const char* nl, const char *sep) { |
| 349 | |
| 350 | VarBindingsTy B = GetVarBindings(store); |
| 351 | Out << "Variables:" << nl; |
| 352 | |
| 353 | bool isFirst = true; |
| 354 | |
| 355 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
| 356 | if (isFirst) isFirst = false; |
| 357 | else Out << nl; |
| 358 | |
| 359 | Out << ' ' << I.getKey()->getName() << " : "; |
| 360 | I.getData().print(Out); |
| 361 | } |
| 362 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 364 | |
| 365 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
| 366 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 368 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 369 | |
Zhongxing Xu | bc678fd | 2008-10-07 01:31:04 +0000 | [diff] [blame] | 370 | f.HandleBinding(*this, store, MRMgr.getVarRegion(I.getKey()),I.getData()); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Ted Kremenek | 60dbad8 | 2008-09-03 03:06:11 +0000 | [diff] [blame] | 374 | StoreManager::BindingsHandler::~BindingsHandler() {} |