blob: cdc8b2723d15cd70cb80a6bce6c58dc84925e964 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xu1c96b242008-10-17 05:57:07 +000029typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000030typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
31typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
32
33static int RegionViewMapTyIndex = 0;
34
35namespace clang {
36template<> struct GRStateTrait<RegionViewMapTy>
37 : public GRStatePartialTrait<RegionViewMapTy> {
38 static void* GDMIndex() { return &RegionViewMapTyIndex; }
39};
40}
Zhongxing Xu17892752008-10-08 02:50:44 +000041
42namespace {
43
44class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
45 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000046 RegionViewTy::Factory RVFactory;
47 RegionViewMapTy::Factory RVMFactory;
48
Zhongxing Xu17892752008-10-08 02:50:44 +000049 GRStateManager& StateMgr;
50 MemRegionManager MRMgr;
51
52public:
53 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000054 : RBFactory(mgr.getAllocator()),
55 RVFactory(mgr.getAllocator()),
56 RVMFactory(mgr.getAllocator()),
57 StateMgr(mgr),
58 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000059
60 virtual ~RegionStoreManager() {}
61
Zhongxing Xu24194ef2008-10-24 01:38:55 +000062 MemRegionManager& getRegionManager() { return MRMgr; }
63
64 // FIXME: Is this function necessary?
65 SVal GetRegionSVal(Store St, const MemRegion* R) {
66 return Retrieve(St, loc::MemRegionVal(R));
67 }
Ted Kremenek4f090272008-10-27 21:54:31 +000068
Zhongxing Xuf22679e2008-11-07 10:38:33 +000069 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000070
Zhongxing Xu143bf822008-10-25 14:18:57 +000071 SVal getLValueString(const GRState* St, const StringLiteral* S);
72
Zhongxing Xuf22679e2008-11-07 10:38:33 +000073 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
74
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000075 SVal getLValueVar(const GRState* St, const VarDecl* VD);
76
77 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
78
79 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
80
Zhongxing Xub1d542a2008-10-24 01:09:32 +000081 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
82
Zhongxing Xue8a964b2008-11-22 13:21:46 +000083 SVal getSizeInElements(const GRState* St, const MemRegion* R);
84
Zhongxing Xub1d542a2008-10-24 01:09:32 +000085 SVal ArrayToPointer(SVal Array);
86
Zhongxing Xucb529b52008-11-16 07:06:26 +000087 std::pair<const GRState*, SVal>
88 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000089
Zhongxing Xu24194ef2008-10-24 01:38:55 +000090 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000091
Zhongxing Xu8485ec62008-10-21 06:27:32 +000092 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000093
Zhongxing Xu24194ef2008-10-24 01:38:55 +000094 Store Remove(Store store, Loc LV) {
95 // FIXME: Implement.
96 return store;
97 }
98
Zhongxing Xu17892752008-10-08 02:50:44 +000099 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000100
101 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
102 /// 'this' object (C++). When used when analyzing a normal function this
103 /// method returns NULL.
104 const MemRegion* getSelfRegion(Store) {
105 assert (false && "Not implemented.");
106 return 0;
107 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000108
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000109 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
110 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000111 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000112
Ted Kremenek42577d12008-11-12 19:18:35 +0000113 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000114
Zhongxing Xu17892752008-10-08 02:50:44 +0000115 static inline RegionBindingsTy GetRegionBindings(Store store) {
116 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
117 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000118
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000119 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000120
121 void iterBindings(Store store, BindingsHandler& f) {
122 // FIXME: Implement.
123 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000124
125private:
126 Loc getVarLoc(const VarDecl* VD) {
127 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
128 }
129
Zhongxing Xud463d442008-11-02 12:13:30 +0000130 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
131 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000132 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
133
Zhongxing Xud463d442008-11-02 12:13:30 +0000134 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
135 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000136 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000137
138 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000139 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000140
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000141 // Utility methods.
142 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
143 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000144 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000145
146 const GRState* AddRegionView(const GRState* St,
147 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000148};
149
150} // end anonymous namespace
151
Ted Kremenek95c7b002008-10-24 01:04:59 +0000152StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000153 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000154}
155
Zhongxing Xu143bf822008-10-25 14:18:57 +0000156SVal RegionStoreManager::getLValueString(const GRState* St,
157 const StringLiteral* S) {
158 return loc::MemRegionVal(MRMgr.getStringRegion(S));
159}
160
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000161SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
162 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
163}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000164
165SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
166 const CompoundLiteralExpr* CL) {
167 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
168}
169
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000170SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
171 SVal Base) {
172 return UnknownVal();
173}
174
175SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
176 const FieldDecl* D) {
177 if (Base.isUnknownOrUndef())
178 return Base;
179
180 Loc BaseL = cast<Loc>(Base);
181 const MemRegion* BaseR = 0;
182
183 switch (BaseL.getSubKind()) {
184 case loc::MemRegionKind:
185 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
186 break;
187
188 case loc::SymbolValKind:
189 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
190 break;
191
192 case loc::GotoLabelKind:
193 case loc::FuncValKind:
194 // These are anormal cases. Flag an undefined value.
195 return UndefinedVal();
196
197 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000198 // While these seem funny, this can happen through casts.
199 // FIXME: What we should return is the field offset. For example,
200 // add the field offset to the integer value. That way funny things
201 // like this work properly: &(((struct foo *) 0xa)->f)
202 return Base;
203
204 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000205 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000206 return Base;
207 }
208
209 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
210}
211
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000212SVal RegionStoreManager::getLValueElement(const GRState* St,
213 SVal Base, SVal Offset) {
214 if (Base.isUnknownOrUndef())
215 return Base;
216
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000217 if (isa<loc::SymbolVal>(Base))
218 return Base;
219
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000220 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
221
Zhongxing Xue4d13932008-11-13 09:48:44 +0000222 // Pointer of any type can be cast and used as array base. We do not support
223 // that case yet.
224 if (!isa<ElementRegion>(BaseL.getRegion())) {
225 // Record what we have seen in real code.
226 assert(isa<FieldRegion>(BaseL.getRegion()));
227 return UnknownVal();
228 }
229
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000230 // We expect BaseR is an ElementRegion, not a base VarRegion.
231
232 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
233
234 SVal Idx = ElemR->getIndex();
235
236 nonloc::ConcreteInt *CI1, *CI2;
237
238 // Only handle integer indices for now.
239 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
240 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000241
242 // Temporary SVal to hold a potential signed APSInt.
243 SVal SignedInt;
244
245 // Index might be unsigned. We have to convert it to signed.
246 if (CI2->getValue().isUnsigned()) {
247 llvm::APSInt SI = CI2->getValue();
248 SI.setIsSigned(true);
249 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
250 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
251 }
252
Zhongxing Xu63123d82008-11-23 04:30:35 +0000253 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000254 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
255 ElemR->getSuperRegion()));
256 }
257
258 return UnknownVal();
259}
260
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000261SVal RegionStoreManager::getSizeInElements(const GRState* St,
262 const MemRegion* R) {
263 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
264 // Get the type of the variable.
265 QualType T = VR->getType(getContext());
266
267 // It must be of array type.
268 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
269
270 // return the size as signed integer.
271 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
272 }
273
274 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000275 const StringLiteral* Str = SR->getStringLiteral();
276 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength(), false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000277 }
278
279 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
280 // FIXME: Unsupported yet.
281 ATR = 0;
282 return UnknownVal();
283 }
284
285 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
286 // FIXME: Unsupported yet.
287 FR = 0;
288 return UnknownVal();
289 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000290
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000291 assert(0 && "Other regions are not supported yet.");
292}
293
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000294// Cast 'pointer to array' to 'pointer to the first element of array'.
295
296SVal RegionStoreManager::ArrayToPointer(SVal Array) {
297 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000298
Zhongxing Xu63123d82008-11-23 04:30:35 +0000299 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000300 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
301
302 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000303}
304
Zhongxing Xucb529b52008-11-16 07:06:26 +0000305std::pair<const GRState*, SVal>
306RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
307 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000308 if (const AllocaRegion* AR =
309 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
310
311 // Create a new region to attach type information to it.
312 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
313
314 // Get the pointer to the first element.
315 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
316 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
317
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000318 // Add a RegionView to base region.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000319 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
320 loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000321 }
322
323 // Default case.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000324 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000325}
326
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000327SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000328 assert(!isa<UnknownVal>(L) && "location unknown");
329 assert(!isa<UndefinedVal>(L) && "location undefined");
330
331 switch (L.getSubKind()) {
332 case loc::MemRegionKind: {
333 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
334 assert(R && "bad region");
335
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000336 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
337 if (TR->getType(getContext())->isStructureType())
338 return RetrieveStruct(S, TR);
339
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000340 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
341 RegionBindingsTy::data_type* V = B.lookup(R);
342 return V ? *V : UnknownVal();
343 }
344
345 case loc::SymbolValKind:
346 return UnknownVal();
347
348 case loc::ConcreteIntKind:
349 return UndefinedVal(); // As in BasicStoreManager.
350
351 case loc::FuncValKind:
352 return L;
353
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000354 default:
355 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000356 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000357 }
358}
359
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000360SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
361 QualType T = R->getType(getContext());
362 assert(T->isStructureType());
363
364 const RecordType* RT = cast<RecordType>(T.getTypePtr());
365 RecordDecl* RD = RT->getDecl();
366 assert(RD->isDefinition());
367
368 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
369
370 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
371 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
372 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000373 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000374
375 SVal FieldValue = data ? *data : UnknownVal();
376
377 StructVal = getBasicVals().consVals(FieldValue, StructVal);
378 }
379
380 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
381}
382
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000383Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000384 if (LV.getSubKind() == loc::SymbolValKind)
385 return store;
386
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000387 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000388
Ted Kremenek993f1c72008-10-17 20:28:54 +0000389 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000390
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000391 assert(R);
392
393 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
394 if (TR->getType(getContext())->isStructureType())
395 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000396
397 RegionBindingsTy B = GetRegionBindings(store);
398 return V.isUnknown()
399 ? RBFactory.Remove(B, R).getRoot()
400 : RBFactory.Add(B, R, V).getRoot();
401}
402
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000403Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
404 QualType T = R->getType(getContext());
405 assert(T->isStructureType());
406
407 const RecordType* RT = cast<RecordType>(T.getTypePtr());
408 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000409
410 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000411 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000412 // function argument.
413 assert(V.isUnknown());
414 return store;
415 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000416
417 RegionBindingsTy B = GetRegionBindings(store);
418
Zhongxing Xud463d442008-11-02 12:13:30 +0000419 if (isa<UnknownVal>(V))
420 return BindStructToVal(store, R, UnknownVal());
421
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000422 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
423
424 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
425 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
426
427 for (; FI != FE; ++FI, ++VI) {
428 assert(VI != VE);
429
430 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
431
432 B = RBFactory.Add(B, FR, *VI);
433 }
434
435 return B.getRoot();
436}
437
Zhongxing Xu17892752008-10-08 02:50:44 +0000438Store RegionStoreManager::getInitialStore() {
439 typedef LiveVariables::AnalysisDataTy LVDataTy;
440 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
441
442 Store St = RBFactory.GetEmptyMap().getRoot();
443
444 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000445 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000446
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000447 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000448 // Punt on static variables for now.
449 if (VD->getStorageClass() == VarDecl::Static)
450 continue;
451
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000452 VarRegion* VR = MRMgr.getVarRegion(VD);
453
Zhongxing Xu17892752008-10-08 02:50:44 +0000454 QualType T = VD->getType();
455 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000456 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000457 // Initialize globals and parameters to symbolic values.
458 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000459 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000460 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000461 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000462 : UndefinedVal();
463
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000464 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000465 }
466 else if (T->isArrayType()) {
467 if (VD->hasGlobalStorage()) // Params cannot have array type.
468 St = BindArrayToSymVal(St, VR);
469 else
470 St = BindArrayToVal(St, VR, UndefinedVal());
471 }
472 else if (T->isStructureType()) {
473 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
474 isa<ImplicitParamDecl>(VD))
475 St = BindStructToSymVal(St, VR);
476 else
477 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000478 }
479 }
480 }
481 return St;
482}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000483
Ted Kremenek42577d12008-11-12 19:18:35 +0000484Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
485 SVal* InitVal, unsigned Count) {
486
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000487 if (VD->hasGlobalStorage()) {
488 // Static global variables should not be visited here.
489 assert(!(VD->getStorageClass() == VarDecl::Static &&
490 VD->isFileVarDecl()));
491 // Process static variables.
492 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000493 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000494 // Only handle pointer and integer static variables.
495
496 QualType T = VD->getType();
497
498 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000499 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000500 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000501
502 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000503 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000504 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000505
506 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000507 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000508 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000509 }
510 }
511 } else {
512 // Process local variables.
513
514 QualType T = VD->getType();
515
Zhongxing Xua82512a2008-10-24 08:42:28 +0000516 VarRegion* VR = MRMgr.getVarRegion(VD);
517
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000518 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000519 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000520 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000521 }
522 else if (T->isArrayType()) {
523 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000524 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000525 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000526 store = InitializeArray(store, VR, *InitVal);
527 }
528 else if (T->isStructureType()) {
529 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000530 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000531 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000532 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000533 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000534
535 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000536 }
537 return store;
538}
539
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000540Store RegionStoreManager::BindCompoundLiteral(Store store,
541 const CompoundLiteralExpr* CL,
542 SVal V) {
543 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
544 store = Bind(store, loc::MemRegionVal(R), V);
545 return store;
546}
547
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000548Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
549 const LiveVariables& Live,
550 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
551 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
552
553 RegionBindingsTy B = GetRegionBindings(store);
554 typedef SVal::symbol_iterator symbol_iterator;
555
556 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
557 // in SymbolicRegions.
558 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
559 SVal X = I.getData();
560 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
561 LSymbols.insert(*SI);
562 }
563
564 return store;
565}
566
Zhongxing Xua071eb02008-10-24 06:01:33 +0000567void RegionStoreManager::print(Store store, std::ostream& Out,
568 const char* nl, const char *sep) {
569 llvm::raw_os_ostream OS(Out);
570 RegionBindingsTy B = GetRegionBindings(store);
571 OS << "Store:" << nl;
572
573 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
574 OS << ' '; I.getKey()->print(OS); OS << " : ";
575 I.getData().print(OS); OS << nl;
576 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000577}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000578
Zhongxing Xud463d442008-11-02 12:13:30 +0000579Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000580 SVal Init) {
581 QualType T = R->getType(getContext());
582 assert(T->isArrayType());
583
584 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
585
586 llvm::APInt Size = CAT->getSize();
587
588 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
589
590 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
591
592 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
593
594 for (; i != Size; ++i) {
595 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
596
597 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
598
599 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
600 // The init list might be shorter than the array decl.
601 if (VI != VE) ++VI;
602 }
603
604 return store;
605}
606
Zhongxing Xud463d442008-11-02 12:13:30 +0000607// Bind all elements of the array to some value.
608Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
609 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000610 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000611 assert(T->isArrayType());
612
Zhongxing Xua82512a2008-10-24 08:42:28 +0000613 // Only handle constant size array for now.
614 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
615
616 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000617 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
618 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000619 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000620
621 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
622
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000623 if (CAT->getElementType()->isStructureType())
624 store = BindStructToVal(store, ER, V);
625 else
626 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000627 }
628 }
629
630 return store;
631}
632
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000633Store RegionStoreManager::BindArrayToSymVal(Store store,
634 const TypedRegion* BaseR) {
635 QualType T = BaseR->getType(getContext());
636 assert(T->isArrayType());
637
638 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
639 llvm::APInt Size = CAT->getSize();
640 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
641 for (; i != Size; ++i) {
642 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
643
644 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
645
646 if (CAT->getElementType()->isStructureType()) {
647 store = BindStructToSymVal(store, ER);
648 }
649 else {
650 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
651 &Idx.getValue(), CAT->getElementType());
652 store = Bind(store, loc::MemRegionVal(ER), V);
653 }
654 }
655 }
656
657 return store;
658}
659
Zhongxing Xud463d442008-11-02 12:13:30 +0000660Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000661 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000662 QualType T = R->getType(getContext());
663 assert(T->isStructureType());
664
665 RecordType* RT = cast<RecordType>(T.getTypePtr());
666 RecordDecl* RD = RT->getDecl();
667 assert(RD->isDefinition());
668
669 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
670 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
671 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
672
673 for (; FI != FE; ++FI) {
674 QualType FTy = (*FI)->getType();
675 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
676
677 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
678 if (VI != VE) {
679 store = Bind(store, loc::MemRegionVal(FR), *VI);
680 ++VI;
681 } else
682 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
683 }
684 else if (FTy->isArrayType()) {
685 if (VI != VE) {
686 store = InitializeArray(store, FR, *VI);
687 ++VI;
688 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000689 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000690 }
691 else if (FTy->isStructureType()) {
692 if (VI != VE) {
693 store = InitializeStruct(store, FR, *VI);
694 ++VI;
695 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000696 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000697 }
698 }
699 return store;
700}
701
Zhongxing Xud463d442008-11-02 12:13:30 +0000702// Bind all fields of the struct to some value.
703Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
704 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000705 QualType T = BaseR->getType(getContext());
706 assert(T->isStructureType());
707
708 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000709 RecordDecl* RD = RT->getDecl();
710 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000711
712 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
713
714 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000715
716 QualType FTy = (*I)->getType();
717 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
718
719 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000720 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000721
722 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000723 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000724
725 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000726 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000727 }
728 }
729
730 return store;
731}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000732
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000733Store RegionStoreManager::BindStructToSymVal(Store store,
734 const TypedRegion* BaseR) {
735 QualType T = BaseR->getType(getContext());
736 assert(T->isStructureType());
737
738 const RecordType* RT = cast<RecordType>(T.getTypePtr());
739 RecordDecl* RD = RT->getDecl();
740 assert(RD->isDefinition());
741
742 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
743
744 for (; I != E; ++I) {
745 QualType FTy = (*I)->getType();
746 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
747
748 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
749 store = Bind(store, loc::MemRegionVal(FR),
750 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
751 }
752 else if (FTy->isArrayType()) {
753 store = BindArrayToSymVal(store, FR);
754 }
755 else if (FTy->isStructureType()) {
756 store = BindStructToSymVal(store, FR);
757 }
758 }
759
760 return store;
761}
762
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000763const GRState* RegionStoreManager::AddRegionView(const GRState* St,
764 const MemRegion* View,
765 const MemRegion* Base) {
766 GRStateRef state(St, StateMgr);
767
768 // First, retrieve the region view of the base region.
769 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
770 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
771
772 // Now add View to the region view.
773 L = RVFactory.Add(View, L);
774
775 // Create a new state with the new region view.
776 return state.set<RegionViewMapTy>(Base, L);
777}