blob: 98b408156f1fabf2e8f64cb773b76570c3ef2b0b [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 Xu9c9ca082008-12-16 02:36:30 +0000114 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000115
Zhongxing Xu17892752008-10-08 02:50:44 +0000116 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000117
118 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
119 /// 'this' object (C++). When used when analyzing a normal function this
120 /// method returns NULL.
121 const MemRegion* getSelfRegion(Store) {
122 assert (false && "Not implemented.");
123 return 0;
124 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000125
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000126 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
127 /// It returns a new Store with these values removed, and populates LSymbols
128 // and DSymbols with the known set of live and dead symbols respectively.
129 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
130 const LiveVariables& Live,
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000131 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000132 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000133
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000134 void UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000135
Ted Kremenek42577d12008-11-12 19:18:35 +0000136 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000137
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000138 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
139
Zhongxing Xu17892752008-10-08 02:50:44 +0000140 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000141 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000142 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000143
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000144 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000145
146 void iterBindings(Store store, BindingsHandler& f) {
147 // FIXME: Implement.
148 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000149
150private:
151 Loc getVarLoc(const VarDecl* VD) {
152 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
153 }
154
Zhongxing Xud463d442008-11-02 12:13:30 +0000155 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
156 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000157 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
158
Zhongxing Xud463d442008-11-02 12:13:30 +0000159 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
160 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000161 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000162
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000163 /// Retrieve the values in a struct and return a CompoundVal, used when doing
164 /// struct copy:
165 /// struct s x, y;
166 /// x = y;
167 /// y's value is retrieved by this method.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000168 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000169
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000170 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000171
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000172 // Utility methods.
173 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
174 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000175 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000176
177 const GRState* AddRegionView(const GRState* St,
178 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000179};
180
181} // end anonymous namespace
182
Ted Kremenek95c7b002008-10-24 01:04:59 +0000183StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000184 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000185}
186
Zhongxing Xu143bf822008-10-25 14:18:57 +0000187SVal RegionStoreManager::getLValueString(const GRState* St,
188 const StringLiteral* S) {
189 return loc::MemRegionVal(MRMgr.getStringRegion(S));
190}
191
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000192SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
193 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
194}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000195
196SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
197 const CompoundLiteralExpr* CL) {
198 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
199}
200
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000201SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
202 SVal Base) {
203 return UnknownVal();
204}
205
206SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
207 const FieldDecl* D) {
208 if (Base.isUnknownOrUndef())
209 return Base;
210
211 Loc BaseL = cast<Loc>(Base);
212 const MemRegion* BaseR = 0;
213
214 switch (BaseL.getSubKind()) {
215 case loc::MemRegionKind:
216 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
217 break;
218
219 case loc::SymbolValKind:
220 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
221 break;
222
223 case loc::GotoLabelKind:
224 case loc::FuncValKind:
225 // These are anormal cases. Flag an undefined value.
226 return UndefinedVal();
227
228 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000229 // While these seem funny, this can happen through casts.
230 // FIXME: What we should return is the field offset. For example,
231 // add the field offset to the integer value. That way funny things
232 // like this work properly: &(((struct foo *) 0xa)->f)
233 return Base;
234
235 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000236 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000237 return Base;
238 }
239
240 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
241}
242
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000243SVal RegionStoreManager::getLValueElement(const GRState* St,
244 SVal Base, SVal Offset) {
245 if (Base.isUnknownOrUndef())
246 return Base;
247
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000248 if (isa<loc::SymbolVal>(Base))
249 return Base;
250
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000251 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
252
Zhongxing Xue4d13932008-11-13 09:48:44 +0000253 // Pointer of any type can be cast and used as array base. We do not support
254 // that case yet.
255 if (!isa<ElementRegion>(BaseL.getRegion())) {
256 // Record what we have seen in real code.
257 assert(isa<FieldRegion>(BaseL.getRegion()));
258 return UnknownVal();
259 }
260
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000261 // We expect BaseR is an ElementRegion, not a base VarRegion.
262
263 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
264
265 SVal Idx = ElemR->getIndex();
266
267 nonloc::ConcreteInt *CI1, *CI2;
268
269 // Only handle integer indices for now.
270 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
271 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000272
Sebastian Redle95db4f2008-11-24 19:35:33 +0000273 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000274 SVal SignedInt;
275
Sebastian Redle95db4f2008-11-24 19:35:33 +0000276 // Index might be unsigned. We have to convert it to signed. It might also
277 // be less wide than the size. We have to extend it.
278 if (CI2->getValue().isUnsigned() ||
279 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000280 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000281 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
282 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000283 SI.setIsSigned(true);
284 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
285 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
286 }
287
Zhongxing Xu63123d82008-11-23 04:30:35 +0000288 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000289 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
Ted Kremenekabb042f2008-12-13 19:24:37 +0000290 ElemR->getArrayRegion()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000291 }
292
293 return UnknownVal();
294}
295
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000296SVal RegionStoreManager::getSizeInElements(const GRState* St,
297 const MemRegion* R) {
298 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
299 // Get the type of the variable.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000300 QualType T = VR->getRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000301
302 // It must be of array type.
303 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
304
305 // return the size as signed integer.
306 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
307 }
308
309 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000310 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000311 // We intentionally made the size value signed because it participates in
312 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000313 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000314 }
315
316 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000317 GRStateRef state(St, StateMgr);
318
319 // Get the size of the super region in bytes.
320 RegionExtentsTy::data_type* T
321 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
322
323 assert(T && "region extent not exist");
324
325 // Assume it's ConcreteInt for now.
326 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
327
328 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000329 QualType LvT = ATR->getLValueType(getContext());
330 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000331
332 uint64_t X = getContext().getTypeSize(ElemTy);
333
334 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
335 false);
336
337 // Calculate the number of elements.
338
339 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
340 // signed?
341 if (SSize.isUnsigned())
342 SSize.setIsSigned(true);
343
344 // FIXME: move this operation into BasicVals.
345 const llvm::APSInt S =
346 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
347
348 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000349 }
350
351 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
352 // FIXME: Unsupported yet.
353 FR = 0;
354 return UnknownVal();
355 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000356
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000357 assert(0 && "Other regions are not supported yet.");
358}
359
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000360// Cast 'pointer to array' to 'pointer to the first element of array'.
361
362SVal RegionStoreManager::ArrayToPointer(SVal Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000363 if (Array.isUnknownOrUndef())
364 return Array;
365
366 if (!isa<loc::MemRegionVal>(Array))
367 return UnknownVal();
368
369 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
370 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
371
372 if (ArrayR)
373 return UnknownVal();
374
Zhongxing Xu63123d82008-11-23 04:30:35 +0000375 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000376 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
377
378 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000379}
380
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000381StoreManager::CastResult
382RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
383 QualType CastToTy) {
384
385 // Return the same region if the region types are compatible.
386 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
387 ASTContext& Ctx = StateMgr.getContext();
388 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
389 QualType Tb = Ctx.getCanonicalType(CastToTy);
390
391 if (Ta == Tb)
392 return CastResult(state, R);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000393 }
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000394
395 const MemRegion* ViewR = MRMgr.getAnonTypedRegion(CastToTy, R);
396 return CastResult(AddRegionView(state, ViewR, R), ViewR);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000397}
398
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000399SVal RegionStoreManager::Retrieve(const GRState* state, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000400 assert(!isa<UnknownVal>(L) && "location unknown");
401 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000402 Store S = state->getStore();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000403
404 switch (L.getSubKind()) {
405 case loc::MemRegionKind: {
406 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
407 assert(R && "bad region");
408
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000409 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000410 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000411 return RetrieveStruct(S, TR);
412
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000413 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
414 RegionBindingsTy::data_type* V = B.lookup(R);
415 return V ? *V : UnknownVal();
416 }
417
418 case loc::SymbolValKind:
419 return UnknownVal();
420
421 case loc::ConcreteIntKind:
422 return UndefinedVal(); // As in BasicStoreManager.
423
424 case loc::FuncValKind:
425 return L;
426
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000427 default:
428 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000429 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000430 }
431}
432
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000433SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000434 // FIXME: Verify we want getRValueType instead of getLValueType.
435 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000436 assert(T->isStructureType());
437
438 const RecordType* RT = cast<RecordType>(T.getTypePtr());
439 RecordDecl* RD = RT->getDecl();
440 assert(RD->isDefinition());
441
442 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
443
Douglas Gregore267ff32008-12-11 20:41:00 +0000444 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000445
Douglas Gregore267ff32008-12-11 20:41:00 +0000446 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
447 FieldEnd = Fields.rend();
448 Field != FieldEnd; ++Field) {
449 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000450 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000451 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000452
453 SVal FieldValue = data ? *data : UnknownVal();
454
455 StructVal = getBasicVals().consVals(FieldValue, StructVal);
456 }
457
458 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
459}
460
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000461Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000462 if (LV.getSubKind() == loc::SymbolValKind)
463 return store;
464
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000465 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000466
Ted Kremenek993f1c72008-10-17 20:28:54 +0000467 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000468
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000469 assert(R);
470
471 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000472 // FIXME: Verify we want getRValueType().
473 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000474 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000475
476 RegionBindingsTy B = GetRegionBindings(store);
477 return V.isUnknown()
478 ? RBFactory.Remove(B, R).getRoot()
479 : RBFactory.Add(B, R, V).getRoot();
480}
481
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000482Store RegionStoreManager::Remove(Store store, Loc L) {
483 RegionBindingsTy B = GetRegionBindings(store);
484
485 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
486 assert(R);
487
488 return RBFactory.Remove(B, R).getRoot();
489}
490
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000491Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000492 // Verify we want getRValueType.
493 QualType T = R->getRValueType(getContext());
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000494 assert(T->isStructureType());
495
496 const RecordType* RT = cast<RecordType>(T.getTypePtr());
497 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000498
499 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000500 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000501 // function argument.
502 assert(V.isUnknown());
503 return store;
504 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000505
506 RegionBindingsTy B = GetRegionBindings(store);
507
Zhongxing Xud463d442008-11-02 12:13:30 +0000508 if (isa<UnknownVal>(V))
509 return BindStructToVal(store, R, UnknownVal());
510
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000511 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
512
513 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
514 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
515
516 for (; FI != FE; ++FI, ++VI) {
517 assert(VI != VE);
518
519 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
520
521 B = RBFactory.Add(B, FR, *VI);
522 }
523
524 return B.getRoot();
525}
526
Zhongxing Xu17892752008-10-08 02:50:44 +0000527Store RegionStoreManager::getInitialStore() {
528 typedef LiveVariables::AnalysisDataTy LVDataTy;
529 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
530
531 Store St = RBFactory.GetEmptyMap().getRoot();
532
533 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000534 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000535
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000536 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000537 // Punt on static variables for now.
538 if (VD->getStorageClass() == VarDecl::Static)
539 continue;
540
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000541 VarRegion* VR = MRMgr.getVarRegion(VD);
542
Zhongxing Xu17892752008-10-08 02:50:44 +0000543 QualType T = VD->getType();
544 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000545 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000546 // Initialize globals and parameters to symbolic values.
547 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000548 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000549 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000550 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000551 : UndefinedVal();
552
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000553 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000554 }
555 else if (T->isArrayType()) {
556 if (VD->hasGlobalStorage()) // Params cannot have array type.
557 St = BindArrayToSymVal(St, VR);
558 else
559 St = BindArrayToVal(St, VR, UndefinedVal());
560 }
561 else if (T->isStructureType()) {
562 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
563 isa<ImplicitParamDecl>(VD))
564 St = BindStructToSymVal(St, VR);
565 else
566 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000567 }
568 }
569 }
570 return St;
571}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000572
Ted Kremenek42577d12008-11-12 19:18:35 +0000573Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
574 SVal* InitVal, unsigned Count) {
575
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000576 if (VD->hasGlobalStorage()) {
577 // Static global variables should not be visited here.
578 assert(!(VD->getStorageClass() == VarDecl::Static &&
579 VD->isFileVarDecl()));
580 // Process static variables.
581 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000582 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000583 // Only handle pointer and integer static variables.
584
585 QualType T = VD->getType();
586
587 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000588 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000589 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000590
591 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000592 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000593 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000594
595 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000596 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000597 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000598 }
599 }
600 } else {
601 // Process local variables.
602
603 QualType T = VD->getType();
604
Zhongxing Xua82512a2008-10-24 08:42:28 +0000605 VarRegion* VR = MRMgr.getVarRegion(VD);
606
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000607 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000608 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000609 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000610 }
611 else if (T->isArrayType()) {
612 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000613 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000614 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000615 store = InitializeArray(store, VR, *InitVal);
616 }
617 else if (T->isStructureType()) {
618 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000619 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000620 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000621 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000622 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000623
624 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000625 }
626 return store;
627}
628
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000629Store RegionStoreManager::BindCompoundLiteral(Store store,
630 const CompoundLiteralExpr* CL,
631 SVal V) {
632 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
633 store = Bind(store, loc::MemRegionVal(R), V);
634 return store;
635}
636
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000637const GRState* RegionStoreManager::setExtent(const GRState* St,
638 const MemRegion* R, SVal Extent) {
639 GRStateRef state(St, StateMgr);
640 return state.set<RegionExtentsTy>(R, Extent);
641}
642
643
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000644void RegionStoreManager::UpdateLiveSymbols(SVal X, LiveSymbolsTy& LSymbols) {
645 for (SVal::symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
646 LSymbols.insert(*SI);
647}
648
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000649Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000650 const LiveVariables& Live,
651 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
652 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
653
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000654 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000655 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000656
657 // Lazily constructed backmap from MemRegions to SubRegions.
658 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
659 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
660
661 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
662 // the ability to reuse memory. This way we can keep TmpAlloc around as
663 // an instance variable of RegionStoreManager (avoiding repeated malloc
664 // overhead).
665 llvm::BumpPtrAllocator TmpAlloc;
666
667 // Factory objects.
668 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
669 SubRegionsTy::Factory SubRegF(TmpAlloc);
670
671 // The backmap from regions to subregions.
672 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
673
674 // Do a pass over the regions in the store. For VarRegions we check if
675 // the variable is still live and if so add it to the list of live roots.
676 // For other regions we populate our region backmap.
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000677 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000678 const MemRegion* R = I.getKey();
679 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
680 if (Live.isLive(Loc, VR->getDecl()))
681 RegionRoots.push_back(VR); // This is a live "root".
682 }
683 else {
684 // Get the super region for R.
685 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
686 // Get the current set of subregions for SuperR.
687 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
688 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
689 // Add R to the subregions of SuperR.
690 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
691
692 // Finally, check if SuperR is a VarRegion. We need to do this
693 // to also mark SuperR as a root (as it may not have a value directly
694 // bound to it in the store).
695 if (const VarRegion* VR = dyn_cast<VarRegion>(SuperR)) {
696 if (Live.isLive(Loc, VR->getDecl()))
697 RegionRoots.push_back(VR); // This is a live "root".
698 }
699 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000700 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000701
702 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
703 // of the store. We want to find all live symbols and dead regions.
704 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
705
706 while (!RegionRoots.empty()) {
707 // Dequeue the next region on the worklist.
708 const MemRegion* R = RegionRoots.back();
709 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000710
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000711 // Check if we have already processed this region.
712 if (Marked.count(R)) continue;
713
714 // Mark this region as processed. This is needed for termination in case
715 // a region is referenced more than once.
716 Marked.insert(R);
717
718 // Mark the symbol for any live SymbolicRegion as "live". This means we
719 // should continue to track that symbol.
720 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
721 LSymbols.insert(SymR->getSymbol());
722
723 // Get the data binding for R (if any).
724 RegionBindingsTy::data_type* Xptr = B.lookup(R);
725 if (Xptr) {
726 SVal X = *Xptr;
727 UpdateLiveSymbols(X, LSymbols); // Update the set of live symbols.
728
729 // If X is a region, then add it the RegionRoots.
730 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
731 RegionRoots.push_back(RegionX->getRegion());
732 }
733
734 // Get the subregions of R. These are RegionRoots as well since they
735 // represent values that are also bound to R.
736 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
737 if (!SRptr) continue;
738 SubRegionsTy SR = *SRptr;
739
740 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
741 RegionRoots.push_back(*I);
742 }
743
744 // We have now scanned the store, marking reachable regions and symbols
745 // as live. We now remove all the regions that are dead from the store
746 // as well as update DSymbols with the set symbols that are now dead.
747
748 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
749 const MemRegion* R = I.getKey();
750
751 // If this region live? Is so, none of its symbols are dead.
752 if (Marked.count(R))
753 continue;
754
755 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000756 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000757
758 // Mark all non-live symbols that this region references as dead.
759 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +0000760 SymbolRef Sym = SymR->getSymbol();
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000761 if (!LSymbols.count(Sym)) DSymbols.insert(Sym);
762 }
763
764 SVal X = I.getData();
765 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
766 for (; SI != SE; ++SI) { if (!LSymbols.count(*SI)) DSymbols.insert(*SI); }
767 }
768
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000769 return store;
770}
771
Zhongxing Xua071eb02008-10-24 06:01:33 +0000772void RegionStoreManager::print(Store store, std::ostream& Out,
773 const char* nl, const char *sep) {
774 llvm::raw_os_ostream OS(Out);
775 RegionBindingsTy B = GetRegionBindings(store);
776 OS << "Store:" << nl;
777
778 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
779 OS << ' '; I.getKey()->print(OS); OS << " : ";
780 I.getData().print(OS); OS << nl;
781 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000782}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000783
Zhongxing Xud463d442008-11-02 12:13:30 +0000784Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000785 SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000786
787 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +0000788 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000789 assert(T->isArrayType());
790
791 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
792
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000793 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000794
Sebastian Redl50038612008-12-02 16:47:35 +0000795 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
796 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000797
798 // Check if the init expr is a StringLiteral.
799 if (isa<loc::MemRegionVal>(Init)) {
800 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
801 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
802 const char* str = S->getStrData();
803 unsigned len = S->getByteLength();
804 unsigned j = 0;
805
806 for (; i < Size; ++i, ++j) {
807 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
808 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
809
810 // Copy bytes from the string literal into the target array. Trailing
811 // bytes in the array that are not covered by the string literal are
812 // initialized to zero.
813 SVal V = (j < len)
814 ? NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true)
815 : NonLoc::MakeVal(getBasicVals(), 0, sizeof(char)*8, true);
816
817 store = Bind(store, loc::MemRegionVal(ER), V);
818 }
819
820 return store;
821 }
822
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000823
824 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
825
826 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
827
Zhongxing Xu6987c7b2008-11-30 05:49:49 +0000828 for (; i < Size; ++i) {
829 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000830 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
831
832 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
833 // The init list might be shorter than the array decl.
834 if (VI != VE) ++VI;
835 }
836
837 return store;
838}
839
Zhongxing Xud463d442008-11-02 12:13:30 +0000840// Bind all elements of the array to some value.
841Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
842 SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000843
844 // FIXME: Verify we want getRValueType.
845 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000846 assert(T->isArrayType());
847
Zhongxing Xua82512a2008-10-24 08:42:28 +0000848 // Only handle constant size array for now.
849 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
850
851 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000852 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000853
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000854 for (; i != Size; ++i) {
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000855 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000856
857 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
858
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000859 if (CAT->getElementType()->isStructureType())
860 store = BindStructToVal(store, ER, V);
861 else
862 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000863 }
864 }
865
866 return store;
867}
868
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000869Store RegionStoreManager::BindArrayToSymVal(Store store,
870 const TypedRegion* BaseR) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000871
872 // FIXME: Verify we want getRValueType.
873 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000874 assert(T->isArrayType());
875
876 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
877 llvm::APInt Size = CAT->getSize();
878 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
879 for (; i != Size; ++i) {
Zhongxing Xu96cb9fb2008-11-28 08:41:39 +0000880 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i, false)));
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000881
882 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
883
884 if (CAT->getElementType()->isStructureType()) {
885 store = BindStructToSymVal(store, ER);
886 }
887 else {
888 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
889 &Idx.getValue(), CAT->getElementType());
890 store = Bind(store, loc::MemRegionVal(ER), V);
891 }
892 }
893 }
894
895 return store;
896}
897
Zhongxing Xud463d442008-11-02 12:13:30 +0000898Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000899 SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000900
901 // FIXME: Verify that we should use getRValueType or getLValueType.
902 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000903 assert(T->isStructureType());
904
905 RecordType* RT = cast<RecordType>(T.getTypePtr());
906 RecordDecl* RD = RT->getDecl();
907 assert(RD->isDefinition());
908
909 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
910 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
911 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
912
913 for (; FI != FE; ++FI) {
914 QualType FTy = (*FI)->getType();
915 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
916
917 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
918 if (VI != VE) {
919 store = Bind(store, loc::MemRegionVal(FR), *VI);
920 ++VI;
921 } else
922 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
923 }
924 else if (FTy->isArrayType()) {
925 if (VI != VE) {
926 store = InitializeArray(store, FR, *VI);
927 ++VI;
928 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000929 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000930 }
931 else if (FTy->isStructureType()) {
932 if (VI != VE) {
933 store = InitializeStruct(store, FR, *VI);
934 ++VI;
935 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000936 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000937 }
938 }
939 return store;
940}
941
Zhongxing Xud463d442008-11-02 12:13:30 +0000942// Bind all fields of the struct to some value.
943Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
944 SVal V) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000945
946 // FIXME: Verify that we should use getLValueType or getRValueType.
947 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000948 assert(T->isStructureType());
949
950 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000951 RecordDecl* RD = RT->getDecl();
952 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000953
954 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
955
956 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000957
958 QualType FTy = (*I)->getType();
959 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
960
961 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000962 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000963
964 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000965 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000966
967 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000968 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000969 }
970 }
971
972 return store;
973}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000974
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000975Store RegionStoreManager::BindStructToSymVal(Store store,
976 const TypedRegion* BaseR) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000977
978 // FIXME: Verify that we should use getLValueType or getRValueType
979 QualType T = BaseR->getRValueType(getContext());
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000980 assert(T->isStructureType());
981
982 const RecordType* RT = cast<RecordType>(T.getTypePtr());
983 RecordDecl* RD = RT->getDecl();
984 assert(RD->isDefinition());
985
986 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
987
988 for (; I != E; ++I) {
989 QualType FTy = (*I)->getType();
990 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
991
992 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
993 store = Bind(store, loc::MemRegionVal(FR),
994 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
995 }
996 else if (FTy->isArrayType()) {
997 store = BindArrayToSymVal(store, FR);
998 }
999 else if (FTy->isStructureType()) {
1000 store = BindStructToSymVal(store, FR);
1001 }
1002 }
1003
1004 return store;
1005}
1006
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001007const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1008 const MemRegion* View,
1009 const MemRegion* Base) {
1010 GRStateRef state(St, StateMgr);
1011
1012 // First, retrieve the region view of the base region.
1013 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
1014 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
1015
1016 // Now add View to the region view.
1017 L = RVFactory.Add(View, L);
1018
1019 // Create a new state with the new region view.
1020 return state.set<RegionViewMapTy>(Base, L);
1021}