blob: 9cdb516068a4d5dbf12cbe4168aac80b23095c9d [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 Kremenekdf165012010-02-02 22:38:47 +0000181public:
182 typedef llvm::ImmutableSet<const MemRegion*> Set;
183 typedef llvm::DenseMap<const MemRegion*, Set> Map;
184private:
185 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000186 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000187public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000188 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000189 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000190
191 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000192 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000193 return true;
194 }
195
196 I->second = F.Add(I->second, SubRegion);
197 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000200 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek59e8f112009-03-03 01:35:36 +0000202 ~RegionStoreSubRegionMap() {}
Ted Kremenekdf165012010-02-02 22:38:47 +0000203
204 const Set *getSubRegions(const MemRegion *Parent) const {
205 Map::const_iterator I = M.find(Parent);
206 return I == M.end() ? NULL : &I->second;
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Ted Kremenek5dc27462009-03-03 02:51:43 +0000209 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000210 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000211
212 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000213 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenekdf165012010-02-02 22:38:47 +0000215 Set S = I->second;
216 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000217 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000218 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Ted Kremenek5dc27462009-03-03 02:51:43 +0000221 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000222 }
Mike Stump1eb44332009-09-09 15:08:12 +0000223};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000224
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000225class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000226 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000227 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000228
229 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
230 SMCache SC;
231
Zhongxing Xu17892752008-10-08 02:50:44 +0000232public:
Mike Stump1eb44332009-09-09 15:08:12 +0000233 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000234 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000235 Features(f),
Ted Kremenekdf165012010-02-02 22:38:47 +0000236 RBFactory(mgr.getAllocator(), 3) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000237
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000238 virtual ~RegionStoreManager() {
239 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
240 delete (*I).second;
241 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000242
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000243 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Zhongxing Xu13d50172009-10-11 08:08:02 +0000245 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Zhongxing Xu13d50172009-10-11 08:08:02 +0000247 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
248 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000249 /// getDefaultBinding - Returns an SVal* representing an optional default
250 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000251 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000252
253 /// setImplicitDefaultValue - Set the default binding for the provided
254 /// MemRegion to the value implicitly defined for compound literals when
255 /// the value is not specified.
256 const GRState *setImplicitDefaultValue(const GRState *state,
257 const MemRegion *R,
258 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000260 /// getLValueString - Returns an SVal representing the lvalue of a
261 /// StringLiteral. Within RegionStore a StringLiteral has an
262 /// associated StringRegion, and the lvalue of a StringLiteral is
263 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000264 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000265
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000266 /// getLValueCompoundLiteral - Returns an SVal representing the
267 /// lvalue of a compound literal. Within RegionStore a compound
268 /// literal has an associated region, and the lvalue of the
269 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000270 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000271
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000272 /// getLValueVar - Returns an SVal that represents the lvalue of a
273 /// variable. Within RegionStore a variable has an associated
274 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000275 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000277 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000278
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000279 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000281 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000282
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000283 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000284
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000285
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000286 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
287 /// type. 'Array' represents the lvalue of the array being decayed
288 /// to a pointer, and the returned SVal represents the decayed
289 /// version of that lvalue (i.e., a pointer to the first element of
290 /// the array). This is called by GRExprEngine when evaluating
291 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000292 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000293
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000294 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000295 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000296
Mike Stump1eb44332009-09-09 15:08:12 +0000297 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000298 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000299 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000300
Ted Kremenek67f28532009-06-17 22:02:04 +0000301 //===-------------------------------------------------------------------===//
302 // Binding values to regions.
303 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000304
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000305 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000306 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-12-03 03:27:11 +0000307 InvalidatedSymbols *IS) {
308 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
309 }
310
311 const GRState *InvalidateRegions(const GRState *state,
312 const MemRegion * const *Begin,
313 const MemRegion * const *End,
314 const Expr *E, unsigned Count,
315 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000317private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000318 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000319 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000321 RegionBindings Add(RegionBindings B, BindingKey K, BindingVal V);
322 RegionBindings Add(RegionBindings B, const MemRegion *R, BindingVal V);
323
324 const BindingVal *Lookup(RegionBindings B, BindingKey K);
325 const BindingVal *Lookup(RegionBindings B, const MemRegion *R);
326
327 RegionBindings Remove(RegionBindings B, BindingKey K);
328 RegionBindings Remove(RegionBindings B, const MemRegion *R);
329 Store Remove(Store store, BindingKey K);
330
Mike Stump1eb44332009-09-09 15:08:12 +0000331public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000332 const GRState *Bind(const GRState *state, Loc LV, SVal V);
333
334 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +0000335 const CompoundLiteralExpr* CL,
336 const LocationContext *LC,
337 SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000339 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
340 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000341
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000342 const GRState *BindDeclWithNoInit(const GRState *state,
343 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000344 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000345 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000346
Ted Kremenek67f28532009-06-17 22:02:04 +0000347 /// BindStruct - Bind a compound value to a structure.
348 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek67f28532009-06-17 22:02:04 +0000350 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
352 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000353 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000354
Ted Kremenek67f28532009-06-17 22:02:04 +0000355 Store Remove(Store store, Loc LV);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357
358 //===------------------------------------------------------------------===//
359 // Loading values from regions.
360 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek67f28532009-06-17 22:02:04 +0000362 /// The high level logic for this method is this:
363 /// Retrieve (L)
364 /// if L has binding
365 /// return L's binding
366 /// else if L is in killset
367 /// return unknown
368 /// else
369 /// if L is on stack or heap
370 /// return undefined
371 /// else
372 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000373 SValuator::CastResult Retrieve(const GRState *state, Loc L,
374 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000375
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000376 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000377
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000378 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000380 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek9031dd72009-07-21 00:12:07 +0000382 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek25c54572009-07-20 22:58:02 +0000384 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000386 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
387 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek67f28532009-06-17 22:02:04 +0000389 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000390 /// struct copy:
391 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000392 /// x = y;
393 /// y's value is retrieved by this method.
394 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek67f28532009-06-17 22:02:04 +0000396 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000398 /// Get the state and region whose binding this region R corresponds to.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000399 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000400 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000402 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
403 const GRState *state,
404 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000405
Ted Kremenek0954cde2009-09-24 04:11:44 +0000406 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
407 QualType T);
408
Ted Kremenek67f28532009-06-17 22:02:04 +0000409 //===------------------------------------------------------------------===//
410 // State pruning.
411 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek67f28532009-06-17 22:02:04 +0000413 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
414 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000415 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000416 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
417
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000418 const GRState *EnterStackFrame(const GRState *state,
419 const StackFrameContext *frame);
420
Ted Kremenek67f28532009-06-17 22:02:04 +0000421 //===------------------------------------------------------------------===//
422 // Region "extents".
423 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000425 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000426 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000427 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000428
429 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000430 // Utility methods.
431 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek451ac092009-08-06 04:50:20 +0000433 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000434 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000435 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000436
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000437 void print(Store store, llvm::raw_ostream& Out, const char* nl,
438 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000439
440 void iterBindings(Store store, BindingsHandler& f) {
441 // FIXME: Implement.
442 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000443
Ted Kremenek67f28532009-06-17 22:02:04 +0000444 // FIXME: Remove.
445 BasicValueFactory& getBasicVals() {
446 return StateMgr.getBasicVals();
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek67f28532009-06-17 22:02:04 +0000449 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000450 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000451};
452
453} // end anonymous namespace
454
Ted Kremenek9af46f52009-06-16 22:36:44 +0000455//===----------------------------------------------------------------------===//
456// RegionStore creation.
457//===----------------------------------------------------------------------===//
458
459StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
460 RegionStoreFeatures F = maximal_features_tag();
461 return new RegionStoreManager(StMgr, F);
462}
463
464StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
465 RegionStoreFeatures F = minimal_features_tag();
466 F.enableFields(true);
467 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000468}
469
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000470void
471RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000472 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000473 const MemRegion *superR = R->getSuperRegion();
474 if (add(superR, R))
475 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000476 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000477}
478
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000479RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000480RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
481 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000482 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000484 llvm::SmallVector<const SubRegion*, 10> WL;
485
Ted Kremenek451ac092009-08-06 04:50:20 +0000486 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000487 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000488 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Mike Stump1eb44332009-09-09 15:08:12 +0000490 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000491 // don't have direct bindings but are super regions of those that do.
492 while (!WL.empty()) {
493 const SubRegion *R = WL.back();
494 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000495 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000496 }
497
Ted Kremenek14453bf2009-03-03 19:02:42 +0000498 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000499}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000500
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000501SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000502 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000503}
504
Ted Kremenek9af46f52009-06-16 22:36:44 +0000505//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000506// Binding invalidation.
507//===----------------------------------------------------------------------===//
508
Ted Kremenekdf165012010-02-02 22:38:47 +0000509
Zhongxing Xu13d50172009-10-11 08:08:02 +0000510void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
511 const MemRegion *R,
512 RegionStoreSubRegionMap &M) {
Ted Kremenekdf165012010-02-02 22:38:47 +0000513
514 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
515 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
516 I != E; ++I)
517 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.
Ted Kremenekdf165012010-02-02 22:38:47 +0000555 if (const RegionStoreSubRegionMap::Set *S = SubRegions->getSubRegions(R))
556 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
557 I != E; ++I)
558 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000559
560 // Get the old binding. Is it a region? If so, add it to the worklist.
561 if (Optional<SVal> V = getDirectBinding(B, R)) {
562 if (const MemRegion *RV = V->getAsRegion())
563 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000564
565 // A symbol? Mark it touched by the invalidation.
566 if (IS) {
567 if (SymbolRef Sym = V->getAsSymbol())
568 IS->insert(Sym);
569 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000570 }
571
Ted Kremenek473e1672009-10-16 00:30:49 +0000572 // Symbolic region? Mark that symbol touched by the invalidation.
573 if (IS) {
574 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
575 IS->insert(SR->getSymbol());
576 }
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000577
578 // BlockDataRegion? If so, invalidate captured variables that are passed
579 // by reference.
580 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
581 for (BlockDataRegion::referenced_vars_iterator
582 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
583 I != E; ++I) {
584 const VarRegion *VR = *I;
585 if (VR->getDecl()->getAttr<BlocksAttr>())
586 WorkList.push_back(VR);
587 }
588 continue;
589 }
Ted Kremenek473e1672009-10-16 00:30:49 +0000590
591 // Handle the region itself.
Ted Kremenekc410d4d2009-12-16 23:53:37 +0000592 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000593 // Invalidate the region by setting its default value to
594 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000595 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000596 Count);
597 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000598 continue;
599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Ted Kremenek87806792009-09-27 20:45:21 +0000601 if (!R->isBoundable())
602 continue;
603
604 const TypedRegion *TR = cast<TypedRegion>(R);
605 QualType T = TR->getValueType(Ctx);
606
607 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000608 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
609
Zhongxing Xu13d50172009-10-11 08:08:02 +0000610 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000611 if (!RD)
612 continue;
613
Zhongxing Xu13d50172009-10-11 08:08:02 +0000614 // Invalidate the region by setting its default value to
615 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000616 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
617 Count);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000618 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000619 continue;
620 }
621
622 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
623 // Set the default value of the array to conjured symbol.
624 DefinedOrUnknownSVal V =
625 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000626 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000627 continue;
628 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000629
630 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
631 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000632 // For fields and elements whose super region has also been invalidated,
633 // only remove the old binding. The super region will get set with a
634 // default value from which we can lazily derive a new symbolic value.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000635 B = Remove(B, R);
Ted Kremeneka5971b32009-09-29 03:34:03 +0000636 continue;
637 }
Ted Kremenek87806792009-09-27 20:45:21 +0000638
Ted Kremenek389c44c2009-09-29 03:12:50 +0000639 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000640 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
641 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000642 B = Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000643 }
644
Ted Kremenek87806792009-09-27 20:45:21 +0000645 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000646 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000647}
648
649//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000650// getLValueXXX methods.
651//===----------------------------------------------------------------------===//
652
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000653/// getLValueString - Returns an SVal representing the lvalue of a
654/// StringLiteral. Within RegionStore a StringLiteral has an
655/// associated StringRegion, and the lvalue of a StringLiteral is the
656/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000657SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000658 return loc::MemRegionVal(MRMgr.getStringRegion(S));
659}
660
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000661/// getLValueVar - Returns an SVal that represents the lvalue of a
662/// variable. Within RegionStore a variable has an associated
663/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000664SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000665 const LocationContext *LC) {
666 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000667}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000668
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000669SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
670 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000671}
672
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000673SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
674 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000675}
676
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000677SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000678 if (Base.isUnknownOrUndef())
679 return Base;
680
681 Loc BaseL = cast<Loc>(Base);
682 const MemRegion* BaseR = 0;
683
684 switch (BaseL.getSubKind()) {
685 case loc::MemRegionKind:
686 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
687 break;
688
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000689 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000690 // These are anormal cases. Flag an undefined value.
691 return UndefinedVal();
692
693 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000694 // While these seem funny, this can happen through casts.
695 // FIXME: What we should return is the field offset. For example,
696 // add the field offset to the integer value. That way funny things
697 // like this work properly: &(((struct foo *) 0xa)->f)
698 return Base;
699
700 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000701 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000702 return Base;
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000705 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
706 // of FieldDecl.
707 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
708 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000709
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000710 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000711}
712
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000713SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
714 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000715
Ted Kremenekde7ec632009-03-09 22:44:49 +0000716 // If the base is an unknown or undefined value, just return it back.
717 // FIXME: For absolute pointer addresses, we just return that value back as
718 // well, although in reality we should return the offset added to that
719 // value.
720 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000721 return Base;
722
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000723 // Only handle integer offsets... for now.
724 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000725 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000726
Zhongxing Xuce760782009-05-09 13:20:07 +0000727 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000728
729 // Pointer of any type can be cast and used as array base.
730 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek46537392009-07-16 01:33:37 +0000732 // Convert the offset to the appropriate size and signedness.
733 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000735 if (!ElemR) {
736 //
737 // If the base region is not an ElementRegion, create one.
738 // This can happen in the following example:
739 //
740 // char *p = __builtin_alloc(10);
741 // p[1] = 8;
742 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000743 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000744 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000745 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000746 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000749 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000751 if (!isa<nonloc::ConcreteInt>(BaseIdx))
752 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000754 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
755 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
756 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek46537392009-07-16 01:33:37 +0000758 // Compute the new index.
759 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek46537392009-07-16 01:33:37 +0000761 // Construct the new ElementRegion.
762 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000763 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000764 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000765}
766
Ted Kremenek9af46f52009-06-16 22:36:44 +0000767//===----------------------------------------------------------------------===//
768// Extents for regions.
769//===----------------------------------------------------------------------===//
770
Zhongxing Xue884ff82009-11-12 02:48:32 +0000771DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000772 const MemRegion *R,
773 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000775 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000776 case MemRegion::CXXThisRegionKind:
777 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000778 case MemRegion::GenericMemSpaceRegionKind:
779 case MemRegion::StackLocalsSpaceRegionKind:
780 case MemRegion::StackArgumentsSpaceRegionKind:
781 case MemRegion::HeapSpaceRegionKind:
782 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000783 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000784 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000785 return UnknownVal();
786
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000787 case MemRegion::FunctionTextRegionKind:
788 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000789 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000790 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000791 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000792
793 // Not yet handled.
794 case MemRegion::AllocaRegionKind:
795 case MemRegion::CompoundLiteralRegionKind:
796 case MemRegion::ElementRegionKind:
797 case MemRegion::FieldRegionKind:
798 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000799 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000800 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000802 case MemRegion::SymbolicRegionKind: {
803 const SVal *Size = state->get<RegionExtents>(R);
804 if (!Size)
805 return UnknownVal();
806 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
807 if (!CI)
808 return UnknownVal();
809
810 CharUnits RegionSize =
811 CharUnits::fromQuantity(CI->getValue().getSExtValue());
812 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
813 assert(RegionSize % EleSize == 0);
814
815 return ValMgr.makeIntVal(RegionSize / EleSize, false);
816 }
817
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000818 case MemRegion::StringRegionKind: {
819 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000820 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000821 // operations with signed indices.
822 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000825 case MemRegion::VarRegionKind: {
826 const VarRegion* VR = cast<VarRegion>(R);
827 // Get the type of the variable.
828 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000830 // FIXME: Handle variable-length arrays.
831 if (isa<VariableArrayType>(T))
832 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000834 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
835 // return the size as signed integer.
836 return ValMgr.makeIntVal(CAT->getSize(), false);
837 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000838
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000839 // Clients can use ordinary variables as if they were arrays. These
840 // essentially are arrays of size 1.
841 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000842 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000845 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000846 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000847}
848
Ted Kremenek67f28532009-06-17 22:02:04 +0000849const GRState *RegionStoreManager::setExtent(const GRState *state,
850 const MemRegion *region,
851 SVal extent) {
852 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000853}
854
855//===----------------------------------------------------------------------===//
856// Location and region casting.
857//===----------------------------------------------------------------------===//
858
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000859/// ArrayToPointer - Emulates the "decay" of an array to a pointer
860/// type. 'Array' represents the lvalue of the array being decayed
861/// to a pointer, and the returned SVal represents the decayed
862/// version of that lvalue (i.e., a pointer to the first element of
863/// the array). This is called by GRExprEngine when evaluating casts
864/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000865SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000866 if (!isa<loc::MemRegionVal>(Array))
867 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Ted Kremenekabb042f2008-12-13 19:24:37 +0000869 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
870 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000872 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000873 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000875 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000876 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000877 ArrayType *AT = cast<ArrayType>(T);
878 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek75185b52009-07-16 00:00:11 +0000880 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000881 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
882 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000883}
884
Ted Kremenek9af46f52009-06-16 22:36:44 +0000885//===----------------------------------------------------------------------===//
886// Pointer arithmetic.
887//===----------------------------------------------------------------------===//
888
Mike Stump1eb44332009-09-09 15:08:12 +0000889SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000890 BinaryOperator::Opcode Op, Loc L, NonLoc R,
891 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000892 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000893 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000894 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000895
Zhongxing Xua1718c72009-04-03 07:33:13 +0000896 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000897 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000898
Ted Kremenek3bccf082009-07-11 00:58:27 +0000899 switch (MR->getKind()) {
900 case MemRegion::SymbolicRegionKind: {
901 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000902 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000903 QualType T = Sym->getType(getContext());
904 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000906 if (const PointerType *PT = T->getAs<PointerType>())
907 EleTy = PT->getPointeeType();
908 else
John McCall183700f2009-09-21 23:43:11 +0000909 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek3bccf082009-07-11 00:58:27 +0000911 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
912 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000913 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000914 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000915 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000916 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000917 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000918 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000919 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
920 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000921 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000922 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000923
Ted Kremenek3bccf082009-07-11 00:58:27 +0000924 case MemRegion::ElementRegionKind: {
925 ER = cast<ElementRegion>(MR);
926 break;
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Ted Kremenek3bccf082009-07-11 00:58:27 +0000929 // Not yet handled.
930 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000931 case MemRegion::StringRegionKind: {
932
933 }
934 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000935 case MemRegion::CompoundLiteralRegionKind:
936 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000937 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000938 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000939 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000941 case MemRegion::FunctionTextRegionKind:
942 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000943 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000944 // Technically this can happen if people do funny things with casts.
945 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenekde0d2632010-01-05 02:18:06 +0000947 case MemRegion::CXXThisRegionKind:
948 assert(0 &&
949 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000950 case MemRegion::GenericMemSpaceRegionKind:
951 case MemRegion::StackLocalsSpaceRegionKind:
952 case MemRegion::StackArgumentsSpaceRegionKind:
953 case MemRegion::HeapSpaceRegionKind:
954 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000955 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000956 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
957 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000958 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000959
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000960 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000961 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000962
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000963 // For now, only support:
964 // (a) concrete integer indices that can easily be resolved
965 // (b) 0 + symbolic index
966 if (Base) {
967 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
968 // FIXME: Should use SValuator here.
969 SVal NewIdx =
970 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000971 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000972 const MemRegion* NewER =
973 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
974 ER->getSuperRegion(), getContext());
975 return ValMgr.makeLoc(NewER);
976 }
977 if (0 == Base->getValue()) {
978 const MemRegion* NewER =
979 MRMgr.getElementRegion(ER->getElementType(), R,
980 ER->getSuperRegion(), getContext());
981 return ValMgr.makeLoc(NewER);
982 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenek5dc27462009-03-03 02:51:43 +0000985 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000986}
987
Ted Kremenek9af46f52009-06-16 22:36:44 +0000988//===----------------------------------------------------------------------===//
989// Loading values from regions.
990//===----------------------------------------------------------------------===//
991
Zhongxing Xu13d50172009-10-11 08:08:02 +0000992Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
993 const MemRegion *R) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000994 if (const BindingVal *BV = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +0000995 return Optional<SVal>::create(BV->getDirectValue());
996
997 return Optional<SVal>();
998}
999
1000Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001001 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001003 if (R->isBoundable())
1004 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1005 if (TR->getValueType(getContext())->isUnionType())
1006 return UnknownVal();
1007
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001008 if (const BindingVal *V = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001009 return Optional<SVal>::create(V->getDefaultValue());
1010
1011 return Optional<SVal>();
1012}
1013
1014Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1015 const MemRegion *R) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001016 if (const BindingVal *BV = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001017 return Optional<SVal>::create(BV->getValue());
1018
1019 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001020}
1021
Ted Kremeneka6275a52009-07-15 02:31:43 +00001022static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1023 RTy = Ctx.getCanonicalType(RTy);
1024 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremeneka6275a52009-07-15 02:31:43 +00001026 if (RTy == UsedTy)
1027 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
1029
Ted Kremenek25c54572009-07-20 22:58:02 +00001030 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +00001031 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +00001032 // represents a reinterpretation.
1033 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001034 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001035 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001036
1037 return PUsedTy && PRTy &&
1038 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001039 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001040 }
1041
1042 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001043}
1044
Ted Kremenek0954cde2009-09-24 04:11:44 +00001045const ElementRegion *
1046RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1047 ASTContext &Ctx = getContext();
1048 SVal idx = ValMgr.makeZeroArrayIndex();
1049 assert(!T.isNull());
1050 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1051}
1052
1053
1054
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001055SValuator::CastResult
1056RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001057
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001058 assert(!isa<UnknownVal>(L) && "location unknown");
1059 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +00001060
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001061 // FIXME: Is this even possible? Shouldn't this be treated as a null
1062 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001063 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001064 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek28172a22009-12-15 23:23:27 +00001065
Ted Kremenek67f28532009-06-17 22:02:04 +00001066 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001067
Zhongxing Xu91844122009-05-20 09:18:48 +00001068 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001069 // Example:
1070 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001071 // char* p = alloca();
1072 // read(p);
1073 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001074 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001075 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Ted Kremenek0954cde2009-09-24 04:11:44 +00001077 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1078 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenek968f0a62009-08-03 21:41:46 +00001080 if (isa<CodeTextRegion>(MR))
1081 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001083 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1084 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001085 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001086 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001087
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001088 // FIXME: We should eventually handle funny addressing. e.g.:
1089 //
1090 // int x = ...;
1091 // int *p = &x;
1092 // char *q = (char*) p;
1093 // char c = *q; // returns the first byte of 'x'.
1094 //
1095 // Such funny addressing will occur due to layering of regions.
1096
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001097#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001098 ASTContext &Ctx = getContext();
1099 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001100 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1101 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001102 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001103 assert(Ctx.getCanonicalType(RTy) ==
1104 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001105 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001106#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001107
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001108 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001109 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001111 // FIXME: Handle unions.
1112 if (RTy->isUnionType())
1113 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001114
1115 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001116 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001117
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001118 // FIXME: handle Vector types.
1119 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001120 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001121
1122 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001123 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001124 CastRetrievedVal(RetrieveField(state, FR), FR,
1125 T, false));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001126
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001127 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1128 // FIXME: Here we actually perform an implicit conversion from the loaded
1129 // value to the element type. Eventually we want to compose these values
1130 // more intelligently. For example, an 'element' can encompass multiple
1131 // bound regions (e.g., several bound bytes), or could be a subset of
1132 // a larger value.
Zhongxing Xu652be342009-11-16 04:49:44 +00001133 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001134 CastRetrievedVal(RetrieveElement(state, ER),
1135 ER, T, false));
1136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001138 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1139 // FIXME: Here we actually perform an implicit conversion from the loaded
1140 // value to the ivar type. What we should model is stores to ivars
1141 // that blow past the extent of the ivar. If the address of the ivar is
1142 // reinterpretted, it is possible we stored a different value that could
1143 // fit within the ivar. Either we need to cast these when storing them
1144 // or reinterpret them lazily (as we do here).
Zhongxing Xu652be342009-11-16 04:49:44 +00001145 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001146 CastRetrievedVal(RetrieveObjCIvar(state, IVR),
1147 IVR, T, false));
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001150 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1151 // FIXME: Here we actually perform an implicit conversion from the loaded
1152 // value to the variable type. What we should model is stores to variables
1153 // that blow past the extent of the variable. If the address of the
1154 // variable is reinterpretted, it is possible we stored a different value
1155 // that could fit within the variable. Either we need to cast these when
1156 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu652be342009-11-16 04:49:44 +00001157 return SValuator::CastResult(state,
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001158 CastRetrievedVal(RetrieveVar(state, VR), VR, T,
1159 false));
1160 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001161
Ted Kremenek451ac092009-08-06 04:50:20 +00001162 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001163 const BindingVal *V = Lookup(B, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001164
1165 // Check if the region has a binding.
1166 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001167 if (SVal const *SV = V->getValue())
1168 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001169
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001170 // The location does not have a bound value. This means that it has
1171 // the value it had upon its creation and/or entry to the analyzed
1172 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001173 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001174 // All stack variables are considered to have undefined values
1175 // upon creation. All heap allocated blocks are considered to
1176 // have undefined values as well unless they are explicitly bound
1177 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001178 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001179 }
1180
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001181 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001182 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001183}
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001185std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001186RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001187 if (Optional<SVal> OV = getDirectBinding(B, R))
1188 if (const nonloc::LazyCompoundVal *V =
1189 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1190 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001192 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1193 const std::pair<const GRState *, const MemRegion *> &X =
1194 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001196 if (X.first)
1197 return std::make_pair(X.first,
1198 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001199 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001200 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1201 const std::pair<const GRState *, const MemRegion *> &X =
1202 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001204 if (X.first)
1205 return std::make_pair(X.first,
1206 MRMgr.getFieldRegionWithSuper(FR, X.second));
1207 }
1208
1209 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1210}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001211
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001212SVal RegionStoreManager::RetrieveElement(const GRState* state,
1213 const ElementRegion* R) {
1214 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001215 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001216 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001217 return *V;
1218
Ted Kremenek921109a2009-07-01 23:19:52 +00001219 const MemRegion* superR = R->getSuperRegion();
1220
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001221 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001222 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001223 // FIXME: Handle loads from strings where the literal is treated as
1224 // an integer, e.g., *((unsigned int*)"hello")
1225 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001226 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001227 if (T != Ctx.getCanonicalType(R->getElementType()))
1228 return UnknownVal();
1229
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001230 const StringLiteral *Str = StrR->getStringLiteral();
1231 SVal Idx = R->getIndex();
1232 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1233 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001234 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001235 if (i > byteLength) {
1236 // Buffer overflow checking in GRExprEngine should handle this case,
1237 // but we shouldn't rely on it to not overflow here if that checking
1238 // is disabled.
1239 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001240 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001241 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001242 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001243 }
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001246 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001247 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001248 if (SymbolRef parentSym = V->getAsSymbol())
1249 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001250
1251 if (V->isUnknownOrUndef())
1252 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001253
1254 // Handle LazyCompoundVals for the immediate super region. Other cases
1255 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001256 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001257 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001259 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1260 return RetrieveElement(LCV->getState(), R);
1261 }
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremeneka6275a52009-07-15 02:31:43 +00001263 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001264 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001265 }
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001266
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001267 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001268}
1269
Mike Stump1eb44332009-09-09 15:08:12 +00001270SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001271 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001272
1273 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001274 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001275 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001276 return *V;
1277
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001278 QualType Ty = R->getValueType(getContext());
1279 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1280}
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001282SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1283 const TypedRegion *R,
1284 QualType Ty,
1285 const MemRegion *superR) {
1286
Mike Stump1eb44332009-09-09 15:08:12 +00001287 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001288 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001290 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001292 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001293 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001294 if (SymbolRef parentSym = D->getAsSymbol())
1295 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001297 if (D->isZeroConstant())
1298 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001300 if (D->isUnknown())
1301 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001303 assert(0 && "Unknown default value");
1304 }
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001306 // If our super region is a field or element itself, walk up the region
1307 // hierarchy to see if there is a default value installed in an ancestor.
1308 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1309 superR = cast<SubRegion>(superR)->getSuperRegion();
1310 continue;
1311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001313 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001314 }
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001316 // Lazy binding?
1317 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001318 const MemRegion *lazyBindingRegion = NULL;
1319 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001321 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001322 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001324 if (isa<ElementRegion>(R))
1325 return RetrieveElement(lazyBindingState,
1326 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001328 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001329 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001330 }
1331
Ted Kremenekde0d2632010-01-05 02:18:06 +00001332 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001333 if (isa<ElementRegion>(R)) {
1334 // Currently we don't reason specially about Clang-style vectors. Check
1335 // if superR is a vector and if so return Unknown.
1336 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1337 if (typedSuperR->getValueType(getContext())->isVectorType())
1338 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001339 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001342 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001345 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001346 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001347}
Mike Stump1eb44332009-09-09 15:08:12 +00001348
1349SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001350 const ObjCIvarRegion* R) {
1351
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001352 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001353 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001354
Zhongxing Xu13d50172009-10-11 08:08:02 +00001355 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001356 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001358 const MemRegion *superR = R->getSuperRegion();
1359
Ted Kremenekab22ee92009-10-20 01:20:57 +00001360 // Check if the super region has a default binding.
1361 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001362 if (SymbolRef parentSym = V->getAsSymbol())
1363 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001365 // Other cases: give up.
1366 return UnknownVal();
1367 }
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Ted Kremenek25c54572009-07-20 22:58:02 +00001369 return RetrieveLazySymbol(state, R);
1370}
1371
Ted Kremenek9031dd72009-07-21 00:12:07 +00001372SVal RegionStoreManager::RetrieveVar(const GRState *state,
1373 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Ted Kremenek9031dd72009-07-21 00:12:07 +00001375 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001376 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Zhongxing Xu13d50172009-10-11 08:08:02 +00001378 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001379 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek9031dd72009-07-21 00:12:07 +00001381 // Lazily derive a value for the VarRegion.
1382 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001384 if (R->hasGlobalsOrParametersStorage() ||
1385 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek28172a22009-12-15 23:23:27 +00001386 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Ted Kremenek9031dd72009-07-21 00:12:07 +00001388 return UndefinedVal();
1389}
1390
Mike Stump1eb44332009-09-09 15:08:12 +00001391SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001392 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Ted Kremenek25c54572009-07-20 22:58:02 +00001394 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001395
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001396 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001397 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001398}
1399
Mike Stump1eb44332009-09-09 15:08:12 +00001400SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1401 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001402 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001403 assert(T->isStructureType());
1404
Zhongxing Xub7507d12009-06-11 07:27:30 +00001405 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001406 RecordDecl* RD = RT->getDecl();
1407 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001408 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001409#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001410 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1411
Ted Kremenek67f28532009-06-17 22:02:04 +00001412 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1413 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001414 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001415
Douglas Gregore267ff32008-12-11 20:41:00 +00001416 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1417 FieldEnd = Fields.rend();
1418 Field != FieldEnd; ++Field) {
1419 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001420 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001421 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001422 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1423 }
1424
Zhongxing Xud91ee272009-06-23 09:02:15 +00001425 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001426#else
1427 return ValMgr.makeLazyCompoundVal(state, R);
1428#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001429}
1430
Ted Kremenek67f28532009-06-17 22:02:04 +00001431SVal RegionStoreManager::RetrieveArray(const GRState *state,
1432 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001433#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001434 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001435 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1436
1437 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001438 uint64_t size = CAT->getSize().getZExtValue();
1439 for (uint64_t i = 0; i < size; ++i) {
1440 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001441 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001442 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001443 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001444 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001445 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1446 }
1447
Zhongxing Xud91ee272009-06-23 09:02:15 +00001448 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001449#else
1450 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1451 return ValMgr.makeLazyCompoundVal(state, R);
1452#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001453}
1454
Ted Kremenek9af46f52009-06-16 22:36:44 +00001455//===----------------------------------------------------------------------===//
1456// Binding values to regions.
1457//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001458
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001459Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001460 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001461 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
1462 return Remove(store, BindingKey::Make(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Ted Kremenek0964a062009-01-21 06:57:53 +00001464 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001465}
1466
Ted Kremenek67f28532009-06-17 22:02:04 +00001467const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001468 if (isa<loc::ConcreteInt>(L))
1469 return state;
1470
Ted Kremenek9af46f52009-06-16 22:36:44 +00001471 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001472 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek9af46f52009-06-16 22:36:44 +00001474 // Check if the region is a struct region.
1475 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1476 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001477 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001479 // Special case: the current region represents a cast and it and the super
1480 // region both have pointer types or intptr_t types. If so, perform the
1481 // bind to the super region.
1482 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001483 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001484 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1485 if (ER->getIndex().isZeroConstant()) {
1486 if (const TypedRegion *superR =
1487 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1488 ASTContext &Ctx = getContext();
1489 QualType superTy = superR->getValueType(Ctx);
1490 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001491
1492 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001493 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001494 SValuator::CastResult cr =
1495 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001496 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1497 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001498 // For now, just invalidate the fields of the struct/union/class.
1499 // FIXME: Precisely handle the fields of the record.
1500 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001501 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001502 }
1503 }
1504 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001505 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1506 // Binding directly to a symbolic region should be treated as binding
1507 // to element 0.
1508 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek852274d2009-12-16 03:18:58 +00001509
1510 // FIXME: Is this the right way to handle symbols that are references?
1511 if (const PointerType *PT = T->getAs<PointerType>())
1512 T = PT->getPointeeType();
1513 else
1514 T = T->getAs<ReferenceType>()->getPointeeType();
1515
Ted Kremenek0954cde2009-09-24 04:11:44 +00001516 R = GetElementZeroRegion(SR, T);
1517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001519 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001520 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001521 return state->makeWithStore(Add(B, R,
1522 BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001523}
1524
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001525const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001526 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001527 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001528
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001529 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001530
Ted Kremenek0964a062009-01-21 06:57:53 +00001531 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001532 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001533 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001534 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001535
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001536 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001537}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001538
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001539// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001540const GRState *
1541RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001542 const CompoundLiteralExpr *CL,
1543 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001544 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001545 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1546 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001547}
1548
Ted Kremenek027e2662009-11-19 20:20:24 +00001549const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1550 const MemRegion *R,
1551 QualType T) {
1552 Store store = state->getStore();
1553 RegionBindings B = GetRegionBindings(store);
1554 SVal V;
1555
1556 if (Loc::IsLocType(T))
1557 V = ValMgr.makeNull();
1558 else if (T->isIntegerType())
1559 V = ValMgr.makeZeroVal(T);
1560 else if (T->isStructureType() || T->isArrayType()) {
1561 // Set the default value to a zero constant when it is a structure
1562 // or array. The type doesn't really matter.
1563 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1564 }
1565 else {
1566 return state;
1567 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001568
1569 return state->makeWithStore(Add(B, R,
1570 BindingVal(V, BindingVal::Default)).getRoot());
Ted Kremenek027e2662009-11-19 20:20:24 +00001571}
1572
Ted Kremenek67f28532009-06-17 22:02:04 +00001573const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001574 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001575 SVal Init) {
Ted Kremenekfee90812010-01-26 23:51:00 +00001576
1577 ASTContext &Ctx = getContext();
1578 const ArrayType *AT =
1579 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1580 QualType ElementTy = AT->getElementType();
1581 Optional<uint64_t> Size;
1582
1583 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1584 Size = CAT->getSize().getZExtValue();
1585
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001586 // Check if the init expr is a StringLiteral.
1587 if (isa<loc::MemRegionVal>(Init)) {
1588 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1589 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1590 const char* str = S->getStrData();
1591 unsigned len = S->getByteLength();
1592 unsigned j = 0;
1593
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001594 // Copy bytes from the string literal into the target array. Trailing bytes
1595 // in the array that are not covered by the string literal are initialized
1596 // to zero.
Ted Kremenekfee90812010-01-26 23:51:00 +00001597
1598 // We assume that string constants are bound to
1599 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001600 uint64_t size = Size.getValue();
Ted Kremenekfee90812010-01-26 23:51:00 +00001601
Ted Kremenek46537392009-07-16 01:33:37 +00001602 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001603 if (j >= len)
1604 break;
1605
Ted Kremenek46537392009-07-16 01:33:37 +00001606 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001607 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1608 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001609
Zhongxing Xud91ee272009-06-23 09:02:15 +00001610 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001611 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001612 }
1613
Ted Kremenek67f28532009-06-17 22:02:04 +00001614 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001615 }
1616
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001617 // Handle lazy compound values.
1618 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1619 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001620
1621 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001622
1623 if (Init.isUnknown())
1624 return setImplicitDefaultValue(state, R, ElementTy);
1625
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001626 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001627 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001628 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Ted Kremenekfee90812010-01-26 23:51:00 +00001630 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001631 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001632 if (VI == VE)
1633 break;
1634
Ted Kremenek46537392009-07-16 01:33:37 +00001635 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001636 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001637
Ted Kremenekfee90812010-01-26 23:51:00 +00001638 if (ElementTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001639 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001640 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001641 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001642 }
1643
Ted Kremenek027e2662009-11-19 20:20:24 +00001644 // If the init list is shorter than the array length, set the
1645 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001646 if (Size.hasValue() && i < Size.getValue())
Ted Kremenek027e2662009-11-19 20:20:24 +00001647 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001648
Ted Kremenek67f28532009-06-17 22:02:04 +00001649 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001650}
1651
Ted Kremenek67f28532009-06-17 22:02:04 +00001652const GRState *
1653RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1654 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Ted Kremenek67f28532009-06-17 22:02:04 +00001656 if (!Features.supportsFields())
1657 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001659 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001660 assert(T->isStructureType());
1661
Ted Kremenek6217b802009-07-29 21:53:49 +00001662 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001663 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001664
1665 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001666 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001667
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001668 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001669 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001670 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Ted Kremenek67f28532009-06-17 22:02:04 +00001672 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1673 // Ignore them and kill the field values.
1674 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001675 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001676
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001677 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001678 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001679
1680 RecordDecl::field_iterator FI, FE;
1681
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001682 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001683
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001684 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001685 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001686
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001687 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001688 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001689
Ted Kremenekcf549592009-09-22 21:19:14 +00001690 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001691 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001692 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001693 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001694 else
1695 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001696 }
1697
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001698 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001699 if (FI != FE) {
1700 Store store = state->getStore();
1701 RegionBindings B = GetRegionBindings(store);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001702 B = Add(B, R, BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001703 state = state->makeWithStore(B.getRoot());
1704 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001705
Ted Kremenek67f28532009-06-17 22:02:04 +00001706 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001707}
1708
Zhongxing Xu13d50172009-10-11 08:08:02 +00001709Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1710 RegionBindings B = GetRegionBindings(store);
1711 llvm::OwningPtr<RegionStoreSubRegionMap>
1712 SubRegions(getRegionStoreSubRegionMap(store));
1713 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001714
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001715 // Set the default value of the struct region to "unknown".
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001716 B = Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001717
Zhongxing Xu13d50172009-10-11 08:08:02 +00001718 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001719}
1720
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001721const GRState*
1722RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1723 const GRState *state,
1724 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001725
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001726 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001727 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001728
Mike Stump1eb44332009-09-09 15:08:12 +00001729 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001730 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001731
Mike Stump1eb44332009-09-09 15:08:12 +00001732 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001733 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001735 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1736 // results in a zero-copy algorithm.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001737 return state->makeWithStore(Add(B, R,
1738 BindingVal(V, BindingVal::Direct)).getRoot());
1739}
1740
1741//===----------------------------------------------------------------------===//
1742// "Raw" retrievals and bindings.
1743//===----------------------------------------------------------------------===//
1744
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001745BindingKey BindingKey::Make(const MemRegion *R) {
1746 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1747 const RegionRawOffset &O = ER->getAsRawOffset();
1748
1749 if (O.getRegion())
1750 return BindingKey(O.getRegion(), O.getByteOffset());
1751
1752 // FIXME: There are some ElementRegions for which we cannot compute
1753 // raw offsets yet, including regions with symbolic offsets.
1754 }
1755
1756 return BindingKey(R, 0);
1757}
1758
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001759RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K,
1760 BindingVal V) {
1761 return RBFactory.Add(B, K, V);
1762}
1763
1764RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
1765 BindingVal V) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001766 return Add(B, BindingKey::Make(R), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001767}
1768
1769const BindingVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
1770 return B.lookup(K);
1771}
1772
1773const BindingVal *RegionStoreManager::Lookup(RegionBindings B,
1774 const MemRegion *R) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001775 return Lookup(B, BindingKey::Make(R));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001776}
1777
1778RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1779 return RBFactory.Remove(B, K);
1780}
1781
1782RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R){
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001783 return Remove(B, BindingKey::Make(R));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001784}
1785
1786Store RegionStoreManager::Remove(Store store, BindingKey K) {
1787 RegionBindings B = GetRegionBindings(store);
1788 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001789}
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Ted Kremenek9af46f52009-06-16 22:36:44 +00001791//===----------------------------------------------------------------------===//
1792// State pruning.
1793//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001794
Mike Stump1eb44332009-09-09 15:08:12 +00001795void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001796 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001797 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001798{
Ted Kremenek781115c2009-10-17 17:45:11 +00001799 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1800
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001801 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001802 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001805 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001806 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001807
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001808 // Do a pass over the regions in the store. For VarRegions we check if
1809 // the variable is still live and if so add it to the list of live roots.
1810 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001811 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001812
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001813 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001814 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001815 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001816 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001817 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001818
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001819 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001820 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001821 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001822 llvm::SmallVector<RBDNode, 10> Postponed;
1823
Zhongxing Xu6800b332009-10-18 04:15:47 +00001824 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001825
Ted Kremenek9af46f52009-06-16 22:36:44 +00001826 while (!IntermediateRoots.empty()) {
1827 const MemRegion* R = IntermediateRoots.back();
1828 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001829
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001830 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001831 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001832 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001833
Ted Kremenek9af46f52009-06-16 22:36:44 +00001834 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001835 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001836 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001837 continue;
1838 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001839
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001840 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001841 llvm::SmallVectorImpl<RBDNode> &Q =
1842 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1843
1844 Q.push_back(std::make_pair(&state, SR));
1845
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001846 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001847 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001848
1849 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001850 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001851 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001852 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001855 // Enqueue the RegionRoots onto WorkList.
1856 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1857 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001858 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001859 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001860 RegionRoots.clear();
1861
Zhongxing Xu6800b332009-10-18 04:15:47 +00001862 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001863
Ted Kremenek01756192009-10-29 05:14:17 +00001864tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001865 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001866 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001867 WorkList.pop_back();
1868
1869 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001870 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001871 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001872 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001874 const MemRegion *R = N.second;
1875 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001876
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001877 // Enqueue subregions.
1878 RegionStoreSubRegionMap *M;
1879
1880 if (&state == state_N)
1881 M = SubRegions.get();
1882 else {
1883 RegionStoreSubRegionMap *& SM = SC[state_N];
1884 if (!SM)
1885 SM = getRegionStoreSubRegionMap(state_N->getStore());
1886 M = SM;
1887 }
Ted Kremenekdf165012010-02-02 22:38:47 +00001888
1889 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1890 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1891 I != E; ++I)
1892 WorkList.push_back(std::make_pair(state_N, *I));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001893
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001894 // Enqueue the super region.
1895 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1896 const MemRegion *superR = SR->getSuperRegion();
1897 if (!isa<MemSpaceRegion>(superR)) {
1898 // If 'R' is a field or an element, we want to keep the bindings
1899 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001900 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001901 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1902 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001903 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001904 }
1905 }
1906
1907 // Mark the symbol for any live SymbolicRegion as "live". This means we
1908 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001909 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001910 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001911
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001912 // For BlockDataRegions, enqueue the VarRegions for variables marked
1913 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001914 // via BlockDeclRefExprs.
1915 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1916 for (BlockDataRegion::referenced_vars_iterator
1917 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001918 RI != RE; ++RI) {
1919 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1920 WorkList.push_back(std::make_pair(state_N, *RI));
1921 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001922 // No possible data bindings on a BlockDataRegion. Continue to the
1923 // next region in the worklist.
1924 continue;
1925 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001926
1927 Store store_N = state_N->getStore();
1928 RegionBindings B_N = GetRegionBindings(store_N);
1929
1930 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001931 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001932
Zhongxing Xu13d50172009-10-11 08:08:02 +00001933 if (V) {
1934 // Check for lazy bindings.
1935 if (const nonloc::LazyCompoundVal *LCV =
1936 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001937
Zhongxing Xu13d50172009-10-11 08:08:02 +00001938 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001939 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001940 }
1941 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001942 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001943 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001944 SI!=SE;++SI)
1945 SymReaper.markLive(*SI);
1946
Zhongxing Xu13d50172009-10-11 08:08:02 +00001947 // If V is a region, then add it to the worklist.
1948 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001949 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001950 }
1951 }
1952 }
1953
Ted Kremenek01756192009-10-29 05:14:17 +00001954 // See if any postponed SymbolicRegions are actually live now, after
1955 // having done a scan.
1956 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1957 E = Postponed.end() ; I != E ; ++I) {
1958 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1959 if (SymReaper.isLive(SR->getSymbol())) {
1960 WorkList.push_back(*I);
1961 I->second = NULL;
1962 }
1963 }
1964 }
1965
1966 if (!WorkList.empty())
1967 goto tryAgain;
1968
Ted Kremenek9af46f52009-06-16 22:36:44 +00001969 // We have now scanned the store, marking reachable regions and symbols
1970 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001971 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001972 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001973 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001974 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001975 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001976 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Ted Kremenek9af46f52009-06-16 22:36:44 +00001978 // Remove this dead region from the store.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001979 store = Remove(store, I.getKey());
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Ted Kremenek9af46f52009-06-16 22:36:44 +00001981 // Mark all non-live symbols that this region references as dead.
1982 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1983 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Zhongxing Xu13d50172009-10-11 08:08:02 +00001985 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001986 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1987 for (; SI != SE; ++SI)
1988 SymReaper.maybeDead(*SI);
1989 }
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001991 // Write the store back.
1992 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001993}
1994
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001995GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1996 StackFrameContext const *frame) {
1997 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1998 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1999
2000 FunctionDecl::param_const_iterator PI = FD->param_begin();
2001
2002 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
2003
2004 // Copy the arg expression value to the arg variables.
2005 for (; AI != AE; ++AI, ++PI) {
2006 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00002007 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00002008 }
2009
2010 return state;
2011}
2012
Ted Kremenek9af46f52009-06-16 22:36:44 +00002013//===----------------------------------------------------------------------===//
2014// Utility methods.
2015//===----------------------------------------------------------------------===//
2016
Ted Kremenek53ba0b62009-06-24 23:06:47 +00002017void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00002018 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00002019 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00002020 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Ted Kremenek451ac092009-08-06 04:50:20 +00002022 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002023 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00002024}