blob: b78c2edb1449ad108e219174ad8ce31308efa797 [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 Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
32// RegionView GDM stuff.
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000033typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
34typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000035static int RegionViewMapTyIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000036namespace clang {
37template<> struct GRStateTrait<RegionViewMapTy>
38 : public GRStatePartialTrait<RegionViewMapTy> {
39 static void* GDMIndex() { return &RegionViewMapTyIndex; }
40};
41}
Zhongxing Xu17892752008-10-08 02:50:44 +000042
Zhongxing Xubaf03a72008-11-24 09:44:56 +000043// RegionExtents GDM stuff.
44// Currently RegionExtents are in bytes. We can change this representation when
45// there are real requirements.
46typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy;
47static int RegionExtentsTyIndex = 0;
48namespace clang {
49template<> struct GRStateTrait<RegionExtentsTy>
50 : public GRStatePartialTrait<RegionExtentsTy> {
51 static void* GDMIndex() { return &RegionExtentsTyIndex; }
52};
53}
54
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000055// KillSet GDM stuff.
Ted Kremenek2ed14be2008-12-05 00:47:52 +000056typedef llvm::ImmutableSet<const MemRegion*> RegionKills;
57static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000058namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000059 template<> struct GRStateTrait<RegionKills>
60 : public GRStatePartialTrait<RegionKills> {
61 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000062 };
63}
64
65
Zhongxing Xu17892752008-10-08 02:50:44 +000066namespace {
67
68class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
69 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000070 RegionViewTy::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000071
Zhongxing Xu17892752008-10-08 02:50:44 +000072 GRStateManager& StateMgr;
73 MemRegionManager MRMgr;
74
75public:
76 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000077 : RBFactory(mgr.getAllocator()),
78 RVFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000079 StateMgr(mgr),
80 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000081
82 virtual ~RegionStoreManager() {}
83
Zhongxing Xu24194ef2008-10-24 01:38:55 +000084 MemRegionManager& getRegionManager() { return MRMgr; }
Ted Kremenek4f090272008-10-27 21:54:31 +000085
Zhongxing Xuf22679e2008-11-07 10:38:33 +000086 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000087
Zhongxing Xu143bf822008-10-25 14:18:57 +000088 SVal getLValueString(const GRState* St, const StringLiteral* S);
89
Zhongxing Xuf22679e2008-11-07 10:38:33 +000090 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
91
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000092 SVal getLValueVar(const GRState* St, const VarDecl* VD);
93
94 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
95
96 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
97
Zhongxing Xub1d542a2008-10-24 01:09:32 +000098 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
99
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000100 SVal getSizeInElements(const GRState* St, const MemRegion* R);
101
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000102 SVal ArrayToPointer(SVal Array);
103
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000104 /// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
105 /// a MemRegion* to a specific location type. 'R' is the region being
106 /// casted and 'CastToTy' the result type of the cast.
107 CastResult CastRegion(const GRState* state, const MemRegion* R,
108 QualType CastToTy);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000109
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000110 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000111
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000112 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000113
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000114 Store Remove(Store store, Loc LV) {
115 // FIXME: Implement.
116 return store;
117 }
118
Zhongxing Xu17892752008-10-08 02:50:44 +0000119 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000120
121 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
122 /// 'this' object (C++). When used when analyzing a normal function this
123 /// method returns NULL.
124 const MemRegion* getSelfRegion(Store) {
125 assert (false && "Not implemented.");
126 return 0;
127 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000128
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000129 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
130 /// It returns a new Store with these values removed, and populates LSymbols
131 // and DSymbols with the known set of live and dead symbols respectively.
132 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
133 const LiveVariables& Live,
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000134 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000135 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000136
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000137 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000138
Ted Kremenek42577d12008-11-12 19:18:35 +0000139 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000140
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000141 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
142
Zhongxing Xu17892752008-10-08 02:50:44 +0000143 static inline RegionBindingsTy GetRegionBindings(Store store) {
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000144 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000145 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000146
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000147 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000148
149 void iterBindings(Store store, BindingsHandler& f) {
150 // FIXME: Implement.
151 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000152
153private:
154 Loc getVarLoc(const VarDecl* VD) {
155 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
156 }
157
Zhongxing Xud463d442008-11-02 12:13:30 +0000158 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
159 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000160 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
161
Zhongxing Xud463d442008-11-02 12:13:30 +0000162 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
163 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000164 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000165
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000166 /// Retrieve the values in a struct and return a CompoundVal, used when doing
167 /// struct copy:
168 /// struct s x, y;
169 /// x = y;
170 /// y's value is retrieved by this method.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000171 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000172
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000173 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000174
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000175 // Utility methods.
176 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
177 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000178 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000179
180 const GRState* AddRegionView(const GRState* St,
181 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000182};
183
184} // end anonymous namespace
185
Ted Kremenek95c7b002008-10-24 01:04:59 +0000186StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000187 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000188}
189
Zhongxing Xu143bf822008-10-25 14:18:57 +0000190SVal RegionStoreManager::getLValueString(const GRState* St,
191 const StringLiteral* S) {
192 return loc::MemRegionVal(MRMgr.getStringRegion(S));
193}
194
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000195SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
196 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
197}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000198
199SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
200 const CompoundLiteralExpr* CL) {
201 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
202}
203
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000204SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
205 SVal Base) {
206 return UnknownVal();
207}
208
209SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
210 const FieldDecl* D) {
211 if (Base.isUnknownOrUndef())
212 return Base;
213
214 Loc BaseL = cast<Loc>(Base);
215 const MemRegion* BaseR = 0;
216
217 switch (BaseL.getSubKind()) {
218 case loc::MemRegionKind:
219 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
220 break;
221
222 case loc::SymbolValKind:
223 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
224 break;
225
226 case loc::GotoLabelKind:
227 case loc::FuncValKind:
228 // These are anormal cases. Flag an undefined value.
229 return UndefinedVal();
230
231 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000232 // While these seem funny, this can happen through casts.
233 // FIXME: What we should return is the field offset. For example,
234 // add the field offset to the integer value. That way funny things
235 // like this work properly: &(((struct foo *) 0xa)->f)
236 return Base;
237
238 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000239 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000240 return Base;
241 }
242
243 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
244}
245
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000246SVal RegionStoreManager::getLValueElement(const GRState* St,
247 SVal Base, SVal Offset) {
248 if (Base.isUnknownOrUndef())
249 return Base;
250
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000251 if (isa<loc::SymbolVal>(Base))
252 return Base;
253
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000254 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
255
Zhongxing Xue4d13932008-11-13 09:48:44 +0000256 // Pointer of any type can be cast and used as array base. We do not support
257 // that case yet.
258 if (!isa<ElementRegion>(BaseL.getRegion())) {
259 // Record what we have seen in real code.
260 assert(isa<FieldRegion>(BaseL.getRegion()));
261 return UnknownVal();
262 }
263
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000264 // We expect BaseR is an ElementRegion, not a base VarRegion.
265
266 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
267
268 SVal Idx = ElemR->getIndex();
269
270 nonloc::ConcreteInt *CI1, *CI2;
271
272 // Only handle integer indices for now.
273 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
274 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000275
Sebastian Redle95db4f2008-11-24 19:35:33 +0000276 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000277 SVal SignedInt;
278
Sebastian Redle95db4f2008-11-24 19:35:33 +0000279 // Index might be unsigned. We have to convert it to signed. It might also
280 // be less wide than the size. We have to extend it.
281 if (CI2->getValue().isUnsigned() ||
282 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000283 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000284 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
285 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000286 SI.setIsSigned(true);
287 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
288 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
289 }
290
Zhongxing Xu63123d82008-11-23 04:30:35 +0000291 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000292 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
Ted Kremenekabb042f2008-12-13 19:24:37 +0000293 ElemR->getArrayRegion()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000294 }
295
296 return UnknownVal();
297}
298
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000299SVal RegionStoreManager::getSizeInElements(const GRState* St,
300 const MemRegion* R) {
301 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
302 // Get the type of the variable.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000303 QualType T = VR->getRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000304
305 // It must be of array type.
306 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
307
308 // return the size as signed integer.
309 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
310 }
311
312 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000313 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000314 // We intentionally made the size value signed because it participates in
315 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000316 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000317 }
318
319 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000320 GRStateRef state(St, StateMgr);
321
322 // Get the size of the super region in bytes.
323 RegionExtentsTy::data_type* T
324 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
325
326 assert(T && "region extent not exist");
327
328 // Assume it's ConcreteInt for now.
329 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
330
331 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000332 QualType LvT = ATR->getLValueType(getContext());
333 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000334
335 uint64_t X = getContext().getTypeSize(ElemTy);
336
337 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
338 false);
339
340 // Calculate the number of elements.
341
342 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
343 // signed?
344 if (SSize.isUnsigned())
345 SSize.setIsSigned(true);
346
347 // FIXME: move this operation into BasicVals.
348 const llvm::APSInt S =
349 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
350
351 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000352 }
353
354 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
355 // FIXME: Unsupported yet.
356 FR = 0;
357 return UnknownVal();
358 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000359
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000360 assert(0 && "Other regions are not supported yet.");
361}
362
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000363// Cast 'pointer to array' to 'pointer to the first element of array'.
364
365SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000366 if (Array.isUnknownOrUndef())
367 return Array;
368
369 if (!isa<loc::MemRegionVal>(Array))
370 return UnknownVal();
371
372 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
373 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
374
375 if (ArrayR)
376 return UnknownVal();
377
Zhongxing Xu63123d82008-11-23 04:30:35 +0000378 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000379 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
380
381 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000382}
383
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000384StoreManager::CastResult
385RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
386 QualType CastToTy) {
387
388 // Return the same region if the region types are compatible.
389 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
390 ASTContext& Ctx = StateMgr.getContext();
391 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
392 QualType Tb = Ctx.getCanonicalType(CastToTy);
393
394 if (Ta == Tb)
395 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000396 }
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000397
398 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
399 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000400}
401
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000402SVal RegionStoreManager::Retrieve(const GRState* state, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000403 assert(!isa<UnknownVal>(L) && "location unknown");
404 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000405 Store S = state->getStore();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000406
407 switch (L.getSubKind()) {
408 case loc::MemRegionKind: {
409 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
410 assert(R && "bad region");
411
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000412 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000413 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000414 return RetrieveStruct(S, TR);
415
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000416 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
417 RegionBindingsTy::data_type* V = B.lookup(R);
418 return V ? *V : UnknownVal();
419 }
420
421 case loc::SymbolValKind:
422 return UnknownVal();
423
424 case loc::ConcreteIntKind:
425 return UndefinedVal(); // As in BasicStoreManager.
426
427 case loc::FuncValKind:
428 return L;
429
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000430 default:
431 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000432 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000433 }
434}
435
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000436SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000437 // FIXME: Verify we want getRValueType instead of getLValueType.
438 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000439 assert(T->isStructureType());
440
441 const RecordType* RT = cast<RecordType>(T.getTypePtr());
442 RecordDecl* RD = RT->getDecl();
443 assert(RD->isDefinition());
444
445 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
446
Douglas Gregore267ff32008-12-11 20:41:00 +0000447 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000448
Douglas Gregore267ff32008-12-11 20:41:00 +0000449 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
450 FieldEnd = Fields.rend();
451 Field != FieldEnd; ++Field) {
452 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000453 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000454 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000455
456 SVal FieldValue = data ? *data : UnknownVal();
457
458 StructVal = getBasicVals().consVals(FieldValue, StructVal);
459 }
460
461 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
462}
463
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000464Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000465 if (LV.getSubKind() == loc::SymbolValKind)
466 return store;
467
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000468 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000469
Ted Kremenek993f1c72008-10-17 20:28:54 +0000470 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000471
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000472 assert(R);
473
474 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000475 // FIXME: Verify we want getRValueType().
476 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000477 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000478
479 RegionBindingsTy B = GetRegionBindings(store);
480 return V.isUnknown()
481 ? RBFactory.Remove(B, R).getRoot()
482 : RBFactory.Add(B, R, V).getRoot();
483}
484
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000485Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000486 // Verify we want getRValueType.
487 QualType T = R->getRValueType(getContext());
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000488 assert(T->isStructureType());
489
490 const RecordType* RT = cast<RecordType>(T.getTypePtr());
491 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000492
493 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000494 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000495 // function argument.
496 assert(V.isUnknown());
497 return store;
498 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000499
500 RegionBindingsTy B = GetRegionBindings(store);
501
Zhongxing Xud463d442008-11-02 12:13:30 +0000502 if (isa<UnknownVal>(V))
503 return BindStructToVal(store, R, UnknownVal());
504
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000505 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
506
507 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
508 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
509
510 for (; FI != FE; ++FI, ++VI) {
511 assert(VI != VE);
512
513 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
514
515 B = RBFactory.Add(B, FR, *VI);
516 }
517
518 return B.getRoot();
519}
520
Zhongxing Xu17892752008-10-08 02:50:44 +0000521Store RegionStoreManager::getInitialStore() {
522 typedef LiveVariables::AnalysisDataTy LVDataTy;
523 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
524
525 Store St = RBFactory.GetEmptyMap().getRoot();
526
527 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000528 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000529
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000530 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000531 // Punt on static variables for now.
532 if (VD->getStorageClass() == VarDecl::Static)
533 continue;
534
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000535 VarRegion* VR = MRMgr.getVarRegion(VD);
536
Zhongxing Xu17892752008-10-08 02:50:44 +0000537 QualType T = VD->getType();
538 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000539 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000540 // Initialize globals and parameters to symbolic values.
541 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000542 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000543 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000544 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000545 : UndefinedVal();
546
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000547 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000548 }
549 else if (T->isArrayType()) {
550 if (VD->hasGlobalStorage()) // Params cannot have array type.
551 St = BindArrayToSymVal(St, VR);
552 else
553 St = BindArrayToVal(St, VR, UndefinedVal());
554 }
555 else if (T->isStructureType()) {
556 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
557 isa<ImplicitParamDecl>(VD))
558 St = BindStructToSymVal(St, VR);
559 else
560 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000561 }
562 }
563 }
564 return St;
565}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000566
Ted Kremenek42577d12008-11-12 19:18:35 +0000567Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
568 SVal* InitVal, unsigned Count) {
569
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000570 if (VD->hasGlobalStorage()) {
571 // Static global variables should not be visited here.
572 assert(!(VD->getStorageClass() == VarDecl::Static &&
573 VD->isFileVarDecl()));
574 // Process static variables.
575 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000576 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000577 // Only handle pointer and integer static variables.
578
579 QualType T = VD->getType();
580
581 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000582 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000583 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000584
585 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000586 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000587 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000588
589 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000590 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000591 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000592 }
593 }
594 } else {
595 // Process local variables.
596
597 QualType T = VD->getType();
598
Zhongxing Xua82512a2008-10-24 08:42:28 +0000599 VarRegion* VR = MRMgr.getVarRegion(VD);
600
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000601 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000602 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000603 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000604 }
605 else if (T->isArrayType()) {
606 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000607 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000608 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000609 store = InitializeArray(store, VR, *InitVal);
610 }
611 else if (T->isStructureType()) {
612 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000613 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000614 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000615 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000616 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000617
618 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000619 }
620 return store;
621}
622
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000623Store RegionStoreManager::BindCompoundLiteral(Store store,
624 const CompoundLiteralExpr* CL,
625 SVal V) {
626 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
627 store = Bind(store, loc::MemRegionVal(R), V);
628 return store;
629}
630
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000631const GRState* RegionStoreManager::setExtent(const GRState* St,
632 const MemRegion* R, SVal Extent) {
633 GRStateRef state(St, StateMgr);
634 return state.set<RegionExtentsTy>(R, Extent);
635}
636
637
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000638void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
639 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
640 LSymbols.insert(*SI);
641}
642
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000643Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000644 const LiveVariables& Live,
645 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
646 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
647
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000648 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000649 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000650
651 // Lazily constructed backmap from MemRegions to SubRegions.
652 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
653 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
654
655 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
656 // the ability to reuse memory. This way we can keep TmpAlloc around as
657 // an instance variable of RegionStoreManager (avoiding repeated malloc
658 // overhead).
659 llvm::BumpPtrAllocator TmpAlloc;
660
661 // Factory objects.
662 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
663 SubRegionsTy::Factory SubRegF(TmpAlloc);
664
665 // The backmap from regions to subregions.
666 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
667
668 // Do a pass over the regions in the store. For VarRegions we check if
669 // the variable is still live and if so add it to the list of live roots.
670 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000671 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000672 const MemRegion* R = I.getKey();
673 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
674 if (Live.isLive(Loc, VR->getDecl()))
675 RegionRoots.push_back(VR); // This is a live "root".
676 }
677 else {
678 // Get the super region for R.
679 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
680 // Get the current set of subregions for SuperR.
681 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
682 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
683 // Add R to the subregions of SuperR.
684 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
685
686 // Finally, check if SuperR is a VarRegion. We need to do this
687 // to also mark SuperR as a root (as it may not have a value directly
688 // bound to it in the store).
689 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
690 if (Live.isLive(Loc, VR->getDecl()))
691 RegionRoots.push_back(VR); // This is a live "root".
692 }
693 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000694 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000695
696 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
697 // of the store. We want to find all live symbols and dead regions.
698 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
699
700 while (!RegionRoots.empty()) {
701 // Dequeue the next region on the worklist.
702 const MemRegion* R = RegionRoots.back();
703 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000704
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000705 // Check if we have already processed this region.
706 if (Marked.count(R)) continue;
707
708 // Mark this region as processed. This is needed for termination in case
709 // a region is referenced more than once.
710 Marked.insert(R);
711
712 // Mark the symbol for any live SymbolicRegion as "live". This means we
713 // should continue to track that symbol.
714 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
715 LSymbols.insert(SymR->getSymbol());
716
717 // Get the data binding for R (if any).
718 RegionBindingsTy::data_type* Xptr = B.lookup(R);
719 if (Xptr) {
720 SVal X = *Xptr;
721 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
722
723 // If X is a region, then add it the RegionRoots.
724 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
725 RegionRoots.push_back(RegionX->getRegion());
726 }
727
728 // Get the subregions of R. These are RegionRoots as well since they
729 // represent values that are also bound to R.
730 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
731 if (!SRptr) continue;
732 SubRegionsTy SR = *SRptr;
733
734 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
735 RegionRoots.push_back(*I);
736 }
737
738 // We have now scanned the store, marking reachable regions and symbols
739 // as live. We now remove all the regions that are dead from the store
740 // as well as update DSymbols with the set symbols that are now dead.
741
742 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
743 const MemRegion* R = I.getKey();
744
745 // If this region live? Is so, none of its symbols are dead.
746 if (Marked.count(R))
747 continue;
748
749 // Remove this dead region from the store.
750 store = Remove(store, loc::MemRegionVal(R));
751
752 // Mark all non-live symbols that this region references as dead.
753 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000754 SymbolRef Sym = SymR->getSymbol();
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000755 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
756 }
757
758 SVal X = I.getData();
759 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
760 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
761 }
762
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000763 return store;
764}
765
Zhongxing Xua071eb02008-10-24 06:01:33 +0000766void RegionStoreManager::print(Store store, std::ostream& Out,
767 const char* nl, const char *sep) {
768 llvm::raw_os_ostream OS(Out);
769 RegionBindingsTy B = GetRegionBindings(store);
770 OS << "Store:" << nl;
771
772 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
773 OS << ' '; I.getKey()->print(OS); OS << " : ";
774 I.getData().print(OS); OS << nl;
775 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000776}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000777
Zhongxing Xud463d442008-11-02 12:13:30 +0000778Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000779 SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000780
781 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000782 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000783 assert(T->isArrayType());
784
785 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
786
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000787 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000788
Sebastian Redl50038612008-12-02 16:47:35 +0000789 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
790 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000791
792 // Check if the init expr is a StringLiteral.
793 if (isa<loc::MemRegionVal>(Init)) {
794 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
795 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
796 const char* str = S->getStrData();
797 unsigned len = S->getByteLength();
798 unsigned j = 0;
799
800 for (; i < Size; ++i, ++j) {
801 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
802 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
803
804 // Copy bytes from the string literal into the target array. Trailing
805 // bytes in the array that are not covered by the string literal are
806 // initialized to zero.
807 SVal V = (j < len)
808 ? NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true)
809 : NonLoc::MakeVal(getBasicVals(), 0, sizeof(char)*8, true);
810
811 store = Bind(store, loc::MemRegionVal(ER), V);
812 }
813
814 return store;
815 }
816
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000817
818 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
819
820 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
821
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000822 for (; i < Size; ++i) {
823 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000824 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
825
826 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
827 // The init list might be shorter than the array decl.
828 if (VI != VE) ++VI;
829 }
830
831 return store;
832}
833
Zhongxing Xud463d442008-11-02 12:13:30 +0000834// Bind all elements of the array to some value.
835Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
836 SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000837
838 // FIXME: Verify we want getRValueType.
839 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000840 assert(T->isArrayType());
841
Zhongxing Xua82512a2008-10-24 08:42:28 +0000842 // Only handle constant size array for now.
843 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
844
845 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000846 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000847
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000848 for (; i != Size; ++i) {
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000849 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000850
851 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
852
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000853 if (CAT->getElementType()->isStructureType())
854 store = BindStructToVal(store, ER, V);
855 else
856 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000857 }
858 }
859
860 return store;
861}
862
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000863Store RegionStoreManager::BindArrayToSymVal(Store store,
864 const TypedRegion* BaseR) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000865
866 // FIXME: Verify we want getRValueType.
867 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000868 assert(T->isArrayType());
869
870 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
871 llvm::APInt Size = CAT->getSize();
872 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
873 for (; i != Size; ++i) {
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000874 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false)));
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000875
876 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
877
878 if (CAT->getElementType()->isStructureType()) {
879 store = BindStructToSymVal(store, ER);
880 }
881 else {
882 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
883 &Idx.getValue(), CAT->getElementType());
884 store = Bind(store, loc::MemRegionVal(ER), V);
885 }
886 }
887 }
888
889 return store;
890}
891
Zhongxing Xud463d442008-11-02 12:13:30 +0000892Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000893 SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000894
895 // FIXME: Verify that we should use getRValueType or getLValueType.
896 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000897 assert(T->isStructureType());
898
899 RecordType* RT = cast<RecordType>(T.getTypePtr());
900 RecordDecl* RD = RT->getDecl();
901 assert(RD->isDefinition());
902
903 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
904 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
905 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
906
907 for (; FI != FE; ++FI) {
908 QualType FTy = (*FI)->getType();
909 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
910
911 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
912 if (VI != VE) {
913 store = Bind(store, loc::MemRegionVal(FR), *VI);
914 ++VI;
915 } else
916 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
917 }
918 else if (FTy->isArrayType()) {
919 if (VI != VE) {
920 store = InitializeArray(store, FR, *VI);
921 ++VI;
922 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000923 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000924 }
925 else if (FTy->isStructureType()) {
926 if (VI != VE) {
927 store = InitializeStruct(store, FR, *VI);
928 ++VI;
929 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000930 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000931 }
932 }
933 return store;
934}
935
Zhongxing Xud463d442008-11-02 12:13:30 +0000936// Bind all fields of the struct to some value.
937Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
938 SVal V) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000939
940 // FIXME: Verify that we should use getLValueType or getRValueType.
941 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000942 assert(T->isStructureType());
943
944 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000945 RecordDecl* RD = RT->getDecl();
946 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000947
948 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
949
950 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000951
952 QualType FTy = (*I)->getType();
953 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
954
955 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000956 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000957
958 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000959 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000960
961 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000962 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000963 }
964 }
965
966 return store;
967}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000968
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000969Store RegionStoreManager::BindStructToSymVal(Store store,
970 const TypedRegion* BaseR) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000971
972 // FIXME: Verify that we should use getLValueType or getRValueType
973 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000974 assert(T->isStructureType());
975
976 const RecordType* RT = cast<RecordType>(T.getTypePtr());
977 RecordDecl* RD = RT->getDecl();
978 assert(RD->isDefinition());
979
980 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
981
982 for (; I != E; ++I) {
983 QualType FTy = (*I)->getType();
984 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
985
986 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
987 store = Bind(store, loc::MemRegionVal(FR),
988 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
989 }
990 else if (FTy->isArrayType()) {
991 store = BindArrayToSymVal(store, FR);
992 }
993 else if (FTy->isStructureType()) {
994 store = BindStructToSymVal(store, FR);
995 }
996 }
997
998 return store;
999}
1000
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001001const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1002 const MemRegion* View,
1003 const MemRegion* Base) {
1004 GRStateRef state(St, StateMgr);
1005
1006 // First, retrieve the region view of the base region.
1007 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
1008 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
1009
1010 // Now add View to the region view.
1011 L = RVFactory.Add(View, L);
1012
1013 // Create a new state with the new region view.
1014 return state.set<RegionViewMapTy>(Base, L);
1015}