blob: 6f632f4bc51d6ddfd5edc2bd8a460b51dd3b7c6f [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xu1c96b242008-10-17 05:57:07 +000029typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000030typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
31typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
32
33static int RegionViewMapTyIndex = 0;
34
35namespace clang {
36template<> struct GRStateTrait<RegionViewMapTy>
37 : public GRStatePartialTrait<RegionViewMapTy> {
38 static void* GDMIndex() { return &RegionViewMapTyIndex; }
39};
40}
Zhongxing Xu17892752008-10-08 02:50:44 +000041
42namespace {
43
44class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
45 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000046 RegionViewTy::Factory RVFactory;
47 RegionViewMapTy::Factory RVMFactory;
48
Zhongxing Xu17892752008-10-08 02:50:44 +000049 GRStateManager& StateMgr;
50 MemRegionManager MRMgr;
51
52public:
53 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000054 : RBFactory(mgr.getAllocator()),
55 RVFactory(mgr.getAllocator()),
56 RVMFactory(mgr.getAllocator()),
57 StateMgr(mgr),
58 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000059
60 virtual ~RegionStoreManager() {}
61
Zhongxing Xu24194ef2008-10-24 01:38:55 +000062 MemRegionManager& getRegionManager() { return MRMgr; }
63
64 // FIXME: Is this function necessary?
65 SVal GetRegionSVal(Store St, const MemRegion* R) {
66 return Retrieve(St, loc::MemRegionVal(R));
67 }
Ted Kremenek4f090272008-10-27 21:54:31 +000068
Zhongxing Xuf22679e2008-11-07 10:38:33 +000069 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000070
Zhongxing Xu143bf822008-10-25 14:18:57 +000071 SVal getLValueString(const GRState* St, const StringLiteral* S);
72
Zhongxing Xuf22679e2008-11-07 10:38:33 +000073 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
74
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000075 SVal getLValueVar(const GRState* St, const VarDecl* VD);
76
77 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
78
79 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
80
Zhongxing Xub1d542a2008-10-24 01:09:32 +000081 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
82
83 SVal ArrayToPointer(SVal Array);
84
Zhongxing Xucb529b52008-11-16 07:06:26 +000085 std::pair<const GRState*, SVal>
86 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000087
Zhongxing Xu24194ef2008-10-24 01:38:55 +000088 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000089
Zhongxing Xu8485ec62008-10-21 06:27:32 +000090 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +000091
Zhongxing Xu24194ef2008-10-24 01:38:55 +000092 Store Remove(Store store, Loc LV) {
93 // FIXME: Implement.
94 return store;
95 }
96
Zhongxing Xu17892752008-10-08 02:50:44 +000097 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +000098
99 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
100 /// 'this' object (C++). When used when analyzing a normal function this
101 /// method returns NULL.
102 const MemRegion* getSelfRegion(Store) {
103 assert (false && "Not implemented.");
104 return 0;
105 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000106
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000107 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
108 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000109 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000110
Ted Kremenek42577d12008-11-12 19:18:35 +0000111 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000112
Zhongxing Xu17892752008-10-08 02:50:44 +0000113 static inline RegionBindingsTy GetRegionBindings(Store store) {
114 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
115 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000116
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000117 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000118
119 void iterBindings(Store store, BindingsHandler& f) {
120 // FIXME: Implement.
121 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000122
123private:
124 Loc getVarLoc(const VarDecl* VD) {
125 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
126 }
127
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000128 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
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 Xu6e3f01c2008-10-31 07:16:08 +0000140 // Utility methods.
141 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
142 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000143
144 const GRState* AddRegionView(const GRState* St,
145 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000146};
147
148} // end anonymous namespace
149
Ted Kremenek95c7b002008-10-24 01:04:59 +0000150StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000151 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000152}
153
Zhongxing Xu143bf822008-10-25 14:18:57 +0000154SVal RegionStoreManager::getLValueString(const GRState* St,
155 const StringLiteral* S) {
156 return loc::MemRegionVal(MRMgr.getStringRegion(S));
157}
158
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000159SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
160 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
161}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000162
163SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
164 const CompoundLiteralExpr* CL) {
165 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
166}
167
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000168SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
169 SVal Base) {
170 return UnknownVal();
171}
172
173SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
174 const FieldDecl* D) {
175 if (Base.isUnknownOrUndef())
176 return Base;
177
178 Loc BaseL = cast<Loc>(Base);
179 const MemRegion* BaseR = 0;
180
181 switch (BaseL.getSubKind()) {
182 case loc::MemRegionKind:
183 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
184 break;
185
186 case loc::SymbolValKind:
187 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
188 break;
189
190 case loc::GotoLabelKind:
191 case loc::FuncValKind:
192 // These are anormal cases. Flag an undefined value.
193 return UndefinedVal();
194
195 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000196 // While these seem funny, this can happen through casts.
197 // FIXME: What we should return is the field offset. For example,
198 // add the field offset to the integer value. That way funny things
199 // like this work properly: &(((struct foo *) 0xa)->f)
200 return Base;
201
202 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000203 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000204 return Base;
205 }
206
207 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
208}
209
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000210SVal RegionStoreManager::getLValueElement(const GRState* St,
211 SVal Base, SVal Offset) {
212 if (Base.isUnknownOrUndef())
213 return Base;
214
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000215 if (isa<loc::SymbolVal>(Base))
216 return Base;
217
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000218 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
219
Zhongxing Xue4d13932008-11-13 09:48:44 +0000220 // Pointer of any type can be cast and used as array base. We do not support
221 // that case yet.
222 if (!isa<ElementRegion>(BaseL.getRegion())) {
223 // Record what we have seen in real code.
224 assert(isa<FieldRegion>(BaseL.getRegion()));
225 return UnknownVal();
226 }
227
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000228 // We expect BaseR is an ElementRegion, not a base VarRegion.
229
230 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
231
232 SVal Idx = ElemR->getIndex();
233
234 nonloc::ConcreteInt *CI1, *CI2;
235
236 // Only handle integer indices for now.
237 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
238 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000239
240 // Temporary SVal to hold a potential signed APSInt.
241 SVal SignedInt;
242
243 // Index might be unsigned. We have to convert it to signed.
244 if (CI2->getValue().isUnsigned()) {
245 llvm::APSInt SI = CI2->getValue();
246 SI.setIsSigned(true);
247 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
248 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
249 }
250
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000251 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
252 *CI2);
253 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
254 ElemR->getSuperRegion()));
255 }
256
257 return UnknownVal();
258}
259
260// Cast 'pointer to array' to 'pointer to the first element of array'.
261
262SVal RegionStoreManager::ArrayToPointer(SVal Array) {
263 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000264 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
265
Zhongxing Xua09300a2008-11-15 05:18:50 +0000266 nonloc::ConcreteInt Idx(BasicVals.getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000267 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
268
269 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000270}
271
Zhongxing Xucb529b52008-11-16 07:06:26 +0000272std::pair<const GRState*, SVal>
273RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
274 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000275 if (const AllocaRegion* AR =
276 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
277
278 // Create a new region to attach type information to it.
279 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
280
281 // Get the pointer to the first element.
282 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
283 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
284
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000285 // Add a RegionView to base region.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000286 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
287 loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000288 }
289
290 // Default case.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000291 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000292}
293
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000294SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000295 assert(!isa<UnknownVal>(L) && "location unknown");
296 assert(!isa<UndefinedVal>(L) && "location undefined");
297
298 switch (L.getSubKind()) {
299 case loc::MemRegionKind: {
300 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
301 assert(R && "bad region");
302
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000303 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
304 if (TR->getType(getContext())->isStructureType())
305 return RetrieveStruct(S, TR);
306
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000307 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
308 RegionBindingsTy::data_type* V = B.lookup(R);
309 return V ? *V : UnknownVal();
310 }
311
312 case loc::SymbolValKind:
313 return UnknownVal();
314
315 case loc::ConcreteIntKind:
316 return UndefinedVal(); // As in BasicStoreManager.
317
318 case loc::FuncValKind:
319 return L;
320
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000321 default:
322 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000323 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000324 }
325}
326
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000327SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
328 QualType T = R->getType(getContext());
329 assert(T->isStructureType());
330
331 const RecordType* RT = cast<RecordType>(T.getTypePtr());
332 RecordDecl* RD = RT->getDecl();
333 assert(RD->isDefinition());
334
335 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
336
337 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
338 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
339 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000340 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000341
342 SVal FieldValue = data ? *data : UnknownVal();
343
344 StructVal = getBasicVals().consVals(FieldValue, StructVal);
345 }
346
347 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
348}
349
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000350Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000351 if (LV.getSubKind() == loc::SymbolValKind)
352 return store;
353
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000354 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000355
Ted Kremenek993f1c72008-10-17 20:28:54 +0000356 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000357
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000358 assert(R);
359
360 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
361 if (TR->getType(getContext())->isStructureType())
362 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000363
364 RegionBindingsTy B = GetRegionBindings(store);
365 return V.isUnknown()
366 ? RBFactory.Remove(B, R).getRoot()
367 : RBFactory.Add(B, R, V).getRoot();
368}
369
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000370Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
371 QualType T = R->getType(getContext());
372 assert(T->isStructureType());
373
374 const RecordType* RT = cast<RecordType>(T.getTypePtr());
375 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000376
377 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000378 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000379 // function argument.
380 assert(V.isUnknown());
381 return store;
382 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000383
384 RegionBindingsTy B = GetRegionBindings(store);
385
Zhongxing Xud463d442008-11-02 12:13:30 +0000386 if (isa<UnknownVal>(V))
387 return BindStructToVal(store, R, UnknownVal());
388
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000389 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
390
391 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
392 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
393
394 for (; FI != FE; ++FI, ++VI) {
395 assert(VI != VE);
396
397 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
398
399 B = RBFactory.Add(B, FR, *VI);
400 }
401
402 return B.getRoot();
403}
404
Zhongxing Xu17892752008-10-08 02:50:44 +0000405Store RegionStoreManager::getInitialStore() {
406 typedef LiveVariables::AnalysisDataTy LVDataTy;
407 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
408
409 Store St = RBFactory.GetEmptyMap().getRoot();
410
411 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000412 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000413
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000414 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000415 // Punt on static variables for now.
416 if (VD->getStorageClass() == VarDecl::Static)
417 continue;
418
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000419 VarRegion* VR = MRMgr.getVarRegion(VD);
420
Zhongxing Xu17892752008-10-08 02:50:44 +0000421 QualType T = VD->getType();
422 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000423 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000424 // Initialize globals and parameters to symbolic values.
425 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000426 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000427 isa<ImplicitParamDecl>(VD))
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000428 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000429 : UndefinedVal();
430
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000431 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000432 }
433 else if (T->isArrayType()) {
434 if (VD->hasGlobalStorage()) // Params cannot have array type.
435 St = BindArrayToSymVal(St, VR);
436 else
437 St = BindArrayToVal(St, VR, UndefinedVal());
438 }
439 else if (T->isStructureType()) {
440 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
441 isa<ImplicitParamDecl>(VD))
442 St = BindStructToSymVal(St, VR);
443 else
444 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000445 }
446 }
447 }
448 return St;
449}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000450
Ted Kremenek42577d12008-11-12 19:18:35 +0000451Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
452 SVal* InitVal, unsigned Count) {
453
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000454 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000455
456 if (VD->hasGlobalStorage()) {
457 // Static global variables should not be visited here.
458 assert(!(VD->getStorageClass() == VarDecl::Static &&
459 VD->isFileVarDecl()));
460 // Process static variables.
461 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000462 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000463 // Only handle pointer and integer static variables.
464
465 QualType T = VD->getType();
466
467 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000468 store = Bind(store, getVarLoc(VD),
469 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000470
471 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000472 store = Bind(store, getVarLoc(VD),
473 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000474
475 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000476 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000477 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000478 }
479 }
480 } else {
481 // Process local variables.
482
483 QualType T = VD->getType();
484
Zhongxing Xua82512a2008-10-24 08:42:28 +0000485 VarRegion* VR = MRMgr.getVarRegion(VD);
486
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000487 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000488 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000489 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000490 }
491 else if (T->isArrayType()) {
492 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000493 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000494 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000495 store = InitializeArray(store, VR, *InitVal);
496 }
497 else if (T->isStructureType()) {
498 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000499 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000500 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000501 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000502 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000503
504 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000505 }
506 return store;
507}
508
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000509Store RegionStoreManager::BindCompoundLiteral(Store store,
510 const CompoundLiteralExpr* CL,
511 SVal V) {
512 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
513 store = Bind(store, loc::MemRegionVal(R), V);
514 return store;
515}
516
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000517Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
518 const LiveVariables& Live,
519 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
520 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
521
522 RegionBindingsTy B = GetRegionBindings(store);
523 typedef SVal::symbol_iterator symbol_iterator;
524
525 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
526 // in SymbolicRegions.
527 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
528 SVal X = I.getData();
529 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
530 LSymbols.insert(*SI);
531 }
532
533 return store;
534}
535
Zhongxing Xua071eb02008-10-24 06:01:33 +0000536void RegionStoreManager::print(Store store, std::ostream& Out,
537 const char* nl, const char *sep) {
538 llvm::raw_os_ostream OS(Out);
539 RegionBindingsTy B = GetRegionBindings(store);
540 OS << "Store:" << nl;
541
542 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
543 OS << ' '; I.getKey()->print(OS); OS << " : ";
544 I.getData().print(OS); OS << nl;
545 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000546}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000547
Zhongxing Xud463d442008-11-02 12:13:30 +0000548Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000549 SVal Init) {
550 QualType T = R->getType(getContext());
551 assert(T->isArrayType());
552
553 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
554
555 llvm::APInt Size = CAT->getSize();
556
557 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
558
559 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
560
561 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
562
563 for (; i != Size; ++i) {
564 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
565
566 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
567
568 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
569 // The init list might be shorter than the array decl.
570 if (VI != VE) ++VI;
571 }
572
573 return store;
574}
575
Zhongxing Xud463d442008-11-02 12:13:30 +0000576// Bind all elements of the array to some value.
577Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
578 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000579 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000580 assert(T->isArrayType());
581
Zhongxing Xua82512a2008-10-24 08:42:28 +0000582 // Only handle constant size array for now.
583 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
584
585 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000586 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
587 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000588 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000589
590 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
591
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000592 if (CAT->getElementType()->isStructureType())
593 store = BindStructToVal(store, ER, V);
594 else
595 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000596 }
597 }
598
599 return store;
600}
601
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000602Store RegionStoreManager::BindArrayToSymVal(Store store,
603 const TypedRegion* BaseR) {
604 QualType T = BaseR->getType(getContext());
605 assert(T->isArrayType());
606
607 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
608 llvm::APInt Size = CAT->getSize();
609 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
610 for (; i != Size; ++i) {
611 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
612
613 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
614
615 if (CAT->getElementType()->isStructureType()) {
616 store = BindStructToSymVal(store, ER);
617 }
618 else {
619 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
620 &Idx.getValue(), CAT->getElementType());
621 store = Bind(store, loc::MemRegionVal(ER), V);
622 }
623 }
624 }
625
626 return store;
627}
628
Zhongxing Xud463d442008-11-02 12:13:30 +0000629Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000630 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000631 QualType T = R->getType(getContext());
632 assert(T->isStructureType());
633
634 RecordType* RT = cast<RecordType>(T.getTypePtr());
635 RecordDecl* RD = RT->getDecl();
636 assert(RD->isDefinition());
637
638 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
639 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
640 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
641
642 for (; FI != FE; ++FI) {
643 QualType FTy = (*FI)->getType();
644 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
645
646 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
647 if (VI != VE) {
648 store = Bind(store, loc::MemRegionVal(FR), *VI);
649 ++VI;
650 } else
651 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
652 }
653 else if (FTy->isArrayType()) {
654 if (VI != VE) {
655 store = InitializeArray(store, FR, *VI);
656 ++VI;
657 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000658 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000659 }
660 else if (FTy->isStructureType()) {
661 if (VI != VE) {
662 store = InitializeStruct(store, FR, *VI);
663 ++VI;
664 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000665 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000666 }
667 }
668 return store;
669}
670
Zhongxing Xud463d442008-11-02 12:13:30 +0000671// Bind all fields of the struct to some value.
672Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
673 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000674 QualType T = BaseR->getType(getContext());
675 assert(T->isStructureType());
676
677 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000678 RecordDecl* RD = RT->getDecl();
679 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000680
681 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
682
683 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000684
685 QualType FTy = (*I)->getType();
686 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
687
688 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000689 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000690
691 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000692 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000693
694 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000695 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000696 }
697 }
698
699 return store;
700}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000701
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000702Store RegionStoreManager::BindStructToSymVal(Store store,
703 const TypedRegion* BaseR) {
704 QualType T = BaseR->getType(getContext());
705 assert(T->isStructureType());
706
707 const RecordType* RT = cast<RecordType>(T.getTypePtr());
708 RecordDecl* RD = RT->getDecl();
709 assert(RD->isDefinition());
710
711 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
712
713 for (; I != E; ++I) {
714 QualType FTy = (*I)->getType();
715 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
716
717 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
718 store = Bind(store, loc::MemRegionVal(FR),
719 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
720 }
721 else if (FTy->isArrayType()) {
722 store = BindArrayToSymVal(store, FR);
723 }
724 else if (FTy->isStructureType()) {
725 store = BindStructToSymVal(store, FR);
726 }
727 }
728
729 return store;
730}
731
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000732const GRState* RegionStoreManager::AddRegionView(const GRState* St,
733 const MemRegion* View,
734 const MemRegion* Base) {
735 GRStateRef state(St, StateMgr);
736
737 // First, retrieve the region view of the base region.
738 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
739 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
740
741 // Now add View to the region view.
742 L = RVFactory.Add(View, L);
743
744 // Create a new state with the new region view.
745 return state.set<RegionViewMapTy>(Base, L);
746}