blob: 732785c0f7201e678143728bd673352d7f8a1860 [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"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xu1c96b242008-10-17 05:57:07 +000029typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000030typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
31typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
32
33static int RegionViewMapTyIndex = 0;
34
35namespace clang {
36template<> struct GRStateTrait<RegionViewMapTy>
37 : public GRStatePartialTrait<RegionViewMapTy> {
38 static void* GDMIndex() { return &RegionViewMapTyIndex; }
39};
40}
Zhongxing Xu17892752008-10-08 02:50:44 +000041
42namespace {
43
44class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
45 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000046 RegionViewTy::Factory RVFactory;
47 RegionViewMapTy::Factory RVMFactory;
48
Zhongxing Xu17892752008-10-08 02:50:44 +000049 GRStateManager& StateMgr;
50 MemRegionManager MRMgr;
51
52public:
53 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000054 : RBFactory(mgr.getAllocator()),
55 RVFactory(mgr.getAllocator()),
56 RVMFactory(mgr.getAllocator()),
57 StateMgr(mgr),
58 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000059
60 virtual ~RegionStoreManager() {}
61
Zhongxing Xu24194ef2008-10-24 01:38:55 +000062 MemRegionManager& getRegionManager() { return MRMgr; }
63
64 // FIXME: Is this function necessary?
65 SVal GetRegionSVal(Store St, const MemRegion* R) {
66 return Retrieve(St, loc::MemRegionVal(R));
67 }
Ted Kremenek4f090272008-10-27 21:54:31 +000068
Zhongxing Xuf22679e2008-11-07 10:38:33 +000069 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000070
Zhongxing Xu143bf822008-10-25 14:18:57 +000071 SVal getLValueString(const GRState* St, const StringLiteral* S);
72
Zhongxing Xuf22679e2008-11-07 10:38:33 +000073 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
74
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000075 SVal getLValueVar(const GRState* St, const VarDecl* VD);
76
77 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
78
79 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
80
Zhongxing Xub1d542a2008-10-24 01:09:32 +000081 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
82
83 SVal ArrayToPointer(SVal Array);
84
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000085 const GRState* CastRegion(const GRState* St, SVal VoidPtr,
86 QualType CastToTy, Stmt* CastE);
87
Zhongxing Xu24194ef2008-10-24 01:38:55 +000088 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000089
Zhongxing Xu8485ec62008-10-21 06:27:32 +000090 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000091
Zhongxing Xu24194ef2008-10-24 01:38:55 +000092 Store Remove(Store store, Loc LV) {
93 // FIXME: Implement.
94 return store;
95 }
96
Zhongxing Xu17892752008-10-08 02:50:44 +000097 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +000098
99 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
100 /// 'this' object (C++). When used when analyzing a normal function this
101 /// method returns NULL.
102 const MemRegion* getSelfRegion(Store) {
103 assert (false && "Not implemented.");
104 return 0;
105 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000106
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000107 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
108 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000109 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000110
Ted Kremenek42577d12008-11-12 19:18:35 +0000111 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000112
Zhongxing Xu17892752008-10-08 02:50:44 +0000113 static inline RegionBindingsTy GetRegionBindings(Store store) {
114 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
115 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000116
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000117 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000118
119 void iterBindings(Store store, BindingsHandler& f) {
120 // FIXME: Implement.
121 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000122
123private:
124 Loc getVarLoc(const VarDecl* VD) {
125 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
126 }
127
Zhongxing Xud463d442008-11-02 12:13:30 +0000128 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
129 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
130 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
131 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000132
133 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000134 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000135 // Utility methods.
136 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
137 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000138
139 const GRState* AddRegionView(const GRState* St,
140 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000141};
142
143} // end anonymous namespace
144
Ted Kremenek95c7b002008-10-24 01:04:59 +0000145StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000146 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000147}
148
Zhongxing Xu143bf822008-10-25 14:18:57 +0000149SVal RegionStoreManager::getLValueString(const GRState* St,
150 const StringLiteral* S) {
151 return loc::MemRegionVal(MRMgr.getStringRegion(S));
152}
153
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000154SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
155 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
156}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000157
158SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
159 const CompoundLiteralExpr* CL) {
160 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
161}
162
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000163SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
164 SVal Base) {
165 return UnknownVal();
166}
167
168SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
169 const FieldDecl* D) {
170 if (Base.isUnknownOrUndef())
171 return Base;
172
173 Loc BaseL = cast<Loc>(Base);
174 const MemRegion* BaseR = 0;
175
176 switch (BaseL.getSubKind()) {
177 case loc::MemRegionKind:
178 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
179 break;
180
181 case loc::SymbolValKind:
182 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
183 break;
184
185 case loc::GotoLabelKind:
186 case loc::FuncValKind:
187 // These are anormal cases. Flag an undefined value.
188 return UndefinedVal();
189
190 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000191 // While these seem funny, this can happen through casts.
192 // FIXME: What we should return is the field offset. For example,
193 // add the field offset to the integer value. That way funny things
194 // like this work properly: &(((struct foo *) 0xa)->f)
195 return Base;
196
197 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000198 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000199 return Base;
200 }
201
202 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
203}
204
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000205SVal RegionStoreManager::getLValueElement(const GRState* St,
206 SVal Base, SVal Offset) {
207 if (Base.isUnknownOrUndef())
208 return Base;
209
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000210 if (isa<loc::SymbolVal>(Base))
211 return Base;
212
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000213 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
214
Zhongxing Xue4d13932008-11-13 09:48:44 +0000215 // Pointer of any type can be cast and used as array base. We do not support
216 // that case yet.
217 if (!isa<ElementRegion>(BaseL.getRegion())) {
218 // Record what we have seen in real code.
219 assert(isa<FieldRegion>(BaseL.getRegion()));
220 return UnknownVal();
221 }
222
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000223 // We expect BaseR is an ElementRegion, not a base VarRegion.
224
225 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
226
227 SVal Idx = ElemR->getIndex();
228
229 nonloc::ConcreteInt *CI1, *CI2;
230
231 // Only handle integer indices for now.
232 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
233 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000234
235 // Temporary SVal to hold a potential signed APSInt.
236 SVal SignedInt;
237
238 // Index might be unsigned. We have to convert it to signed.
239 if (CI2->getValue().isUnsigned()) {
240 llvm::APSInt SI = CI2->getValue();
241 SI.setIsSigned(true);
242 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
243 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
244 }
245
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000246 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
247 *CI2);
248 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
249 ElemR->getSuperRegion()));
250 }
251
252 return UnknownVal();
253}
254
255// Cast 'pointer to array' to 'pointer to the first element of array'.
256
257SVal RegionStoreManager::ArrayToPointer(SVal Array) {
258 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000259 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
260
Zhongxing Xua09300a2008-11-15 05:18:50 +0000261 nonloc::ConcreteInt Idx(BasicVals.getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000262 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
263
264 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000265}
266
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000267const GRState* RegionStoreManager::CastRegion(const GRState* St,
268 SVal VoidPtr,
269 QualType CastToTy,
270 Stmt* CastE) {
271 if (const AllocaRegion* AR =
272 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
273
274 // Create a new region to attach type information to it.
275 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
276
277 // Get the pointer to the first element.
278 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
279 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
280
281 St = StateMgr.BindExpr(St, CastE, loc::MemRegionVal(ER));
282
283 // Add a RegionView to base region.
284 return AddRegionView(St, TR, AR);
285 }
286
287 // Default case.
288 return St;
289}
290
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000291SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000292 assert(!isa<UnknownVal>(L) && "location unknown");
293 assert(!isa<UndefinedVal>(L) && "location undefined");
294
295 switch (L.getSubKind()) {
296 case loc::MemRegionKind: {
297 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
298 assert(R && "bad region");
299
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000300 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
301 if (TR->getType(getContext())->isStructureType())
302 return RetrieveStruct(S, TR);
303
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000304 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
305 RegionBindingsTy::data_type* V = B.lookup(R);
306 return V ? *V : UnknownVal();
307 }
308
309 case loc::SymbolValKind:
310 return UnknownVal();
311
312 case loc::ConcreteIntKind:
313 return UndefinedVal(); // As in BasicStoreManager.
314
315 case loc::FuncValKind:
316 return L;
317
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000318 default:
319 assert(false && "Invalid Location");
320 break;
321 }
322}
323
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000324SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
325 QualType T = R->getType(getContext());
326 assert(T->isStructureType());
327
328 const RecordType* RT = cast<RecordType>(T.getTypePtr());
329 RecordDecl* RD = RT->getDecl();
330 assert(RD->isDefinition());
331
332 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
333
334 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
335 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
336 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000337 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000338
339 SVal FieldValue = data ? *data : UnknownVal();
340
341 StructVal = getBasicVals().consVals(FieldValue, StructVal);
342 }
343
344 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
345}
346
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000347Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000348 if (LV.getSubKind() == loc::SymbolValKind)
349 return store;
350
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000351 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000352
Ted Kremenek993f1c72008-10-17 20:28:54 +0000353 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000354
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000355 assert(R);
356
357 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
358 if (TR->getType(getContext())->isStructureType())
359 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000360
361 RegionBindingsTy B = GetRegionBindings(store);
362 return V.isUnknown()
363 ? RBFactory.Remove(B, R).getRoot()
364 : RBFactory.Add(B, R, V).getRoot();
365}
366
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000367Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
368 QualType T = R->getType(getContext());
369 assert(T->isStructureType());
370
371 const RecordType* RT = cast<RecordType>(T.getTypePtr());
372 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000373
374 if (!RD->isDefinition()) {
375 // This can only occur when a pointer of imcomplete struct type is used as a
376 // function argument.
377 assert(V.isUnknown());
378 return store;
379 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000380
381 RegionBindingsTy B = GetRegionBindings(store);
382
Zhongxing Xud463d442008-11-02 12:13:30 +0000383 if (isa<UnknownVal>(V))
384 return BindStructToVal(store, R, UnknownVal());
385
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000386 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
387
388 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
389 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
390
391 for (; FI != FE; ++FI, ++VI) {
392 assert(VI != VE);
393
394 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
395
396 B = RBFactory.Add(B, FR, *VI);
397 }
398
399 return B.getRoot();
400}
401
Zhongxing Xu17892752008-10-08 02:50:44 +0000402Store RegionStoreManager::getInitialStore() {
403 typedef LiveVariables::AnalysisDataTy LVDataTy;
404 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
405
406 Store St = RBFactory.GetEmptyMap().getRoot();
407
408 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000409 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000410
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000411 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000412 // Punt on static variables for now.
413 if (VD->getStorageClass() == VarDecl::Static)
414 continue;
415
416 QualType T = VD->getType();
417 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000418 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000419 // Initialize globals and parameters to symbolic values.
420 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000421 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000422 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000423 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000424 : UndefinedVal();
425
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000426 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu17892752008-10-08 02:50:44 +0000427 }
428 }
429 }
430 return St;
431}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000432
Ted Kremenek42577d12008-11-12 19:18:35 +0000433Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
434 SVal* InitVal, unsigned Count) {
435
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000436 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000437
438 if (VD->hasGlobalStorage()) {
439 // Static global variables should not be visited here.
440 assert(!(VD->getStorageClass() == VarDecl::Static &&
441 VD->isFileVarDecl()));
442 // Process static variables.
443 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000444 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000445 // Only handle pointer and integer static variables.
446
447 QualType T = VD->getType();
448
449 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000450 store = Bind(store, getVarLoc(VD),
451 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000452
453 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000454 store = Bind(store, getVarLoc(VD),
455 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000456
457 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000458 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000459 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000460 }
461 }
462 } else {
463 // Process local variables.
464
465 QualType T = VD->getType();
466
Zhongxing Xua82512a2008-10-24 08:42:28 +0000467 VarRegion* VR = MRMgr.getVarRegion(VD);
468
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000469 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000470 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000471 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000472 }
473 else if (T->isArrayType()) {
474 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000475 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000476 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000477 store = InitializeArray(store, VR, *InitVal);
478 }
479 else if (T->isStructureType()) {
480 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000481 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000482 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000483 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000484 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000485
486 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000487 }
488 return store;
489}
490
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000491Store RegionStoreManager::BindCompoundLiteral(Store store,
492 const CompoundLiteralExpr* CL,
493 SVal V) {
494 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
495 store = Bind(store, loc::MemRegionVal(R), V);
496 return store;
497}
498
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000499Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
500 const LiveVariables& Live,
501 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
502 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
503
504 RegionBindingsTy B = GetRegionBindings(store);
505 typedef SVal::symbol_iterator symbol_iterator;
506
507 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
508 // in SymbolicRegions.
509 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
510 SVal X = I.getData();
511 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
512 LSymbols.insert(*SI);
513 }
514
515 return store;
516}
517
Zhongxing Xua071eb02008-10-24 06:01:33 +0000518void RegionStoreManager::print(Store store, std::ostream& Out,
519 const char* nl, const char *sep) {
520 llvm::raw_os_ostream OS(Out);
521 RegionBindingsTy B = GetRegionBindings(store);
522 OS << "Store:" << nl;
523
524 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
525 OS << ' '; I.getKey()->print(OS); OS << " : ";
526 I.getData().print(OS); OS << nl;
527 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000528}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000529
Zhongxing Xud463d442008-11-02 12:13:30 +0000530Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000531 SVal Init) {
532 QualType T = R->getType(getContext());
533 assert(T->isArrayType());
534
535 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
536
537 llvm::APInt Size = CAT->getSize();
538
539 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
540
541 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
542
543 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
544
545 for (; i != Size; ++i) {
546 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
547
548 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
549
550 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
551 // The init list might be shorter than the array decl.
552 if (VI != VE) ++VI;
553 }
554
555 return store;
556}
557
Zhongxing Xud463d442008-11-02 12:13:30 +0000558// Bind all elements of the array to some value.
559Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
560 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000561 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000562 assert(T->isArrayType());
563
Zhongxing Xua82512a2008-10-24 08:42:28 +0000564 // Only handle constant size array for now.
565 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
566
567 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000568 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
569 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000570 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000571
572 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
573
Zhongxing Xud463d442008-11-02 12:13:30 +0000574 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000575 }
576 }
577
578 return store;
579}
580
Zhongxing Xud463d442008-11-02 12:13:30 +0000581Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000582 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000583 QualType T = R->getType(getContext());
584 assert(T->isStructureType());
585
586 RecordType* RT = cast<RecordType>(T.getTypePtr());
587 RecordDecl* RD = RT->getDecl();
588 assert(RD->isDefinition());
589
590 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
591 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
592 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
593
594 for (; FI != FE; ++FI) {
595 QualType FTy = (*FI)->getType();
596 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
597
598 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
599 if (VI != VE) {
600 store = Bind(store, loc::MemRegionVal(FR), *VI);
601 ++VI;
602 } else
603 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
604 }
605 else if (FTy->isArrayType()) {
606 if (VI != VE) {
607 store = InitializeArray(store, FR, *VI);
608 ++VI;
609 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000610 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000611 }
612 else if (FTy->isStructureType()) {
613 if (VI != VE) {
614 store = InitializeStruct(store, FR, *VI);
615 ++VI;
616 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000617 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000618 }
619 }
620 return store;
621}
622
Zhongxing Xud463d442008-11-02 12:13:30 +0000623// Bind all fields of the struct to some value.
624Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
625 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000626 QualType T = BaseR->getType(getContext());
627 assert(T->isStructureType());
628
629 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000630 RecordDecl* RD = RT->getDecl();
631 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000632
633 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
634
635 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000636
637 QualType FTy = (*I)->getType();
638 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
639
640 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000641 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000642
643 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000644 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000645
646 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000647 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000648 }
649 }
650
651 return store;
652}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000653
654const GRState* RegionStoreManager::AddRegionView(const GRState* St,
655 const MemRegion* View,
656 const MemRegion* Base) {
657 GRStateRef state(St, StateMgr);
658
659 // First, retrieve the region view of the base region.
660 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
661 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
662
663 // Now add View to the region view.
664 L = RVFactory.Add(View, L);
665
666 // Create a new state with the new region view.
667 return state.set<RegionViewMapTy>(Base, L);
668}