blob: 528419f0a886cf21de4da28893c50bc06f5102b7 [file] [log] [blame]
Zhongxing Xud9959ae2008-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//===----------------------------------------------------------------------===//
Ted Kremenekd6b87082010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
18#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Checker/PathSensitive/GRState.h"
20#include "clang/Checker/PathSensitive/GRStateTrait.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek2f6eb142009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xuea8c48d2009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu228b0d42010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xuceca8062008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xu1359e002008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremenekfa417142009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek920ad712009-07-22 04:35:42 +000033
Ted Kremenek8e994a22010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek8e994a22010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek8e994a22010-01-11 00:07:44 +000040public:
Ted Kremenek64efd0d2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
44 uint64_t Offset;
Ted Kremenek8e994a22010-01-11 00:07:44 +000045
Ted Kremenek64efd0d2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubd96bf12010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremenek64efd0d2010-02-03 03:06:46 +000048public:
49
50 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
52
53 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremenek8e994a22010-01-11 00:07:44 +000055
56 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek8e994a22010-01-11 00:07:44 +000059 }
Ted Kremenek64efd0d2010-02-03 03:06:46 +000060
61 static BindingKey Make(const MemRegion *R, Kind k);
62
63 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
70
71 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremenek8e994a22010-01-11 00:07:44 +000075};
76} // end anonymous namespace
77
78namespace llvm {
79 static inline
80 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek8e994a22010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xu9165ed62008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek8e994a22010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremenek64efd0d2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xu9165ed62008-11-24 09:44:56 +000093
Ted Kremenekae189ec2008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump11289f42009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek4533a552009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump11289f42009-09-09 15:08:12 +0000105
Ted Kremenek4533a552009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek4533a552009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek4533a552009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump11289f42009-09-09 15:08:12 +0000114
Ted Kremenek4533a552009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000121// Region "Extents"
122//===----------------------------------------------------------------------===//
123//
124// MemRegions represent chunks of memory with a size (their "extent"). This
125// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenek682c3a62009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenekae189ec2008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenekae189ec2008-12-24 01:05:03 +0000130 template<> struct GRStateTrait<RegionExtents>
131 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
132 static void* GDMIndex() { return &RegionExtentsIndex; }
133 };
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000134}
135
Ted Kremenekae189ec2008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000137// Utility functions.
138//===----------------------------------------------------------------------===//
139
140static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
141 if (ty->isAnyPointerType())
142 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenek677779a2008-12-04 02:08:27 +0000151
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000152namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekb251eb62010-02-02 22:38:47 +0000155public:
156 typedef llvm::ImmutableSet<const MemRegion*> Set;
157 typedef llvm::DenseMap<const MemRegion*, Set> Map;
158private:
159 Set::Factory F;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000161public:
Ted Kremenek844a7292009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenek844a7292009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek68c1f012009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenek844a7292009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenekfa417142009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremenekb251eb62010-02-02 22:38:47 +0000177
178 const Set *getSubRegions(const MemRegion *Parent) const {
179 Map::const_iterator I = M.find(Parent);
180 return I == M.end() ? NULL : &I->second;
181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000187 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenekb251eb62010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000192 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000193 }
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000195 return true;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000196 }
Mike Stump11289f42009-09-09 15:08:12 +0000197};
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000198
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000199
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek4533a552009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek2c85f172009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremenekcc224242009-09-29 06:35:00 +0000203
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000204 typedef llvm::DenseMap<Store, RegionStoreSubRegionMap*> SMCache;
Ted Kremenekcc224242009-09-29 06:35:00 +0000205 SMCache SC;
206
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000207public:
Mike Stump11289f42009-09-09 15:08:12 +0000208 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek43015262009-07-29 21:43:22 +0000209 : StoreManager(mgr),
Ted Kremenek4533a552009-06-16 22:36:44 +0000210 Features(f),
Ted Kremenek33617e02010-02-04 04:14:49 +0000211 RBFactory(mgr.getAllocator()) {}
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000212
Ted Kremenekcc224242009-09-29 06:35:00 +0000213 virtual ~RegionStoreManager() {
214 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
215 delete (*I).second;
216 }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000217
Zhongxing Xuf6682042010-02-05 05:18:47 +0000218 SubRegionMap *getSubRegionMap(Store store) {
219 return getRegionStoreSubRegionMap(store);
220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000222 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000224 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
225 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000226 /// getDefaultBinding - Returns an SVal* representing an optional default
227 /// binding associated with a region and its subregions.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000228 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek439a6d12009-11-19 20:20:24 +0000229
230 /// setImplicitDefaultValue - Set the default binding for the provided
231 /// MemRegion to the value implicitly defined for compound literals when
232 /// the value is not specified.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000233 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek2907ab72008-12-24 07:46:32 +0000235 /// getLValueString - Returns an SVal representing the lvalue of a
236 /// StringLiteral. Within RegionStore a StringLiteral has an
237 /// associated StringRegion, and the lvalue of a StringLiteral is
238 /// the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000239 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000240
Ted Kremenek2907ab72008-12-24 07:46:32 +0000241 /// getLValueCompoundLiteral - Returns an SVal representing the
242 /// lvalue of a compound literal. Within RegionStore a compound
243 /// literal has an associated region, and the lvalue of the
244 /// compound literal is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000245 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000246
Ted Kremenek2907ab72008-12-24 07:46:32 +0000247 /// getLValueVar - Returns an SVal that represents the lvalue of a
248 /// variable. Within RegionStore a variable has an associated
249 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000250 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000252 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000253
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000254 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump11289f42009-09-09 15:08:12 +0000255
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000256 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000257
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000258 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000259
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000260
Ted Kremenek2907ab72008-12-24 07:46:32 +0000261 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
262 /// type. 'Array' represents the lvalue of the array being decayed
263 /// to a pointer, and the returned SVal represents the decayed
264 /// version of that lvalue (i.e., a pointer to the first element of
265 /// the array). This is called by GRExprEngine when evaluating
266 /// casts from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000267 SVal ArrayToPointer(Loc Array);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000268
Zhongxing Xu0d081f32010-02-05 05:24:20 +0000269 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000270
Mike Stump11289f42009-09-09 15:08:12 +0000271 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000272 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000273 }
Ted Kremenek608677a2009-08-21 23:25:54 +0000274
Ted Kremenek609df302009-06-17 22:02:04 +0000275 //===-------------------------------------------------------------------===//
276 // Binding values to regions.
277 //===-------------------------------------------------------------------===//
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000278
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000279 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
280 unsigned Count, InvalidatedSymbols *IS) {
281 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS);
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000282 }
283
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000284 Store InvalidateRegions(Store store,
285 const MemRegion * const *Begin,
286 const MemRegion * const *End,
287 const Expr *E, unsigned Count,
288 InvalidatedSymbols *IS);
Mike Stump11289f42009-09-09 15:08:12 +0000289
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000290public: // Made public for helper classes.
291
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000292 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremenekfa417142009-08-06 01:20:57 +0000293 RegionStoreSubRegionMap &M);
Mike Stump11289f42009-09-09 15:08:12 +0000294
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000295 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
296
297 RegionBindings Add(RegionBindings B, const MemRegion *R,
298 BindingKey::Kind k, SVal V);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000299
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000300 const SVal *Lookup(RegionBindings B, BindingKey K);
301 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000302
303 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000304 RegionBindings Remove(RegionBindings B, const MemRegion *R,
305 BindingKey::Kind k);
306
307 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
308 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
309 }
310
Ted Kremenek8e994a22010-01-11 00:07:44 +0000311 Store Remove(Store store, BindingKey K);
312
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000313public: // Part of public interface to class.
314
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000315 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek609df302009-06-17 22:02:04 +0000316
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000317 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
318 const LocationContext *LC, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000319
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000320 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000321
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000322 Store BindDeclWithNoInit(Store store, const VarRegion *) {
323 return store;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000324 }
Zhongxing Xu83aff702008-10-21 05:29:26 +0000325
Ted Kremenek609df302009-06-17 22:02:04 +0000326 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000327 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000329 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000330
331 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000332 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000333
Ted Kremenek609df302009-06-17 22:02:04 +0000334 Store Remove(Store store, Loc LV);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000335
Ted Kremenek609df302009-06-17 22:02:04 +0000336
337 //===------------------------------------------------------------------===//
338 // Loading values from regions.
339 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek609df302009-06-17 22:02:04 +0000341 /// The high level logic for this method is this:
342 /// Retrieve (L)
343 /// if L has binding
344 /// return L's binding
345 /// else if L is in killset
346 /// return unknown
347 /// else
348 /// if L is on stack or heap
349 /// return undefined
350 /// else
351 /// return symbolic
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000352 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xue67ea5c2009-06-25 04:50:44 +0000353
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000354 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xu2d160732009-06-25 05:29:39 +0000355
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000356 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000357
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000358 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000359
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000360 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000361
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000362 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000363
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000364 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek040e3b92009-08-06 22:33:36 +0000365 QualType Ty, const MemRegion *superR);
Mike Stump11289f42009-09-09 15:08:12 +0000366
Ted Kremenek609df302009-06-17 22:02:04 +0000367 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump11289f42009-09-09 15:08:12 +0000368 /// struct copy:
369 /// struct s x, y;
Ted Kremenek609df302009-06-17 22:02:04 +0000370 /// x = y;
371 /// y's value is retrieved by this method.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000372 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000373
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000374 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Zhongxing Xufd62a332009-12-21 06:52:24 +0000376 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000377 std::pair<Store, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +0000378 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000379
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000380 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
381 const TypedRegion *R);
Ted Kremenek609df302009-06-17 22:02:04 +0000382
Ted Kremenek267e45a2009-09-24 04:11:44 +0000383 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
384 QualType T);
385
Ted Kremenek609df302009-06-17 22:02:04 +0000386 //===------------------------------------------------------------------===//
387 // State pruning.
388 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek609df302009-06-17 22:02:04 +0000390 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
391 /// It returns a new Store with these values removed.
Zhongxing Xuad0ef842010-02-05 05:34:29 +0000392 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek609df302009-06-17 22:02:04 +0000393 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
394
Zhongxing Xudaa41762009-10-13 02:24:55 +0000395 const GRState *EnterStackFrame(const GRState *state,
396 const StackFrameContext *frame);
397
Ted Kremenek609df302009-06-17 22:02:04 +0000398 //===------------------------------------------------------------------===//
399 // Region "extents".
400 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000401
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000402 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xu383c2732009-11-12 02:48:32 +0000403 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000404 const MemRegion* R, QualType EleTy);
Ted Kremenek609df302009-06-17 22:02:04 +0000405
406 //===------------------------------------------------------------------===//
Ted Kremenek609df302009-06-17 22:02:04 +0000407 // Utility methods.
408 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000409
Ted Kremenek2c85f172009-08-06 04:50:20 +0000410 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000411 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000412 }
Zhongxing Xucebb7412008-10-24 01:38:55 +0000413
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000414 void print(Store store, llvm::raw_ostream& Out, const char* nl,
415 const char *sep);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000416
417 void iterBindings(Store store, BindingsHandler& f) {
418 // FIXME: Implement.
419 }
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000420
Ted Kremenek609df302009-06-17 22:02:04 +0000421 // FIXME: Remove.
422 BasicValueFactory& getBasicVals() {
423 return StateMgr.getBasicVals();
424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek609df302009-06-17 22:02:04 +0000426 // FIXME: Remove.
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000427 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000428};
429
430} // end anonymous namespace
431
Ted Kremenek4533a552009-06-16 22:36:44 +0000432//===----------------------------------------------------------------------===//
433// RegionStore creation.
434//===----------------------------------------------------------------------===//
435
436StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
437 RegionStoreFeatures F = maximal_features_tag();
438 return new RegionStoreManager(StMgr, F);
439}
440
441StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
442 RegionStoreFeatures F = minimal_features_tag();
443 F.enableFields(true);
444 return new RegionStoreManager(StMgr, F);
Ted Kremenek6779f892008-10-24 01:04:59 +0000445}
446
Ted Kremenekfa417142009-08-06 01:20:57 +0000447void
448RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump11289f42009-09-09 15:08:12 +0000449 const SubRegion *R) {
Ted Kremenekfa417142009-08-06 01:20:57 +0000450 const MemRegion *superR = R->getSuperRegion();
451 if (add(superR, R))
452 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump11289f42009-09-09 15:08:12 +0000453 WL.push_back(sr);
Ted Kremenekfa417142009-08-06 01:20:57 +0000454}
455
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000456RegionStoreSubRegionMap*
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000457RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
458 RegionBindings B = GetRegionBindings(store);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000459 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump11289f42009-09-09 15:08:12 +0000460
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000461 llvm::SmallVector<const SubRegion*, 10> WL;
462
Ted Kremenek2c85f172009-08-06 04:50:20 +0000463 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek8e994a22010-01-11 00:07:44 +0000464 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremenekfa417142009-08-06 01:20:57 +0000465 M->process(WL, R);
Mike Stump11289f42009-09-09 15:08:12 +0000466
Mike Stump11289f42009-09-09 15:08:12 +0000467 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000468 // don't have direct bindings but are super regions of those that do.
469 while (!WL.empty()) {
470 const SubRegion *R = WL.back();
471 WL.pop_back();
Ted Kremenekfa417142009-08-06 01:20:57 +0000472 M->process(WL, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000473 }
474
Ted Kremenek9f276d62009-03-03 19:02:42 +0000475 return M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000476}
Ted Kremenek2907ab72008-12-24 07:46:32 +0000477
Ted Kremenek4533a552009-06-16 22:36:44 +0000478//===----------------------------------------------------------------------===//
Ted Kremenekbca70672009-07-29 18:16:25 +0000479// Binding invalidation.
480//===----------------------------------------------------------------------===//
481
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000482void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
483 const MemRegion *R,
484 RegionStoreSubRegionMap &M) {
Ted Kremenekb251eb62010-02-02 22:38:47 +0000485
486 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
487 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
488 I != E; ++I)
489 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000490
491 B = Remove(B, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000492}
493
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000494namespace {
495class InvalidateRegionsWorker {
Ted Kremenekd9605642010-02-03 04:16:00 +0000496 typedef BumpVector<BindingKey> RegionCluster;
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000497 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenekd9605642010-02-03 04:16:00 +0000498 typedef llvm::SmallVector<std::pair<const MemRegion *,RegionCluster*>, 10>
499 WorkList;
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000501 BumpVectorContext BVC;
502 ClusterMap ClusterM;
503 WorkList WL;
504public:
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000505 Store InvalidateRegions(RegionStoreManager &RM, Store store,
506 const MemRegion * const *I,const MemRegion * const *E,
507 const Expr *Ex, unsigned Count,
508 StoreManager::InvalidatedSymbols *IS,
509 ASTContext &Ctx, ValueManager &ValMgr);
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000510
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000511private:
Ted Kremenekd9605642010-02-03 04:16:00 +0000512 void AddToWorkList(BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000513 void AddToWorkList(const MemRegion *R);
Ted Kremenekd9605642010-02-03 04:16:00 +0000514 void AddToCluster(BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000515 RegionCluster **getCluster(const MemRegion *R);
516};
517}
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000518
Ted Kremenekd9605642010-02-03 04:16:00 +0000519void InvalidateRegionsWorker::AddToCluster(BindingKey K) {
520 const MemRegion *R = K.getRegion();
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000521 const MemRegion *baseR = R->getBaseRegion();
522 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenekd9605642010-02-03 04:16:00 +0000523 assert(*CPtr);
524 (*CPtr)->push_back(K, BVC);
525}
526
527void InvalidateRegionsWorker::AddToWorkList(BindingKey K) {
528 AddToWorkList(K.getRegion());
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000529}
530
531void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000532 const MemRegion *baseR = R->getBaseRegion();
533 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000534 if (RegionCluster *C = *CPtr) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000535 WL.push_back(std::make_pair(baseR, C));
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000536 *CPtr = NULL;
537 }
538}
539
540InvalidateRegionsWorker::RegionCluster **
541InvalidateRegionsWorker::getCluster(const MemRegion *R) {
542 RegionCluster *&CRef = ClusterM[R];
543 if (!CRef) {
544 void *Mem = BVC.getAllocator().Allocate<RegionCluster>();
545 CRef = new (Mem) RegionCluster(BVC, 10);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000546 }
547 return &CRef;
548}
549
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000550Store InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM,
551 Store store,
552 const MemRegion * const *I,
553 const MemRegion * const *E,
554 const Expr *Ex, unsigned Count,
555 StoreManager::InvalidatedSymbols *IS,
556 ASTContext &Ctx,
557 ValueManager &ValMgr) {
558 RegionBindings B = RegionStoreManager::GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000559
560 // Scan the entire store and make the region clusters.
561 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000562 AddToCluster(RI.getKey());
563 if (const MemRegion *R = RI.getData().getAsRegion()) {
564 // Generate a cluster, but don't add the region to the cluster
565 // if there aren't any bindings.
566 getCluster(R->getBaseRegion());
567 }
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000568 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000569
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000570 // Add the cluster for I .. E to a worklist.
571 for ( ; I != E; ++I)
572 AddToWorkList(*I);
573
574 while (!WL.empty()) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000575 const MemRegion *baseR;
576 RegionCluster *C;
577 llvm::tie(baseR, C) = WL.back();
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000578 WL.pop_back();
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000579
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000580 for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) {
Ted Kremenekd9605642010-02-03 04:16:00 +0000581 BindingKey K = *I;
Ted Kremenek1eb68092009-10-16 00:30:49 +0000582
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000583 // Get the old binding. Is it a region? If so, add it to the worklist.
Ted Kremenekd9605642010-02-03 04:16:00 +0000584 if (const SVal *V = RM.Lookup(B, K)) {
585 if (const MemRegion *R = V->getAsRegion())
586 AddToWorkList(R);
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000587
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000588 // A symbol? Mark it touched by the invalidation.
589 if (IS)
590 if (SymbolRef Sym = V->getAsSymbol())
591 IS->insert(Sym);
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000592 }
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000593
Ted Kremenekd9605642010-02-03 04:16:00 +0000594 B = RM.Remove(B, K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000595 }
Ted Kremenekd9605642010-02-03 04:16:00 +0000596
597 // Now inspect the base region.
598
599 if (IS) {
600 // Symbolic region? Mark that symbol touched by the invalidation.
601 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
602 IS->insert(SR->getSymbol());
603 }
604
605 // BlockDataRegion? If so, invalidate captured variables that are passed
606 // by reference.
607 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
608 for (BlockDataRegion::referenced_vars_iterator
609 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
610 I != E; ++I) {
611 const VarRegion *VR = *I;
612 if (VR->getDecl()->getAttr<BlocksAttr>())
613 AddToWorkList(VR);
614 }
615 continue;
616 }
617
618 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
619 // Invalidate the region by setting its default value to
620 // conjured symbol. The type of the symbol is irrelavant.
621 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
622 Count);
623 B = RM.Add(B, baseR, BindingKey::Default, V);
624 continue;
625 }
626
627 if (!baseR->isBoundable())
628 continue;
629
630 const TypedRegion *TR = cast<TypedRegion>(baseR);
631 QualType T = TR->getValueType(Ctx);
632
633 // Invalidate the binding.
634 if (const RecordType *RT = T->getAsStructureType()) {
635 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
636 // No record definition. There is nothing we can do.
637 if (!RD) {
638 B = RM.Remove(B, baseR);
639 continue;
640 }
641
642 // Invalidate the region by setting its default value to
643 // conjured symbol. The type of the symbol is irrelavant.
644 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
645 Count);
646 B = RM.Add(B, baseR, BindingKey::Default, V);
647 continue;
648 }
649
650 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
651 // Set the default value of the array to conjured symbol.
652 DefinedOrUnknownSVal V =
653 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
654 B = RM.Add(B, baseR, BindingKey::Default, V);
655 continue;
656 }
657
658 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
659 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
660 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenekfa417142009-08-06 01:20:57 +0000661 }
662
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000663 // Create a new state with the updated bindings.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000664 return B.getRoot();
Ted Kremenekbca70672009-07-29 18:16:25 +0000665}
666
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000667Store RegionStoreManager::InvalidateRegions(Store store,
668 const MemRegion * const *I,
669 const MemRegion * const *E,
670 const Expr *Ex, unsigned Count,
671 InvalidatedSymbols *IS) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000672 InvalidateRegionsWorker W;
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000673 return W.InvalidateRegions(*this, store, I, E, Ex, Count, IS, getContext(),
674 StateMgr.getValueManager());
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000675}
676
677
Ted Kremenekbca70672009-07-29 18:16:25 +0000678//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000679// getLValueXXX methods.
680//===----------------------------------------------------------------------===//
681
Ted Kremenek2907ab72008-12-24 07:46:32 +0000682/// getLValueString - Returns an SVal representing the lvalue of a
683/// StringLiteral. Within RegionStore a StringLiteral has an
684/// associated StringRegion, and the lvalue of a StringLiteral is the
685/// lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000686SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000687 return loc::MemRegionVal(MRMgr.getStringRegion(S));
688}
689
Ted Kremenek2907ab72008-12-24 07:46:32 +0000690/// getLValueVar - Returns an SVal that represents the lvalue of a
691/// variable. Within RegionStore a variable has an associated
692/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000693SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000694 const LocationContext *LC) {
695 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000696}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000697
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000698SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
699 return getLValueFieldOrIvar(D, Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000700}
701
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000702SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
703 return getLValueFieldOrIvar(D, Base);
Ted Kremenekd3c82762009-03-05 04:50:08 +0000704}
705
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000706SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000707 if (Base.isUnknownOrUndef())
708 return Base;
709
710 Loc BaseL = cast<Loc>(Base);
711 const MemRegion* BaseR = 0;
712
713 switch (BaseL.getSubKind()) {
714 case loc::MemRegionKind:
715 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
716 break;
717
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000718 case loc::GotoLabelKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000719 // These are anormal cases. Flag an undefined value.
720 return UndefinedVal();
721
722 case loc::ConcreteIntKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000723 // While these seem funny, this can happen through casts.
724 // FIXME: What we should return is the field offset. For example,
725 // add the field offset to the integer value. That way funny things
726 // like this work properly: &(((struct foo *) 0xa)->f)
727 return Base;
728
729 default:
Zhongxing Xue79a4e62008-11-07 08:57:30 +0000730 assert(0 && "Unhandled Base.");
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000731 return Base;
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Ted Kremenekd3c82762009-03-05 04:50:08 +0000734 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
735 // of FieldDecl.
736 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
737 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000738
Ted Kremenekd3c82762009-03-05 04:50:08 +0000739 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000740}
741
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000742SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
743 SVal Base) {
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000744
Ted Kremenek06032222009-03-09 22:44:49 +0000745 // If the base is an unknown or undefined value, just return it back.
746 // FIXME: For absolute pointer addresses, we just return that value back as
747 // well, although in reality we should return the offset added to that
748 // value.
749 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu36d4ade2008-10-27 12:23:17 +0000750 return Base;
751
Ted Kremenek92d48a72009-01-22 20:27:48 +0000752 // Only handle integer offsets... for now.
753 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000754 return UnknownVal();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000755
Zhongxing Xu7c382642009-05-09 13:20:07 +0000756 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000757
758 // Pointer of any type can be cast and used as array base.
759 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump11289f42009-09-09 15:08:12 +0000760
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000761 // Convert the offset to the appropriate size and signedness.
762 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Ted Kremenek92d48a72009-01-22 20:27:48 +0000764 if (!ElemR) {
765 //
766 // If the base region is not an ElementRegion, create one.
767 // This can happen in the following example:
768 //
769 // char *p = __builtin_alloc(10);
770 // p[1] = 8;
771 //
Zhongxing Xu7c382642009-05-09 13:20:07 +0000772 // Observe that 'p' binds to an AllocaRegion.
Ted Kremenek92d48a72009-01-22 20:27:48 +0000773 //
Ted Kremenek02e50892009-05-04 06:18:28 +0000774 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000775 BaseRegion, getContext()));
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000776 }
Mike Stump11289f42009-09-09 15:08:12 +0000777
Ted Kremenek92d48a72009-01-22 20:27:48 +0000778 SVal BaseIdx = ElemR->getIndex();
Mike Stump11289f42009-09-09 15:08:12 +0000779
Ted Kremenek92d48a72009-01-22 20:27:48 +0000780 if (!isa<nonloc::ConcreteInt>(BaseIdx))
781 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000782
Ted Kremenek92d48a72009-01-22 20:27:48 +0000783 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
784 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
785 assert(BaseIdxI.isSigned());
Mike Stump11289f42009-09-09 15:08:12 +0000786
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000787 // Compute the new index.
788 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump11289f42009-09-09 15:08:12 +0000789
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000790 // Construct the new ElementRegion.
791 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000792 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump11289f42009-09-09 15:08:12 +0000793 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000794}
795
Ted Kremenek4533a552009-06-16 22:36:44 +0000796//===----------------------------------------------------------------------===//
797// Extents for regions.
798//===----------------------------------------------------------------------===//
799
Zhongxing Xu383c2732009-11-12 02:48:32 +0000800DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000801 const MemRegion *R,
802 QualType EleTy) {
Mike Stump11289f42009-09-09 15:08:12 +0000803
Ted Kremenek94575aa2009-07-10 22:30:06 +0000804 switch (R->getKind()) {
Ted Kremenekacd71a42010-01-05 02:18:06 +0000805 case MemRegion::CXXThisRegionKind:
806 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000807 case MemRegion::GenericMemSpaceRegionKind:
808 case MemRegion::StackLocalsSpaceRegionKind:
809 case MemRegion::StackArgumentsSpaceRegionKind:
810 case MemRegion::HeapSpaceRegionKind:
811 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000812 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000813 assert(0 && "Cannot index into a MemSpace");
Mike Stump11289f42009-09-09 15:08:12 +0000814 return UnknownVal();
815
Ted Kremenek10a50e72009-11-25 01:32:22 +0000816 case MemRegion::FunctionTextRegionKind:
817 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000818 case MemRegion::BlockDataRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000819 // Technically this can happen if people do funny things with casts.
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000820 return UnknownVal();
Ted Kremenek94575aa2009-07-10 22:30:06 +0000821
822 // Not yet handled.
823 case MemRegion::AllocaRegionKind:
824 case MemRegion::CompoundLiteralRegionKind:
825 case MemRegion::ElementRegionKind:
826 case MemRegion::FieldRegionKind:
827 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000828 case MemRegion::CXXObjectRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000829 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000830
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000831 case MemRegion::SymbolicRegionKind: {
832 const SVal *Size = state->get<RegionExtents>(R);
833 if (!Size)
834 return UnknownVal();
835 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
836 if (!CI)
837 return UnknownVal();
838
839 CharUnits RegionSize =
840 CharUnits::fromQuantity(CI->getValue().getSExtValue());
841 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
842 assert(RegionSize % EleSize == 0);
843
844 return ValMgr.makeIntVal(RegionSize / EleSize, false);
845 }
846
Ted Kremenek94575aa2009-07-10 22:30:06 +0000847 case MemRegion::StringRegionKind: {
848 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000849 // We intentionally made the size value signed because it participates in
Ted Kremenek94575aa2009-07-10 22:30:06 +0000850 // operations with signed indices.
851 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000852 }
Mike Stump11289f42009-09-09 15:08:12 +0000853
Ted Kremenek94575aa2009-07-10 22:30:06 +0000854 case MemRegion::VarRegionKind: {
855 const VarRegion* VR = cast<VarRegion>(R);
856 // Get the type of the variable.
857 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000858
Ted Kremenek94575aa2009-07-10 22:30:06 +0000859 // FIXME: Handle variable-length arrays.
860 if (isa<VariableArrayType>(T))
861 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000862
Ted Kremenek94575aa2009-07-10 22:30:06 +0000863 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
864 // return the size as signed integer.
865 return ValMgr.makeIntVal(CAT->getSize(), false);
866 }
Ted Kremenekca7935d2009-08-02 05:15:23 +0000867
Ted Kremenek94575aa2009-07-10 22:30:06 +0000868 // Clients can use ordinary variables as if they were arrays. These
869 // essentially are arrays of size 1.
870 return ValMgr.makeIntVal(1, false);
Zhongxing Xuea8c48d2009-05-06 11:51:48 +0000871 }
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000872 }
Mike Stump11289f42009-09-09 15:08:12 +0000873
Ted Kremenek94575aa2009-07-10 22:30:06 +0000874 assert(0 && "Unreachable");
Ted Kremenek47ad37d2009-01-06 19:12:06 +0000875 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000876}
877
Ted Kremenek609df302009-06-17 22:02:04 +0000878const GRState *RegionStoreManager::setExtent(const GRState *state,
879 const MemRegion *region,
880 SVal extent) {
881 return state->set<RegionExtents>(region, extent);
Ted Kremenek4533a552009-06-16 22:36:44 +0000882}
883
884//===----------------------------------------------------------------------===//
885// Location and region casting.
886//===----------------------------------------------------------------------===//
887
Ted Kremenek2907ab72008-12-24 07:46:32 +0000888/// ArrayToPointer - Emulates the "decay" of an array to a pointer
889/// type. 'Array' represents the lvalue of the array being decayed
890/// to a pointer, and the returned SVal represents the decayed
891/// version of that lvalue (i.e., a pointer to the first element of
892/// the array). This is called by GRExprEngine when evaluating casts
893/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000894SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000895 if (!isa<loc::MemRegionVal>(Array))
896 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenekf065b152008-12-13 19:24:37 +0000898 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
899 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000900
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000901 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000902 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000903
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000904 // Strip off typedefs from the ArrayRegion's ValueType.
John McCalla1925362009-09-29 23:03:30 +0000905 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000906 ArrayType *AT = cast<ArrayType>(T);
907 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenekccc22922009-07-16 00:00:11 +0000909 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenek721fcc02009-12-04 00:26:31 +0000910 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
911 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000912}
913
Ted Kremenek4533a552009-06-16 22:36:44 +0000914//===----------------------------------------------------------------------===//
915// Pointer arithmetic.
916//===----------------------------------------------------------------------===//
917
Zhongxing Xu0d081f32010-02-05 05:24:20 +0000918SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000919 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000920 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000921 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000922 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000923
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000924 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000925 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000926
Ted Kremenekf6f04612009-07-11 00:58:27 +0000927 switch (MR->getKind()) {
928 case MemRegion::SymbolicRegionKind: {
929 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000930 SymbolRef Sym = SR->getSymbol();
Ted Kremenekd1d60662009-08-25 22:55:09 +0000931 QualType T = Sym->getType(getContext());
932 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000933
Ted Kremenekd1d60662009-08-25 22:55:09 +0000934 if (const PointerType *PT = T->getAs<PointerType>())
935 EleTy = PT->getPointeeType();
936 else
John McCall9dd450b2009-09-21 23:43:11 +0000937 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000938
Ted Kremenekf6f04612009-07-11 00:58:27 +0000939 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
940 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000941 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000942 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000943 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000944 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000945 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000946 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenekf6f04612009-07-11 00:58:27 +0000947 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
948 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000949 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000950 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000951
Ted Kremenekf6f04612009-07-11 00:58:27 +0000952 case MemRegion::ElementRegionKind: {
953 ER = cast<ElementRegion>(MR);
954 break;
955 }
Mike Stump11289f42009-09-09 15:08:12 +0000956
Ted Kremenekf6f04612009-07-11 00:58:27 +0000957 // Not yet handled.
958 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000959 case MemRegion::StringRegionKind: {
960
961 }
962 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000963 case MemRegion::CompoundLiteralRegionKind:
964 case MemRegion::FieldRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000965 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000966 case MemRegion::CXXObjectRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000967 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000968
Ted Kremenek10a50e72009-11-25 01:32:22 +0000969 case MemRegion::FunctionTextRegionKind:
970 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000971 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000972 // Technically this can happen if people do funny things with casts.
973 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000974
Ted Kremenekacd71a42010-01-05 02:18:06 +0000975 case MemRegion::CXXThisRegionKind:
976 assert(0 &&
977 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000978 case MemRegion::GenericMemSpaceRegionKind:
979 case MemRegion::StackLocalsSpaceRegionKind:
980 case MemRegion::StackArgumentsSpaceRegionKind:
981 case MemRegion::HeapSpaceRegionKind:
982 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000983 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000984 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
985 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000986 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000987
Zhongxing Xue7d14932009-03-02 07:52:23 +0000988 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000989 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +0000990
Ted Kremenek8ec57712009-10-06 01:39:48 +0000991 // For now, only support:
992 // (a) concrete integer indices that can easily be resolved
993 // (b) 0 + symbolic index
994 if (Base) {
995 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
996 // FIXME: Should use SValuator here.
997 SVal NewIdx =
998 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000999 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +00001000 const MemRegion* NewER =
1001 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
1002 ER->getSuperRegion(), getContext());
1003 return ValMgr.makeLoc(NewER);
1004 }
1005 if (0 == Base->getValue()) {
1006 const MemRegion* NewER =
1007 MRMgr.getElementRegion(ER->getElementType(), R,
1008 ER->getSuperRegion(), getContext());
1009 return ValMgr.makeLoc(NewER);
1010 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +00001011 }
Mike Stump11289f42009-09-09 15:08:12 +00001012
Ted Kremenek4c8a5812009-03-03 02:51:43 +00001013 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +00001014}
1015
Ted Kremenek4533a552009-06-16 22:36:44 +00001016//===----------------------------------------------------------------------===//
1017// Loading values from regions.
1018//===----------------------------------------------------------------------===//
1019
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001020Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001021 const MemRegion *R) {
1022 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
1023 return *V;
1024
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001025 return Optional<SVal>();
1026}
1027
1028Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001029 const MemRegion *R) {
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001030 if (R->isBoundable())
1031 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1032 if (TR->getValueType(getContext())->isUnionType())
1033 return UnknownVal();
1034
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001035 if (const SVal *V = Lookup(B, R, BindingKey::Default))
1036 return *V;
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001037
1038 return Optional<SVal>();
1039}
1040
1041Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1042 const MemRegion *R) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001043
1044 if (Optional<SVal> V = getDirectBinding(B, R))
1045 return V;
1046
1047 return getDefaultBinding(B, R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001048}
1049
Ted Kremeneke6fea682009-07-15 02:31:43 +00001050static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1051 RTy = Ctx.getCanonicalType(RTy);
1052 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump11289f42009-09-09 15:08:12 +00001053
Ted Kremeneke6fea682009-07-15 02:31:43 +00001054 if (RTy == UsedTy)
1055 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001056
1057
Ted Kremenek834e2f62009-07-20 22:58:02 +00001058 // Recursively check the types. We basically want to see if a pointer value
Mike Stump11289f42009-09-09 15:08:12 +00001059 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek834e2f62009-07-20 22:58:02 +00001060 // represents a reinterpretation.
1061 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump11289f42009-09-09 15:08:12 +00001062 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001063 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek834e2f62009-07-20 22:58:02 +00001064
1065 return PUsedTy && PRTy &&
1066 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump11289f42009-09-09 15:08:12 +00001067 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek834e2f62009-07-20 22:58:02 +00001068 }
1069
1070 return true;
Ted Kremeneke6fea682009-07-15 02:31:43 +00001071}
1072
Ted Kremenek267e45a2009-09-24 04:11:44 +00001073const ElementRegion *
1074RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1075 ASTContext &Ctx = getContext();
1076 SVal idx = ValMgr.makeZeroArrayIndex();
1077 assert(!T.isNull());
1078 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1079}
Ted Kremenek267e45a2009-09-24 04:11:44 +00001080
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001081SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu83aff702008-10-21 05:29:26 +00001082 assert(!isa<UnknownVal>(L) && "location unknown");
1083 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek9158fb72009-12-15 23:23:27 +00001084
Ted Kremenek2907ab72008-12-24 07:46:32 +00001085 // FIXME: Is this even possible? Shouldn't this be treated as a null
1086 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001087 if (isa<loc::ConcreteInt>(L))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001088 return UndefinedVal();
Ted Kremenek9158fb72009-12-15 23:23:27 +00001089
Ted Kremenek609df302009-06-17 22:02:04 +00001090 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001091
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001092 // FIXME: return symbolic value for these cases.
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001093 // Example:
1094 // void f(int* p) { int x = *p; }
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001095 // char* p = alloca();
1096 // read(p);
1097 // c = *p;
Ted Kremenek0c37d192009-07-14 20:48:22 +00001098 if (isa<AllocaRegion>(MR))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001099 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001100
Ted Kremenek267e45a2009-09-24 04:11:44 +00001101 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1102 MR = GetElementZeroRegion(SR, T);
Mike Stump11289f42009-09-09 15:08:12 +00001103
Ted Kremenek0bb32e32009-08-03 21:41:46 +00001104 if (isa<CodeTextRegion>(MR))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001105 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001106
Ted Kremenek2907ab72008-12-24 07:46:32 +00001107 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1108 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +00001109 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001110 QualType RTy = R->getValueType(getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001111
Ted Kremenek2907ab72008-12-24 07:46:32 +00001112 // FIXME: We should eventually handle funny addressing. e.g.:
1113 //
1114 // int x = ...;
1115 // int *p = &x;
1116 // char *q = (char*) p;
1117 // char c = *q; // returns the first byte of 'x'.
1118 //
1119 // Such funny addressing will occur due to layering of regions.
1120
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001121#if 0
Ted Kremeneke6fea682009-07-15 02:31:43 +00001122 ASTContext &Ctx = getContext();
1123 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001124 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1125 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001126 RTy = T;
Ted Kremenek57fa7e32009-07-15 04:23:32 +00001127 assert(Ctx.getCanonicalType(RTy) ==
1128 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump11289f42009-09-09 15:08:12 +00001129 }
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001130#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001131
Zhongxing Xuce270a62009-03-09 09:15:51 +00001132 if (RTy->isStructureType())
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001133 return RetrieveStruct(store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001134
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001135 // FIXME: Handle unions.
1136 if (RTy->isUnionType())
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001137 return UnknownVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001138
1139 if (RTy->isArrayType())
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001140 return RetrieveArray(store, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001141
Zhongxing Xuce270a62009-03-09 09:15:51 +00001142 // FIXME: handle Vector types.
1143 if (RTy->isVectorType())
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001144 return UnknownVal();
Zhongxing Xu0628f532009-06-28 14:16:39 +00001145
1146 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001147 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu0628f532009-06-28 14:16:39 +00001148
Ted Kremenekbe909b52010-01-11 02:33:26 +00001149 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1150 // FIXME: Here we actually perform an implicit conversion from the loaded
1151 // value to the element type. Eventually we want to compose these values
1152 // more intelligently. For example, an 'element' can encompass multiple
1153 // bound regions (e.g., several bound bytes), or could be a subset of
1154 // a larger value.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001155 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Ted Kremenekbe909b52010-01-11 02:33:26 +00001158 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1159 // FIXME: Here we actually perform an implicit conversion from the loaded
1160 // value to the ivar type. What we should model is stores to ivars
1161 // that blow past the extent of the ivar. If the address of the ivar is
1162 // reinterpretted, it is possible we stored a different value that could
1163 // fit within the ivar. Either we need to cast these when storing them
1164 // or reinterpret them lazily (as we do here).
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001165 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001166 }
Mike Stump11289f42009-09-09 15:08:12 +00001167
Ted Kremenekbe909b52010-01-11 02:33:26 +00001168 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1169 // FIXME: Here we actually perform an implicit conversion from the loaded
1170 // value to the variable type. What we should model is stores to variables
1171 // that blow past the extent of the variable. If the address of the
1172 // variable is reinterpretted, it is possible we stored a different value
1173 // that could fit within the variable. Either we need to cast these when
1174 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001175 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001176 }
Ted Kremenek834e2f62009-07-20 22:58:02 +00001177
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001178 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001179 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001180
1181 // Check if the region has a binding.
1182 if (V)
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001183 return *V;
Ted Kremenek2907ab72008-12-24 07:46:32 +00001184
Ted Kremenek2907ab72008-12-24 07:46:32 +00001185 // The location does not have a bound value. This means that it has
1186 // the value it had upon its creation and/or entry to the analyzed
1187 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekacd71a42010-01-05 02:18:06 +00001188 if (R->hasStackNonParametersStorage()) {
Ted Kremenek2907ab72008-12-24 07:46:32 +00001189 // All stack variables are considered to have undefined values
1190 // upon creation. All heap allocated blocks are considered to
1191 // have undefined values as well unless they are explicitly bound
1192 // to specific values.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001193 return UndefinedVal();
Ted Kremenek2907ab72008-12-24 07:46:32 +00001194 }
1195
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001196 // All other values are symbolic.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001197 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xu83aff702008-10-21 05:29:26 +00001198}
Mike Stump11289f42009-09-09 15:08:12 +00001199
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001200std::pair<Store, const MemRegion *>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001201RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001202 if (Optional<SVal> OV = getDirectBinding(B, R))
1203 if (const nonloc::LazyCompoundVal *V =
1204 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001205 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001206
Ted Kremenekfa417142009-08-06 01:20:57 +00001207 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001208 const std::pair<Store, const MemRegion *> &X =
Ted Kremenekfa417142009-08-06 01:20:57 +00001209 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001210
Ted Kremenekfa417142009-08-06 01:20:57 +00001211 if (X.first)
1212 return std::make_pair(X.first,
1213 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001214 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001215 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001216 const std::pair<Store, const MemRegion *> &X =
Ted Kremenekfa417142009-08-06 01:20:57 +00001217 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001218
Ted Kremenekfa417142009-08-06 01:20:57 +00001219 if (X.first)
1220 return std::make_pair(X.first,
1221 MRMgr.getFieldRegionWithSuper(FR, X.second));
1222 }
1223
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001224 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremenekfa417142009-08-06 01:20:57 +00001225}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001226
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001227SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xu2d160732009-06-25 05:29:39 +00001228 const ElementRegion* R) {
1229 // Check if the region has a binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001230 RegionBindings B = GetRegionBindings(store);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001231 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001232 return *V;
1233
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001234 const MemRegion* superR = R->getSuperRegion();
1235
Zhongxing Xu2d160732009-06-25 05:29:39 +00001236 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001237 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek228539f2009-09-29 16:36:48 +00001238 // FIXME: Handle loads from strings where the literal is treated as
1239 // an integer, e.g., *((unsigned int*)"hello")
1240 ASTContext &Ctx = getContext();
Douglas Gregor4ef1d402009-11-09 22:08:55 +00001241 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001242 if (T != Ctx.getCanonicalType(R->getElementType()))
1243 return UnknownVal();
1244
Zhongxing Xu2d160732009-06-25 05:29:39 +00001245 const StringLiteral *Str = StrR->getStringLiteral();
1246 SVal Idx = R->getIndex();
1247 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1248 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001249 int64_t byteLength = Str->getByteLength();
Ted Kremenekb5850f92009-09-05 17:59:01 +00001250 if (i > byteLength) {
1251 // Buffer overflow checking in GRExprEngine should handle this case,
1252 // but we shouldn't rely on it to not overflow here if that checking
1253 // is disabled.
1254 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001255 }
Ted Kremenekb5850f92009-09-05 17:59:01 +00001256 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001257 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001258 }
1259 }
Mike Stump11289f42009-09-09 15:08:12 +00001260
Ted Kremenek040e3b92009-08-06 22:33:36 +00001261 // Check if the immediate super region has a direct binding.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001262 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneke6fea682009-07-15 02:31:43 +00001263 if (SymbolRef parentSym = V->getAsSymbol())
1264 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek920ad712009-07-22 04:35:42 +00001265
1266 if (V->isUnknownOrUndef())
1267 return *V;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001268
1269 // Handle LazyCompoundVals for the immediate super region. Other cases
1270 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump11289f42009-09-09 15:08:12 +00001271 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek040e3b92009-08-06 22:33:36 +00001272 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump11289f42009-09-09 15:08:12 +00001273
Ted Kremenek040e3b92009-08-06 22:33:36 +00001274 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001275 return RetrieveElement(LCV->getStore(), R);
Ted Kremenek040e3b92009-08-06 22:33:36 +00001276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Ted Kremeneke6fea682009-07-15 02:31:43 +00001278 // Other cases: give up.
Zhongxing Xu61e66922009-07-03 06:11:41 +00001279 return UnknownVal();
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001280 }
Ted Kremenekbe909b52010-01-11 02:33:26 +00001281
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001282 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001283}
1284
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001285SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001286 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001287
1288 // Check if the region has a binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001289 RegionBindings B = GetRegionBindings(store);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001290 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001291 return *V;
1292
Ted Kremenek040e3b92009-08-06 22:33:36 +00001293 QualType Ty = R->getValueType(getContext());
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001294 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek040e3b92009-08-06 22:33:36 +00001295}
Mike Stump11289f42009-09-09 15:08:12 +00001296
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001297SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001298 const TypedRegion *R,
1299 QualType Ty,
1300 const MemRegion *superR) {
1301
Mike Stump11289f42009-09-09 15:08:12 +00001302 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001303 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001304
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001305 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001306
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001307 while (superR) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001308 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001309 if (SymbolRef parentSym = D->getAsSymbol())
1310 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001311
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001312 if (D->isZeroConstant())
1313 return ValMgr.makeZeroVal(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001314
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001315 if (D->isUnknown())
1316 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001317
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001318 assert(0 && "Unknown default value");
1319 }
Mike Stump11289f42009-09-09 15:08:12 +00001320
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001321 // If our super region is a field or element itself, walk up the region
1322 // hierarchy to see if there is a default value installed in an ancestor.
1323 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1324 superR = cast<SubRegion>(superR)->getSuperRegion();
1325 continue;
1326 }
Mike Stump11289f42009-09-09 15:08:12 +00001327
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001328 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001329 }
Mike Stump11289f42009-09-09 15:08:12 +00001330
Ted Kremenekfa417142009-08-06 01:20:57 +00001331 // Lazy binding?
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001332 Store lazyBindingStore = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001333 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001334 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001335
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001336 if (lazyBindingStore) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001337 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump11289f42009-09-09 15:08:12 +00001338
Ted Kremenek040e3b92009-08-06 22:33:36 +00001339 if (isa<ElementRegion>(R))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001340 return RetrieveElement(lazyBindingStore,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001341 cast<ElementRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001342
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001343 return RetrieveField(lazyBindingStore,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001344 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001345 }
1346
Ted Kremenekacd71a42010-01-05 02:18:06 +00001347 if (R->hasStackNonParametersStorage()) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001348 if (isa<ElementRegion>(R)) {
1349 // Currently we don't reason specially about Clang-style vectors. Check
1350 // if superR is a vector and if so return Unknown.
1351 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1352 if (typedSuperR->getValueType(getContext())->isVectorType())
1353 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001354 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001357 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001360 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001361 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001362}
Mike Stump11289f42009-09-09 15:08:12 +00001363
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001364SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek48029552009-07-15 06:09:28 +00001365
Ted Kremenek48029552009-07-15 06:09:28 +00001366 // Check if the region has a binding.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001367 RegionBindings B = GetRegionBindings(store);
Ted Kremenek48029552009-07-15 06:09:28 +00001368
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001369 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001370 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001371
Ted Kremenek48029552009-07-15 06:09:28 +00001372 const MemRegion *superR = R->getSuperRegion();
1373
Ted Kremenek481c1212009-10-20 01:20:57 +00001374 // Check if the super region has a default binding.
1375 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001376 if (SymbolRef parentSym = V->getAsSymbol())
1377 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001378
Ted Kremenek48029552009-07-15 06:09:28 +00001379 // Other cases: give up.
1380 return UnknownVal();
1381 }
Mike Stump11289f42009-09-09 15:08:12 +00001382
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001383 return RetrieveLazySymbol(R);
Ted Kremenek834e2f62009-07-20 22:58:02 +00001384}
1385
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001386SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001387
Ted Kremenekfe12f882009-07-21 00:12:07 +00001388 // Check if the region has a binding.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001389 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001390
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001391 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001392 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001393
Ted Kremenekfe12f882009-07-21 00:12:07 +00001394 // Lazily derive a value for the VarRegion.
1395 const VarDecl *VD = R->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001396
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +00001397 if (R->hasGlobalsOrParametersStorage() ||
1398 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek9158fb72009-12-15 23:23:27 +00001399 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001400
Ted Kremenekfe12f882009-07-21 00:12:07 +00001401 return UndefinedVal();
1402}
1403
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001404SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001405
Ted Kremenek834e2f62009-07-20 22:58:02 +00001406 QualType valTy = R->getValueType(getContext());
Ted Kremenek920ad712009-07-22 04:35:42 +00001407
Ted Kremenek48029552009-07-15 06:09:28 +00001408 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001409 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek48029552009-07-15 06:09:28 +00001410}
1411
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001412SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001413 QualType T = R->getValueType(getContext());
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001414 assert(T->isStructureType());
1415
Zhongxing Xud85a9912009-06-11 07:27:30 +00001416 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001417 RecordDecl* RD = RT->getDecl();
1418 assert(RD->isDefinition());
Mike Stumpc700b362009-08-06 12:56:50 +00001419 (void)RD;
Ted Kremenekfa417142009-08-06 01:20:57 +00001420#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001421 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1422
Ted Kremenek609df302009-06-17 22:02:04 +00001423 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1424 // reverse iterator, we should implement one.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001425 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001426
Douglas Gregor7a4fad12008-12-11 20:41:00 +00001427 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1428 FieldEnd = Fields.rend();
1429 Field != FieldEnd; ++Field) {
1430 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001431 QualType FTy = (*Field)->getType();
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001432 SVal FieldValue = Retrieve(store, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001433 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1434 }
1435
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001436 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001437#else
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001438 return ValMgr.makeLazyCompoundVal(store, R);
Ted Kremenekfa417142009-08-06 01:20:57 +00001439#endif
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001440}
1441
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001442SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremenekfa417142009-08-06 01:20:57 +00001443#if USE_EXPLICIT_COMPOUND
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001444 QualType T = R->getValueType(getContext());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001445 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1446
1447 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001448 uint64_t size = CAT->getSize().getZExtValue();
1449 for (uint64_t i = 0; i < size; ++i) {
1450 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu838a0db2009-06-16 09:55:50 +00001451 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump11289f42009-09-09 15:08:12 +00001452 getContext());
Ted Kremenek02e50892009-05-04 06:18:28 +00001453 QualType ETy = ER->getElementType();
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001454 SVal ElementVal = Retrieve(store, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001455 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1456 }
1457
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001458 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001459#else
1460 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001461 return ValMgr.makeLazyCompoundVal(store, R);
Ted Kremenekfa417142009-08-06 01:20:57 +00001462#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001463}
1464
Ted Kremenek4533a552009-06-16 22:36:44 +00001465//===----------------------------------------------------------------------===//
1466// Binding values to regions.
1467//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001468
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001469Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001470 if (isa<loc::MemRegionVal>(L))
Ted Kremenekbe909b52010-01-11 02:33:26 +00001471 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001472 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump11289f42009-09-09 15:08:12 +00001473
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001474 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001475}
1476
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001477Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001478 if (isa<loc::ConcreteInt>(L))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001479 return store;
Zhongxing Xud260db12009-06-28 10:16:11 +00001480
Ted Kremenek4533a552009-06-16 22:36:44 +00001481 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001482 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001483
Ted Kremenek4533a552009-06-16 22:36:44 +00001484 // Check if the region is a struct region.
1485 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1486 if (TR->getValueType(getContext())->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001487 return BindStruct(store, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001488
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001489 // Special case: the current region represents a cast and it and the super
1490 // region both have pointer types or intptr_t types. If so, perform the
1491 // bind to the super region.
1492 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump11289f42009-09-09 15:08:12 +00001493 // loads that treat integers as pointers and vis versa.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001494 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1495 if (ER->getIndex().isZeroConstant()) {
1496 if (const TypedRegion *superR =
1497 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1498 ASTContext &Ctx = getContext();
1499 QualType superTy = superR->getValueType(Ctx);
1500 QualType erTy = ER->getValueType(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001501
1502 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001503 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu319deb82010-02-04 04:56:43 +00001504 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001505 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001506 }
Ted Kremenek25c9c142009-09-21 22:58:52 +00001507 // For now, just invalidate the fields of the struct/union/class.
1508 // FIXME: Precisely handle the fields of the record.
1509 if (superTy->isRecordType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001510 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001511 }
1512 }
1513 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001514 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1515 // Binding directly to a symbolic region should be treated as binding
1516 // to element 0.
1517 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001518
1519 // FIXME: Is this the right way to handle symbols that are references?
1520 if (const PointerType *PT = T->getAs<PointerType>())
1521 T = PT->getPointeeType();
1522 else
1523 T = T->getAs<ReferenceType>()->getPointeeType();
1524
Ted Kremenek267e45a2009-09-24 04:11:44 +00001525 R = GetElementZeroRegion(SR, T);
1526 }
Mike Stump11289f42009-09-09 15:08:12 +00001527
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001528 // Perform the binding.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001529 RegionBindings B = GetRegionBindings(store);
1530 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek4533a552009-06-16 22:36:44 +00001531}
1532
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001533Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
1534 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001535
Ted Kremenekb006b822009-11-04 00:09:15 +00001536 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001537
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001538 if (T->isArrayType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001539 return BindArray(store, VR, InitVal);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001540 if (T->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001541 return BindStruct(store, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001542
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001543 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001544}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001545
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001546// FIXME: this method should be merged into Bind().
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001547Store RegionStoreManager::BindCompoundLiteral(Store store,
1548 const CompoundLiteralExpr *CL,
1549 const LocationContext *LC,
1550 SVal V) {
1551 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek04af9f22009-12-07 22:05:27 +00001552 V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001553}
1554
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001555Store RegionStoreManager::setImplicitDefaultValue(Store store,
1556 const MemRegion *R,
1557 QualType T) {
Ted Kremenek439a6d12009-11-19 20:20:24 +00001558 RegionBindings B = GetRegionBindings(store);
1559 SVal V;
1560
1561 if (Loc::IsLocType(T))
1562 V = ValMgr.makeNull();
1563 else if (T->isIntegerType())
1564 V = ValMgr.makeZeroVal(T);
1565 else if (T->isStructureType() || T->isArrayType()) {
1566 // Set the default value to a zero constant when it is a structure
1567 // or array. The type doesn't really matter.
1568 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1569 }
1570 else {
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001571 return store;
Ted Kremenek439a6d12009-11-19 20:20:24 +00001572 }
Ted Kremenek8e994a22010-01-11 00:07:44 +00001573
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001574 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek439a6d12009-11-19 20:20:24 +00001575}
1576
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001577Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
1578 SVal Init) {
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001579
1580 ASTContext &Ctx = getContext();
1581 const ArrayType *AT =
1582 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1583 QualType ElementTy = AT->getElementType();
1584 Optional<uint64_t> Size;
1585
1586 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1587 Size = CAT->getSize().getZExtValue();
1588
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001589 // Check if the init expr is a StringLiteral.
1590 if (isa<loc::MemRegionVal>(Init)) {
1591 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1592 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1593 const char* str = S->getStrData();
1594 unsigned len = S->getByteLength();
1595 unsigned j = 0;
1596
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001597 // Copy bytes from the string literal into the target array. Trailing bytes
1598 // in the array that are not covered by the string literal are initialized
1599 // to zero.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001600
1601 // We assume that string constants are bound to
1602 // constant arrays.
Ted Kremenek968999b2010-01-27 16:31:37 +00001603 uint64_t size = Size.getValue();
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001604
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001605 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001606 if (j >= len)
1607 break;
1608
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001609 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001610 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1611 getContext());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001612
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001613 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001614 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001615 }
1616
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001617 return store;
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001618 }
1619
Ted Kremenekfa417142009-08-06 01:20:57 +00001620 // Handle lazy compound values.
1621 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001622 return CopyLazyBindings(*LCV, store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001623
1624 // Remaining case: explicit compound values.
Ted Kremenek439a6d12009-11-19 20:20:24 +00001625
1626 if (Init.isUnknown())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001627 return setImplicitDefaultValue(store, R, ElementTy);
Ted Kremenek439a6d12009-11-19 20:20:24 +00001628
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001629 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001630 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001631 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001632
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001633 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001634 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001635 if (VI == VE)
1636 break;
1637
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001638 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001639 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001640
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001641 if (ElementTy->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001642 store = BindStruct(store, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001643 else
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001644 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001645 }
1646
Ted Kremenek439a6d12009-11-19 20:20:24 +00001647 // If the init list is shorter than the array length, set the
1648 // array default value.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001649 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001650 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001651
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001652 return store;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001653}
1654
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001655Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1656 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001657
Ted Kremenek609df302009-06-17 22:02:04 +00001658 if (!Features.supportsFields())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001659 return store;
Mike Stump11289f42009-09-09 15:08:12 +00001660
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001661 QualType T = R->getValueType(getContext());
Zhongxing Xub393b502008-10-31 10:53:01 +00001662 assert(T->isStructureType());
1663
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001664 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001665 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001666
1667 if (!RD->isDefinition())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001668 return store;
Zhongxing Xub393b502008-10-31 10:53:01 +00001669
Ted Kremenekfa417142009-08-06 01:20:57 +00001670 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001671 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001672 return CopyLazyBindings(*LCV, store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001673
Ted Kremenek609df302009-06-17 22:02:04 +00001674 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1675 // Ignore them and kill the field values.
1676 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001677 return KillStruct(store, R);
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001678
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001679 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001680 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001681
1682 RecordDecl::field_iterator FI, FE;
1683
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001684 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001685
Zhongxing Xu0442e962009-06-23 05:43:16 +00001686 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001687 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001688
Zhongxing Xub393b502008-10-31 10:53:01 +00001689 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001690 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001691
Ted Kremenek30030012009-09-22 21:19:14 +00001692 if (FTy->isArrayType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001693 store = BindArray(store, FR, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001694 else if (FTy->isStructureType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001695 store = BindStruct(store, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001696 else
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001697 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001698 }
1699
Zhongxing Xu0442e962009-06-23 05:43:16 +00001700 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001701 if (FI != FE) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001702 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001703 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001704 store = B.getRoot();
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001705 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001706
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001707 return store;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001708}
1709
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001710Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1711 RegionBindings B = GetRegionBindings(store);
1712 llvm::OwningPtr<RegionStoreSubRegionMap>
1713 SubRegions(getRegionStoreSubRegionMap(store));
1714 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001715
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001716 // Set the default value of the struct region to "unknown".
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001717 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001718}
1719
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001720Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1721 Store store, const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001722
Ted Kremenekfa417142009-08-06 01:20:57 +00001723 // Nuke the old bindings stemming from R.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001724 RegionBindings B = GetRegionBindings(store);
Ted Kremenekfa417142009-08-06 01:20:57 +00001725
Mike Stump11289f42009-09-09 15:08:12 +00001726 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001727 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekfa417142009-08-06 01:20:57 +00001728
Mike Stump11289f42009-09-09 15:08:12 +00001729 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001730 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001731
Ted Kremenekfa417142009-08-06 01:20:57 +00001732 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1733 // results in a zero-copy algorithm.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001734 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek8e994a22010-01-11 00:07:44 +00001735}
1736
1737//===----------------------------------------------------------------------===//
1738// "Raw" retrievals and bindings.
1739//===----------------------------------------------------------------------===//
1740
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001741BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekbe909b52010-01-11 02:33:26 +00001742 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1743 const RegionRawOffset &O = ER->getAsRawOffset();
1744
1745 if (O.getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001746 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001747
1748 // FIXME: There are some ElementRegions for which we cannot compute
1749 // raw offsets yet, including regions with symbolic offsets.
1750 }
1751
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001752 return BindingKey(R, 0, k);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001753}
1754
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001755RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001756 return RBFactory.Add(B, K, V);
1757}
1758
1759RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001760 BindingKey::Kind k, SVal V) {
1761 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek8e994a22010-01-11 00:07:44 +00001762}
1763
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001764const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001765 return B.lookup(K);
1766}
1767
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001768const SVal *RegionStoreManager::Lookup(RegionBindings B,
1769 const MemRegion *R,
1770 BindingKey::Kind k) {
1771 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001772}
1773
1774RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1775 return RBFactory.Remove(B, K);
1776}
1777
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001778RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1779 BindingKey::Kind k){
1780 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001781}
1782
1783Store RegionStoreManager::Remove(Store store, BindingKey K) {
1784 RegionBindings B = GetRegionBindings(store);
1785 return Remove(B, K).getRoot();
Ted Kremenekfa417142009-08-06 01:20:57 +00001786}
Mike Stump11289f42009-09-09 15:08:12 +00001787
Ted Kremenek4533a552009-06-16 22:36:44 +00001788//===----------------------------------------------------------------------===//
1789// State pruning.
1790//===----------------------------------------------------------------------===//
Ted Kremenekcc224242009-09-29 06:35:00 +00001791
Zhongxing Xuad0ef842010-02-05 05:34:29 +00001792Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
1793 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001794 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001795{
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001796 typedef std::pair<Store, const MemRegion *> RBDNode;
Ted Kremenek9f3a6432009-10-17 17:45:11 +00001797
Ted Kremenek2c85f172009-08-06 04:50:20 +00001798 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001799
Ted Kremenek4533a552009-06-16 22:36:44 +00001800 // The backmap from regions to subregions.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001801 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001802 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekcc224242009-09-29 06:35:00 +00001803
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001804 // Do a pass over the regions in the store. For VarRegions we check if
1805 // the variable is still live and if so add it to the list of live roots.
1806 // For other regions we populate our region backmap.
Ted Kremenek4533a552009-06-16 22:36:44 +00001807 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenekcc224242009-09-29 06:35:00 +00001808
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001809 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001810 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001811 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001812 IntermediateRoots.push_back(R);
Ted Kremenek4533a552009-06-16 22:36:44 +00001813 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001814
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001815 // Process the "intermediate" roots to find if they are referenced by
Mike Stump11289f42009-09-09 15:08:12 +00001816 // real roots.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001817 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001818 llvm::SmallVector<RBDNode, 10> Postponed;
1819
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001820 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001821
Ted Kremenek4533a552009-06-16 22:36:44 +00001822 while (!IntermediateRoots.empty()) {
1823 const MemRegion* R = IntermediateRoots.back();
1824 IntermediateRoots.pop_back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001825
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001826 if (IntermediateVisited.count(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001827 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001828 IntermediateVisited.insert(R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001829
Ted Kremenek4533a552009-06-16 22:36:44 +00001830 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +00001831 if (SymReaper.isLive(Loc, VR))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001832 WorkList.push_back(std::make_pair(store, VR));
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001833 continue;
1834 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001835
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001836 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001837 llvm::SmallVectorImpl<RBDNode> &Q =
1838 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1839
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001840 Q.push_back(std::make_pair(store, SR));
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001841
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001842 continue;
Ted Kremenek4533a552009-06-16 22:36:44 +00001843 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001844
1845 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001846 if (const SubRegion* superR =
Ted Kremenekcc224242009-09-29 06:35:00 +00001847 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001848 IntermediateRoots.push_back(superR);
Ted Kremenek4533a552009-06-16 22:36:44 +00001849 }
Mike Stump11289f42009-09-09 15:08:12 +00001850
Ted Kremenekcc224242009-09-29 06:35:00 +00001851 // Enqueue the RegionRoots onto WorkList.
1852 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1853 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001854 WorkList.push_back(std::make_pair(store, *I));
Mike Stump11289f42009-09-09 15:08:12 +00001855 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001856 RegionRoots.clear();
1857
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001858 llvm::DenseSet<RBDNode> Visited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001859
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001860tryAgain:
Ted Kremenekcc224242009-09-29 06:35:00 +00001861 while (!WorkList.empty()) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001862 RBDNode N = WorkList.back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001863 WorkList.pop_back();
1864
1865 // Have we visited this node before?
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001866 if (Visited.count(N))
Ted Kremenekcc224242009-09-29 06:35:00 +00001867 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001868 Visited.insert(N);
Mike Stump11289f42009-09-09 15:08:12 +00001869
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001870 const MemRegion *R = N.second;
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001871 Store store_N = N.first;
Ted Kremenekcc224242009-09-29 06:35:00 +00001872
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001873 // Enqueue subregions.
1874 RegionStoreSubRegionMap *M;
1875
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001876 if (store == store_N)
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001877 M = SubRegions.get();
1878 else {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001879 RegionStoreSubRegionMap *& SM = SC[store_N];
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001880 if (!SM)
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001881 SM = getRegionStoreSubRegionMap(store_N);
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001882 M = SM;
1883 }
Ted Kremenekb251eb62010-02-02 22:38:47 +00001884
1885 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1886 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1887 I != E; ++I)
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001888 WorkList.push_back(std::make_pair(store_N, *I));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001889
Ted Kremenekcc224242009-09-29 06:35:00 +00001890 // Enqueue the super region.
1891 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1892 const MemRegion *superR = SR->getSuperRegion();
1893 if (!isa<MemSpaceRegion>(superR)) {
1894 // If 'R' is a field or an element, we want to keep the bindings
1895 // for the other fields and elements around. The reason is that
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001896 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8b2f5d32009-10-17 07:32:08 +00001897 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1898 || isa<ObjCIvarRegion>(R));
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001899 WorkList.push_back(std::make_pair(store_N, superR));
Ted Kremenekcc224242009-09-29 06:35:00 +00001900 }
1901 }
1902
1903 // Mark the symbol for any live SymbolicRegion as "live". This means we
1904 // should continue to track that symbol.
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001905 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001906 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001907
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001908 // For BlockDataRegions, enqueue the VarRegions for variables marked
1909 // with __block (passed-by-reference).
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001910 // via BlockDeclRefExprs.
1911 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1912 for (BlockDataRegion::referenced_vars_iterator
1913 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001914 RI != RE; ++RI) {
1915 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001916 WorkList.push_back(std::make_pair(store_N, *RI));
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001917 }
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001918 // No possible data bindings on a BlockDataRegion. Continue to the
1919 // next region in the worklist.
1920 continue;
1921 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001922
Ted Kremenekcc224242009-09-29 06:35:00 +00001923 RegionBindings B_N = GetRegionBindings(store_N);
1924
1925 // Get the data binding for R (if any).
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001926 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001927
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001928 if (V) {
1929 // Check for lazy bindings.
1930 if (const nonloc::LazyCompoundVal *LCV =
1931 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001932
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001933 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001934 WorkList.push_back(std::make_pair(D->getStore(), D->getRegion()));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001935 }
1936 else {
Ted Kremenekcc224242009-09-29 06:35:00 +00001937 // Update the set of live symbols.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001938 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenekcc224242009-09-29 06:35:00 +00001939 SI!=SE;++SI)
1940 SymReaper.markLive(*SI);
1941
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001942 // If V is a region, then add it to the worklist.
1943 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001944 WorkList.push_back(std::make_pair(store_N, RX));
Ted Kremenekcc224242009-09-29 06:35:00 +00001945 }
1946 }
1947 }
1948
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001949 // See if any postponed SymbolicRegions are actually live now, after
1950 // having done a scan.
1951 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1952 E = Postponed.end() ; I != E ; ++I) {
1953 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1954 if (SymReaper.isLive(SR->getSymbol())) {
1955 WorkList.push_back(*I);
1956 I->second = NULL;
1957 }
1958 }
1959 }
1960
1961 if (!WorkList.empty())
1962 goto tryAgain;
1963
Ted Kremenek4533a552009-06-16 22:36:44 +00001964 // We have now scanned the store, marking reachable regions and symbols
1965 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00001966 // as well as update DSymbols with the set symbols that are now dead.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001967 Store new_store = store;
Ted Kremenek2c85f172009-08-06 04:50:20 +00001968 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001969 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek4533a552009-06-16 22:36:44 +00001970 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001971 if (Visited.count(std::make_pair(store, R)))
Ted Kremenek4533a552009-06-16 22:36:44 +00001972 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001973
Ted Kremenek4533a552009-06-16 22:36:44 +00001974 // Remove this dead region from the store.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001975 new_store = Remove(new_store, I.getKey());
Mike Stump11289f42009-09-09 15:08:12 +00001976
Ted Kremenek4533a552009-06-16 22:36:44 +00001977 // Mark all non-live symbols that this region references as dead.
1978 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1979 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00001980
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001981 SVal X = I.getData();
Ted Kremenekf106ab92009-08-02 05:00:15 +00001982 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1983 for (; SI != SE; ++SI)
1984 SymReaper.maybeDead(*SI);
1985 }
Mike Stump11289f42009-09-09 15:08:12 +00001986
Zhongxing Xuad0ef842010-02-05 05:34:29 +00001987 return new_store;
Ted Kremenek4533a552009-06-16 22:36:44 +00001988}
1989
Zhongxing Xudaa41762009-10-13 02:24:55 +00001990GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1991 StackFrameContext const *frame) {
1992 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1993 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1994
1995 FunctionDecl::param_const_iterator PI = FD->param_begin();
1996
1997 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1998
1999 // Copy the arg expression value to the arg variables.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00002000 Store store = state->getStore();
Zhongxing Xudaa41762009-10-13 02:24:55 +00002001 for (; AI != AE; ++AI, ++PI) {
2002 SVal ArgVal = state->getSVal(*AI);
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00002003 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xudaa41762009-10-13 02:24:55 +00002004 }
2005
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00002006 return state->makeWithStore(store);
Zhongxing Xudaa41762009-10-13 02:24:55 +00002007}
2008
Ted Kremenek4533a552009-06-16 22:36:44 +00002009//===----------------------------------------------------------------------===//
2010// Utility methods.
2011//===----------------------------------------------------------------------===//
2012
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002013void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00002014 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00002015 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00002016 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002017
Ted Kremenek2c85f172009-08-06 04:50:20 +00002018 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002019 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00002020}