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