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