blob: 376921f00e64da2661fc69b8e0c22dc71564a1ff [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//===----------------------------------------------------------------------===//
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
18#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Checker/PathSensitive/GRState.h"
20#include "clang/Checker/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
35// Representation of value bindings.
36//===----------------------------------------------------------------------===//
37
Zhongxing Xu13d50172009-10-11 08:08:02 +000038namespace {
39class BindingVal {
40public:
41 enum BindingKind { Direct, Default };
42private:
43 SVal Value;
44 BindingKind Kind;
45
46public:
47 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
48
49 bool isDefault() const { return Kind == Default; }
50
51 const SVal *getValue() const { return &Value; }
52
53 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
54
55 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
56
57 void Profile(llvm::FoldingSetNodeID& ID) const {
58 Value.Profile(ID);
59 ID.AddInteger(Kind);
60 }
61
62 inline bool operator==(const BindingVal& R) const {
63 return Value == R.Value && Kind == R.Kind;
64 }
65
66 inline bool operator!=(const BindingVal& R) const {
67 return !(*this == R);
68 }
69};
70}
71
72namespace llvm {
73static inline
74llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
75 if (V.isDefault())
76 os << "(default) ";
77 else
78 os << "(direct) ";
79 os << *V.getValue();
80 return os;
81}
82} // end llvm namespace
83
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084//===----------------------------------------------------------------------===//
85// Representation of binding keys.
86//===----------------------------------------------------------------------===//
87
88namespace {
89 class BindingKey : public std::pair<const MemRegion*, uint64_t> {
90public:
Ted Kremenekc50e6df2010-01-11 02:33:26 +000091 explicit BindingKey(const MemRegion *r, uint64_t offset)
92 : std::pair<const MemRegion*,uint64_t>(r, offset) { assert(r); }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000093
94 const MemRegion *getRegion() const { return first; }
95 uint64_t getOffset() const { return second; }
96
97 void Profile(llvm::FoldingSetNodeID& ID) const {
98 ID.AddPointer(getRegion());
99 ID.AddInteger(getOffset());
100 }
Ted Kremenekc50e6df2010-01-11 02:33:26 +0000101
102 static BindingKey Make(const MemRegion *R);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000103};
104} // end anonymous namespace
105
106namespace llvm {
107 static inline
108 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
109 os << '(' << K.getRegion() << ',' << K.getOffset() << ')';
110 return os;
111 }
112} // end llvm namespace
113
114//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000115// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000116//===----------------------------------------------------------------------===//
117
118typedef llvm::ImmutableMap<BindingKey, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000119
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000120//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000121// Fine-grained control of RegionStoreManager.
122//===----------------------------------------------------------------------===//
123
124namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000125struct minimal_features_tag {};
126struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000128class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000129 bool SupportsFields;
130 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek9af46f52009-06-16 22:36:44 +0000132public:
133 RegionStoreFeatures(minimal_features_tag) :
134 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek9af46f52009-06-16 22:36:44 +0000136 RegionStoreFeatures(maximal_features_tag) :
137 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenek9af46f52009-06-16 22:36:44 +0000139 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek9af46f52009-06-16 22:36:44 +0000141 bool supportsFields() const { return SupportsFields; }
142 bool supportsRemaining() const { return SupportsRemaining; }
143};
144}
145
146//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000147// Region "Extents"
148//===----------------------------------------------------------------------===//
149//
150// MemRegions represent chunks of memory with a size (their "extent"). This
151// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000152//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000153namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000154static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000155namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000156 template<> struct GRStateTrait<RegionExtents>
157 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
158 static void* GDMIndex() { return &RegionExtentsIndex; }
159 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000160}
161
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000162//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000163// Utility functions.
164//===----------------------------------------------------------------------===//
165
166static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
167 if (ty->isAnyPointerType())
168 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000170 return ty->isIntegerType() && ty->isScalarType() &&
171 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
172}
173
174//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000175// Main RegionStore logic.
176//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000177
Zhongxing Xu17892752008-10-08 02:50:44 +0000178namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000180class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000181 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000182 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000183 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000184 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000185public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000186 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000187 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000188
189 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000190 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000191 return true;
192 }
193
194 I->second = F.Add(I->second, SubRegion);
195 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000198 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek59e8f112009-03-03 01:35:36 +0000200 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek5dc27462009-03-03 02:51:43 +0000202 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000203 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000204
205 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000206 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Ted Kremenek59e8f112009-03-03 01:35:36 +0000208 llvm::ImmutableSet<const MemRegion*> S = I->second;
209 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
210 SI != SE; ++SI) {
211 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000212 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek5dc27462009-03-03 02:51:43 +0000215 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000216 }
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000218 typedef SetTy::iterator iterator;
219
220 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
221 Map::iterator I = M.find(R);
222 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
223 return std::make_pair(S.begin(), S.end());
224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000226
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000227class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000228 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000229 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000230
231 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
232 SMCache SC;
233
Zhongxing Xu17892752008-10-08 02:50:44 +0000234public:
Mike Stump1eb44332009-09-09 15:08:12 +0000235 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000236 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000237 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000238 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000239
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000240 virtual ~RegionStoreManager() {
241 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
242 delete (*I).second;
243 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000244
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000245 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Zhongxing Xu13d50172009-10-11 08:08:02 +0000247 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Zhongxing Xu13d50172009-10-11 08:08:02 +0000249 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
250 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000251 /// getDefaultBinding - Returns an SVal* representing an optional default
252 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000253 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000254
255 /// setImplicitDefaultValue - Set the default binding for the provided
256 /// MemRegion to the value implicitly defined for compound literals when
257 /// the value is not specified.
258 const GRState *setImplicitDefaultValue(const GRState *state,
259 const MemRegion *R,
260 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000262 /// getLValueString - Returns an SVal representing the lvalue of a
263 /// StringLiteral. Within RegionStore a StringLiteral has an
264 /// associated StringRegion, and the lvalue of a StringLiteral is
265 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000266 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000267
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000268 /// getLValueCompoundLiteral - Returns an SVal representing the
269 /// lvalue of a compound literal. Within RegionStore a compound
270 /// literal has an associated region, and the lvalue of the
271 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000272 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000273
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000274 /// getLValueVar - Returns an SVal that represents the lvalue of a
275 /// variable. Within RegionStore a variable has an associated
276 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000277 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000279 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000280
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000281 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000283 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000284
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000285 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000286
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000287
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000288 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
289 /// type. 'Array' represents the lvalue of the array being decayed
290 /// to a pointer, and the returned SVal represents the decayed
291 /// version of that lvalue (i.e., a pointer to the first element of
292 /// the array). This is called by GRExprEngine when evaluating
293 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000294 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000295
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000296 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000297 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000298
Mike Stump1eb44332009-09-09 15:08:12 +0000299 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000300 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000301 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000302
Ted Kremenek67f28532009-06-17 22:02:04 +0000303 //===-------------------------------------------------------------------===//
304 // Binding values to regions.
305 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000306
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000307 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000308 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-12-03 03:27:11 +0000309 InvalidatedSymbols *IS) {
310 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
311 }
312
313 const GRState *InvalidateRegions(const GRState *state,
314 const MemRegion * const *Begin,
315 const MemRegion * const *End,
316 const Expr *E, unsigned Count,
317 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000319private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000320 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000321 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000323 RegionBindings Add(RegionBindings B, BindingKey K, BindingVal V);
324 RegionBindings Add(RegionBindings B, const MemRegion *R, BindingVal V);
325
326 const BindingVal *Lookup(RegionBindings B, BindingKey K);
327 const BindingVal *Lookup(RegionBindings B, const MemRegion *R);
328
329 RegionBindings Remove(RegionBindings B, BindingKey K);
330 RegionBindings Remove(RegionBindings B, const MemRegion *R);
331 Store Remove(Store store, BindingKey K);
332
Mike Stump1eb44332009-09-09 15:08:12 +0000333public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000334 const GRState *Bind(const GRState *state, Loc LV, SVal V);
335
336 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +0000337 const CompoundLiteralExpr* CL,
338 const LocationContext *LC,
339 SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000341 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
342 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000343
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000344 const GRState *BindDeclWithNoInit(const GRState *state,
345 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000346 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000347 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000348
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 /// BindStruct - Bind a compound value to a structure.
350 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000353
354 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000355 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 Store Remove(Store store, Loc LV);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000358
Ted Kremenek67f28532009-06-17 22:02:04 +0000359
360 //===------------------------------------------------------------------===//
361 // Loading values from regions.
362 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek67f28532009-06-17 22:02:04 +0000364 /// The high level logic for this method is this:
365 /// Retrieve (L)
366 /// if L has binding
367 /// return L's binding
368 /// else if L is in killset
369 /// return unknown
370 /// else
371 /// if L is on stack or heap
372 /// return undefined
373 /// else
374 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000375 SValuator::CastResult Retrieve(const GRState *state, Loc L,
376 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000377
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000378 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000379
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000380 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000382 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek9031dd72009-07-21 00:12:07 +0000384 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek25c54572009-07-20 22:58:02 +0000386 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000388 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
389 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek67f28532009-06-17 22:02:04 +0000391 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000392 /// struct copy:
393 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000394 /// x = y;
395 /// y's value is retrieved by this method.
396 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek67f28532009-06-17 22:02:04 +0000398 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000400 /// Get the state and region whose binding this region R corresponds to.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000401 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000402 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000404 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
405 const GRState *state,
406 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000407
Ted Kremenek0954cde2009-09-24 04:11:44 +0000408 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
409 QualType T);
410
Ted Kremenek67f28532009-06-17 22:02:04 +0000411 //===------------------------------------------------------------------===//
412 // State pruning.
413 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek67f28532009-06-17 22:02:04 +0000415 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
416 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000417 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000418 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
419
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000420 const GRState *EnterStackFrame(const GRState *state,
421 const StackFrameContext *frame);
422
Ted Kremenek67f28532009-06-17 22:02:04 +0000423 //===------------------------------------------------------------------===//
424 // Region "extents".
425 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000427 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000428 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000429 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000430
431 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000432 // Utility methods.
433 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek451ac092009-08-06 04:50:20 +0000435 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000436 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000437 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000438
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000439 void print(Store store, llvm::raw_ostream& Out, const char* nl,
440 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000441
442 void iterBindings(Store store, BindingsHandler& f) {
443 // FIXME: Implement.
444 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000445
Ted Kremenek67f28532009-06-17 22:02:04 +0000446 // FIXME: Remove.
447 BasicValueFactory& getBasicVals() {
448 return StateMgr.getBasicVals();
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek67f28532009-06-17 22:02:04 +0000451 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000452 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000453};
454
455} // end anonymous namespace
456
Ted Kremenek9af46f52009-06-16 22:36:44 +0000457//===----------------------------------------------------------------------===//
458// RegionStore creation.
459//===----------------------------------------------------------------------===//
460
461StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
462 RegionStoreFeatures F = maximal_features_tag();
463 return new RegionStoreManager(StMgr, F);
464}
465
466StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
467 RegionStoreFeatures F = minimal_features_tag();
468 F.enableFields(true);
469 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000470}
471
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000472void
473RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000474 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000475 const MemRegion *superR = R->getSuperRegion();
476 if (add(superR, R))
477 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000478 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000479}
480
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000481RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000482RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
483 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000484 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000486 llvm::SmallVector<const SubRegion*, 10> WL;
487
Ted Kremenek451ac092009-08-06 04:50:20 +0000488 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000489 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000490 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Mike Stump1eb44332009-09-09 15:08:12 +0000492 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000493 // don't have direct bindings but are super regions of those that do.
494 while (!WL.empty()) {
495 const SubRegion *R = WL.back();
496 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000497 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000498 }
499
Ted Kremenek14453bf2009-03-03 19:02:42 +0000500 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000501}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000502
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000503SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000504 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000505}
506
Ted Kremenek9af46f52009-06-16 22:36:44 +0000507//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000508// Binding invalidation.
509//===----------------------------------------------------------------------===//
510
Zhongxing Xu13d50172009-10-11 08:08:02 +0000511void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
512 const MemRegion *R,
513 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000514 RegionStoreSubRegionMap::iterator I, E;
515
516 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000517 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000518
519 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000520}
521
Ted Kremenek81a95832009-12-03 03:27:11 +0000522const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
523 const MemRegion * const *I,
524 const MemRegion * const *E,
525 const Expr *Ex,
526 unsigned Count,
527 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000528 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek87806792009-09-27 20:45:21 +0000530 // Get the mapping of regions -> subregions.
531 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000532 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000533
534 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000535
Ted Kremenek87806792009-09-27 20:45:21 +0000536 llvm::DenseMap<const MemRegion *, unsigned> Visited;
537 llvm::SmallVector<const MemRegion *, 10> WorkList;
Ted Kremenek81a95832009-12-03 03:27:11 +0000538
539 for ( ; I != E; ++I) {
540 // Strip away casts.
541 WorkList.push_back((*I)->StripCasts());
542 }
Ted Kremenek87806792009-09-27 20:45:21 +0000543
544 while (!WorkList.empty()) {
Ted Kremenek81a95832009-12-03 03:27:11 +0000545 const MemRegion *R = WorkList.back();
Ted Kremenek87806792009-09-27 20:45:21 +0000546 WorkList.pop_back();
547
548 // Have we visited this region before?
549 unsigned &visited = Visited[R];
550 if (visited)
551 continue;
552 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek87806792009-09-27 20:45:21 +0000554 // Add subregions to work list.
555 RegionStoreSubRegionMap::iterator I, E;
556 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
557 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000558
559 // Get the old binding. Is it a region? If so, add it to the worklist.
560 if (Optional<SVal> V = getDirectBinding(B, R)) {
561 if (const MemRegion *RV = V->getAsRegion())
562 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000563
564 // A symbol? Mark it touched by the invalidation.
565 if (IS) {
566 if (SymbolRef Sym = V->getAsSymbol())
567 IS->insert(Sym);
568 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000569 }
570
Ted Kremenek473e1672009-10-16 00:30:49 +0000571 // Symbolic region? Mark that symbol touched by the invalidation.
572 if (IS) {
573 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
574 IS->insert(SR->getSymbol());
575 }
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000576
577 // BlockDataRegion? If so, invalidate captured variables that are passed
578 // by reference.
579 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
580 for (BlockDataRegion::referenced_vars_iterator
581 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
582 I != E; ++I) {
583 const VarRegion *VR = *I;
584 if (VR->getDecl()->getAttr<BlocksAttr>())
585 WorkList.push_back(VR);
586 }
587 continue;
588 }
Ted Kremenek473e1672009-10-16 00:30:49 +0000589
590 // Handle the region itself.
Ted Kremenekc410d4d2009-12-16 23:53:37 +0000591 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000592 // Invalidate the region by setting its default value to
593 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000594 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000595 Count);
596 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000597 continue;
598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Ted Kremenek87806792009-09-27 20:45:21 +0000600 if (!R->isBoundable())
601 continue;
602
603 const TypedRegion *TR = cast<TypedRegion>(R);
604 QualType T = TR->getValueType(Ctx);
605
606 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000607 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
608
Zhongxing Xu13d50172009-10-11 08:08:02 +0000609 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000610 if (!RD)
611 continue;
612
Zhongxing Xu13d50172009-10-11 08:08:02 +0000613 // Invalidate the region by setting its default value to
614 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000615 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
616 Count);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000617 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000618 continue;
619 }
620
621 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
622 // Set the default value of the array to conjured symbol.
623 DefinedOrUnknownSVal V =
624 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000625 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000626 continue;
627 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000628
629 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
630 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000631 // For fields and elements whose super region has also been invalidated,
632 // only remove the old binding. The super region will get set with a
633 // default value from which we can lazily derive a new symbolic value.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000634 B = Remove(B, R);
Ted Kremeneka5971b32009-09-29 03:34:03 +0000635 continue;
636 }
Ted Kremenek87806792009-09-27 20:45:21 +0000637
Ted Kremenek389c44c2009-09-29 03:12:50 +0000638 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000639 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
640 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000641 B = Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000642 }
643
Ted Kremenek87806792009-09-27 20:45:21 +0000644 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000645 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000646}
647
648//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000649// getLValueXXX methods.
650//===----------------------------------------------------------------------===//
651
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000652/// getLValueString - Returns an SVal representing the lvalue of a
653/// StringLiteral. Within RegionStore a StringLiteral has an
654/// associated StringRegion, and the lvalue of a StringLiteral is the
655/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000656SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000657 return loc::MemRegionVal(MRMgr.getStringRegion(S));
658}
659
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000660/// getLValueVar - Returns an SVal that represents the lvalue of a
661/// variable. Within RegionStore a variable has an associated
662/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000663SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000664 const LocationContext *LC) {
665 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000666}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000667
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000668SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
669 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000670}
671
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000672SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
673 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000674}
675
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000676SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000677 if (Base.isUnknownOrUndef())
678 return Base;
679
680 Loc BaseL = cast<Loc>(Base);
681 const MemRegion* BaseR = 0;
682
683 switch (BaseL.getSubKind()) {
684 case loc::MemRegionKind:
685 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
686 break;
687
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000688 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000689 // These are anormal cases. Flag an undefined value.
690 return UndefinedVal();
691
692 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000693 // While these seem funny, this can happen through casts.
694 // FIXME: What we should return is the field offset. For example,
695 // add the field offset to the integer value. That way funny things
696 // like this work properly: &(((struct foo *) 0xa)->f)
697 return Base;
698
699 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000700 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000701 return Base;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000704 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
705 // of FieldDecl.
706 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
707 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000708
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000709 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000710}
711
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000712SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
713 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000714
Ted Kremenekde7ec632009-03-09 22:44:49 +0000715 // If the base is an unknown or undefined value, just return it back.
716 // FIXME: For absolute pointer addresses, we just return that value back as
717 // well, although in reality we should return the offset added to that
718 // value.
719 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000720 return Base;
721
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000722 // Only handle integer offsets... for now.
723 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000724 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000725
Zhongxing Xuce760782009-05-09 13:20:07 +0000726 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000727
728 // Pointer of any type can be cast and used as array base.
729 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek46537392009-07-16 01:33:37 +0000731 // Convert the offset to the appropriate size and signedness.
732 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000734 if (!ElemR) {
735 //
736 // If the base region is not an ElementRegion, create one.
737 // This can happen in the following example:
738 //
739 // char *p = __builtin_alloc(10);
740 // p[1] = 8;
741 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000742 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000743 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000744 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000745 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000748 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000750 if (!isa<nonloc::ConcreteInt>(BaseIdx))
751 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000753 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
754 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
755 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek46537392009-07-16 01:33:37 +0000757 // Compute the new index.
758 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek46537392009-07-16 01:33:37 +0000760 // Construct the new ElementRegion.
761 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000762 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000763 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000764}
765
Ted Kremenek9af46f52009-06-16 22:36:44 +0000766//===----------------------------------------------------------------------===//
767// Extents for regions.
768//===----------------------------------------------------------------------===//
769
Zhongxing Xue884ff82009-11-12 02:48:32 +0000770DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000771 const MemRegion *R,
772 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000774 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000775 case MemRegion::CXXThisRegionKind:
776 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000777 case MemRegion::GenericMemSpaceRegionKind:
778 case MemRegion::StackLocalsSpaceRegionKind:
779 case MemRegion::StackArgumentsSpaceRegionKind:
780 case MemRegion::HeapSpaceRegionKind:
781 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000782 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000783 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000784 return UnknownVal();
785
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000786 case MemRegion::FunctionTextRegionKind:
787 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000788 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000789 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000790 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000791
792 // Not yet handled.
793 case MemRegion::AllocaRegionKind:
794 case MemRegion::CompoundLiteralRegionKind:
795 case MemRegion::ElementRegionKind:
796 case MemRegion::FieldRegionKind:
797 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000798 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000799 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000801 case MemRegion::SymbolicRegionKind: {
802 const SVal *Size = state->get<RegionExtents>(R);
803 if (!Size)
804 return UnknownVal();
805 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
806 if (!CI)
807 return UnknownVal();
808
809 CharUnits RegionSize =
810 CharUnits::fromQuantity(CI->getValue().getSExtValue());
811 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
812 assert(RegionSize % EleSize == 0);
813
814 return ValMgr.makeIntVal(RegionSize / EleSize, false);
815 }
816
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000817 case MemRegion::StringRegionKind: {
818 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000819 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000820 // operations with signed indices.
821 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000824 case MemRegion::VarRegionKind: {
825 const VarRegion* VR = cast<VarRegion>(R);
826 // Get the type of the variable.
827 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000829 // FIXME: Handle variable-length arrays.
830 if (isa<VariableArrayType>(T))
831 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000833 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
834 // return the size as signed integer.
835 return ValMgr.makeIntVal(CAT->getSize(), false);
836 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000837
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000838 // Clients can use ordinary variables as if they were arrays. These
839 // essentially are arrays of size 1.
840 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000841 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000844 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000845 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000846}
847
Ted Kremenek67f28532009-06-17 22:02:04 +0000848const GRState *RegionStoreManager::setExtent(const GRState *state,
849 const MemRegion *region,
850 SVal extent) {
851 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000852}
853
854//===----------------------------------------------------------------------===//
855// Location and region casting.
856//===----------------------------------------------------------------------===//
857
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000858/// ArrayToPointer - Emulates the "decay" of an array to a pointer
859/// type. 'Array' represents the lvalue of the array being decayed
860/// to a pointer, and the returned SVal represents the decayed
861/// version of that lvalue (i.e., a pointer to the first element of
862/// the array). This is called by GRExprEngine when evaluating casts
863/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000864SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000865 if (!isa<loc::MemRegionVal>(Array))
866 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenekabb042f2008-12-13 19:24:37 +0000868 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
869 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000871 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000872 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000874 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000875 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000876 ArrayType *AT = cast<ArrayType>(T);
877 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek75185b52009-07-16 00:00:11 +0000879 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000880 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
881 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000882}
883
Ted Kremenek9af46f52009-06-16 22:36:44 +0000884//===----------------------------------------------------------------------===//
885// Pointer arithmetic.
886//===----------------------------------------------------------------------===//
887
Mike Stump1eb44332009-09-09 15:08:12 +0000888SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000889 BinaryOperator::Opcode Op, Loc L, NonLoc R,
890 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000891 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000892 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000893 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000894
Zhongxing Xua1718c72009-04-03 07:33:13 +0000895 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000896 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000897
Ted Kremenek3bccf082009-07-11 00:58:27 +0000898 switch (MR->getKind()) {
899 case MemRegion::SymbolicRegionKind: {
900 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000901 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000902 QualType T = Sym->getType(getContext());
903 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000905 if (const PointerType *PT = T->getAs<PointerType>())
906 EleTy = PT->getPointeeType();
907 else
John McCall183700f2009-09-21 23:43:11 +0000908 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremenek3bccf082009-07-11 00:58:27 +0000910 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
911 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000912 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000913 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000914 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000915 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000916 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000917 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000918 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
919 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000920 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000921 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000922
Ted Kremenek3bccf082009-07-11 00:58:27 +0000923 case MemRegion::ElementRegionKind: {
924 ER = cast<ElementRegion>(MR);
925 break;
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek3bccf082009-07-11 00:58:27 +0000928 // Not yet handled.
929 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000930 case MemRegion::StringRegionKind: {
931
932 }
933 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000934 case MemRegion::CompoundLiteralRegionKind:
935 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000936 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000937 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000938 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000940 case MemRegion::FunctionTextRegionKind:
941 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000942 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000943 // Technically this can happen if people do funny things with casts.
944 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Ted Kremenekde0d2632010-01-05 02:18:06 +0000946 case MemRegion::CXXThisRegionKind:
947 assert(0 &&
948 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000949 case MemRegion::GenericMemSpaceRegionKind:
950 case MemRegion::StackLocalsSpaceRegionKind:
951 case MemRegion::StackArgumentsSpaceRegionKind:
952 case MemRegion::HeapSpaceRegionKind:
953 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000954 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000955 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
956 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000957 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000958
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000959 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000960 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000961
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000962 // For now, only support:
963 // (a) concrete integer indices that can easily be resolved
964 // (b) 0 + symbolic index
965 if (Base) {
966 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
967 // FIXME: Should use SValuator here.
968 SVal NewIdx =
969 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000970 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000971 const MemRegion* NewER =
972 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
973 ER->getSuperRegion(), getContext());
974 return ValMgr.makeLoc(NewER);
975 }
976 if (0 == Base->getValue()) {
977 const MemRegion* NewER =
978 MRMgr.getElementRegion(ER->getElementType(), R,
979 ER->getSuperRegion(), getContext());
980 return ValMgr.makeLoc(NewER);
981 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek5dc27462009-03-03 02:51:43 +0000984 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000985}
986
Ted Kremenek9af46f52009-06-16 22:36:44 +0000987//===----------------------------------------------------------------------===//
988// Loading values from regions.
989//===----------------------------------------------------------------------===//
990
Zhongxing Xu13d50172009-10-11 08:08:02 +0000991Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
992 const MemRegion *R) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000993 if (const BindingVal *BV = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +0000994 return Optional<SVal>::create(BV->getDirectValue());
995
996 return Optional<SVal>();
997}
998
999Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001000 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001002 if (R->isBoundable())
1003 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1004 if (TR->getValueType(getContext())->isUnionType())
1005 return UnknownVal();
1006
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001007 if (const BindingVal *V = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001008 return Optional<SVal>::create(V->getDefaultValue());
1009
1010 return Optional<SVal>();
1011}
1012
1013Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1014 const MemRegion *R) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001015 if (const BindingVal *BV = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001016 return Optional<SVal>::create(BV->getValue());
1017
1018 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001019}
1020
Ted Kremeneka6275a52009-07-15 02:31:43 +00001021static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1022 RTy = Ctx.getCanonicalType(RTy);
1023 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremeneka6275a52009-07-15 02:31:43 +00001025 if (RTy == UsedTy)
1026 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
1028
Ted Kremenek25c54572009-07-20 22:58:02 +00001029 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +00001030 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +00001031 // represents a reinterpretation.
1032 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001033 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001034 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001035
1036 return PUsedTy && PRTy &&
1037 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001038 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001039 }
1040
1041 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001042}
1043
Ted Kremenek0954cde2009-09-24 04:11:44 +00001044const ElementRegion *
1045RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1046 ASTContext &Ctx = getContext();
1047 SVal idx = ValMgr.makeZeroArrayIndex();
1048 assert(!T.isNull());
1049 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1050}
1051
1052
1053
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001054SValuator::CastResult
1055RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001056
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001057 assert(!isa<UnknownVal>(L) && "location unknown");
1058 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +00001059
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001060 // FIXME: Is this even possible? Shouldn't this be treated as a null
1061 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001062 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001063 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek28172a22009-12-15 23:23:27 +00001064
Ted Kremenek67f28532009-06-17 22:02:04 +00001065 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001066
Zhongxing Xu91844122009-05-20 09:18:48 +00001067 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001068 // Example:
1069 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001070 // char* p = alloca();
1071 // read(p);
1072 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001073 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001074 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Ted Kremenek0954cde2009-09-24 04:11:44 +00001076 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1077 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Ted Kremenek968f0a62009-08-03 21:41:46 +00001079 if (isa<CodeTextRegion>(MR))
1080 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001082 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1083 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001084 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001085 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001086
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001087 // FIXME: We should eventually handle funny addressing. e.g.:
1088 //
1089 // int x = ...;
1090 // int *p = &x;
1091 // char *q = (char*) p;
1092 // char c = *q; // returns the first byte of 'x'.
1093 //
1094 // Such funny addressing will occur due to layering of regions.
1095
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001096#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001097 ASTContext &Ctx = getContext();
1098 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001099 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1100 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001101 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001102 assert(Ctx.getCanonicalType(RTy) ==
1103 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001104 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001105#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001106
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001107 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001108 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001110 // FIXME: Handle unions.
1111 if (RTy->isUnionType())
1112 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001113
1114 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001115 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001116
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001117 // FIXME: handle Vector types.
1118 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001119 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001120
1121 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001122 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001123 CastRetrievedVal(RetrieveField(state, FR), FR,
1124 T, false));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001125
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001126 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1127 // FIXME: Here we actually perform an implicit conversion from the loaded
1128 // value to the element type. Eventually we want to compose these values
1129 // more intelligently. For example, an 'element' can encompass multiple
1130 // bound regions (e.g., several bound bytes), or could be a subset of
1131 // a larger value.
Zhongxing Xu652be342009-11-16 04:49:44 +00001132 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001133 CastRetrievedVal(RetrieveElement(state, ER),
1134 ER, T, false));
1135 }
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 Xu652be342009-11-16 04:49:44 +00001144 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001145 CastRetrievedVal(RetrieveObjCIvar(state, IVR),
1146 IVR, T, false));
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001149 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1150 // FIXME: Here we actually perform an implicit conversion from the loaded
1151 // value to the variable type. What we should model is stores to variables
1152 // that blow past the extent of the variable. If the address of the
1153 // variable is reinterpretted, it is possible we stored a different value
1154 // that could fit within the variable. Either we need to cast these when
1155 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu652be342009-11-16 04:49:44 +00001156 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001157 CastRetrievedVal(RetrieveVar(state, VR), VR, T,
1158 false));
1159 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001160
Ted Kremenek451ac092009-08-06 04:50:20 +00001161 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001162 const BindingVal *V = Lookup(B, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001163
1164 // Check if the region has a binding.
1165 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001166 if (SVal const *SV = V->getValue())
1167 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001168
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001169 // The location does not have a bound value. This means that it has
1170 // the value it had upon its creation and/or entry to the analyzed
1171 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001172 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001173 // All stack variables are considered to have undefined values
1174 // upon creation. All heap allocated blocks are considered to
1175 // have undefined values as well unless they are explicitly bound
1176 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001177 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001178 }
1179
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001180 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001181 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001182}
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001184std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001185RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001186 if (Optional<SVal> OV = getDirectBinding(B, R))
1187 if (const nonloc::LazyCompoundVal *V =
1188 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1189 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001191 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1192 const std::pair<const GRState *, const MemRegion *> &X =
1193 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001195 if (X.first)
1196 return std::make_pair(X.first,
1197 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001198 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001199 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1200 const std::pair<const GRState *, const MemRegion *> &X =
1201 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001203 if (X.first)
1204 return std::make_pair(X.first,
1205 MRMgr.getFieldRegionWithSuper(FR, X.second));
1206 }
1207
1208 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1209}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001210
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001211SVal RegionStoreManager::RetrieveElement(const GRState* state,
1212 const ElementRegion* R) {
1213 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001214 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001215 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001216 return *V;
1217
Ted Kremenek921109a2009-07-01 23:19:52 +00001218 const MemRegion* superR = R->getSuperRegion();
1219
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001220 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001221 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001222 // FIXME: Handle loads from strings where the literal is treated as
1223 // an integer, e.g., *((unsigned int*)"hello")
1224 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001225 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001226 if (T != Ctx.getCanonicalType(R->getElementType()))
1227 return UnknownVal();
1228
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001229 const StringLiteral *Str = StrR->getStringLiteral();
1230 SVal Idx = R->getIndex();
1231 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1232 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001233 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001234 if (i > byteLength) {
1235 // Buffer overflow checking in GRExprEngine should handle this case,
1236 // but we shouldn't rely on it to not overflow here if that checking
1237 // is disabled.
1238 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001239 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001240 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001241 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001242 }
1243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001245 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001246 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001247 if (SymbolRef parentSym = V->getAsSymbol())
1248 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001249
1250 if (V->isUnknownOrUndef())
1251 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001252
1253 // Handle LazyCompoundVals for the immediate super region. Other cases
1254 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001255 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001256 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001258 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1259 return RetrieveElement(LCV->getState(), R);
1260 }
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Ted Kremeneka6275a52009-07-15 02:31:43 +00001262 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001263 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001264 }
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001265
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001266 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001267}
1268
Mike Stump1eb44332009-09-09 15:08:12 +00001269SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001270 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001271
1272 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001273 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001274 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001275 return *V;
1276
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001277 QualType Ty = R->getValueType(getContext());
1278 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1279}
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001281SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1282 const TypedRegion *R,
1283 QualType Ty,
1284 const MemRegion *superR) {
1285
Mike Stump1eb44332009-09-09 15:08:12 +00001286 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001287 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001289 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001291 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001292 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001293 if (SymbolRef parentSym = D->getAsSymbol())
1294 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001296 if (D->isZeroConstant())
1297 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001299 if (D->isUnknown())
1300 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001302 assert(0 && "Unknown default value");
1303 }
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001305 // If our super region is a field or element itself, walk up the region
1306 // hierarchy to see if there is a default value installed in an ancestor.
1307 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1308 superR = cast<SubRegion>(superR)->getSuperRegion();
1309 continue;
1310 }
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001312 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001313 }
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001315 // Lazy binding?
1316 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001317 const MemRegion *lazyBindingRegion = NULL;
1318 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001320 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001321 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001323 if (isa<ElementRegion>(R))
1324 return RetrieveElement(lazyBindingState,
1325 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001327 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001328 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001329 }
1330
Ted Kremenekde0d2632010-01-05 02:18:06 +00001331 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001332 if (isa<ElementRegion>(R)) {
1333 // Currently we don't reason specially about Clang-style vectors. Check
1334 // if superR is a vector and if so return Unknown.
1335 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1336 if (typedSuperR->getValueType(getContext())->isVectorType())
1337 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001338 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001339 }
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001341 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001344 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001345 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001346}
Mike Stump1eb44332009-09-09 15:08:12 +00001347
1348SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001349 const ObjCIvarRegion* R) {
1350
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001351 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001352 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001353
Zhongxing Xu13d50172009-10-11 08:08:02 +00001354 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001355 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001357 const MemRegion *superR = R->getSuperRegion();
1358
Ted Kremenekab22ee92009-10-20 01:20:57 +00001359 // Check if the super region has a default binding.
1360 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001361 if (SymbolRef parentSym = V->getAsSymbol())
1362 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001364 // Other cases: give up.
1365 return UnknownVal();
1366 }
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Ted Kremenek25c54572009-07-20 22:58:02 +00001368 return RetrieveLazySymbol(state, R);
1369}
1370
Ted Kremenek9031dd72009-07-21 00:12:07 +00001371SVal RegionStoreManager::RetrieveVar(const GRState *state,
1372 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Ted Kremenek9031dd72009-07-21 00:12:07 +00001374 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001375 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Zhongxing Xu13d50172009-10-11 08:08:02 +00001377 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001378 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Ted Kremenek9031dd72009-07-21 00:12:07 +00001380 // Lazily derive a value for the VarRegion.
1381 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001383 if (R->hasGlobalsOrParametersStorage() ||
1384 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek28172a22009-12-15 23:23:27 +00001385 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenek9031dd72009-07-21 00:12:07 +00001387 return UndefinedVal();
1388}
1389
Mike Stump1eb44332009-09-09 15:08:12 +00001390SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001391 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenek25c54572009-07-20 22:58:02 +00001393 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001394
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001395 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001396 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001397}
1398
Mike Stump1eb44332009-09-09 15:08:12 +00001399SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1400 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001401 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001402 assert(T->isStructureType());
1403
Zhongxing Xub7507d12009-06-11 07:27:30 +00001404 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001405 RecordDecl* RD = RT->getDecl();
1406 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001407 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001408#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001409 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1410
Ted Kremenek67f28532009-06-17 22:02:04 +00001411 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1412 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001413 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001414
Douglas Gregore267ff32008-12-11 20:41:00 +00001415 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1416 FieldEnd = Fields.rend();
1417 Field != FieldEnd; ++Field) {
1418 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001419 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001420 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001421 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1422 }
1423
Zhongxing Xud91ee272009-06-23 09:02:15 +00001424 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001425#else
1426 return ValMgr.makeLazyCompoundVal(state, R);
1427#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001428}
1429
Ted Kremenek67f28532009-06-17 22:02:04 +00001430SVal RegionStoreManager::RetrieveArray(const GRState *state,
1431 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001432#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001433 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001434 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1435
1436 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001437 uint64_t size = CAT->getSize().getZExtValue();
1438 for (uint64_t i = 0; i < size; ++i) {
1439 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001440 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001441 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001442 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001443 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001444 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1445 }
1446
Zhongxing Xud91ee272009-06-23 09:02:15 +00001447 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001448#else
1449 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1450 return ValMgr.makeLazyCompoundVal(state, R);
1451#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001452}
1453
Ted Kremenek9af46f52009-06-16 22:36:44 +00001454//===----------------------------------------------------------------------===//
1455// Binding values to regions.
1456//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001457
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001458Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001459 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001460 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
1461 return Remove(store, BindingKey::Make(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Ted Kremenek0964a062009-01-21 06:57:53 +00001463 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001464}
1465
Ted Kremenek67f28532009-06-17 22:02:04 +00001466const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001467 if (isa<loc::ConcreteInt>(L))
1468 return state;
1469
Ted Kremenek9af46f52009-06-16 22:36:44 +00001470 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001471 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Ted Kremenek9af46f52009-06-16 22:36:44 +00001473 // Check if the region is a struct region.
1474 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1475 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001476 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001478 // Special case: the current region represents a cast and it and the super
1479 // region both have pointer types or intptr_t types. If so, perform the
1480 // bind to the super region.
1481 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001482 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001483 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1484 if (ER->getIndex().isZeroConstant()) {
1485 if (const TypedRegion *superR =
1486 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1487 ASTContext &Ctx = getContext();
1488 QualType superTy = superR->getValueType(Ctx);
1489 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001490
1491 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001492 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001493 SValuator::CastResult cr =
1494 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001495 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1496 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001497 // For now, just invalidate the fields of the struct/union/class.
1498 // FIXME: Precisely handle the fields of the record.
1499 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001500 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001501 }
1502 }
1503 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001504 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1505 // Binding directly to a symbolic region should be treated as binding
1506 // to element 0.
1507 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek852274d2009-12-16 03:18:58 +00001508
1509 // FIXME: Is this the right way to handle symbols that are references?
1510 if (const PointerType *PT = T->getAs<PointerType>())
1511 T = PT->getPointeeType();
1512 else
1513 T = T->getAs<ReferenceType>()->getPointeeType();
1514
Ted Kremenek0954cde2009-09-24 04:11:44 +00001515 R = GetElementZeroRegion(SR, T);
1516 }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001518 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001519 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001520 return state->makeWithStore(Add(B, R,
1521 BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001522}
1523
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001524const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001525 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001526 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001527
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001528 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001529
Ted Kremenek0964a062009-01-21 06:57:53 +00001530 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001531 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001532 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001533 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001534
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001535 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001536}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001537
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001538// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001539const GRState *
1540RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001541 const CompoundLiteralExpr *CL,
1542 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001543 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001544 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1545 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001546}
1547
Ted Kremenek027e2662009-11-19 20:20:24 +00001548const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1549 const MemRegion *R,
1550 QualType T) {
1551 Store store = state->getStore();
1552 RegionBindings B = GetRegionBindings(store);
1553 SVal V;
1554
1555 if (Loc::IsLocType(T))
1556 V = ValMgr.makeNull();
1557 else if (T->isIntegerType())
1558 V = ValMgr.makeZeroVal(T);
1559 else if (T->isStructureType() || T->isArrayType()) {
1560 // Set the default value to a zero constant when it is a structure
1561 // or array. The type doesn't really matter.
1562 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1563 }
1564 else {
1565 return state;
1566 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001567
1568 return state->makeWithStore(Add(B, R,
1569 BindingVal(V, BindingVal::Default)).getRoot());
Ted Kremenek027e2662009-11-19 20:20:24 +00001570}
1571
Ted Kremenek67f28532009-06-17 22:02:04 +00001572const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001573 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001574 SVal Init) {
Ted Kremenekfee90812010-01-26 23:51:00 +00001575
1576 ASTContext &Ctx = getContext();
1577 const ArrayType *AT =
1578 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1579 QualType ElementTy = AT->getElementType();
1580 Optional<uint64_t> Size;
1581
1582 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1583 Size = CAT->getSize().getZExtValue();
1584
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001585 // Check if the init expr is a StringLiteral.
1586 if (isa<loc::MemRegionVal>(Init)) {
1587 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1588 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1589 const char* str = S->getStrData();
1590 unsigned len = S->getByteLength();
1591 unsigned j = 0;
1592
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001593 // Copy bytes from the string literal into the target array. Trailing bytes
1594 // in the array that are not covered by the string literal are initialized
1595 // to zero.
Ted Kremenekfee90812010-01-26 23:51:00 +00001596
1597 // We assume that string constants are bound to
1598 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001599 uint64_t size = Size.getValue();
Ted Kremenekfee90812010-01-26 23:51:00 +00001600
Ted Kremenek46537392009-07-16 01:33:37 +00001601 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001602 if (j >= len)
1603 break;
1604
Ted Kremenek46537392009-07-16 01:33:37 +00001605 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001606 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1607 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001608
Zhongxing Xud91ee272009-06-23 09:02:15 +00001609 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001610 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001611 }
1612
Ted Kremenek67f28532009-06-17 22:02:04 +00001613 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001614 }
1615
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001616 // Handle lazy compound values.
1617 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1618 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001619
1620 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001621
1622 if (Init.isUnknown())
1623 return setImplicitDefaultValue(state, R, ElementTy);
1624
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001625 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001626 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001627 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenekfee90812010-01-26 23:51:00 +00001629 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001630 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001631 if (VI == VE)
1632 break;
1633
Ted Kremenek46537392009-07-16 01:33:37 +00001634 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001635 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001636
Ted Kremenekfee90812010-01-26 23:51:00 +00001637 if (ElementTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001638 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001639 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001640 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001641 }
1642
Ted Kremenek027e2662009-11-19 20:20:24 +00001643 // If the init list is shorter than the array length, set the
1644 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001645 if (Size.hasValue() && i < Size.getValue())
Ted Kremenek027e2662009-11-19 20:20:24 +00001646 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001647
Ted Kremenek67f28532009-06-17 22:02:04 +00001648 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001649}
1650
Ted Kremenek67f28532009-06-17 22:02:04 +00001651const GRState *
1652RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1653 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Ted Kremenek67f28532009-06-17 22:02:04 +00001655 if (!Features.supportsFields())
1656 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001658 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001659 assert(T->isStructureType());
1660
Ted Kremenek6217b802009-07-29 21:53:49 +00001661 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001662 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001663
1664 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001665 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001666
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001667 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001668 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001669 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Ted Kremenek67f28532009-06-17 22:02:04 +00001671 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1672 // Ignore them and kill the field values.
1673 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001674 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001675
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001676 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001677 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001678
1679 RecordDecl::field_iterator FI, FE;
1680
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001681 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001682
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001683 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001684 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001685
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001686 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001687 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001688
Ted Kremenekcf549592009-09-22 21:19:14 +00001689 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001690 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001691 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001692 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001693 else
1694 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001695 }
1696
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001697 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001698 if (FI != FE) {
1699 Store store = state->getStore();
1700 RegionBindings B = GetRegionBindings(store);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001701 B = Add(B, R, BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001702 state = state->makeWithStore(B.getRoot());
1703 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001704
Ted Kremenek67f28532009-06-17 22:02:04 +00001705 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001706}
1707
Zhongxing Xu13d50172009-10-11 08:08:02 +00001708Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1709 RegionBindings B = GetRegionBindings(store);
1710 llvm::OwningPtr<RegionStoreSubRegionMap>
1711 SubRegions(getRegionStoreSubRegionMap(store));
1712 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001713
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001714 // Set the default value of the struct region to "unknown".
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001715 B = Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001716
Zhongxing Xu13d50172009-10-11 08:08:02 +00001717 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001718}
1719
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001720const GRState*
1721RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1722 const GRState *state,
1723 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001724
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001725 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001726 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001727
Mike Stump1eb44332009-09-09 15:08:12 +00001728 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001729 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001730
Mike Stump1eb44332009-09-09 15:08:12 +00001731 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001732 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001734 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1735 // results in a zero-copy algorithm.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001736 return state->makeWithStore(Add(B, R,
1737 BindingVal(V, BindingVal::Direct)).getRoot());
1738}
1739
1740//===----------------------------------------------------------------------===//
1741// "Raw" retrievals and bindings.
1742//===----------------------------------------------------------------------===//
1743
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001744BindingKey BindingKey::Make(const MemRegion *R) {
1745 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1746 const RegionRawOffset &O = ER->getAsRawOffset();
1747
1748 if (O.getRegion())
1749 return BindingKey(O.getRegion(), O.getByteOffset());
1750
1751 // FIXME: There are some ElementRegions for which we cannot compute
1752 // raw offsets yet, including regions with symbolic offsets.
1753 }
1754
1755 return BindingKey(R, 0);
1756}
1757
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001758RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K,
1759 BindingVal V) {
1760 return RBFactory.Add(B, K, V);
1761}
1762
1763RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
1764 BindingVal V) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001765 return Add(B, BindingKey::Make(R), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001766}
1767
1768const BindingVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
1769 return B.lookup(K);
1770}
1771
1772const BindingVal *RegionStoreManager::Lookup(RegionBindings B,
1773 const MemRegion *R) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001774 return Lookup(B, BindingKey::Make(R));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001775}
1776
1777RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1778 return RBFactory.Remove(B, K);
1779}
1780
1781RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R){
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001782 return Remove(B, BindingKey::Make(R));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001783}
1784
1785Store RegionStoreManager::Remove(Store store, BindingKey K) {
1786 RegionBindings B = GetRegionBindings(store);
1787 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001788}
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Ted Kremenek9af46f52009-06-16 22:36:44 +00001790//===----------------------------------------------------------------------===//
1791// State pruning.
1792//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001793
Mike Stump1eb44332009-09-09 15:08:12 +00001794void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001795 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001796 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001797{
Ted Kremenek781115c2009-10-17 17:45:11 +00001798 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1799
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001800 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001801 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Ted Kremenek9af46f52009-06-16 22:36:44 +00001803 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001804 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001805 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001806
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001807 // Do a pass over the regions in the store. For VarRegions we check if
1808 // the variable is still live and if so add it to the list of live roots.
1809 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001810 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001811
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001812 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001813 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001814 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001815 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001816 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001817
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001818 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001819 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001820 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001821 llvm::SmallVector<RBDNode, 10> Postponed;
1822
Zhongxing Xu6800b332009-10-18 04:15:47 +00001823 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001824
Ted Kremenek9af46f52009-06-16 22:36:44 +00001825 while (!IntermediateRoots.empty()) {
1826 const MemRegion* R = IntermediateRoots.back();
1827 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001828
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001829 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001830 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001831 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001832
Ted Kremenek9af46f52009-06-16 22:36:44 +00001833 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001834 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001835 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001836 continue;
1837 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001838
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001839 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001840 llvm::SmallVectorImpl<RBDNode> &Q =
1841 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1842
1843 Q.push_back(std::make_pair(&state, SR));
1844
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001845 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001846 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001847
1848 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001849 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001850 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001851 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001852 }
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001854 // Enqueue the RegionRoots onto WorkList.
1855 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1856 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001857 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001858 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001859 RegionRoots.clear();
1860
Zhongxing Xu6800b332009-10-18 04:15:47 +00001861 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001862
Ted Kremenek01756192009-10-29 05:14:17 +00001863tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001864 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001865 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001866 WorkList.pop_back();
1867
1868 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001869 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001870 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001871 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001873 const MemRegion *R = N.second;
1874 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001875
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001876 // Enqueue subregions.
1877 RegionStoreSubRegionMap *M;
1878
1879 if (&state == state_N)
1880 M = SubRegions.get();
1881 else {
1882 RegionStoreSubRegionMap *& SM = SC[state_N];
1883 if (!SM)
1884 SM = getRegionStoreSubRegionMap(state_N->getStore());
1885 M = SM;
1886 }
1887
1888 RegionStoreSubRegionMap::iterator I, E;
1889 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1890 WorkList.push_back(std::make_pair(state_N, *I));
1891
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001892 // Enqueue the super region.
1893 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1894 const MemRegion *superR = SR->getSuperRegion();
1895 if (!isa<MemSpaceRegion>(superR)) {
1896 // If 'R' is a field or an element, we want to keep the bindings
1897 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001898 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001899 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1900 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001901 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001902 }
1903 }
1904
1905 // Mark the symbol for any live SymbolicRegion as "live". This means we
1906 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001907 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001908 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001909
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001910 // For BlockDataRegions, enqueue the VarRegions for variables marked
1911 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001912 // via BlockDeclRefExprs.
1913 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1914 for (BlockDataRegion::referenced_vars_iterator
1915 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001916 RI != RE; ++RI) {
1917 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1918 WorkList.push_back(std::make_pair(state_N, *RI));
1919 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001920 // No possible data bindings on a BlockDataRegion. Continue to the
1921 // next region in the worklist.
1922 continue;
1923 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001924
1925 Store store_N = state_N->getStore();
1926 RegionBindings B_N = GetRegionBindings(store_N);
1927
1928 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001929 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001930
Zhongxing Xu13d50172009-10-11 08:08:02 +00001931 if (V) {
1932 // Check for lazy bindings.
1933 if (const nonloc::LazyCompoundVal *LCV =
1934 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001935
Zhongxing Xu13d50172009-10-11 08:08:02 +00001936 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001937 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001938 }
1939 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001940 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001941 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001942 SI!=SE;++SI)
1943 SymReaper.markLive(*SI);
1944
Zhongxing Xu13d50172009-10-11 08:08:02 +00001945 // If V is a region, then add it to the worklist.
1946 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001947 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001948 }
1949 }
1950 }
1951
Ted Kremenek01756192009-10-29 05:14:17 +00001952 // See if any postponed SymbolicRegions are actually live now, after
1953 // having done a scan.
1954 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1955 E = Postponed.end() ; I != E ; ++I) {
1956 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1957 if (SymReaper.isLive(SR->getSymbol())) {
1958 WorkList.push_back(*I);
1959 I->second = NULL;
1960 }
1961 }
1962 }
1963
1964 if (!WorkList.empty())
1965 goto tryAgain;
1966
Ted Kremenek9af46f52009-06-16 22:36:44 +00001967 // We have now scanned the store, marking reachable regions and symbols
1968 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001969 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001970 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001971 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001972 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001973 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001974 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Ted Kremenek9af46f52009-06-16 22:36:44 +00001976 // Remove this dead region from the store.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001977 store = Remove(store, I.getKey());
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Ted Kremenek9af46f52009-06-16 22:36:44 +00001979 // Mark all non-live symbols that this region references as dead.
1980 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1981 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Zhongxing Xu13d50172009-10-11 08:08:02 +00001983 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001984 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1985 for (; SI != SE; ++SI)
1986 SymReaper.maybeDead(*SI);
1987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001989 // Write the store back.
1990 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001991}
1992
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001993GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1994 StackFrameContext const *frame) {
1995 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1996 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1997
1998 FunctionDecl::param_const_iterator PI = FD->param_begin();
1999
2000 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
2001
2002 // Copy the arg expression value to the arg variables.
2003 for (; AI != AE; ++AI, ++PI) {
2004 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00002005 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00002006 }
2007
2008 return state;
2009}
2010
Ted Kremenek9af46f52009-06-16 22:36:44 +00002011//===----------------------------------------------------------------------===//
2012// Utility methods.
2013//===----------------------------------------------------------------------===//
2014
Ted Kremenek53ba0b62009-06-24 23:06:47 +00002015void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00002016 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00002017 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00002018 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Ted Kremenek451ac092009-08-06 04:50:20 +00002020 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002021 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00002022}