blob: 183120842f407799c58ea24feaede6dc99ef56cc [file] [log] [blame]
Zhongxing Xu79c57f82008-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 Xu8fbe7ae2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xuca892b82008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu79c57f82008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xu097fc982008-10-17 05:57:07 +000029typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xu8fbe7ae2008-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 Xu79c57f82008-10-08 02:50:44 +000041
42namespace {
43
44class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
45 RegionBindingsTy::Factory RBFactory;
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000046 RegionViewTy::Factory RVFactory;
47 RegionViewMapTy::Factory RVMFactory;
48
Zhongxing Xu79c57f82008-10-08 02:50:44 +000049 GRStateManager& StateMgr;
50 MemRegionManager MRMgr;
51
52public:
53 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xu8fbe7ae2008-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 Xu79c57f82008-10-08 02:50:44 +000059
60 virtual ~RegionStoreManager() {}
61
Zhongxing Xue4b6fc22008-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 Kremenekd83daa52008-10-27 21:54:31 +000068
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000069 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000070
Zhongxing Xu2abba442008-10-25 14:18:57 +000071 SVal getLValueString(const GRState* St, const StringLiteral* S);
72
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000073 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
74
Zhongxing Xu6f1b5152008-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 Xu0972d0a2008-10-24 01:09:32 +000081 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
82
83 SVal ArrayToPointer(SVal Array);
84
Zhongxing Xu0a322a52008-11-16 07:06:26 +000085 std::pair<const GRState*, SVal>
86 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000087
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000088 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xu6f1b5152008-10-22 13:44:38 +000089
Zhongxing Xu73249322008-10-21 06:27:32 +000090 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +000091
Zhongxing Xue4b6fc22008-10-24 01:38:55 +000092 Store Remove(Store store, Loc LV) {
93 // FIXME: Implement.
94 return store;
95 }
96
Zhongxing Xu79c57f82008-10-08 02:50:44 +000097 Store getInitialStore();
Ted Kremenek73a36c92008-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 Xu79c57f82008-10-08 02:50:44 +0000106
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000107 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
108 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xuab42da32008-11-10 09:39:04 +0000109 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000110
Ted Kremenek37b78a12008-11-12 19:18:35 +0000111 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000112
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000113 static inline RegionBindingsTy GetRegionBindings(Store store) {
114 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
115 }
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000116
Zhongxing Xu6149e882008-10-24 04:33:15 +0000117 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000118
119 void iterBindings(Store store, BindingsHandler& f) {
120 // FIXME: Implement.
121 }
Zhongxing Xu702d4702008-10-24 08:42:28 +0000122
123private:
124 Loc getVarLoc(const VarDecl* VD) {
125 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
126 }
127
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000128 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
129 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
130 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
131 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000132
133 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xu29454f42008-10-31 08:10:01 +0000134 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000135 // Utility methods.
136 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
137 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000138
139 const GRState* AddRegionView(const GRState* St,
140 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000141};
142
143} // end anonymous namespace
144
Ted Kremenekc3803992008-10-24 01:04:59 +0000145StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xue4b6fc22008-10-24 01:38:55 +0000146 return new RegionStoreManager(StMgr);
Ted Kremenekc3803992008-10-24 01:04:59 +0000147}
148
Zhongxing Xu2abba442008-10-25 14:18:57 +0000149SVal RegionStoreManager::getLValueString(const GRState* St,
150 const StringLiteral* S) {
151 return loc::MemRegionVal(MRMgr.getStringRegion(S));
152}
153
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000154SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
155 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
156}
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000157
158SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
159 const CompoundLiteralExpr* CL) {
160 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
161}
162
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000163SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
164 SVal Base) {
165 return UnknownVal();
166}
167
168SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
169 const FieldDecl* D) {
170 if (Base.isUnknownOrUndef())
171 return Base;
172
173 Loc BaseL = cast<Loc>(Base);
174 const MemRegion* BaseR = 0;
175
176 switch (BaseL.getSubKind()) {
177 case loc::MemRegionKind:
178 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
179 break;
180
181 case loc::SymbolValKind:
182 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
183 break;
184
185 case loc::GotoLabelKind:
186 case loc::FuncValKind:
187 // These are anormal cases. Flag an undefined value.
188 return UndefinedVal();
189
190 case loc::ConcreteIntKind:
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000191 // While these seem funny, this can happen through casts.
192 // FIXME: What we should return is the field offset. For example,
193 // add the field offset to the integer value. That way funny things
194 // like this work properly: &(((struct foo *) 0xa)->f)
195 return Base;
196
197 default:
Zhongxing Xuedfbcd92008-11-07 08:57:30 +0000198 assert(0 && "Unhandled Base.");
Zhongxing Xu6f1b5152008-10-22 13:44:38 +0000199 return Base;
200 }
201
202 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
203}
204
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000205SVal RegionStoreManager::getLValueElement(const GRState* St,
206 SVal Base, SVal Offset) {
207 if (Base.isUnknownOrUndef())
208 return Base;
209
Zhongxing Xu13a05fa2008-10-27 12:23:17 +0000210 if (isa<loc::SymbolVal>(Base))
211 return Base;
212
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000213 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
214
Zhongxing Xu853c6f62008-11-13 09:48:44 +0000215 // Pointer of any type can be cast and used as array base. We do not support
216 // that case yet.
217 if (!isa<ElementRegion>(BaseL.getRegion())) {
218 // Record what we have seen in real code.
219 assert(isa<FieldRegion>(BaseL.getRegion()));
220 return UnknownVal();
221 }
222
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000223 // We expect BaseR is an ElementRegion, not a base VarRegion.
224
225 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
226
227 SVal Idx = ElemR->getIndex();
228
229 nonloc::ConcreteInt *CI1, *CI2;
230
231 // Only handle integer indices for now.
232 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
233 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xuec12b812008-11-13 09:15:14 +0000234
235 // Temporary SVal to hold a potential signed APSInt.
236 SVal SignedInt;
237
238 // Index might be unsigned. We have to convert it to signed.
239 if (CI2->getValue().isUnsigned()) {
240 llvm::APSInt SI = CI2->getValue();
241 SI.setIsSigned(true);
242 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
243 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
244 }
245
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000246 SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add,
247 *CI2);
248 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
249 ElemR->getSuperRegion()));
250 }
251
252 return UnknownVal();
253}
254
255// Cast 'pointer to array' to 'pointer to the first element of array'.
256
257SVal RegionStoreManager::ArrayToPointer(SVal Array) {
258 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu2abba442008-10-25 14:18:57 +0000259 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
260
Zhongxing Xub7d9bb52008-11-15 05:18:50 +0000261 nonloc::ConcreteInt Idx(BasicVals.getZeroWithPtrWidth(false));
Zhongxing Xu5ac8bf12008-10-26 02:23:57 +0000262 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
263
264 return loc::MemRegionVal(ER);
Zhongxing Xu0972d0a2008-10-24 01:09:32 +0000265}
266
Zhongxing Xu0a322a52008-11-16 07:06:26 +0000267std::pair<const GRState*, SVal>
268RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
269 QualType CastToTy, Stmt* CastE) {
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000270 if (const AllocaRegion* AR =
271 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
272
273 // Create a new region to attach type information to it.
274 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
275
276 // Get the pointer to the first element.
277 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
278 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
279
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000280 // Add a RegionView to base region.
Zhongxing Xu0a322a52008-11-16 07:06:26 +0000281 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
282 loc::MemRegionVal(ER));
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000283 }
284
285 // Default case.
Zhongxing Xu0a322a52008-11-16 07:06:26 +0000286 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000287}
288
Zhongxing Xu73249322008-10-21 06:27:32 +0000289SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000290 assert(!isa<UnknownVal>(L) && "location unknown");
291 assert(!isa<UndefinedVal>(L) && "location undefined");
292
293 switch (L.getSubKind()) {
294 case loc::MemRegionKind: {
295 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
296 assert(R && "bad region");
297
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000298 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
299 if (TR->getType(getContext())->isStructureType())
300 return RetrieveStruct(S, TR);
301
Zhongxing Xue3954d12008-10-21 05:29:26 +0000302 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
303 RegionBindingsTy::data_type* V = B.lookup(R);
304 return V ? *V : UnknownVal();
305 }
306
307 case loc::SymbolValKind:
308 return UnknownVal();
309
310 case loc::ConcreteIntKind:
311 return UndefinedVal(); // As in BasicStoreManager.
312
313 case loc::FuncValKind:
314 return L;
315
Zhongxing Xue3954d12008-10-21 05:29:26 +0000316 default:
317 assert(false && "Invalid Location");
Ted Kremenek2ced1b32008-11-19 00:27:37 +0000318 return L;
Zhongxing Xue3954d12008-10-21 05:29:26 +0000319 }
320}
321
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000322SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
323 QualType T = R->getType(getContext());
324 assert(T->isStructureType());
325
326 const RecordType* RT = cast<RecordType>(T.getTypePtr());
327 RecordDecl* RD = RT->getDecl();
328 assert(RD->isDefinition());
329
330 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
331
332 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
333 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
334 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu29454f42008-10-31 08:10:01 +0000335 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6f267e52008-10-31 07:16:08 +0000336
337 SVal FieldValue = data ? *data : UnknownVal();
338
339 StructVal = getBasicVals().consVals(FieldValue, StructVal);
340 }
341
342 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
343}
344
Zhongxing Xu73249322008-10-21 06:27:32 +0000345Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xue7c8a132008-10-27 09:24:07 +0000346 if (LV.getSubKind() == loc::SymbolValKind)
347 return store;
348
Zhongxing Xu097fc982008-10-17 05:57:07 +0000349 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000350
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000351 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000352
Zhongxing Xu29454f42008-10-31 08:10:01 +0000353 assert(R);
354
355 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
356 if (TR->getType(getContext())->isStructureType())
357 return BindStruct(store, TR, V);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000358
359 RegionBindingsTy B = GetRegionBindings(store);
360 return V.isUnknown()
361 ? RBFactory.Remove(B, R).getRoot()
362 : RBFactory.Add(B, R, V).getRoot();
363}
364
Zhongxing Xu29454f42008-10-31 08:10:01 +0000365Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
366 QualType T = R->getType(getContext());
367 assert(T->isStructureType());
368
369 const RecordType* RT = cast<RecordType>(T.getTypePtr());
370 RecordDecl* RD = RT->getDecl();
Zhongxing Xuca1c2522008-11-13 08:41:36 +0000371
372 if (!RD->isDefinition()) {
373 // This can only occur when a pointer of imcomplete struct type is used as a
374 // function argument.
375 assert(V.isUnknown());
376 return store;
377 }
Zhongxing Xu29454f42008-10-31 08:10:01 +0000378
379 RegionBindingsTy B = GetRegionBindings(store);
380
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000381 if (isa<UnknownVal>(V))
382 return BindStructToVal(store, R, UnknownVal());
383
Zhongxing Xu29454f42008-10-31 08:10:01 +0000384 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
385
386 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
387 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
388
389 for (; FI != FE; ++FI, ++VI) {
390 assert(VI != VE);
391
392 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
393
394 B = RBFactory.Add(B, FR, *VI);
395 }
396
397 return B.getRoot();
398}
399
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000400Store RegionStoreManager::getInitialStore() {
401 typedef LiveVariables::AnalysisDataTy LVDataTy;
402 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
403
404 Store St = RBFactory.GetEmptyMap().getRoot();
405
406 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregord2baafd2008-10-21 16:13:35 +0000407 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000408
Douglas Gregord2baafd2008-10-21 16:13:35 +0000409 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000410 // Punt on static variables for now.
411 if (VD->getStorageClass() == VarDecl::Static)
412 continue;
413
414 QualType T = VD->getType();
415 // Only handle pointers and integers for now.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000416 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000417 // Initialize globals and parameters to symbolic values.
418 // Initialize local variables to undefined.
Zhongxing Xu097fc982008-10-17 05:57:07 +0000419 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000420 isa<ImplicitParamDecl>(VD))
Zhongxing Xu097fc982008-10-17 05:57:07 +0000421 ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD)
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000422 : UndefinedVal();
423
Zhongxing Xu73249322008-10-21 06:27:32 +0000424 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000425 }
426 }
427 }
428 return St;
429}
Zhongxing Xue3954d12008-10-21 05:29:26 +0000430
Ted Kremenek37b78a12008-11-12 19:18:35 +0000431Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
432 SVal* InitVal, unsigned Count) {
433
Zhongxing Xue3954d12008-10-21 05:29:26 +0000434 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Zhongxing Xue3954d12008-10-21 05:29:26 +0000435
436 if (VD->hasGlobalStorage()) {
437 // Static global variables should not be visited here.
438 assert(!(VD->getStorageClass() == VarDecl::Static &&
439 VD->isFileVarDecl()));
440 // Process static variables.
441 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000442 if (!InitVal) {
Zhongxing Xue3954d12008-10-21 05:29:26 +0000443 // Only handle pointer and integer static variables.
444
445 QualType T = VD->getType();
446
447 if (Loc::IsLocType(T))
Zhongxing Xu73249322008-10-21 06:27:32 +0000448 store = Bind(store, getVarLoc(VD),
449 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xue3954d12008-10-21 05:29:26 +0000450
451 else if (T->isIntegerType())
Zhongxing Xu73249322008-10-21 06:27:32 +0000452 store = Bind(store, getVarLoc(VD),
453 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xub30a0732008-10-31 10:24:47 +0000454
455 // Other types of static local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000456 } else {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000457 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000458 }
459 }
460 } else {
461 // Process local variables.
462
463 QualType T = VD->getType();
464
Zhongxing Xu702d4702008-10-24 08:42:28 +0000465 VarRegion* VR = MRMgr.getVarRegion(VD);
466
Zhongxing Xue3954d12008-10-21 05:29:26 +0000467 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek37b78a12008-11-12 19:18:35 +0000468 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu702d4702008-10-24 08:42:28 +0000469 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek37b78a12008-11-12 19:18:35 +0000470 }
471 else if (T->isArrayType()) {
472 if (!InitVal)
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000473 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xub30a0732008-10-31 10:24:47 +0000474 else
Ted Kremenek37b78a12008-11-12 19:18:35 +0000475 store = InitializeArray(store, VR, *InitVal);
476 }
477 else if (T->isStructureType()) {
478 if (!InitVal)
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000479 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000480 else
Ted Kremenek37b78a12008-11-12 19:18:35 +0000481 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xue3954d12008-10-21 05:29:26 +0000482 }
Zhongxing Xub30a0732008-10-31 10:24:47 +0000483
484 // Other types of local variables are not handled yet.
Zhongxing Xue3954d12008-10-21 05:29:26 +0000485 }
486 return store;
487}
488
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +0000489Store RegionStoreManager::BindCompoundLiteral(Store store,
490 const CompoundLiteralExpr* CL,
491 SVal V) {
492 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
493 store = Bind(store, loc::MemRegionVal(R), V);
494 return store;
495}
496
Zhongxing Xuab42da32008-11-10 09:39:04 +0000497Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
498 const LiveVariables& Live,
499 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
500 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
501
502 RegionBindingsTy B = GetRegionBindings(store);
503 typedef SVal::symbol_iterator symbol_iterator;
504
505 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
506 // in SymbolicRegions.
507 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
508 SVal X = I.getData();
509 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
510 LSymbols.insert(*SI);
511 }
512
513 return store;
514}
515
Zhongxing Xuca892b82008-10-24 06:01:33 +0000516void RegionStoreManager::print(Store store, std::ostream& Out,
517 const char* nl, const char *sep) {
518 llvm::raw_os_ostream OS(Out);
519 RegionBindingsTy B = GetRegionBindings(store);
520 OS << "Store:" << nl;
521
522 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
523 OS << ' '; I.getKey()->print(OS); OS << " : ";
524 I.getData().print(OS); OS << nl;
525 }
Zhongxing Xu6149e882008-10-24 04:33:15 +0000526}
Zhongxing Xu702d4702008-10-24 08:42:28 +0000527
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000528Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xub30a0732008-10-31 10:24:47 +0000529 SVal Init) {
530 QualType T = R->getType(getContext());
531 assert(T->isArrayType());
532
533 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
534
535 llvm::APInt Size = CAT->getSize();
536
537 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
538
539 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
540
541 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
542
543 for (; i != Size; ++i) {
544 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
545
546 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
547
548 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
549 // The init list might be shorter than the array decl.
550 if (VI != VE) ++VI;
551 }
552
553 return store;
554}
555
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000556// Bind all elements of the array to some value.
557Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
558 SVal V){
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000559 QualType T = BaseR->getType(getContext());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000560 assert(T->isArrayType());
561
Zhongxing Xu702d4702008-10-24 08:42:28 +0000562 // Only handle constant size array for now.
563 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
564
565 llvm::APInt Size = CAT->getSize();
Zhongxing Xub30a0732008-10-31 10:24:47 +0000566 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
567 for (; i != Size; ++i) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000568 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xu702d4702008-10-24 08:42:28 +0000569
570 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
571
Zhongxing Xu42ca6cc2008-11-18 13:11:04 +0000572 if (CAT->getElementType()->isStructureType())
573 store = BindStructToVal(store, ER, V);
574 else
575 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000576 }
577 }
578
579 return store;
580}
581
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000582Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000583 SVal Init) {
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000584 QualType T = R->getType(getContext());
585 assert(T->isStructureType());
586
587 RecordType* RT = cast<RecordType>(T.getTypePtr());
588 RecordDecl* RD = RT->getDecl();
589 assert(RD->isDefinition());
590
591 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
592 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
593 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
594
595 for (; FI != FE; ++FI) {
596 QualType FTy = (*FI)->getType();
597 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
598
599 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
600 if (VI != VE) {
601 store = Bind(store, loc::MemRegionVal(FR), *VI);
602 ++VI;
603 } else
604 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
605 }
606 else if (FTy->isArrayType()) {
607 if (VI != VE) {
608 store = InitializeArray(store, FR, *VI);
609 ++VI;
610 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000611 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000612 }
613 else if (FTy->isStructureType()) {
614 if (VI != VE) {
615 store = InitializeStruct(store, FR, *VI);
616 ++VI;
617 } else
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000618 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuc00c55a2008-10-31 10:53:01 +0000619 }
620 }
621 return store;
622}
623
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000624// Bind all fields of the struct to some value.
625Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
626 SVal V) {
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000627 QualType T = BaseR->getType(getContext());
628 assert(T->isStructureType());
629
630 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xu702d4702008-10-24 08:42:28 +0000631 RecordDecl* RD = RT->getDecl();
632 assert(RD->isDefinition());
Zhongxing Xu3dd9ec82008-10-31 11:02:48 +0000633
634 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
635
636 for (; I != E; ++I) {
Zhongxing Xu702d4702008-10-24 08:42:28 +0000637
638 QualType FTy = (*I)->getType();
639 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
640
641 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000642 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000643
644 } else if (FTy->isArrayType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000645 store = BindArrayToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000646
647 } else if (FTy->isStructureType()) {
Zhongxing Xu9ef12d32008-11-02 12:13:30 +0000648 store = BindStructToVal(store, FR, V);
Zhongxing Xu702d4702008-10-24 08:42:28 +0000649 }
650 }
651
652 return store;
653}
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000654
655const GRState* RegionStoreManager::AddRegionView(const GRState* St,
656 const MemRegion* View,
657 const MemRegion* Base) {
658 GRStateRef state(St, StateMgr);
659
660 // First, retrieve the region view of the base region.
661 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
662 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
663
664 // Now add View to the region view.
665 L = RVFactory.Add(View, L);
666
667 // Create a new state with the new region view.
668 return state.set<RegionViewMapTy>(Base, L);
669}