blob: 8a64ec8d24a9278ffb961ab16b298842687684f5 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000017#include "clang/AST/CharUnits.h"
Zhongxing Xuc5063572010-03-16 13:14:16 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Checker/PathSensitive/GRState.h"
24#include "clang/Checker/PathSensitive/GRStateTrait.h"
25#include "clang/Checker/PathSensitive/MemRegion.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000027#include "llvm/ADT/ImmutableMap.h"
28#include "llvm/ADT/Optional.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000029#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000030
31using namespace clang;
Ted Kremenek66d51422010-04-09 20:26:58 +000032using llvm::Optional;
Zhongxing Xu17892752008-10-08 02:50:44 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000040public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000044 uint64_t Offset;
45
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubfcaf802010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048public:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000049
Ted Kremeneke393f4a2010-02-03 03:06:46 +000050 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000052
Ted Kremeneke393f4a2010-02-03 03:06:46 +000053 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000055
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000056 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000059 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000060
Ted Kremeneke393f4a2010-02-03 03:06:46 +000061 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000062
Ted Kremeneke393f4a2010-02-03 03:06:46 +000063 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 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000070
Ted Kremeneke393f4a2010-02-03 03:06:46 +000071 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000075};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000076} // end anonymous namespace
77
78namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000079 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000080 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremeneke393f4a2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000093
Ted Kremenek50dc1b32008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek9af46f52009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9af46f52009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek9af46f52009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-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 Kremenekd6cfbe42009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenek50dc1b32008-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 Xubaf03a72008-11-24 09:44:56 +0000134}
135
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-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 Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000151
Zhongxing Xu17892752008-10-08 02:50:44 +0000152namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-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 Kremenek59e8f112009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000161public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000177
Ted Kremenekdf165012010-02-02 22:38:47 +0000178 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 Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek5dc27462009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000187 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenekdf165012010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000192 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek5dc27462009-03-03 02:51:43 +0000195 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000198
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000199
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000203
Zhongxing Xu17892752008-10-08 02:50:44 +0000204public:
Mike Stump1eb44332009-09-09 15:08:12 +0000205 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000206 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000207 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000208 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000209
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000210 SubRegionMap *getSubRegionMap(Store store) {
211 return getRegionStoreSubRegionMap(store);
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Zhongxing Xu13d50172009-10-11 08:08:02 +0000214 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Zhongxing Xu13d50172009-10-11 08:08:02 +0000216 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
217 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000218 /// getDefaultBinding - Returns an SVal* representing an optional default
219 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000220 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000221
Ted Kremenek027e2662009-11-19 20:20:24 +0000222 /// setImplicitDefaultValue - Set the default binding for the provided
223 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000224 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000225 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000227 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
228 /// type. 'Array' represents the lvalue of the array being decayed
229 /// to a pointer, and the returned SVal represents the decayed
230 /// version of that lvalue (i.e., a pointer to the first element of
231 /// the array). This is called by GRExprEngine when evaluating
232 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000233 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000234
Zhongxing Xu461147f2010-02-05 05:24:20 +0000235 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000236
Mike Stump1eb44332009-09-09 15:08:12 +0000237 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000238 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000239 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000240
Ted Kremenek67f28532009-06-17 22:02:04 +0000241 //===-------------------------------------------------------------------===//
242 // Binding values to regions.
243 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000244
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000245 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000246 unsigned Count, InvalidatedSymbols *IS) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000247 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS,
248 false);
Ted Kremenek81a95832009-12-03 03:27:11 +0000249 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000250
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000251 Store InvalidateRegions(Store store,
252 const MemRegion * const *Begin,
253 const MemRegion * const *End,
254 const Expr *E, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000255 InvalidatedSymbols *IS,
256 bool invalidateGlobals);
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000258public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000259
Zhongxing Xu13d50172009-10-11 08:08:02 +0000260 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000261 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000263 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
264
265 RegionBindings Add(RegionBindings B, const MemRegion *R,
266 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000267
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000268 const SVal *Lookup(RegionBindings B, BindingKey K);
269 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000270
271 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000272 RegionBindings Remove(RegionBindings B, const MemRegion *R,
273 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000274
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000275 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
276 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000277 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000278
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000279 Store Remove(Store store, BindingKey K);
280
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000281public: // Part of public interface to class.
282
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000283 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000284
Zhongxing Xu54460092010-06-01 04:49:26 +0000285 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000286 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xu54460092010-06-01 04:49:26 +0000287 RegionBindings B = GetRegionBindings(store);
288 assert(!Lookup(B, R, BindingKey::Default));
289 assert(!Lookup(B, R, BindingKey::Direct));
290 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xua5ce9662010-06-01 03:01:33 +0000291 }
292
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000293 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
294 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000296 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000297
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000298 Store BindDeclWithNoInit(Store store, const VarRegion *) {
299 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000300 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000301
Ted Kremenek67f28532009-06-17 22:02:04 +0000302 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000303 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000305 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
307 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000308 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000309
Ted Kremenek67f28532009-06-17 22:02:04 +0000310 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000311
Ted Kremenek67f28532009-06-17 22:02:04 +0000312
313 //===------------------------------------------------------------------===//
314 // Loading values from regions.
315 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek67f28532009-06-17 22:02:04 +0000317 /// The high level logic for this method is this:
318 /// Retrieve (L)
319 /// if L has binding
320 /// return L's binding
321 /// else if L is in killset
322 /// return unknown
323 /// else
324 /// if L is on stack or heap
325 /// return undefined
326 /// else
327 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000328 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000329
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000330 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000331
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000332 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Zhongxing Xu576bb922010-02-05 03:01:53 +0000334 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Zhongxing Xu576bb922010-02-05 03:01:53 +0000336 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Zhongxing Xu576bb922010-02-05 03:01:53 +0000338 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000340 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000341 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek67f28532009-06-17 22:02:04 +0000343 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000344 /// struct copy:
345 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000346 /// x = y;
347 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000348 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Zhongxing Xu576bb922010-02-05 03:01:53 +0000350 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000352 /// Used to lazily generate derived symbols for bindings that are defined
353 /// implicitly by default bindings in a super region.
354 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
355 const MemRegion *superR,
356 const TypedRegion *R, QualType Ty);
357
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000358 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000359 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000360 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000362 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
363 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000364
365 //===------------------------------------------------------------------===//
366 // State pruning.
367 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek67f28532009-06-17 22:02:04 +0000369 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
370 /// It returns a new Store with these values removed.
Jordy Rose7dadf792010-07-01 20:09:55 +0000371 const GRState *RemoveDeadBindings(GRState &state,
Zhongxing Xu95798982010-05-26 03:27:35 +0000372 const StackFrameContext *LCtx,
373 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000374 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
375
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000376 const GRState *EnterStackFrame(const GRState *state,
377 const StackFrameContext *frame);
378
Ted Kremenek67f28532009-06-17 22:02:04 +0000379 //===------------------------------------------------------------------===//
380 // Region "extents".
381 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Zhongxing Xuab280992010-05-25 04:59:19 +0000383 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent){
384 return state->set<RegionExtents>(R, Extent);
385 }
386
387 Optional<SVal> getExtent(const GRState *state, const MemRegion *R) {
388 const SVal *V = state->get<RegionExtents>(R);
389 if (V)
390 return *V;
391 else
392 return Optional<SVal>();
393 }
394
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000395 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000396 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000397
398 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000399 // Utility methods.
400 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek451ac092009-08-06 04:50:20 +0000402 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000403 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000404 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000405
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000406 void print(Store store, llvm::raw_ostream& Out, const char* nl,
407 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000408
409 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek0e9910f2010-06-17 00:24:42 +0000410 RegionBindings B = GetRegionBindings(store);
411 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
412 const BindingKey &K = I.getKey();
413 if (!K.isDirect())
414 continue;
415 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
416 // FIXME: Possibly incorporate the offset?
417 if (!f.HandleBinding(*this, store, R, I.getData()))
418 return;
419 }
420 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek67f28532009-06-17 22:02:04 +0000423 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000424 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000425};
426
427} // end anonymous namespace
428
Ted Kremenek9af46f52009-06-16 22:36:44 +0000429//===----------------------------------------------------------------------===//
430// RegionStore creation.
431//===----------------------------------------------------------------------===//
432
433StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
434 RegionStoreFeatures F = maximal_features_tag();
435 return new RegionStoreManager(StMgr, F);
436}
437
438StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
439 RegionStoreFeatures F = minimal_features_tag();
440 F.enableFields(true);
441 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000442}
443
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000444void
445RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000446 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000447 const MemRegion *superR = R->getSuperRegion();
448 if (add(superR, R))
449 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000450 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000451}
452
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000453RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000454RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
455 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000456 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000458 llvm::SmallVector<const SubRegion*, 10> WL;
459
Ted Kremenek451ac092009-08-06 04:50:20 +0000460 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000461 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000462 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Mike Stump1eb44332009-09-09 15:08:12 +0000464 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000465 // don't have direct bindings but are super regions of those that do.
466 while (!WL.empty()) {
467 const SubRegion *R = WL.back();
468 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000469 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000470 }
471
Ted Kremenek14453bf2009-03-03 19:02:42 +0000472 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000473}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000474
Ted Kremenek9af46f52009-06-16 22:36:44 +0000475//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000476// Region Cluster analysis.
477//===----------------------------------------------------------------------===//
478
479namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000480template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000481class ClusterAnalysis {
482protected:
483 typedef BumpVector<BindingKey> RegionCluster;
484 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000485 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
486 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
487 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000488
489 BumpVectorContext BVC;
490 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000491 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000492
493 RegionStoreManager &RM;
494 ASTContext &Ctx;
495 ValueManager &ValMgr;
496
Ted Kremenek5499b842010-03-10 16:32:56 +0000497 RegionBindings B;
498
Ted Kremeneka4fab032010-03-10 07:19:59 +0000499public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000500 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
501 RegionBindings b)
502 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
503 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000504
Ted Kremenek5499b842010-03-10 16:32:56 +0000505 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000506
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000507 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000508 const MemRegion *R = K.getRegion();
509 const MemRegion *baseR = R->getBaseRegion();
510 RegionCluster &C = getCluster(baseR);
511 C.push_back(K, BVC);
512 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000513 return C;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000514 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000515
Ted Kremenek5499b842010-03-10 16:32:56 +0000516 bool isVisited(const MemRegion *R) {
517 return (bool) Visited[&getCluster(R->getBaseRegion())];
518 }
519
520 RegionCluster& getCluster(const MemRegion *R) {
521 RegionCluster *&CRef = ClusterM[R];
522 if (!CRef) {
523 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
524 CRef = new (Mem) RegionCluster(BVC, 10);
525 }
526 return *CRef;
527 }
528
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000529 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000530 // Scan the entire set of bindings and make the region clusters.
531 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000532 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenek5499b842010-03-10 16:32:56 +0000533 if (const MemRegion *R = RI.getData().getAsRegion()) {
534 // Generate a cluster, but don't add the region to the cluster
535 // if there aren't any bindings.
536 getCluster(R->getBaseRegion());
537 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000538 if (includeGlobals) {
539 const MemRegion *R = RI.getKey().getRegion();
540 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
541 AddToWorkList(R, C);
542 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000543 }
544 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000545
546 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
547 if (unsigned &visited = Visited[&C])
548 return false;
549 else
550 visited = 1;
551
552 WL.push_back(std::make_pair(R, &C));
553 return true;
554 }
555
556 bool AddToWorkList(BindingKey K) {
557 return AddToWorkList(K.getRegion());
558 }
559
560 bool AddToWorkList(const MemRegion *R) {
561 const MemRegion *baseR = R->getBaseRegion();
562 return AddToWorkList(baseR, getCluster(baseR));
563 }
564
565 void RunWorkList() {
566 while (!WL.empty()) {
567 const MemRegion *baseR;
568 RegionCluster *C;
569 llvm::tie(baseR, C) = WL.back();
570 WL.pop_back();
571
572 // First visit the cluster.
573 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
574
Ted Kremenek75a2d942010-04-01 00:15:55 +0000575 // Next, visit the base region.
576 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000577 }
578 }
579
580public:
581 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
582 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000583 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000584};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000585}
586
587//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000588// Binding invalidation.
589//===----------------------------------------------------------------------===//
590
Zhongxing Xu13d50172009-10-11 08:08:02 +0000591void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
592 const MemRegion *R,
593 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000594
Ted Kremenekdf165012010-02-02 22:38:47 +0000595 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
596 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
597 I != E; ++I)
598 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000599
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000600 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000601}
602
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000603namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000604class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
605{
606 const Expr *Ex;
607 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000608 StoreManager::InvalidatedSymbols *IS;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000609public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000610 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000611 GRStateManager &stateMgr,
612 RegionBindings b,
613 const Expr *ex, unsigned count,
614 StoreManager::InvalidatedSymbols *is)
615 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
616 Ex(ex), Count(count), IS(is) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000617
Ted Kremenek5499b842010-03-10 16:32:56 +0000618 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000619 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000620
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000621private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000622 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000623};
Ted Kremenek5b290652010-02-03 04:16:00 +0000624}
625
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000626void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000627 // A symbol? Mark it touched by the invalidation.
628 if (IS)
629 if (SymbolRef Sym = V.getAsSymbol())
630 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000631
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000632 if (const MemRegion *R = V.getAsRegion()) {
633 AddToWorkList(R);
634 return;
635 }
636
637 // Is it a LazyCompoundVal? All references get invalidated as well.
638 if (const nonloc::LazyCompoundVal *LCS =
639 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
640
641 const MemRegion *LazyR = LCS->getRegion();
642 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
643
644 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
645 const MemRegion *baseR = RI.getKey().getRegion();
646 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
647 VisitBinding(RI.getData());
648 }
649
650 return;
651 }
652}
653
Ted Kremenek5499b842010-03-10 16:32:56 +0000654void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
655 BindingKey *I, BindingKey *E) {
656 for ( ; I != E; ++I) {
657 // Get the old binding. Is it a region? If so, add it to the worklist.
658 const BindingKey &K = *I;
659 if (const SVal *V = RM.Lookup(B, K))
660 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000661
Ted Kremenek5499b842010-03-10 16:32:56 +0000662 B = RM.Remove(B, K);
663 }
664}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000665
Ted Kremenek75a2d942010-04-01 00:15:55 +0000666void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000667 if (IS) {
668 // Symbolic region? Mark that symbol touched by the invalidation.
669 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
670 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000671 }
672
Ted Kremenek5499b842010-03-10 16:32:56 +0000673 // BlockDataRegion? If so, invalidate captured variables that are passed
674 // by reference.
675 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
676 for (BlockDataRegion::referenced_vars_iterator
677 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
678 BI != BE; ++BI) {
679 const VarRegion *VR = *BI;
680 const VarDecl *VD = VR->getDecl();
681 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
682 AddToWorkList(VR);
683 }
684 return;
685 }
686
687 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
688 // Invalidate the region by setting its default value to
689 // conjured symbol. The type of the symbol is irrelavant.
690 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
691 Count);
692 B = RM.Add(B, baseR, BindingKey::Default, V);
693 return;
694 }
695
696 if (!baseR->isBoundable())
697 return;
698
699 const TypedRegion *TR = cast<TypedRegion>(baseR);
700 QualType T = TR->getValueType(Ctx);
701
702 // Invalidate the binding.
703 if (const RecordType *RT = T->getAsStructureType()) {
704 const RecordDecl *RD = RT->getDecl()->getDefinition();
705 // No record definition. There is nothing we can do.
706 if (!RD) {
707 B = RM.Remove(B, baseR);
708 return;
709 }
710
711 // Invalidate the region by setting its default value to
712 // conjured symbol. The type of the symbol is irrelavant.
713 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
714 Count);
715 B = RM.Add(B, baseR, BindingKey::Default, V);
716 return;
717 }
718
719 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
720 // Set the default value of the array to conjured symbol.
721 DefinedOrUnknownSVal V =
722 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
723 B = RM.Add(B, baseR, BindingKey::Default, V);
724 return;
725 }
726
727 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
728 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
729 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000730}
731
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000732Store RegionStoreManager::InvalidateRegions(Store store,
733 const MemRegion * const *I,
734 const MemRegion * const *E,
735 const Expr *Ex, unsigned Count,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000736 InvalidatedSymbols *IS,
737 bool invalidateGlobals) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000738 InvalidateRegionsWorker W(*this, StateMgr,
739 RegionStoreManager::GetRegionBindings(store),
740 Ex, Count, IS);
741
742 // Scan the bindings and generate the clusters.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000743 W.GenerateClusters(invalidateGlobals);
Ted Kremenek5499b842010-03-10 16:32:56 +0000744
745 // Add I .. E to the worklist.
746 for ( ; I != E; ++I)
747 W.AddToWorkList(*I);
748
749 W.RunWorkList();
750
751 // Return the new bindings.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000752 RegionBindings B = W.getRegionBindings();
753
754 if (invalidateGlobals) {
755 // Bind the non-static globals memory space to a new symbol that we will
756 // use to derive the bindings for all non-static globals.
757 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
758 SVal V =
759 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
760 /* symbol type, doesn't matter */ Ctx.IntTy,
761 Count);
762 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
763 }
764
765 return B.getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000766}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000767
Ted Kremenek9af46f52009-06-16 22:36:44 +0000768//===----------------------------------------------------------------------===//
769// Extents for regions.
770//===----------------------------------------------------------------------===//
771
Zhongxing Xue884ff82009-11-12 02:48:32 +0000772DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000773 const MemRegion *R,
774 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000776 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000777 case MemRegion::CXXThisRegionKind:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000778 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000779 case MemRegion::GenericMemSpaceRegionKind:
780 case MemRegion::StackLocalsSpaceRegionKind:
781 case MemRegion::StackArgumentsSpaceRegionKind:
782 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000783 case MemRegion::NonStaticGlobalSpaceRegionKind:
784 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000785 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000786 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000787 return UnknownVal();
788
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000789 case MemRegion::FunctionTextRegionKind:
790 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000791 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000792 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000793 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000794
795 // Not yet handled.
796 case MemRegion::AllocaRegionKind:
797 case MemRegion::CompoundLiteralRegionKind:
798 case MemRegion::ElementRegionKind:
799 case MemRegion::FieldRegionKind:
800 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000801 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000802 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000804 case MemRegion::SymbolicRegionKind: {
805 const SVal *Size = state->get<RegionExtents>(R);
806 if (!Size)
807 return UnknownVal();
808 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
809 if (!CI)
810 return UnknownVal();
811
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000812 CharUnits RegionSize =
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000813 CharUnits::fromQuantity(CI->getValue().getSExtValue());
814 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
815 assert(RegionSize % EleSize == 0);
816
817 return ValMgr.makeIntVal(RegionSize / EleSize, false);
818 }
819
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000820 case MemRegion::StringRegionKind: {
821 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000822 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000823 // operations with signed indices.
824 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000827 case MemRegion::VarRegionKind: {
828 const VarRegion* VR = cast<VarRegion>(R);
Jordy Rose4d912b22010-06-25 23:23:04 +0000829 ASTContext& Ctx = getContext();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000830 // Get the type of the variable.
Jordy Rose4d912b22010-06-25 23:23:04 +0000831 QualType T = VR->getDesugaredValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000833 // FIXME: Handle variable-length arrays.
834 if (isa<VariableArrayType>(T))
835 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Jordy Rose4d912b22010-06-25 23:23:04 +0000837 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
838
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000839 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
840 // return the size as signed integer.
Jordy Rose4d912b22010-06-25 23:23:04 +0000841 CharUnits RealEleSize = Ctx.getTypeSizeInChars(CAT->getElementType());
842 CharUnits::QuantityType EleRatio = RealEleSize / EleSize;
843 int64_t Length = CAT->getSize().getSExtValue();
844 return ValMgr.makeIntVal(Length * EleRatio, false);
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000845 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000846
Zhongxing Xu9618b852010-04-01 08:20:27 +0000847 // Clients can reinterpret ordinary variables as arrays, possibly of
848 // another type. The width is rounded down to ensure that an access is
849 // entirely within bounds.
Jordy Rose4d912b22010-06-25 23:23:04 +0000850 CharUnits VarSize = Ctx.getTypeSizeInChars(T);
Zhongxing Xu9618b852010-04-01 08:20:27 +0000851 return ValMgr.makeIntVal(VarSize / EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000852 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000855 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000856 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000857}
858
Ted Kremenek9af46f52009-06-16 22:36:44 +0000859//===----------------------------------------------------------------------===//
860// Location and region casting.
861//===----------------------------------------------------------------------===//
862
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000863/// ArrayToPointer - Emulates the "decay" of an array to a pointer
864/// type. 'Array' represents the lvalue of the array being decayed
865/// to a pointer, and the returned SVal represents the decayed
866/// version of that lvalue (i.e., a pointer to the first element of
867/// the array). This is called by GRExprEngine when evaluating casts
868/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000869SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000870 if (!isa<loc::MemRegionVal>(Array))
871 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenekabb042f2008-12-13 19:24:37 +0000873 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
874 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000876 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000877 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000879 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000880 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000881 ArrayType *AT = cast<ArrayType>(T);
882 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek75185b52009-07-16 00:00:11 +0000884 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000885 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
886 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000887}
888
Ted Kremenek9af46f52009-06-16 22:36:44 +0000889//===----------------------------------------------------------------------===//
890// Pointer arithmetic.
891//===----------------------------------------------------------------------===//
892
Zhongxing Xu461147f2010-02-05 05:24:20 +0000893SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000894 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000895 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000896 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000897 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000898
Jordy Roseeac4a002010-06-28 08:26:15 +0000899 // Special case for zero RHS.
900 if (R.isZeroConstant()) {
901 switch (Op) {
902 default:
903 // Handle it normally.
904 break;
905 case BinaryOperator::Add:
906 case BinaryOperator::Sub:
907 // FIXME: does this need to be casted to match resultTy?
908 return L;
909 }
910 }
911
Zhongxing Xua1718c72009-04-03 07:33:13 +0000912 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000913 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000914
Ted Kremenek3bccf082009-07-11 00:58:27 +0000915 switch (MR->getKind()) {
916 case MemRegion::SymbolicRegionKind: {
917 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000918 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000919 QualType T = Sym->getType(getContext());
920 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000922 if (const PointerType *PT = T->getAs<PointerType>())
923 EleTy = PT->getPointeeType();
924 else
John McCall183700f2009-09-21 23:43:11 +0000925 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenek3bccf082009-07-11 00:58:27 +0000927 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
928 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000929 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000930 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000931 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000932 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenek3f8612b2010-06-22 23:58:31 +0000933 QualType EleTy = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000934 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
935 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000936 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000937 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000938
Ted Kremenek3bccf082009-07-11 00:58:27 +0000939 case MemRegion::ElementRegionKind: {
940 ER = cast<ElementRegion>(MR);
941 break;
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek3bccf082009-07-11 00:58:27 +0000944 // Not yet handled.
945 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000946 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000947
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000948 }
949 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000950 case MemRegion::CompoundLiteralRegionKind:
951 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000952 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000953 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000954 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000956 case MemRegion::FunctionTextRegionKind:
957 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000958 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000959 // Technically this can happen if people do funny things with casts.
960 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Ted Kremenekde0d2632010-01-05 02:18:06 +0000962 case MemRegion::CXXThisRegionKind:
963 assert(0 &&
964 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000965 case MemRegion::GenericMemSpaceRegionKind:
966 case MemRegion::StackLocalsSpaceRegionKind:
967 case MemRegion::StackArgumentsSpaceRegionKind:
968 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000969 case MemRegion::NonStaticGlobalSpaceRegionKind:
970 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000971 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000972 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
973 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000974 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000975
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000976 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000977 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000978
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000979 // For now, only support:
980 // (a) concrete integer indices that can easily be resolved
981 // (b) 0 + symbolic index
982 if (Base) {
983 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
984 // FIXME: Should use SValuator here.
985 SVal NewIdx =
986 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000987 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000988 const MemRegion* NewER =
989 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
990 ER->getSuperRegion(), getContext());
991 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000992 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000993 if (0 == Base->getValue()) {
994 const MemRegion* NewER =
995 MRMgr.getElementRegion(ER->getElementType(), R,
996 ER->getSuperRegion(), getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000997 return ValMgr.makeLoc(NewER);
998 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Ted Kremenek5dc27462009-03-03 02:51:43 +00001001 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +00001002}
1003
Ted Kremenek9af46f52009-06-16 22:36:44 +00001004//===----------------------------------------------------------------------===//
1005// Loading values from regions.
1006//===----------------------------------------------------------------------===//
1007
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001008Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xubdfa85f2010-05-29 06:23:24 +00001009 const MemRegion *R) {
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001010
1011 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
1012 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001013
Zhongxing Xu13d50172009-10-11 08:08:02 +00001014 return Optional<SVal>();
1015}
1016
1017Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001018 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001019 if (R->isBoundable())
1020 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1021 if (TR->getValueType(getContext())->isUnionType())
1022 return UnknownVal();
1023
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001024 if (const SVal *V = Lookup(B, R, BindingKey::Default))
1025 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +00001026
1027 return Optional<SVal>();
1028}
1029
1030Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1031 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001032
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001033 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001034 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001035
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001036 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001037}
1038
Ted Kremeneka6275a52009-07-15 02:31:43 +00001039static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1040 RTy = Ctx.getCanonicalType(RTy);
1041 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremeneka6275a52009-07-15 02:31:43 +00001043 if (RTy == UsedTy)
1044 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001045
1046
Ted Kremenek25c54572009-07-20 22:58:02 +00001047 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +00001048 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +00001049 // represents a reinterpretation.
1050 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001051 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001052 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001053
1054 return PUsedTy && PRTy &&
1055 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001056 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001057 }
1058
1059 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001060}
1061
Zhongxing Xu576bb922010-02-05 03:01:53 +00001062SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001063 assert(!isa<UnknownVal>(L) && "location unknown");
1064 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001065
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001066 // FIXME: Is this even possible? Shouldn't this be treated as a null
1067 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001068 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001069 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001070
Ted Kremenek67f28532009-06-17 22:02:04 +00001071 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001072
Tom Care7b050302010-06-25 18:22:31 +00001073 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
1074 if (T.isNull()) {
1075 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
1076 T = SR->getSymbol()->getType(getContext());
1077 }
Zhongxing Xu81491852010-02-08 08:43:02 +00001078 MR = GetElementZeroRegion(MR, T);
Tom Care7b050302010-06-25 18:22:31 +00001079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001081 if (isa<CodeTextRegion>(MR)) {
1082 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001083 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001086 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1087 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001088 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001089 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001090
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001091 // FIXME: We should eventually handle funny addressing. e.g.:
1092 //
1093 // int x = ...;
1094 // int *p = &x;
1095 // char *q = (char*) p;
1096 // char c = *q; // returns the first byte of 'x'.
1097 //
1098 // Such funny addressing will occur due to layering of regions.
1099
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001100#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001101 ASTContext &Ctx = getContext();
1102 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001103 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1104 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001105 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001106 assert(Ctx.getCanonicalType(RTy) ==
1107 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001108 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001109#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001110
Douglas Gregorfb87b892010-04-26 21:31:17 +00001111 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001112 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001114 // FIXME: Handle unions.
1115 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001116 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001117
1118 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001119 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001120
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001121 // FIXME: handle Vector types.
1122 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001123 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001124
1125 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001126 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001127
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001128 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1129 // FIXME: Here we actually perform an implicit conversion from the loaded
1130 // value to the element type. Eventually we want to compose these values
1131 // more intelligently. For example, an 'element' can encompass multiple
1132 // bound regions (e.g., several bound bytes), or could be a subset of
1133 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001134 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001137 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1138 // FIXME: Here we actually perform an implicit conversion from the loaded
1139 // value to the ivar type. What we should model is stores to ivars
1140 // that blow past the extent of the ivar. If the address of the ivar is
1141 // reinterpretted, it is possible we stored a different value that could
1142 // fit within the ivar. Either we need to cast these when storing them
1143 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001144 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001147 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1148 // FIXME: Here we actually perform an implicit conversion from the loaded
1149 // value to the variable type. What we should model is stores to variables
1150 // that blow past the extent of the variable. If the address of the
1151 // variable is reinterpretted, it is possible we stored a different value
1152 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001153 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001154 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001155 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001156
Zhongxing Xu576bb922010-02-05 03:01:53 +00001157 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001158 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001159
1160 // Check if the region has a binding.
1161 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001162 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001163
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001164 // The location does not have a bound value. This means that it has
1165 // the value it had upon its creation and/or entry to the analyzed
1166 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001167 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001168 // All stack variables are considered to have undefined values
1169 // upon creation. All heap allocated blocks are considered to
1170 // have undefined values as well unless they are explicitly bound
1171 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001172 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001173 }
1174
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001175 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001176 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001177}
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001179std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001180RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001181 if (Optional<SVal> OV = getDirectBinding(B, R))
1182 if (const nonloc::LazyCompoundVal *V =
1183 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001184 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001186 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001187 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001188 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001190 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001191 return std::make_pair(X.first,
1192 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001193 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001194 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001195 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001196 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001198 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001199 return std::make_pair(X.first,
1200 MRMgr.getFieldRegionWithSuper(FR, X.second));
1201 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001202 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001203 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001204 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001205}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001206
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001207SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001208 const ElementRegion* R) {
1209 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001210 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001211 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001212 return *V;
1213
Ted Kremenek921109a2009-07-01 23:19:52 +00001214 const MemRegion* superR = R->getSuperRegion();
1215
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001216 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001217 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001218 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001219 // an integer, e.g., *((unsigned int*)"hello")
1220 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001221 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001222 if (T != Ctx.getCanonicalType(R->getElementType()))
1223 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001224
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001225 const StringLiteral *Str = StrR->getStringLiteral();
1226 SVal Idx = R->getIndex();
1227 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1228 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001229 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001230 if (i > byteLength) {
1231 // Buffer overflow checking in GRExprEngine should handle this case,
1232 // but we shouldn't rely on it to not overflow here if that checking
1233 // is disabled.
1234 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001235 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001236 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001237 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001238 }
1239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremeneka709b872010-05-31 01:22:04 +00001241 // Handle the case where we are indexing into a larger scalar object.
1242 // For example, this handles:
1243 // int x = ...
1244 // char *y = &x;
1245 // return *y;
1246 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1247 const RegionRawOffset &O = R->getAsRawOffset();
1248 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
1249 QualType baseT = baseR->getValueType(Ctx);
1250 if (baseT->isScalarType()) {
1251 QualType elemT = R->getElementType();
1252 if (elemT->isScalarType()) {
1253 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1254 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1255 if (SymbolRef parentSym = V->getAsSymbol())
1256 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001257
Ted Kremeneka709b872010-05-31 01:22:04 +00001258 if (V->isUnknownOrUndef())
1259 return *V;
1260 // Other cases: give up. We are indexing into a larger object
1261 // that has some value, but we don't know how to handle that yet.
1262 return UnknownVal();
1263 }
1264 }
1265 }
Zhongxing Xu42c67bf2010-05-29 06:49:04 +00001266 }
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001267 }
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001268 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001269}
1270
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001271SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001272 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001273
1274 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001275 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001276 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001277 return *V;
1278
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001279 QualType Ty = R->getValueType(getContext());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001280 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001281}
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001283Optional<SVal>
1284RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1285 const MemRegion *superR,
1286 const TypedRegion *R,
1287 QualType Ty) {
1288
1289 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1290 if (SymbolRef parentSym = D->getAsSymbol())
1291 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1292
1293 if (D->isZeroConstant())
1294 return ValMgr.makeZeroVal(Ty);
1295
1296 if (D->isUnknownOrUndef())
1297 return *D;
1298
1299 assert(0 && "Unknown default value");
1300 }
1301
1302 return Optional<SVal>();
1303}
1304
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001305SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001306 const TypedRegion *R,
1307 QualType Ty,
1308 const MemRegion *superR) {
1309
Mike Stump1eb44332009-09-09 15:08:12 +00001310 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001311 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001313 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001315 while (superR) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001316 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1317 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001319 // If our super region is a field or element itself, walk up the region
1320 // hierarchy to see if there is a default value installed in an ancestor.
1321 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1322 superR = cast<SubRegion>(superR)->getSuperRegion();
1323 continue;
1324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001326 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001327 }
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001329 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001330 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001331 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001332 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001334 if (lazyBindingRegion) {
1335 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1336 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001337 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001338 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001339 }
1340
Ted Kremenekde0d2632010-01-05 02:18:06 +00001341 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001342 if (isa<ElementRegion>(R)) {
1343 // Currently we don't reason specially about Clang-style vectors. Check
1344 // if superR is a vector and if so return Unknown.
1345 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1346 if (typedSuperR->getValueType(getContext())->isVectorType())
1347 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001348 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001349 }
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001351 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001354 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001355 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001356}
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Zhongxing Xu576bb922010-02-05 03:01:53 +00001358SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001359
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001360 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001361 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001362
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001363 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001364 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001366 const MemRegion *superR = R->getSuperRegion();
1367
Ted Kremenekab22ee92009-10-20 01:20:57 +00001368 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001369 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001370 if (SymbolRef parentSym = V->getAsSymbol())
1371 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001373 // Other cases: give up.
1374 return UnknownVal();
1375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Zhongxing Xu576bb922010-02-05 03:01:53 +00001377 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001378}
1379
Zhongxing Xu576bb922010-02-05 03:01:53 +00001380SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Ted Kremenek9031dd72009-07-21 00:12:07 +00001382 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001383 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001385 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001386 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Ted Kremenek9031dd72009-07-21 00:12:07 +00001388 // Lazily derive a value for the VarRegion.
1389 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001390 QualType T = VD->getType();
1391 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001392
1393 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001394 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001395 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Ted Kremenek4dc15662010-02-06 03:57:59 +00001397 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001398 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001399 // Is 'VD' declared constant? If so, retrieve the constant value.
1400 QualType CT = Ctx.getCanonicalType(T);
1401 if (CT.isConstQualified()) {
1402 const Expr *Init = VD->getInit();
1403 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1404 if (Init)
1405 if (const IntegerLiteral *IL =
1406 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1407 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1408 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1409 IL->getType());
1410 }
1411 }
1412
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001413 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1414 return V.getValue();
1415
Zhongxing Xu14d23282010-03-01 06:56:52 +00001416 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Ted Kremenek4dc15662010-02-06 03:57:59 +00001419 if (T->isIntegerType())
1420 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001421 if (T->isPointerType())
1422 return ValMgr.makeNull();
1423
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001424 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001425 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001426
Ted Kremenek9031dd72009-07-21 00:12:07 +00001427 return UndefinedVal();
1428}
1429
Zhongxing Xu576bb922010-02-05 03:01:53 +00001430SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Ted Kremenek25c54572009-07-20 22:58:02 +00001432 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001433
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001434 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001435 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001436}
1437
Zhongxing Xu576bb922010-02-05 03:01:53 +00001438SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001439 QualType T = R->getValueType(getContext());
Douglas Gregorfb87b892010-04-26 21:31:17 +00001440 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001441 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001442}
1443
Zhongxing Xu576bb922010-02-05 03:01:53 +00001444SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001445 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001446 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001447}
1448
Ted Kremenek9af46f52009-06-16 22:36:44 +00001449//===----------------------------------------------------------------------===//
1450// Binding values to regions.
1451//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001452
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001453Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001454 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001455 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001456 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Ted Kremenek0964a062009-01-21 06:57:53 +00001458 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001459}
1460
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001461Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001462 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001463 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001464
Ted Kremenek9af46f52009-06-16 22:36:44 +00001465 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001466 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Ted Kremenek9af46f52009-06-16 22:36:44 +00001468 // Check if the region is a struct region.
1469 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Douglas Gregorfb87b892010-04-26 21:31:17 +00001470 if (TR->getValueType(getContext())->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001471 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001473 // Special case: the current region represents a cast and it and the super
1474 // region both have pointer types or intptr_t types. If so, perform the
1475 // bind to the super region.
1476 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001477 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001478 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1479 if (ER->getIndex().isZeroConstant()) {
1480 if (const TypedRegion *superR =
1481 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1482 ASTContext &Ctx = getContext();
1483 QualType superTy = superR->getValueType(Ctx);
1484 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001485
1486 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001487 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001488 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001489 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001490 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001491 // For now, just invalidate the fields of the struct/union/class.
1492 // FIXME: Precisely handle the fields of the record.
1493 if (superTy->isRecordType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001494 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001495 }
1496 }
1497 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001498 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1499 // Binding directly to a symbolic region should be treated as binding
1500 // to element 0.
1501 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001502
Ted Kremenek852274d2009-12-16 03:18:58 +00001503 // FIXME: Is this the right way to handle symbols that are references?
1504 if (const PointerType *PT = T->getAs<PointerType>())
1505 T = PT->getPointeeType();
1506 else
1507 T = T->getAs<ReferenceType>()->getPointeeType();
1508
Ted Kremenek0954cde2009-09-24 04:11:44 +00001509 R = GetElementZeroRegion(SR, T);
1510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001512 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001513 RegionBindings B = GetRegionBindings(store);
1514 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001515}
1516
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001517Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001518 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001519
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001520 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001521
Ted Kremenek0964a062009-01-21 06:57:53 +00001522 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001523 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001524 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001525 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001526
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001527 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001528}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001529
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001530// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001531Store RegionStoreManager::BindCompoundLiteral(Store store,
1532 const CompoundLiteralExpr *CL,
1533 const LocationContext *LC,
1534 SVal V) {
1535 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001536 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001537}
1538
Zhongxing Xua5ce9662010-06-01 03:01:33 +00001539
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001540Store RegionStoreManager::setImplicitDefaultValue(Store store,
1541 const MemRegion *R,
1542 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001543 RegionBindings B = GetRegionBindings(store);
1544 SVal V;
1545
1546 if (Loc::IsLocType(T))
1547 V = ValMgr.makeNull();
1548 else if (T->isIntegerType())
1549 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001550 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001551 // Set the default value to a zero constant when it is a structure
1552 // or array. The type doesn't really matter.
1553 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1554 }
1555 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001556 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001557 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001558
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001559 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001560}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001561
1562Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001563 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001564
Ted Kremenekfee90812010-01-26 23:51:00 +00001565 ASTContext &Ctx = getContext();
1566 const ArrayType *AT =
1567 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001568 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001569 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001570
Ted Kremenekfee90812010-01-26 23:51:00 +00001571 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1572 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001573
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001574 // Check if the init expr is a StringLiteral.
1575 if (isa<loc::MemRegionVal>(Init)) {
1576 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1577 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1578 const char* str = S->getStrData();
1579 unsigned len = S->getByteLength();
1580 unsigned j = 0;
1581
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001582 // Copy bytes from the string literal into the target array. Trailing bytes
1583 // in the array that are not covered by the string literal are initialized
1584 // to zero.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001585
Ted Kremenekfee90812010-01-26 23:51:00 +00001586 // We assume that string constants are bound to
1587 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001588 uint64_t size = Size.getValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001589
Ted Kremenek46537392009-07-16 01:33:37 +00001590 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001591 if (j >= len)
1592 break;
1593
Ted Kremenek46537392009-07-16 01:33:37 +00001594 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001595 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1596 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001597
Zhongxing Xud91ee272009-06-23 09:02:15 +00001598 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001599 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001600 }
1601
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001602 return store;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001603 }
1604
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001605 // Handle lazy compound values.
1606 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001607 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001608
1609 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001610
Ted Kremenek027e2662009-11-19 20:20:24 +00001611 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001612 return setImplicitDefaultValue(store, R, ElementTy);
1613
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001614 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001615 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001616 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenekfee90812010-01-26 23:51:00 +00001618 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001619 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001620 if (VI == VE)
1621 break;
1622
Ted Kremenek46537392009-07-16 01:33:37 +00001623 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001624 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001625
Douglas Gregorfb87b892010-04-26 21:31:17 +00001626 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001627 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001628 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001629 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001630 }
1631
Ted Kremenek027e2662009-11-19 20:20:24 +00001632 // If the init list is shorter than the array length, set the
1633 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001634 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001635 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001636
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001637 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001638}
1639
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001640Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1641 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Ted Kremenek67f28532009-06-17 22:02:04 +00001643 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001644 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001646 QualType T = R->getValueType(getContext());
Douglas Gregorfb87b892010-04-26 21:31:17 +00001647 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001648
Ted Kremenek6217b802009-07-29 21:53:49 +00001649 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001650 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001651
1652 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001653 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001654
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001655 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001656 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001657 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Ted Kremenek67f28532009-06-17 22:02:04 +00001659 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1660 // Ignore them and kill the field values.
1661 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001662 return KillStruct(store, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001663
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001664 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001665 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001666
1667 RecordDecl::field_iterator FI, FE;
1668
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001669 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001670
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001671 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001672 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001673
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001674 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001675 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001676
Ted Kremenekcf549592009-09-22 21:19:14 +00001677 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001678 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001679 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001680 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001681 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001682 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001683 }
1684
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001685 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001686 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001687 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001688 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001689 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001690 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001691
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001692 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001693}
1694
Zhongxing Xu13d50172009-10-11 08:08:02 +00001695Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1696 RegionBindings B = GetRegionBindings(store);
1697 llvm::OwningPtr<RegionStoreSubRegionMap>
1698 SubRegions(getRegionStoreSubRegionMap(store));
1699 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001700
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001701 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001702 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001703}
1704
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001705Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1706 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001707
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001708 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001709 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001710
Mike Stump1eb44332009-09-09 15:08:12 +00001711 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001712 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001713
Mike Stump1eb44332009-09-09 15:08:12 +00001714 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001715 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001717 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1718 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001719 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001720}
1721
1722//===----------------------------------------------------------------------===//
1723// "Raw" retrievals and bindings.
1724//===----------------------------------------------------------------------===//
1725
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001726BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001727 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1728 const RegionRawOffset &O = ER->getAsRawOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001729
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001730 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001731 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001732
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001733 // FIXME: There are some ElementRegions for which we cannot compute
1734 // raw offsets yet, including regions with symbolic offsets.
1735 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001736
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001737 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001738}
1739
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001740RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001741 return RBFactory.Add(B, K, V);
1742}
1743
1744RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001745 BindingKey::Kind k, SVal V) {
1746 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001747}
1748
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001749const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001750 return B.lookup(K);
1751}
1752
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001753const SVal *RegionStoreManager::Lookup(RegionBindings B,
1754 const MemRegion *R,
1755 BindingKey::Kind k) {
1756 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001757}
1758
1759RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1760 return RBFactory.Remove(B, K);
1761}
1762
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001763RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1764 BindingKey::Kind k){
1765 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001766}
1767
1768Store RegionStoreManager::Remove(Store store, BindingKey K) {
1769 RegionBindings B = GetRegionBindings(store);
1770 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001771}
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Ted Kremenek9af46f52009-06-16 22:36:44 +00001773//===----------------------------------------------------------------------===//
1774// State pruning.
1775//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001776
Ted Kremenek5499b842010-03-10 16:32:56 +00001777namespace {
1778class RemoveDeadBindingsWorker :
1779 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1780 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1781 SymbolReaper &SymReaper;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001782 const StackFrameContext *CurrentLCtx;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001783
Ted Kremenek5499b842010-03-10 16:32:56 +00001784public:
1785 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1786 RegionBindings b, SymbolReaper &symReaper,
Jordy Rose7dadf792010-07-01 20:09:55 +00001787 const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001788 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rose7dadf792010-07-01 20:09:55 +00001789 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001790
1791 // Called by ClusterAnalysis.
1792 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1793 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001794
Ted Kremenek75a2d942010-04-01 00:15:55 +00001795 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001796 bool UpdatePostponed();
1797 void VisitBinding(SVal V);
1798};
1799}
1800
1801void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1802 RegionCluster &C) {
1803
1804 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rose7dadf792010-07-01 20:09:55 +00001805 if (SymReaper.isLive(VR))
Ted Kremenek5499b842010-03-10 16:32:56 +00001806 AddToWorkList(baseR, C);
1807
1808 return;
1809 }
1810
1811 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1812 if (SymReaper.isLive(SR->getSymbol()))
1813 AddToWorkList(SR, C);
1814 else
1815 Postponed.push_back(SR);
1816
1817 return;
1818 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001819
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001820 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1821 AddToWorkList(baseR, C);
1822 return;
1823 }
1824
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001825 // CXXThisRegion in the current or parent location context is live.
1826 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001827 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001828 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1829 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1830 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1831 AddToWorkList(TR, C);
1832 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001833}
1834
1835void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1836 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001837 for ( ; I != E; ++I)
1838 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001839}
1840
1841void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1842 // Is it a LazyCompoundVal? All referenced regions are live as well.
1843 if (const nonloc::LazyCompoundVal *LCS =
1844 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1845
1846 const MemRegion *LazyR = LCS->getRegion();
1847 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1848 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
1849 const MemRegion *baseR = RI.getKey().getRegion();
1850 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
1851 VisitBinding(RI.getData());
1852 }
1853 return;
1854 }
1855
1856 // If V is a region, then add it to the worklist.
1857 if (const MemRegion *R = V.getAsRegion())
1858 AddToWorkList(R);
1859
1860 // Update the set of live symbols.
1861 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1862 SI!=SE;++SI)
1863 SymReaper.markLive(*SI);
1864}
1865
Ted Kremenek75a2d942010-04-01 00:15:55 +00001866void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1867 const MemRegion *R = K.getRegion();
1868
Ted Kremenek5499b842010-03-10 16:32:56 +00001869 // Mark this region "live" by adding it to the worklist. This will cause
1870 // use to visit all regions in the cluster (if we haven't visited them
1871 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001872 if (AddToWorkList(R)) {
1873 // Mark the symbol for any live SymbolicRegion as "live". This means we
1874 // should continue to track that symbol.
1875 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1876 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001877
Ted Kremenek75a2d942010-04-01 00:15:55 +00001878 // For BlockDataRegions, enqueue the VarRegions for variables marked
1879 // with __block (passed-by-reference).
1880 // via BlockDeclRefExprs.
1881 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1882 for (BlockDataRegion::referenced_vars_iterator
1883 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1884 RI != RE; ++RI) {
1885 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1886 AddToWorkList(*RI);
1887 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001888
Ted Kremenek75a2d942010-04-01 00:15:55 +00001889 // No possible data bindings on a BlockDataRegion.
1890 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001891 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001892 }
1893
Ted Kremenek75a2d942010-04-01 00:15:55 +00001894 // Visit the data binding for K.
1895 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001896 VisitBinding(*V);
1897}
1898
1899bool RemoveDeadBindingsWorker::UpdatePostponed() {
1900 // See if any postponed SymbolicRegions are actually live now, after
1901 // having done a scan.
1902 bool changed = false;
1903
1904 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1905 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1906 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1907 if (SymReaper.isLive(SR->getSymbol())) {
1908 changed |= AddToWorkList(SR);
1909 *I = NULL;
1910 }
1911 }
1912 }
1913
1914 return changed;
1915}
1916
Jordy Rose7dadf792010-07-01 20:09:55 +00001917const GRState *RegionStoreManager::RemoveDeadBindings(GRState &state,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001918 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001919 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001920 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001921{
Zhongxing Xu95798982010-05-26 03:27:35 +00001922 RegionBindings B = GetRegionBindings(state.getStore());
Jordy Rose7dadf792010-07-01 20:09:55 +00001923 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001924 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Ted Kremenek5499b842010-03-10 16:32:56 +00001926 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001927 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001928 E=RegionRoots.end(); I!=E; ++I)
1929 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001930
Ted Kremenek5499b842010-03-10 16:32:56 +00001931 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001932
Ted Kremenek9af46f52009-06-16 22:36:44 +00001933 // We have now scanned the store, marking reachable regions and symbols
1934 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001935 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001936 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001937 const BindingKey &K = I.getKey();
1938
Ted Kremenekb7118f72010-03-10 16:38:41 +00001939 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001940 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001941 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Ted Kremenek5499b842010-03-10 16:32:56 +00001943 // Remove the dead entry.
1944 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Ted Kremenek5499b842010-03-10 16:32:56 +00001946 // Mark all non-live symbols that this binding references as dead.
1947 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001948 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001949
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001950 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001951 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1952 for (; SI != SE; ++SI)
1953 SymReaper.maybeDead(*SI);
1954 }
Zhongxing Xu95798982010-05-26 03:27:35 +00001955 state.setStore(B.getRoot());
1956 const GRState *s = StateMgr.getPersistentState(state);
1957 // Remove the extents of dead symbolic regions.
Zhongxing Xued4214c2010-05-26 03:36:08 +00001958 llvm::ImmutableMap<const MemRegion*,SVal> Extents = s->get<RegionExtents>();
Zhongxing Xu95798982010-05-26 03:27:35 +00001959 for (llvm::ImmutableMap<const MemRegion *, SVal>::iterator I=Extents.begin(),
1960 E = Extents.end(); I != E; ++I) {
1961 if (!W.isVisited(I->first))
1962 s = s->remove<RegionExtents>(I->first);
1963 }
1964 return s;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001965}
1966
Ted Kremenek5499b842010-03-10 16:32:56 +00001967
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001968GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1969 StackFrameContext const *frame) {
1970 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001971 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001972 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001973
1974 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1975 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1976
1977 // Copy the arg expression value to the arg variables.
1978 for (; AI != AE; ++AI, ++PI) {
1979 SVal ArgVal = state->getSVal(*AI);
1980 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1981 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001982 } else if (const CXXConstructExpr *CE =
Zhongxing Xuc5063572010-03-16 13:14:16 +00001983 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001984 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xuc5063572010-03-16 13:14:16 +00001985 AE = CE->arg_end();
1986
1987 // Copy the arg expression value to the arg variables.
1988 for (; AI != AE; ++AI, ++PI) {
1989 SVal ArgVal = state->getSVal(*AI);
1990 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1991 }
1992 } else
1993 assert(0 && "Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001994
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001995 return state->makeWithStore(store);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001996}
1997
Ted Kremenek9af46f52009-06-16 22:36:44 +00001998//===----------------------------------------------------------------------===//
1999// Utility methods.
2000//===----------------------------------------------------------------------===//
2001
Ted Kremenek53ba0b62009-06-24 23:06:47 +00002002void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00002003 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00002004 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00002005 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Ted Kremenek451ac092009-08-06 04:50:20 +00002007 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002008 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00002009}