blob: b09c49ec09350616a7bca2cb2c3cacd4618def1f [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- 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 defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
19#include "clang/Analysis/Analyses/LiveVariables.h"
20
21#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000022#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000023#include "llvm/Support/Compiler.h"
24
25using namespace clang;
26
Zhongxing Xu1c96b242008-10-17 05:57:07 +000027typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu17892752008-10-08 02:50:44 +000028
29namespace {
30
31class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
32 RegionBindingsTy::Factory RBFactory;
33 GRStateManager& StateMgr;
34 MemRegionManager MRMgr;
35
36public:
37 RegionStoreManager(GRStateManager& mgr)
38 : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {}
39
40 virtual ~RegionStoreManager() {}
41
Zhongxing Xu24194ef2008-10-24 01:38:55 +000042 MemRegionManager& getRegionManager() { return MRMgr; }
43
44 // FIXME: Is this function necessary?
45 SVal GetRegionSVal(Store St, const MemRegion* R) {
46 return Retrieve(St, loc::MemRegionVal(R));
47 }
Ted Kremenek4f090272008-10-27 21:54:31 +000048
Zhongxing Xuf22679e2008-11-07 10:38:33 +000049 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000050
Zhongxing Xu143bf822008-10-25 14:18:57 +000051 SVal getLValueString(const GRState* St, const StringLiteral* S);
52
Zhongxing Xuf22679e2008-11-07 10:38:33 +000053 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
54
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000055 SVal getLValueVar(const GRState* St, const VarDecl* VD);
56
57 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
58
59 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
60
Zhongxing Xub1d542a2008-10-24 01:09:32 +000061 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
62
63 SVal ArrayToPointer(SVal Array);
64
Zhongxing Xu24194ef2008-10-24 01:38:55 +000065 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000066
Zhongxing Xu8485ec62008-10-21 06:27:32 +000067 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000068
Zhongxing Xu24194ef2008-10-24 01:38:55 +000069 Store Remove(Store store, Loc LV) {
70 // FIXME: Implement.
71 return store;
72 }
73
Zhongxing Xu17892752008-10-08 02:50:44 +000074 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +000075
76 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
77 /// 'this' object (C++). When used when analyzing a normal function this
78 /// method returns NULL.
79 const MemRegion* getSelfRegion(Store) {
80 assert (false && "Not implemented.");
81 return 0;
82 }
Zhongxing Xu17892752008-10-08 02:50:44 +000083
Zhongxing Xu24194ef2008-10-24 01:38:55 +000084 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
85 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +000086 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000087
Ted Kremenek42577d12008-11-12 19:18:35 +000088 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +000089
Zhongxing Xu17892752008-10-08 02:50:44 +000090 static inline RegionBindingsTy GetRegionBindings(Store store) {
91 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
92 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +000093
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +000094 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000095
96 void iterBindings(Store store, BindingsHandler& f) {
97 // FIXME: Implement.
98 }
Zhongxing Xua82512a2008-10-24 08:42:28 +000099
100private:
101 Loc getVarLoc(const VarDecl* VD) {
102 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
103 }
104
Zhongxing Xud463d442008-11-02 12:13:30 +0000105 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
106 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
107 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
108 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000109
110 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000111 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000112 // Utility methods.
113 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
114 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000115};
116
117} // end anonymous namespace
118
Ted Kremenek95c7b002008-10-24 01:04:59 +0000119StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000120 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000121}
122
Zhongxing Xu143bf822008-10-25 14:18:57 +0000123SVal RegionStoreManager::getLValueString(const GRState* St,
124 const StringLiteral* S) {
125 return loc::MemRegionVal(MRMgr.getStringRegion(S));
126}
127
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000128SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
129 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
130}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000131
132SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
133 const CompoundLiteralExpr* CL) {
134 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
135}
136
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000137SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
138 SVal Base) {
139 return UnknownVal();
140}
141
142SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
143 const FieldDecl* D) {
144 if (Base.isUnknownOrUndef())
145 return Base;
146
147 Loc BaseL = cast<Loc>(Base);
148 const MemRegion* BaseR = 0;
149
150 switch (BaseL.getSubKind()) {
151 case loc::MemRegionKind:
152 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
153 break;
154
155 case loc::SymbolValKind:
156 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
157 break;
158
159 case loc::GotoLabelKind:
160 case loc::FuncValKind:
161 // These are anormal cases. Flag an undefined value.
162 return UndefinedVal();
163
164 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000165 // While these seem funny, this can happen through casts.
166 // FIXME: What we should return is the field offset. For example,
167 // add the field offset to the integer value. That way funny things
168 // like this work properly: &(((struct foo *) 0xa)->f)
169 return Base;
170
171 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000172 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000173 return Base;
174 }
175
176 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
177}
178
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000179SVal RegionStoreManager::getLValueElement(const GRState* St,
180 SVal Base, SVal Offset) {
181 if (Base.isUnknownOrUndef())
182 return Base;
183
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000184 if (isa<loc::SymbolVal>(Base))
185 return Base;
186
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000187 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
188
Zhongxing Xue4d13932008-11-13 09:48:44 +0000189 // Pointer of any type can be cast and used as array base. We do not support
190 // that case yet.
191 if (!isa<ElementRegion>(BaseL.getRegion())) {
192 // Record what we have seen in real code.
193 assert(isa<FieldRegion>(BaseL.getRegion()));
194 return UnknownVal();
195 }
196
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000197 // We expect BaseR is an ElementRegion, not a base VarRegion.
198
199 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
200
201 SVal Idx = ElemR->getIndex();
202
203 nonloc::ConcreteInt *CI1, *CI2;
204
205 // Only handle integer indices for now.
206 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
207 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000208
209 // Temporary SVal to hold a potential signed APSInt.
210 SVal SignedInt;
211
212 // Index might be unsigned. We have to convert it to signed.
213 if (CI2->getValue().isUnsigned()) {
214 llvm::APSInt SI = CI2->getValue();
215 SI.setIsSigned(true);
216 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
217 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
218 }
219
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000220 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
221 *CI2);
222 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
223 ElemR->getSuperRegion()));
224 }
225
226 return UnknownVal();
227}
228
229// Cast 'pointer to array' to 'pointer to the first element of array'.
230
231SVal RegionStoreManager::ArrayToPointer(SVal Array) {
232 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000233 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
234
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000235 // FIXME: Find a better way to get bit width.
236 nonloc::ConcreteInt Idx(BasicVals.getValue(0, 32, false));
237 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
238
239 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000240}
241
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000242SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000243 assert(!isa<UnknownVal>(L) && "location unknown");
244 assert(!isa<UndefinedVal>(L) && "location undefined");
245
246 switch (L.getSubKind()) {
247 case loc::MemRegionKind: {
248 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
249 assert(R && "bad region");
250
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000251 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
252 if (TR->getType(getContext())->isStructureType())
253 return RetrieveStruct(S, TR);
254
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000255 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
256 RegionBindingsTy::data_type* V = B.lookup(R);
257 return V ? *V : UnknownVal();
258 }
259
260 case loc::SymbolValKind:
261 return UnknownVal();
262
263 case loc::ConcreteIntKind:
264 return UndefinedVal(); // As in BasicStoreManager.
265
266 case loc::FuncValKind:
267 return L;
268
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000269 default:
270 assert(false && "Invalid Location");
271 break;
272 }
273}
274
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000275SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
276 QualType T = R->getType(getContext());
277 assert(T->isStructureType());
278
279 const RecordType* RT = cast<RecordType>(T.getTypePtr());
280 RecordDecl* RD = RT->getDecl();
281 assert(RD->isDefinition());
282
283 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
284
285 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
286 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
287 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000288 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000289
290 SVal FieldValue = data ? *data : UnknownVal();
291
292 StructVal = getBasicVals().consVals(FieldValue, StructVal);
293 }
294
295 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
296}
297
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000298Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000299 if (LV.getSubKind() == loc::SymbolValKind)
300 return store;
301
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000302 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000303
Ted Kremenek993f1c72008-10-17 20:28:54 +0000304 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000305
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000306 assert(R);
307
308 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
309 if (TR->getType(getContext())->isStructureType())
310 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000311
312 RegionBindingsTy B = GetRegionBindings(store);
313 return V.isUnknown()
314 ? RBFactory.Remove(B, R).getRoot()
315 : RBFactory.Add(B, R, V).getRoot();
316}
317
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000318Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
319 QualType T = R->getType(getContext());
320 assert(T->isStructureType());
321
322 const RecordType* RT = cast<RecordType>(T.getTypePtr());
323 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000324
325 if (!RD->isDefinition()) {
326 // This can only occur when a pointer of imcomplete struct type is used as a
327 // function argument.
328 assert(V.isUnknown());
329 return store;
330 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000331
332 RegionBindingsTy B = GetRegionBindings(store);
333
Zhongxing Xud463d442008-11-02 12:13:30 +0000334 if (isa<UnknownVal>(V))
335 return BindStructToVal(store, R, UnknownVal());
336
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000337 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
338
339 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
340 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
341
342 for (; FI != FE; ++FI, ++VI) {
343 assert(VI != VE);
344
345 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
346
347 B = RBFactory.Add(B, FR, *VI);
348 }
349
350 return B.getRoot();
351}
352
Zhongxing Xu17892752008-10-08 02:50:44 +0000353Store RegionStoreManager::getInitialStore() {
354 typedef LiveVariables::AnalysisDataTy LVDataTy;
355 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
356
357 Store St = RBFactory.GetEmptyMap().getRoot();
358
359 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000360 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000361
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000362 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000363 // Punt on static variables for now.
364 if (VD->getStorageClass() == VarDecl::Static)
365 continue;
366
367 QualType T = VD->getType();
368 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000369 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000370 // Initialize globals and parameters to symbolic values.
371 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000372 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000373 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000374 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000375 : UndefinedVal();
376
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000377 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu17892752008-10-08 02:50:44 +0000378 }
379 }
380 }
381 return St;
382}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000383
Ted Kremenek42577d12008-11-12 19:18:35 +0000384Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
385 SVal* InitVal, unsigned Count) {
386
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000387 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000388
389 if (VD->hasGlobalStorage()) {
390 // Static global variables should not be visited here.
391 assert(!(VD->getStorageClass() == VarDecl::Static &&
392 VD->isFileVarDecl()));
393 // Process static variables.
394 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000395 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000396 // Only handle pointer and integer static variables.
397
398 QualType T = VD->getType();
399
400 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000401 store = Bind(store, getVarLoc(VD),
402 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000403
404 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000405 store = Bind(store, getVarLoc(VD),
406 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000407
408 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000409 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000410 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000411 }
412 }
413 } else {
414 // Process local variables.
415
416 QualType T = VD->getType();
417
Zhongxing Xua82512a2008-10-24 08:42:28 +0000418 VarRegion* VR = MRMgr.getVarRegion(VD);
419
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000420 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000421 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000422 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000423 }
424 else if (T->isArrayType()) {
425 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000426 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000427 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000428 store = InitializeArray(store, VR, *InitVal);
429 }
430 else if (T->isStructureType()) {
431 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000432 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000433 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000434 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000435 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000436
437 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000438 }
439 return store;
440}
441
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000442Store RegionStoreManager::BindCompoundLiteral(Store store,
443 const CompoundLiteralExpr* CL,
444 SVal V) {
445 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
446 store = Bind(store, loc::MemRegionVal(R), V);
447 return store;
448}
449
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000450Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
451 const LiveVariables& Live,
452 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
453 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
454
455 RegionBindingsTy B = GetRegionBindings(store);
456 typedef SVal::symbol_iterator symbol_iterator;
457
458 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
459 // in SymbolicRegions.
460 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
461 SVal X = I.getData();
462 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
463 LSymbols.insert(*SI);
464 }
465
466 return store;
467}
468
Zhongxing Xua071eb02008-10-24 06:01:33 +0000469void RegionStoreManager::print(Store store, std::ostream& Out,
470 const char* nl, const char *sep) {
471 llvm::raw_os_ostream OS(Out);
472 RegionBindingsTy B = GetRegionBindings(store);
473 OS << "Store:" << nl;
474
475 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
476 OS << ' '; I.getKey()->print(OS); OS << " : ";
477 I.getData().print(OS); OS << nl;
478 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000479}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000480
Zhongxing Xud463d442008-11-02 12:13:30 +0000481Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000482 SVal Init) {
483 QualType T = R->getType(getContext());
484 assert(T->isArrayType());
485
486 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
487
488 llvm::APInt Size = CAT->getSize();
489
490 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
491
492 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
493
494 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
495
496 for (; i != Size; ++i) {
497 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
498
499 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
500
501 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
502 // The init list might be shorter than the array decl.
503 if (VI != VE) ++VI;
504 }
505
506 return store;
507}
508
Zhongxing Xud463d442008-11-02 12:13:30 +0000509// Bind all elements of the array to some value.
510Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
511 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000512 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000513 assert(T->isArrayType());
514
Zhongxing Xua82512a2008-10-24 08:42:28 +0000515 // Only handle constant size array for now.
516 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
517
518 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000519 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
520 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000521 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000522
523 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
524
Zhongxing Xud463d442008-11-02 12:13:30 +0000525 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000526 }
527 }
528
529 return store;
530}
531
Zhongxing Xud463d442008-11-02 12:13:30 +0000532Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000533 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000534 QualType T = R->getType(getContext());
535 assert(T->isStructureType());
536
537 RecordType* RT = cast<RecordType>(T.getTypePtr());
538 RecordDecl* RD = RT->getDecl();
539 assert(RD->isDefinition());
540
541 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
542 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
543 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
544
545 for (; FI != FE; ++FI) {
546 QualType FTy = (*FI)->getType();
547 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
548
549 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
550 if (VI != VE) {
551 store = Bind(store, loc::MemRegionVal(FR), *VI);
552 ++VI;
553 } else
554 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
555 }
556 else if (FTy->isArrayType()) {
557 if (VI != VE) {
558 store = InitializeArray(store, FR, *VI);
559 ++VI;
560 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000561 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000562 }
563 else if (FTy->isStructureType()) {
564 if (VI != VE) {
565 store = InitializeStruct(store, FR, *VI);
566 ++VI;
567 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000568 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000569 }
570 }
571 return store;
572}
573
Zhongxing Xud463d442008-11-02 12:13:30 +0000574// Bind all fields of the struct to some value.
575Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
576 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000577 QualType T = BaseR->getType(getContext());
578 assert(T->isStructureType());
579
580 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000581 RecordDecl* RD = RT->getDecl();
582 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000583
584 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
585
586 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000587
588 QualType FTy = (*I)->getType();
589 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
590
591 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000592 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000593
594 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000595 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000596
597 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000598 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000599 }
600 }
601
602 return store;
603}