blob: 88e4eb621141790d959910113c71a78af4548621 [file] [log] [blame]
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Ted Kremenekd6b87082010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
18#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Checker/PathSensitive/GRState.h"
20#include "clang/Checker/PathSensitive/GRStateTrait.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek2f6eb142009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xuea8c48d2009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu228b0d42010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xuceca8062008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xu1359e002008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremenekfa417142009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek920ad712009-07-22 04:35:42 +000033
Ted Kremenek8e994a22010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek8e994a22010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek8e994a22010-01-11 00:07:44 +000040public:
Ted Kremenek64efd0d2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
44 uint64_t Offset;
Ted Kremenek8e994a22010-01-11 00:07:44 +000045
Ted Kremenek64efd0d2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
47 : P(r, (unsigned) k), Offset(offset) {}
48public:
49
50 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
52
53 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremenek8e994a22010-01-11 00:07:44 +000055
56 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek8e994a22010-01-11 00:07:44 +000059 }
Ted Kremenek64efd0d2010-02-03 03:06:46 +000060
61 static BindingKey Make(const MemRegion *R, Kind k);
62
63 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
70
71 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremenek8e994a22010-01-11 00:07:44 +000075};
76} // end anonymous namespace
77
78namespace llvm {
79 static inline
80 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek8e994a22010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xu9165ed62008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek8e994a22010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremenek64efd0d2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xu9165ed62008-11-24 09:44:56 +000093
Ted Kremenekae189ec2008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump11289f42009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek4533a552009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump11289f42009-09-09 15:08:12 +0000105
Ted Kremenek4533a552009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek4533a552009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek4533a552009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump11289f42009-09-09 15:08:12 +0000114
Ted Kremenek4533a552009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000121// Region "Extents"
122//===----------------------------------------------------------------------===//
123//
124// MemRegions represent chunks of memory with a size (their "extent"). This
125// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenek682c3a62009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenekae189ec2008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenekae189ec2008-12-24 01:05:03 +0000130 template<> struct GRStateTrait<RegionExtents>
131 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
132 static void* GDMIndex() { return &RegionExtentsIndex; }
133 };
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000134}
135
Ted Kremenekae189ec2008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000137// Utility functions.
138//===----------------------------------------------------------------------===//
139
140static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
141 if (ty->isAnyPointerType())
142 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenek677779a2008-12-04 02:08:27 +0000151
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000152namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekb251eb62010-02-02 22:38:47 +0000155public:
156 typedef llvm::ImmutableSet<const MemRegion*> Set;
157 typedef llvm::DenseMap<const MemRegion*, Set> Map;
158private:
159 Set::Factory F;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000161public:
Ted Kremenek844a7292009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenek844a7292009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek68c1f012009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenek844a7292009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000172 }
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenekfa417142009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremenekb251eb62010-02-02 22:38:47 +0000177
178 const Set *getSubRegions(const MemRegion *Parent) const {
179 Map::const_iterator I = M.find(Parent);
180 return I == M.end() ? NULL : &I->second;
181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000187 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenekb251eb62010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000192 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000193 }
Mike Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000195 return true;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000196 }
Mike Stump11289f42009-09-09 15:08:12 +0000197};
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000198
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000199
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek4533a552009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek2c85f172009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremenekcc224242009-09-29 06:35:00 +0000203
204 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
205 SMCache SC;
206
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000207public:
Mike Stump11289f42009-09-09 15:08:12 +0000208 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek43015262009-07-29 21:43:22 +0000209 : StoreManager(mgr),
Ted Kremenek4533a552009-06-16 22:36:44 +0000210 Features(f),
Ted Kremenekb251eb62010-02-02 22:38:47 +0000211 RBFactory(mgr.getAllocator(), 3) {}
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000212
Ted Kremenekcc224242009-09-29 06:35:00 +0000213 virtual ~RegionStoreManager() {
214 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
215 delete (*I).second;
216 }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000217
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000218 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump11289f42009-09-09 15:08:12 +0000219
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000220 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump11289f42009-09-09 15:08:12 +0000221
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000222 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
223 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000224 /// getDefaultBinding - Returns an SVal* representing an optional default
225 /// binding associated with a region and its subregions.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000226 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek439a6d12009-11-19 20:20:24 +0000227
228 /// setImplicitDefaultValue - Set the default binding for the provided
229 /// MemRegion to the value implicitly defined for compound literals when
230 /// the value is not specified.
231 const GRState *setImplicitDefaultValue(const GRState *state,
232 const MemRegion *R,
233 QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek2907ab72008-12-24 07:46:32 +0000235 /// getLValueString - Returns an SVal representing the lvalue of a
236 /// StringLiteral. Within RegionStore a StringLiteral has an
237 /// associated StringRegion, and the lvalue of a StringLiteral is
238 /// the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000239 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000240
Ted Kremenek2907ab72008-12-24 07:46:32 +0000241 /// getLValueCompoundLiteral - Returns an SVal representing the
242 /// lvalue of a compound literal. Within RegionStore a compound
243 /// literal has an associated region, and the lvalue of the
244 /// compound literal is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000245 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000246
Ted Kremenek2907ab72008-12-24 07:46:32 +0000247 /// getLValueVar - Returns an SVal that represents the lvalue of a
248 /// variable. Within RegionStore a variable has an associated
249 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000250 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000252 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000253
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000254 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump11289f42009-09-09 15:08:12 +0000255
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000256 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000257
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000258 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000259
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000260
Ted Kremenek2907ab72008-12-24 07:46:32 +0000261 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
262 /// type. 'Array' represents the lvalue of the array being decayed
263 /// to a pointer, and the returned SVal represents the decayed
264 /// version of that lvalue (i.e., a pointer to the first element of
265 /// the array). This is called by GRExprEngine when evaluating
266 /// casts from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000267 SVal ArrayToPointer(Loc Array);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000268
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000269 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000270 NonLoc R, QualType resultTy);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000271
Mike Stump11289f42009-09-09 15:08:12 +0000272 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000273 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000274 }
Ted Kremenek608677a2009-08-21 23:25:54 +0000275
Ted Kremenek609df302009-06-17 22:02:04 +0000276 //===-------------------------------------------------------------------===//
277 // Binding values to regions.
278 //===-------------------------------------------------------------------===//
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000279
Ted Kremenekbca70672009-07-29 18:16:25 +0000280 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek1eb68092009-10-16 00:30:49 +0000281 const Expr *E, unsigned Count,
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000282 InvalidatedSymbols *IS) {
283 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
284 }
285
286 const GRState *InvalidateRegions(const GRState *state,
287 const MemRegion * const *Begin,
288 const MemRegion * const *End,
289 const Expr *E, unsigned Count,
290 InvalidatedSymbols *IS);
Mike Stump11289f42009-09-09 15:08:12 +0000291
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000292public: // Made public for helper classes.
293
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000294 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremenekfa417142009-08-06 01:20:57 +0000295 RegionStoreSubRegionMap &M);
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000297 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
298
299 RegionBindings Add(RegionBindings B, const MemRegion *R,
300 BindingKey::Kind k, SVal V);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000301
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000302 const SVal *Lookup(RegionBindings B, BindingKey K);
303 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000304
305 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000306 RegionBindings Remove(RegionBindings B, const MemRegion *R,
307 BindingKey::Kind k);
308
309 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
310 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
311 }
312
Ted Kremenek8e994a22010-01-11 00:07:44 +0000313 Store Remove(Store store, BindingKey K);
314
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000315public: // Part of public interface to class.
316
Ted Kremenek609df302009-06-17 22:02:04 +0000317 const GRState *Bind(const GRState *state, Loc LV, SVal V);
318
319 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek04af9f22009-12-07 22:05:27 +0000320 const CompoundLiteralExpr* CL,
321 const LocationContext *LC,
322 SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenekb006b822009-11-04 00:09:15 +0000324 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
325 SVal InitVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000326
Ted Kremenekb006b822009-11-04 00:09:15 +0000327 const GRState *BindDeclWithNoInit(const GRState *state,
328 const VarRegion *) {
Ted Kremenek609df302009-06-17 22:02:04 +0000329 return state;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000330 }
Zhongxing Xu83aff702008-10-21 05:29:26 +0000331
Ted Kremenek609df302009-06-17 22:02:04 +0000332 /// BindStruct - Bind a compound value to a structure.
333 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek609df302009-06-17 22:02:04 +0000335 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000336
337 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000338 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000339
Ted Kremenek609df302009-06-17 22:02:04 +0000340 Store Remove(Store store, Loc LV);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000341
Ted Kremenek609df302009-06-17 22:02:04 +0000342
343 //===------------------------------------------------------------------===//
344 // Loading values from regions.
345 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek609df302009-06-17 22:02:04 +0000347 /// The high level logic for this method is this:
348 /// Retrieve (L)
349 /// if L has binding
350 /// return L's binding
351 /// else if L is in killset
352 /// return unknown
353 /// else
354 /// if L is on stack or heap
355 /// return undefined
356 /// else
357 /// return symbolic
Ted Kremenekac7c7242009-07-21 21:03:30 +0000358 SValuator::CastResult Retrieve(const GRState *state, Loc L,
359 QualType T = QualType());
Zhongxing Xue67ea5c2009-06-25 04:50:44 +0000360
Ted Kremenek48029552009-07-15 06:09:28 +0000361 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xu2d160732009-06-25 05:29:39 +0000362
Ted Kremenek48029552009-07-15 06:09:28 +0000363 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenek48029552009-07-15 06:09:28 +0000365 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000366
Ted Kremenekfe12f882009-07-21 00:12:07 +0000367 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenek834e2f62009-07-20 22:58:02 +0000369 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek040e3b92009-08-06 22:33:36 +0000371 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
372 QualType Ty, const MemRegion *superR);
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenek609df302009-06-17 22:02:04 +0000374 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump11289f42009-09-09 15:08:12 +0000375 /// struct copy:
376 /// struct s x, y;
Ted Kremenek609df302009-06-17 22:02:04 +0000377 /// x = y;
378 /// y's value is retrieved by this method.
379 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek609df302009-06-17 22:02:04 +0000381 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000382
Zhongxing Xufd62a332009-12-21 06:52:24 +0000383 /// Get the state and region whose binding this region R corresponds to.
Ted Kremenekfa417142009-08-06 01:20:57 +0000384 std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +0000385 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenekfa417142009-08-06 01:20:57 +0000387 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
388 const GRState *state,
389 const TypedRegion *R);
Ted Kremenek609df302009-06-17 22:02:04 +0000390
Ted Kremenek267e45a2009-09-24 04:11:44 +0000391 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
392 QualType T);
393
Ted Kremenek609df302009-06-17 22:02:04 +0000394 //===------------------------------------------------------------------===//
395 // State pruning.
396 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek609df302009-06-17 22:02:04 +0000398 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
399 /// It returns a new Store with these values removed.
Ted Kremenekcee28a42009-08-02 04:45:08 +0000400 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek609df302009-06-17 22:02:04 +0000401 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
402
Zhongxing Xudaa41762009-10-13 02:24:55 +0000403 const GRState *EnterStackFrame(const GRState *state,
404 const StackFrameContext *frame);
405
Ted Kremenek609df302009-06-17 22:02:04 +0000406 //===------------------------------------------------------------------===//
407 // Region "extents".
408 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000409
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000410 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xu383c2732009-11-12 02:48:32 +0000411 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000412 const MemRegion* R, QualType EleTy);
Ted Kremenek609df302009-06-17 22:02:04 +0000413
414 //===------------------------------------------------------------------===//
Ted Kremenek609df302009-06-17 22:02:04 +0000415 // Utility methods.
416 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ted Kremenek2c85f172009-08-06 04:50:20 +0000418 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000419 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000420 }
Zhongxing Xucebb7412008-10-24 01:38:55 +0000421
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000422 void print(Store store, llvm::raw_ostream& Out, const char* nl,
423 const char *sep);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000424
425 void iterBindings(Store store, BindingsHandler& f) {
426 // FIXME: Implement.
427 }
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000428
Ted Kremenek609df302009-06-17 22:02:04 +0000429 // FIXME: Remove.
430 BasicValueFactory& getBasicVals() {
431 return StateMgr.getBasicVals();
432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek609df302009-06-17 22:02:04 +0000434 // FIXME: Remove.
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000435 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000436};
437
438} // end anonymous namespace
439
Ted Kremenek4533a552009-06-16 22:36:44 +0000440//===----------------------------------------------------------------------===//
441// RegionStore creation.
442//===----------------------------------------------------------------------===//
443
444StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
445 RegionStoreFeatures F = maximal_features_tag();
446 return new RegionStoreManager(StMgr, F);
447}
448
449StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
450 RegionStoreFeatures F = minimal_features_tag();
451 F.enableFields(true);
452 return new RegionStoreManager(StMgr, F);
Ted Kremenek6779f892008-10-24 01:04:59 +0000453}
454
Ted Kremenekfa417142009-08-06 01:20:57 +0000455void
456RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump11289f42009-09-09 15:08:12 +0000457 const SubRegion *R) {
Ted Kremenekfa417142009-08-06 01:20:57 +0000458 const MemRegion *superR = R->getSuperRegion();
459 if (add(superR, R))
460 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump11289f42009-09-09 15:08:12 +0000461 WL.push_back(sr);
Ted Kremenekfa417142009-08-06 01:20:57 +0000462}
463
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000464RegionStoreSubRegionMap*
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000465RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
466 RegionBindings B = GetRegionBindings(store);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000467 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000469 llvm::SmallVector<const SubRegion*, 10> WL;
470
Ted Kremenek2c85f172009-08-06 04:50:20 +0000471 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek8e994a22010-01-11 00:07:44 +0000472 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremenekfa417142009-08-06 01:20:57 +0000473 M->process(WL, R);
Mike Stump11289f42009-09-09 15:08:12 +0000474
Mike Stump11289f42009-09-09 15:08:12 +0000475 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000476 // don't have direct bindings but are super regions of those that do.
477 while (!WL.empty()) {
478 const SubRegion *R = WL.back();
479 WL.pop_back();
Ted Kremenekfa417142009-08-06 01:20:57 +0000480 M->process(WL, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000481 }
482
Ted Kremenek9f276d62009-03-03 19:02:42 +0000483 return M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000484}
Ted Kremenek2907ab72008-12-24 07:46:32 +0000485
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000486SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000487 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000488}
489
Ted Kremenek4533a552009-06-16 22:36:44 +0000490//===----------------------------------------------------------------------===//
Ted Kremenekbca70672009-07-29 18:16:25 +0000491// Binding invalidation.
492//===----------------------------------------------------------------------===//
493
Ted Kremenekb251eb62010-02-02 22:38:47 +0000494
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000495void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
496 const MemRegion *R,
497 RegionStoreSubRegionMap &M) {
Ted Kremenekb251eb62010-02-02 22:38:47 +0000498
499 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
500 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
501 I != E; ++I)
502 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000503
504 B = Remove(B, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000505}
506
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000507namespace {
508class InvalidateRegionsWorker {
509 typedef BumpVector<const MemRegion *> RegionCluster;
510 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
511 typedef llvm::SmallVector<RegionCluster*, 10> WorkList;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000513 BumpVectorContext BVC;
514 ClusterMap ClusterM;
515 WorkList WL;
516public:
517 const GRState *InvalidateRegions(RegionStoreManager &RM,
518 const GRState *state,
519 const MemRegion * const *I,
520 const MemRegion * const *E,
521 const Expr *Ex,
522 unsigned Count,
523 StoreManager::InvalidatedSymbols *IS);
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000524
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000525private:
526 void AddToWorkList(const MemRegion *R);
527 void AddToCluster(const MemRegion *R);
528 RegionCluster **getCluster(const MemRegion *R);
529};
530}
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000531
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000532void InvalidateRegionsWorker::AddToCluster(const MemRegion *R) {
533 const MemRegion *baseR = R->getBaseRegion();
534 RegionCluster **CPtr = getCluster(baseR);
535 if (R != baseR) {
536 assert(*CPtr);
537 (*CPtr)->push_back(R, BVC);
538 }
539}
540
541void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
542 RegionCluster **CPtr = getCluster( R->getBaseRegion());
543 if (RegionCluster *C = *CPtr) {
544 WL.push_back(C);
545 *CPtr = NULL;
546 }
547}
548
549InvalidateRegionsWorker::RegionCluster **
550InvalidateRegionsWorker::getCluster(const MemRegion *R) {
551 RegionCluster *&CRef = ClusterM[R];
552 if (!CRef) {
553 void *Mem = BVC.getAllocator().Allocate<RegionCluster>();
554 CRef = new (Mem) RegionCluster(BVC, 10);
555 CRef->push_back(R, BVC);
556 }
557 return &CRef;
558}
559
560const GRState *
561InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM,
562 const GRState *state,
563 const MemRegion * const *I,
564 const MemRegion * const *E,
565 const Expr *Ex, unsigned Count,
566 StoreManager::InvalidatedSymbols *IS)
567{
568 ASTContext &Ctx = state->getStateManager().getContext();
569 ValueManager &ValMgr = state->getStateManager().getValueManager();
570 RegionBindings B = RegionStoreManager::GetRegionBindings(state->getStore());
571
572 // Scan the entire store and make the region clusters.
573 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
574 AddToCluster(RI.getKey().getRegion());
575 if (const MemRegion *R = RI.getData().getAsRegion())
576 AddToCluster(R);
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000577 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000578
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000579 // Add the cluster for I .. E to a worklist.
580 for ( ; I != E; ++I)
581 AddToWorkList(*I);
582
583 while (!WL.empty()) {
584 RegionCluster *C = WL.back();
585 WL.pop_back();
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000586
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000587 for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) {
588 const MemRegion *R = *I;
Ted Kremenek1eb68092009-10-16 00:30:49 +0000589
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000590 // Get the old binding. Is it a region? If so, add it to the worklist.
591 if (Optional<SVal> V = RM.getDirectBinding(B, R)) {
592 if (const MemRegion *RV = V->getAsRegion())
593 AddToWorkList(RV);
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000594
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000595 // A symbol? Mark it touched by the invalidation.
596 if (IS)
597 if (SymbolRef Sym = V->getAsSymbol())
598 IS->insert(Sym);
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000599 }
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000600
601 // Symbolic region? Mark that symbol touched by the invalidation.
602 if (IS)
603 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
604 IS->insert(SR->getSymbol());
605
606 // BlockDataRegion? If so, invalidate captured variables that are passed
607 // by reference.
608 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
609 for (BlockDataRegion::referenced_vars_iterator
610 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
611 I != E; ++I) {
612 const VarRegion *VR = *I;
613 if (VR->getDecl()->getAttr<BlocksAttr>())
614 AddToWorkList(VR);
615 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000616 continue;
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000617 }
618
619 // Handle the region itself.
620 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
621 // Invalidate the region by setting its default value to
622 // conjured symbol. The type of the symbol is irrelavant.
623 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
624 Count);
625 B = RM.Add(B, R, BindingKey::Default, V);
626 continue;
627 }
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000628
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000629 if (!R->isBoundable())
630 continue;
631
632 const TypedRegion *TR = cast<TypedRegion>(R);
633 QualType T = TR->getValueType(Ctx);
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000634
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000635 // Invalidate the binding.
636 if (const RecordType *RT = T->getAsStructureType()) {
637 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
638 // No record definition. There is nothing we can do.
639 if (!RD) {
640 B = RM.Remove(B, R);
641 continue;
642 }
643
644 // Invalidate the region by setting its default value to
645 // conjured symbol. The type of the symbol is irrelavant.
646 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
647 Count);
648 B = RM.Add(B, R, BindingKey::Default, V);
649 continue;
650 }
651 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
652 // Set the default value of the array to conjured symbol.
653 DefinedOrUnknownSVal V =
654 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
655 B = RM.Add(B, R, BindingKey::Default, V);
656 continue;
657 }
658
659 // For fields and elements that aren't themselves structs or arrays,
660 // just remove the binding. Base regions will get default values from
661 // which the fields and elements will get lazily symbolicated.
662 if (isa<FieldRegion>(R) || isa<ElementRegion>(R)) {
663 B = RM.Remove(B, R, BindingKey::Direct);
664 continue;
665 }
666
667
668 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
669 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
670 B = RM.Add(B, R, BindingKey::Direct, V);
671 }
Ted Kremenekfa417142009-08-06 01:20:57 +0000672 }
673
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000674 // Create a new state with the updated bindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000675 return state->makeWithStore(B.getRoot());
Ted Kremenekbca70672009-07-29 18:16:25 +0000676}
677
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000678const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
679 const MemRegion * const *I,
680 const MemRegion * const *E,
681 const Expr *Ex,
682 unsigned Count,
683 InvalidatedSymbols *IS) {
684 InvalidateRegionsWorker W;
685 return W.InvalidateRegions(*this, state, I, E, Ex, Count, IS);
686}
687
688
Ted Kremenekbca70672009-07-29 18:16:25 +0000689//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000690// getLValueXXX methods.
691//===----------------------------------------------------------------------===//
692
Ted Kremenek2907ab72008-12-24 07:46:32 +0000693/// getLValueString - Returns an SVal representing the lvalue of a
694/// StringLiteral. Within RegionStore a StringLiteral has an
695/// associated StringRegion, and the lvalue of a StringLiteral is the
696/// lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000697SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000698 return loc::MemRegionVal(MRMgr.getStringRegion(S));
699}
700
Ted Kremenek2907ab72008-12-24 07:46:32 +0000701/// getLValueVar - Returns an SVal that represents the lvalue of a
702/// variable. Within RegionStore a variable has an associated
703/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000704SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000705 const LocationContext *LC) {
706 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000707}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000708
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000709SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
710 return getLValueFieldOrIvar(D, Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000711}
712
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000713SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
714 return getLValueFieldOrIvar(D, Base);
Ted Kremenekd3c82762009-03-05 04:50:08 +0000715}
716
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000717SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000718 if (Base.isUnknownOrUndef())
719 return Base;
720
721 Loc BaseL = cast<Loc>(Base);
722 const MemRegion* BaseR = 0;
723
724 switch (BaseL.getSubKind()) {
725 case loc::MemRegionKind:
726 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
727 break;
728
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000729 case loc::GotoLabelKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000730 // These are anormal cases. Flag an undefined value.
731 return UndefinedVal();
732
733 case loc::ConcreteIntKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000734 // While these seem funny, this can happen through casts.
735 // FIXME: What we should return is the field offset. For example,
736 // add the field offset to the integer value. That way funny things
737 // like this work properly: &(((struct foo *) 0xa)->f)
738 return Base;
739
740 default:
Zhongxing Xue79a4e62008-11-07 08:57:30 +0000741 assert(0 && "Unhandled Base.");
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000742 return Base;
743 }
Mike Stump11289f42009-09-09 15:08:12 +0000744
Ted Kremenekd3c82762009-03-05 04:50:08 +0000745 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
746 // of FieldDecl.
747 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
748 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000749
Ted Kremenekd3c82762009-03-05 04:50:08 +0000750 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000751}
752
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000753SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
754 SVal Base) {
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000755
Ted Kremenek06032222009-03-09 22:44:49 +0000756 // If the base is an unknown or undefined value, just return it back.
757 // FIXME: For absolute pointer addresses, we just return that value back as
758 // well, although in reality we should return the offset added to that
759 // value.
760 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu36d4ade2008-10-27 12:23:17 +0000761 return Base;
762
Ted Kremenek92d48a72009-01-22 20:27:48 +0000763 // Only handle integer offsets... for now.
764 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000765 return UnknownVal();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000766
Zhongxing Xu7c382642009-05-09 13:20:07 +0000767 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000768
769 // Pointer of any type can be cast and used as array base.
770 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000772 // Convert the offset to the appropriate size and signedness.
773 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000774
Ted Kremenek92d48a72009-01-22 20:27:48 +0000775 if (!ElemR) {
776 //
777 // If the base region is not an ElementRegion, create one.
778 // This can happen in the following example:
779 //
780 // char *p = __builtin_alloc(10);
781 // p[1] = 8;
782 //
Zhongxing Xu7c382642009-05-09 13:20:07 +0000783 // Observe that 'p' binds to an AllocaRegion.
Ted Kremenek92d48a72009-01-22 20:27:48 +0000784 //
Ted Kremenek02e50892009-05-04 06:18:28 +0000785 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000786 BaseRegion, getContext()));
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000787 }
Mike Stump11289f42009-09-09 15:08:12 +0000788
Ted Kremenek92d48a72009-01-22 20:27:48 +0000789 SVal BaseIdx = ElemR->getIndex();
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek92d48a72009-01-22 20:27:48 +0000791 if (!isa<nonloc::ConcreteInt>(BaseIdx))
792 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ted Kremenek92d48a72009-01-22 20:27:48 +0000794 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
795 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
796 assert(BaseIdxI.isSigned());
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000798 // Compute the new index.
799 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000801 // Construct the new ElementRegion.
802 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000803 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump11289f42009-09-09 15:08:12 +0000804 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000805}
806
Ted Kremenek4533a552009-06-16 22:36:44 +0000807//===----------------------------------------------------------------------===//
808// Extents for regions.
809//===----------------------------------------------------------------------===//
810
Zhongxing Xu383c2732009-11-12 02:48:32 +0000811DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000812 const MemRegion *R,
813 QualType EleTy) {
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek94575aa2009-07-10 22:30:06 +0000815 switch (R->getKind()) {
Ted Kremenekacd71a42010-01-05 02:18:06 +0000816 case MemRegion::CXXThisRegionKind:
817 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000818 case MemRegion::GenericMemSpaceRegionKind:
819 case MemRegion::StackLocalsSpaceRegionKind:
820 case MemRegion::StackArgumentsSpaceRegionKind:
821 case MemRegion::HeapSpaceRegionKind:
822 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000823 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000824 assert(0 && "Cannot index into a MemSpace");
Mike Stump11289f42009-09-09 15:08:12 +0000825 return UnknownVal();
826
Ted Kremenek10a50e72009-11-25 01:32:22 +0000827 case MemRegion::FunctionTextRegionKind:
828 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000829 case MemRegion::BlockDataRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000830 // Technically this can happen if people do funny things with casts.
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000831 return UnknownVal();
Ted Kremenek94575aa2009-07-10 22:30:06 +0000832
833 // Not yet handled.
834 case MemRegion::AllocaRegionKind:
835 case MemRegion::CompoundLiteralRegionKind:
836 case MemRegion::ElementRegionKind:
837 case MemRegion::FieldRegionKind:
838 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000839 case MemRegion::CXXObjectRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000840 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000841
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000842 case MemRegion::SymbolicRegionKind: {
843 const SVal *Size = state->get<RegionExtents>(R);
844 if (!Size)
845 return UnknownVal();
846 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
847 if (!CI)
848 return UnknownVal();
849
850 CharUnits RegionSize =
851 CharUnits::fromQuantity(CI->getValue().getSExtValue());
852 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
853 assert(RegionSize % EleSize == 0);
854
855 return ValMgr.makeIntVal(RegionSize / EleSize, false);
856 }
857
Ted Kremenek94575aa2009-07-10 22:30:06 +0000858 case MemRegion::StringRegionKind: {
859 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000860 // We intentionally made the size value signed because it participates in
Ted Kremenek94575aa2009-07-10 22:30:06 +0000861 // operations with signed indices.
862 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000863 }
Mike Stump11289f42009-09-09 15:08:12 +0000864
Ted Kremenek94575aa2009-07-10 22:30:06 +0000865 case MemRegion::VarRegionKind: {
866 const VarRegion* VR = cast<VarRegion>(R);
867 // Get the type of the variable.
868 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000869
Ted Kremenek94575aa2009-07-10 22:30:06 +0000870 // FIXME: Handle variable-length arrays.
871 if (isa<VariableArrayType>(T))
872 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000873
Ted Kremenek94575aa2009-07-10 22:30:06 +0000874 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
875 // return the size as signed integer.
876 return ValMgr.makeIntVal(CAT->getSize(), false);
877 }
Ted Kremenekca7935d2009-08-02 05:15:23 +0000878
Ted Kremenek94575aa2009-07-10 22:30:06 +0000879 // Clients can use ordinary variables as if they were arrays. These
880 // essentially are arrays of size 1.
881 return ValMgr.makeIntVal(1, false);
Zhongxing Xuea8c48d2009-05-06 11:51:48 +0000882 }
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000883 }
Mike Stump11289f42009-09-09 15:08:12 +0000884
Ted Kremenek94575aa2009-07-10 22:30:06 +0000885 assert(0 && "Unreachable");
Ted Kremenek47ad37d2009-01-06 19:12:06 +0000886 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000887}
888
Ted Kremenek609df302009-06-17 22:02:04 +0000889const GRState *RegionStoreManager::setExtent(const GRState *state,
890 const MemRegion *region,
891 SVal extent) {
892 return state->set<RegionExtents>(region, extent);
Ted Kremenek4533a552009-06-16 22:36:44 +0000893}
894
895//===----------------------------------------------------------------------===//
896// Location and region casting.
897//===----------------------------------------------------------------------===//
898
Ted Kremenek2907ab72008-12-24 07:46:32 +0000899/// ArrayToPointer - Emulates the "decay" of an array to a pointer
900/// type. 'Array' represents the lvalue of the array being decayed
901/// to a pointer, and the returned SVal represents the decayed
902/// version of that lvalue (i.e., a pointer to the first element of
903/// the array). This is called by GRExprEngine when evaluating casts
904/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000905SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000906 if (!isa<loc::MemRegionVal>(Array))
907 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenekf065b152008-12-13 19:24:37 +0000909 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
910 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000911
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000912 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000913 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000914
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000915 // Strip off typedefs from the ArrayRegion's ValueType.
John McCalla1925362009-09-29 23:03:30 +0000916 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000917 ArrayType *AT = cast<ArrayType>(T);
918 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000919
Ted Kremenekccc22922009-07-16 00:00:11 +0000920 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenek721fcc02009-12-04 00:26:31 +0000921 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
922 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000923}
924
Ted Kremenek4533a552009-06-16 22:36:44 +0000925//===----------------------------------------------------------------------===//
926// Pointer arithmetic.
927//===----------------------------------------------------------------------===//
928
Mike Stump11289f42009-09-09 15:08:12 +0000929SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000930 BinaryOperator::Opcode Op, Loc L, NonLoc R,
931 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000932 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000933 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000934 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000935
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000936 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000937 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000938
Ted Kremenekf6f04612009-07-11 00:58:27 +0000939 switch (MR->getKind()) {
940 case MemRegion::SymbolicRegionKind: {
941 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000942 SymbolRef Sym = SR->getSymbol();
Ted Kremenekd1d60662009-08-25 22:55:09 +0000943 QualType T = Sym->getType(getContext());
944 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000945
Ted Kremenekd1d60662009-08-25 22:55:09 +0000946 if (const PointerType *PT = T->getAs<PointerType>())
947 EleTy = PT->getPointeeType();
948 else
John McCall9dd450b2009-09-21 23:43:11 +0000949 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000950
Ted Kremenekf6f04612009-07-11 00:58:27 +0000951 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
952 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000953 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000954 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000955 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000956 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000957 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000958 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenekf6f04612009-07-11 00:58:27 +0000959 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
960 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000961 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000962 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000963
Ted Kremenekf6f04612009-07-11 00:58:27 +0000964 case MemRegion::ElementRegionKind: {
965 ER = cast<ElementRegion>(MR);
966 break;
967 }
Mike Stump11289f42009-09-09 15:08:12 +0000968
Ted Kremenekf6f04612009-07-11 00:58:27 +0000969 // Not yet handled.
970 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000971 case MemRegion::StringRegionKind: {
972
973 }
974 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000975 case MemRegion::CompoundLiteralRegionKind:
976 case MemRegion::FieldRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000977 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000978 case MemRegion::CXXObjectRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000979 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000980
Ted Kremenek10a50e72009-11-25 01:32:22 +0000981 case MemRegion::FunctionTextRegionKind:
982 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000983 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000984 // Technically this can happen if people do funny things with casts.
985 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000986
Ted Kremenekacd71a42010-01-05 02:18:06 +0000987 case MemRegion::CXXThisRegionKind:
988 assert(0 &&
989 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000990 case MemRegion::GenericMemSpaceRegionKind:
991 case MemRegion::StackLocalsSpaceRegionKind:
992 case MemRegion::StackArgumentsSpaceRegionKind:
993 case MemRegion::HeapSpaceRegionKind:
994 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000995 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000996 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
997 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000998 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000999
Zhongxing Xue7d14932009-03-02 07:52:23 +00001000 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +00001001 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +00001002
Ted Kremenek8ec57712009-10-06 01:39:48 +00001003 // For now, only support:
1004 // (a) concrete integer indices that can easily be resolved
1005 // (b) 0 + symbolic index
1006 if (Base) {
1007 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
1008 // FIXME: Should use SValuator here.
1009 SVal NewIdx =
1010 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001011 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +00001012 const MemRegion* NewER =
1013 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
1014 ER->getSuperRegion(), getContext());
1015 return ValMgr.makeLoc(NewER);
1016 }
1017 if (0 == Base->getValue()) {
1018 const MemRegion* NewER =
1019 MRMgr.getElementRegion(ER->getElementType(), R,
1020 ER->getSuperRegion(), getContext());
1021 return ValMgr.makeLoc(NewER);
1022 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +00001023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Ted Kremenek4c8a5812009-03-03 02:51:43 +00001025 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +00001026}
1027
Ted Kremenek4533a552009-06-16 22:36:44 +00001028//===----------------------------------------------------------------------===//
1029// Loading values from regions.
1030//===----------------------------------------------------------------------===//
1031
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001032Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001033 const MemRegion *R) {
1034 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
1035 return *V;
1036
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001037 return Optional<SVal>();
1038}
1039
1040Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001041 const MemRegion *R) {
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001042 if (R->isBoundable())
1043 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
1044 if (TR->getValueType(getContext())->isUnionType())
1045 return UnknownVal();
1046
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001047 if (const SVal *V = Lookup(B, R, BindingKey::Default))
1048 return *V;
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001049
1050 return Optional<SVal>();
1051}
1052
1053Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
1054 const MemRegion *R) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001055
1056 if (Optional<SVal> V = getDirectBinding(B, R))
1057 return V;
1058
1059 return getDefaultBinding(B, R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001060}
1061
Ted Kremeneke6fea682009-07-15 02:31:43 +00001062static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1063 RTy = Ctx.getCanonicalType(RTy);
1064 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump11289f42009-09-09 15:08:12 +00001065
Ted Kremeneke6fea682009-07-15 02:31:43 +00001066 if (RTy == UsedTy)
1067 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001068
1069
Ted Kremenek834e2f62009-07-20 22:58:02 +00001070 // Recursively check the types. We basically want to see if a pointer value
Mike Stump11289f42009-09-09 15:08:12 +00001071 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek834e2f62009-07-20 22:58:02 +00001072 // represents a reinterpretation.
1073 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump11289f42009-09-09 15:08:12 +00001074 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001075 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek834e2f62009-07-20 22:58:02 +00001076
1077 return PUsedTy && PRTy &&
1078 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump11289f42009-09-09 15:08:12 +00001079 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek834e2f62009-07-20 22:58:02 +00001080 }
1081
1082 return true;
Ted Kremeneke6fea682009-07-15 02:31:43 +00001083}
1084
Ted Kremenek267e45a2009-09-24 04:11:44 +00001085const ElementRegion *
1086RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1087 ASTContext &Ctx = getContext();
1088 SVal idx = ValMgr.makeZeroArrayIndex();
1089 assert(!T.isNull());
1090 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1091}
1092
1093
1094
Ted Kremenekac7c7242009-07-21 21:03:30 +00001095SValuator::CastResult
1096RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek609df302009-06-17 22:02:04 +00001097
Zhongxing Xu83aff702008-10-21 05:29:26 +00001098 assert(!isa<UnknownVal>(L) && "location unknown");
1099 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek9158fb72009-12-15 23:23:27 +00001100
Ted Kremenek2907ab72008-12-24 07:46:32 +00001101 // FIXME: Is this even possible? Shouldn't this be treated as a null
1102 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001103 if (isa<loc::ConcreteInt>(L))
Ted Kremenekac7c7242009-07-21 21:03:30 +00001104 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek9158fb72009-12-15 23:23:27 +00001105
Ted Kremenek609df302009-06-17 22:02:04 +00001106 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001107
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001108 // FIXME: return symbolic value for these cases.
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001109 // Example:
1110 // void f(int* p) { int x = *p; }
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001111 // char* p = alloca();
1112 // read(p);
1113 // c = *p;
Ted Kremenek0c37d192009-07-14 20:48:22 +00001114 if (isa<AllocaRegion>(MR))
Ted Kremenekac7c7242009-07-21 21:03:30 +00001115 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001116
Ted Kremenek267e45a2009-09-24 04:11:44 +00001117 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1118 MR = GetElementZeroRegion(SR, T);
Mike Stump11289f42009-09-09 15:08:12 +00001119
Ted Kremenek0bb32e32009-08-03 21:41:46 +00001120 if (isa<CodeTextRegion>(MR))
1121 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001122
Ted Kremenek2907ab72008-12-24 07:46:32 +00001123 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1124 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +00001125 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001126 QualType RTy = R->getValueType(getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001127
Ted Kremenek2907ab72008-12-24 07:46:32 +00001128 // FIXME: We should eventually handle funny addressing. e.g.:
1129 //
1130 // int x = ...;
1131 // int *p = &x;
1132 // char *q = (char*) p;
1133 // char c = *q; // returns the first byte of 'x'.
1134 //
1135 // Such funny addressing will occur due to layering of regions.
1136
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001137#if 0
Ted Kremeneke6fea682009-07-15 02:31:43 +00001138 ASTContext &Ctx = getContext();
1139 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001140 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1141 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001142 RTy = T;
Ted Kremenek57fa7e32009-07-15 04:23:32 +00001143 assert(Ctx.getCanonicalType(RTy) ==
1144 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump11289f42009-09-09 15:08:12 +00001145 }
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001146#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001147
Zhongxing Xuce270a62009-03-09 09:15:51 +00001148 if (RTy->isStructureType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001149 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump11289f42009-09-09 15:08:12 +00001150
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001151 // FIXME: Handle unions.
1152 if (RTy->isUnionType())
1153 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001154
1155 if (RTy->isArrayType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001156 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001157
Zhongxing Xuce270a62009-03-09 09:15:51 +00001158 // FIXME: handle Vector types.
1159 if (RTy->isVectorType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001160 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu0628f532009-06-28 14:16:39 +00001161
1162 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001163 return SValuator::CastResult(state,
Ted Kremenekbe909b52010-01-11 02:33:26 +00001164 CastRetrievedVal(RetrieveField(state, FR), FR,
1165 T, false));
Zhongxing Xu0628f532009-06-28 14:16:39 +00001166
Ted Kremenekbe909b52010-01-11 02:33:26 +00001167 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1168 // FIXME: Here we actually perform an implicit conversion from the loaded
1169 // value to the element type. Eventually we want to compose these values
1170 // more intelligently. For example, an 'element' can encompass multiple
1171 // bound regions (e.g., several bound bytes), or could be a subset of
1172 // a larger value.
Zhongxing Xu731f4622009-11-16 04:49:44 +00001173 return SValuator::CastResult(state,
Ted Kremenekbe909b52010-01-11 02:33:26 +00001174 CastRetrievedVal(RetrieveElement(state, ER),
1175 ER, T, false));
1176 }
Mike Stump11289f42009-09-09 15:08:12 +00001177
Ted Kremenekbe909b52010-01-11 02:33:26 +00001178 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1179 // FIXME: Here we actually perform an implicit conversion from the loaded
1180 // value to the ivar type. What we should model is stores to ivars
1181 // that blow past the extent of the ivar. If the address of the ivar is
1182 // reinterpretted, it is possible we stored a different value that could
1183 // fit within the ivar. Either we need to cast these when storing them
1184 // or reinterpret them lazily (as we do here).
Zhongxing Xu731f4622009-11-16 04:49:44 +00001185 return SValuator::CastResult(state,
Ted Kremenekbe909b52010-01-11 02:33:26 +00001186 CastRetrievedVal(RetrieveObjCIvar(state, IVR),
1187 IVR, T, false));
1188 }
Mike Stump11289f42009-09-09 15:08:12 +00001189
Ted Kremenekbe909b52010-01-11 02:33:26 +00001190 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1191 // FIXME: Here we actually perform an implicit conversion from the loaded
1192 // value to the variable type. What we should model is stores to variables
1193 // that blow past the extent of the variable. If the address of the
1194 // variable is reinterpretted, it is possible we stored a different value
1195 // that could fit within the variable. Either we need to cast these when
1196 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu731f4622009-11-16 04:49:44 +00001197 return SValuator::CastResult(state,
Ted Kremenekbe909b52010-01-11 02:33:26 +00001198 CastRetrievedVal(RetrieveVar(state, VR), VR, T,
1199 false));
1200 }
Ted Kremenek834e2f62009-07-20 22:58:02 +00001201
Ted Kremenek2c85f172009-08-06 04:50:20 +00001202 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001203 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001204
1205 // Check if the region has a binding.
1206 if (V)
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001207 return SValuator::CastResult(state, *V);
Ted Kremenek2907ab72008-12-24 07:46:32 +00001208
Ted Kremenek2907ab72008-12-24 07:46:32 +00001209 // The location does not have a bound value. This means that it has
1210 // the value it had upon its creation and/or entry to the analyzed
1211 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekacd71a42010-01-05 02:18:06 +00001212 if (R->hasStackNonParametersStorage()) {
Ted Kremenek2907ab72008-12-24 07:46:32 +00001213 // All stack variables are considered to have undefined values
1214 // upon creation. All heap allocated blocks are considered to
1215 // have undefined values as well unless they are explicitly bound
1216 // to specific values.
Ted Kremenekac7c7242009-07-21 21:03:30 +00001217 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek2907ab72008-12-24 07:46:32 +00001218 }
1219
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001220 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001221 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(R, RTy));
Zhongxing Xu83aff702008-10-21 05:29:26 +00001222}
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenekfa417142009-08-06 01:20:57 +00001224std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001225RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001226 if (Optional<SVal> OV = getDirectBinding(B, R))
1227 if (const nonloc::LazyCompoundVal *V =
1228 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1229 return std::make_pair(V->getState(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001230
Ted Kremenekfa417142009-08-06 01:20:57 +00001231 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1232 const std::pair<const GRState *, const MemRegion *> &X =
1233 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001234
Ted Kremenekfa417142009-08-06 01:20:57 +00001235 if (X.first)
1236 return std::make_pair(X.first,
1237 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001238 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001239 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1240 const std::pair<const GRState *, const MemRegion *> &X =
1241 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001242
Ted Kremenekfa417142009-08-06 01:20:57 +00001243 if (X.first)
1244 return std::make_pair(X.first,
1245 MRMgr.getFieldRegionWithSuper(FR, X.second));
1246 }
1247
1248 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1249}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001250
Zhongxing Xu2d160732009-06-25 05:29:39 +00001251SVal RegionStoreManager::RetrieveElement(const GRState* state,
1252 const ElementRegion* R) {
1253 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001254 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekbe909b52010-01-11 02:33:26 +00001255 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001256 return *V;
1257
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001258 const MemRegion* superR = R->getSuperRegion();
1259
Zhongxing Xu2d160732009-06-25 05:29:39 +00001260 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001261 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek228539f2009-09-29 16:36:48 +00001262 // FIXME: Handle loads from strings where the literal is treated as
1263 // an integer, e.g., *((unsigned int*)"hello")
1264 ASTContext &Ctx = getContext();
Douglas Gregor4ef1d402009-11-09 22:08:55 +00001265 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001266 if (T != Ctx.getCanonicalType(R->getElementType()))
1267 return UnknownVal();
1268
Zhongxing Xu2d160732009-06-25 05:29:39 +00001269 const StringLiteral *Str = StrR->getStringLiteral();
1270 SVal Idx = R->getIndex();
1271 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1272 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001273 int64_t byteLength = Str->getByteLength();
Ted Kremenekb5850f92009-09-05 17:59:01 +00001274 if (i > byteLength) {
1275 // Buffer overflow checking in GRExprEngine should handle this case,
1276 // but we shouldn't rely on it to not overflow here if that checking
1277 // is disabled.
1278 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001279 }
Ted Kremenekb5850f92009-09-05 17:59:01 +00001280 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001281 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001282 }
1283 }
Mike Stump11289f42009-09-09 15:08:12 +00001284
Ted Kremenek040e3b92009-08-06 22:33:36 +00001285 // Check if the immediate super region has a direct binding.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001286 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneke6fea682009-07-15 02:31:43 +00001287 if (SymbolRef parentSym = V->getAsSymbol())
1288 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek920ad712009-07-22 04:35:42 +00001289
1290 if (V->isUnknownOrUndef())
1291 return *V;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001292
1293 // Handle LazyCompoundVals for the immediate super region. Other cases
1294 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump11289f42009-09-09 15:08:12 +00001295 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek040e3b92009-08-06 22:33:36 +00001296 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump11289f42009-09-09 15:08:12 +00001297
Ted Kremenek040e3b92009-08-06 22:33:36 +00001298 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1299 return RetrieveElement(LCV->getState(), R);
1300 }
Mike Stump11289f42009-09-09 15:08:12 +00001301
Ted Kremeneke6fea682009-07-15 02:31:43 +00001302 // Other cases: give up.
Zhongxing Xu61e66922009-07-03 06:11:41 +00001303 return UnknownVal();
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001304 }
Ted Kremenekbe909b52010-01-11 02:33:26 +00001305
Ted Kremenek040e3b92009-08-06 22:33:36 +00001306 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001307}
1308
Mike Stump11289f42009-09-09 15:08:12 +00001309SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001310 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001311
1312 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001313 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001314 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001315 return *V;
1316
Ted Kremenek040e3b92009-08-06 22:33:36 +00001317 QualType Ty = R->getValueType(getContext());
1318 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1319}
Mike Stump11289f42009-09-09 15:08:12 +00001320
Ted Kremenek040e3b92009-08-06 22:33:36 +00001321SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1322 const TypedRegion *R,
1323 QualType Ty,
1324 const MemRegion *superR) {
1325
Mike Stump11289f42009-09-09 15:08:12 +00001326 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001327 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001328
Ted Kremenek040e3b92009-08-06 22:33:36 +00001329 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001330
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001331 while (superR) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001332 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001333 if (SymbolRef parentSym = D->getAsSymbol())
1334 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001335
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001336 if (D->isZeroConstant())
1337 return ValMgr.makeZeroVal(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001338
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001339 if (D->isUnknown())
1340 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001341
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001342 assert(0 && "Unknown default value");
1343 }
Mike Stump11289f42009-09-09 15:08:12 +00001344
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001345 // If our super region is a field or element itself, walk up the region
1346 // hierarchy to see if there is a default value installed in an ancestor.
1347 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1348 superR = cast<SubRegion>(superR)->getSuperRegion();
1349 continue;
1350 }
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001352 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001353 }
Mike Stump11289f42009-09-09 15:08:12 +00001354
Ted Kremenekfa417142009-08-06 01:20:57 +00001355 // Lazy binding?
1356 const GRState *lazyBindingState = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001357 const MemRegion *lazyBindingRegion = NULL;
1358 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Ted Kremenekfa417142009-08-06 01:20:57 +00001360 if (lazyBindingState) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001361 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump11289f42009-09-09 15:08:12 +00001362
Ted Kremenek040e3b92009-08-06 22:33:36 +00001363 if (isa<ElementRegion>(R))
1364 return RetrieveElement(lazyBindingState,
1365 cast<ElementRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001366
Ted Kremenekfa417142009-08-06 01:20:57 +00001367 return RetrieveField(lazyBindingState,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001368 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001369 }
1370
Ted Kremenekacd71a42010-01-05 02:18:06 +00001371 if (R->hasStackNonParametersStorage()) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001372 if (isa<ElementRegion>(R)) {
1373 // Currently we don't reason specially about Clang-style vectors. Check
1374 // if superR is a vector and if so return Unknown.
1375 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1376 if (typedSuperR->getValueType(getContext())->isVectorType())
1377 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001378 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001379 }
Mike Stump11289f42009-09-09 15:08:12 +00001380
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001381 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001384 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001385 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001386}
Mike Stump11289f42009-09-09 15:08:12 +00001387
1388SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek48029552009-07-15 06:09:28 +00001389 const ObjCIvarRegion* R) {
1390
Ted Kremenek48029552009-07-15 06:09:28 +00001391 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001392 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek48029552009-07-15 06:09:28 +00001393
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001394 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001395 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001396
Ted Kremenek48029552009-07-15 06:09:28 +00001397 const MemRegion *superR = R->getSuperRegion();
1398
Ted Kremenek481c1212009-10-20 01:20:57 +00001399 // Check if the super region has a default binding.
1400 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001401 if (SymbolRef parentSym = V->getAsSymbol())
1402 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001403
Ted Kremenek48029552009-07-15 06:09:28 +00001404 // Other cases: give up.
1405 return UnknownVal();
1406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Ted Kremenek834e2f62009-07-20 22:58:02 +00001408 return RetrieveLazySymbol(state, R);
1409}
1410
Ted Kremenekfe12f882009-07-21 00:12:07 +00001411SVal RegionStoreManager::RetrieveVar(const GRState *state,
1412 const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001413
Ted Kremenekfe12f882009-07-21 00:12:07 +00001414 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001415 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001416
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001417 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001418 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001419
Ted Kremenekfe12f882009-07-21 00:12:07 +00001420 // Lazily derive a value for the VarRegion.
1421 const VarDecl *VD = R->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001422
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +00001423 if (R->hasGlobalsOrParametersStorage() ||
1424 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek9158fb72009-12-15 23:23:27 +00001425 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001426
Ted Kremenekfe12f882009-07-21 00:12:07 +00001427 return UndefinedVal();
1428}
1429
Mike Stump11289f42009-09-09 15:08:12 +00001430SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek834e2f62009-07-20 22:58:02 +00001431 const TypedRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001432
Ted Kremenek834e2f62009-07-20 22:58:02 +00001433 QualType valTy = R->getValueType(getContext());
Ted Kremenek920ad712009-07-22 04:35:42 +00001434
Ted Kremenek48029552009-07-15 06:09:28 +00001435 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001436 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek48029552009-07-15 06:09:28 +00001437}
1438
Mike Stump11289f42009-09-09 15:08:12 +00001439SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1440 const TypedRegion* R) {
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001441 QualType T = R->getValueType(getContext());
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001442 assert(T->isStructureType());
1443
Zhongxing Xud85a9912009-06-11 07:27:30 +00001444 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001445 RecordDecl* RD = RT->getDecl();
1446 assert(RD->isDefinition());
Mike Stumpc700b362009-08-06 12:56:50 +00001447 (void)RD;
Ted Kremenekfa417142009-08-06 01:20:57 +00001448#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001449 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1450
Ted Kremenek609df302009-06-17 22:02:04 +00001451 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1452 // reverse iterator, we should implement one.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001453 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001454
Douglas Gregor7a4fad12008-12-11 20:41:00 +00001455 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1456 FieldEnd = Fields.rend();
1457 Field != FieldEnd; ++Field) {
1458 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001459 QualType FTy = (*Field)->getType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001460 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001461 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1462 }
1463
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001464 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001465#else
1466 return ValMgr.makeLazyCompoundVal(state, R);
1467#endif
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001468}
1469
Ted Kremenek609df302009-06-17 22:02:04 +00001470SVal RegionStoreManager::RetrieveArray(const GRState *state,
1471 const TypedRegion * R) {
Ted Kremenekfa417142009-08-06 01:20:57 +00001472#if USE_EXPLICIT_COMPOUND
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001473 QualType T = R->getValueType(getContext());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001474 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1475
1476 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001477 uint64_t size = CAT->getSize().getZExtValue();
1478 for (uint64_t i = 0; i < size; ++i) {
1479 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu838a0db2009-06-16 09:55:50 +00001480 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump11289f42009-09-09 15:08:12 +00001481 getContext());
Ted Kremenek02e50892009-05-04 06:18:28 +00001482 QualType ETy = ER->getElementType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001483 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001484 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1485 }
1486
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001487 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001488#else
1489 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1490 return ValMgr.makeLazyCompoundVal(state, R);
1491#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001492}
1493
Ted Kremenek4533a552009-06-16 22:36:44 +00001494//===----------------------------------------------------------------------===//
1495// Binding values to regions.
1496//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001497
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001498Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001499 if (isa<loc::MemRegionVal>(L))
Ted Kremenekbe909b52010-01-11 02:33:26 +00001500 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001501 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump11289f42009-09-09 15:08:12 +00001502
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001503 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001504}
1505
Ted Kremenek609df302009-06-17 22:02:04 +00001506const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001507 if (isa<loc::ConcreteInt>(L))
1508 return state;
1509
Ted Kremenek4533a552009-06-16 22:36:44 +00001510 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001511 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001512
Ted Kremenek4533a552009-06-16 22:36:44 +00001513 // Check if the region is a struct region.
1514 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1515 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001516 return BindStruct(state, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001517
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001518 // Special case: the current region represents a cast and it and the super
1519 // region both have pointer types or intptr_t types. If so, perform the
1520 // bind to the super region.
1521 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump11289f42009-09-09 15:08:12 +00001522 // loads that treat integers as pointers and vis versa.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001523 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1524 if (ER->getIndex().isZeroConstant()) {
1525 if (const TypedRegion *superR =
1526 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1527 ASTContext &Ctx = getContext();
1528 QualType superTy = superR->getValueType(Ctx);
1529 QualType erTy = ER->getValueType(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001530
1531 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001532 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump11289f42009-09-09 15:08:12 +00001533 SValuator::CastResult cr =
1534 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001535 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1536 }
Ted Kremenek25c9c142009-09-21 22:58:52 +00001537 // For now, just invalidate the fields of the struct/union/class.
1538 // FIXME: Precisely handle the fields of the record.
1539 if (superTy->isRecordType())
Ted Kremenek1eb68092009-10-16 00:30:49 +00001540 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001541 }
1542 }
1543 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001544 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1545 // Binding directly to a symbolic region should be treated as binding
1546 // to element 0.
1547 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001548
1549 // FIXME: Is this the right way to handle symbols that are references?
1550 if (const PointerType *PT = T->getAs<PointerType>())
1551 T = PT->getPointeeType();
1552 else
1553 T = T->getAs<ReferenceType>()->getPointeeType();
1554
Ted Kremenek267e45a2009-09-24 04:11:44 +00001555 R = GetElementZeroRegion(SR, T);
1556 }
Mike Stump11289f42009-09-09 15:08:12 +00001557
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001558 // Perform the binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001559 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001560 return state->makeWithStore(Add(B, R, BindingKey::Direct, V).getRoot());
Ted Kremenek4533a552009-06-16 22:36:44 +00001561}
1562
Ted Kremenek14536f62009-08-21 22:28:32 +00001563const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekb006b822009-11-04 00:09:15 +00001564 const VarRegion *VR,
Ted Kremenek14536f62009-08-21 22:28:32 +00001565 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001566
Ted Kremenekb006b822009-11-04 00:09:15 +00001567 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001568
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001569 if (T->isArrayType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001570 return BindArray(ST, VR, InitVal);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001571 if (T->isStructureType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001572 return BindStruct(ST, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001573
Ted Kremenek14536f62009-08-21 22:28:32 +00001574 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001575}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001576
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001577// FIXME: this method should be merged into Bind().
Ted Kremenek609df302009-06-17 22:02:04 +00001578const GRState *
1579RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek04af9f22009-12-07 22:05:27 +00001580 const CompoundLiteralExpr *CL,
1581 const LocationContext *LC,
Ted Kremenek609df302009-06-17 22:02:04 +00001582 SVal V) {
Ted Kremenek04af9f22009-12-07 22:05:27 +00001583 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1584 V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001585}
1586
Ted Kremenek439a6d12009-11-19 20:20:24 +00001587const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1588 const MemRegion *R,
1589 QualType T) {
1590 Store store = state->getStore();
1591 RegionBindings B = GetRegionBindings(store);
1592 SVal V;
1593
1594 if (Loc::IsLocType(T))
1595 V = ValMgr.makeNull();
1596 else if (T->isIntegerType())
1597 V = ValMgr.makeZeroVal(T);
1598 else if (T->isStructureType() || T->isArrayType()) {
1599 // Set the default value to a zero constant when it is a structure
1600 // or array. The type doesn't really matter.
1601 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1602 }
1603 else {
1604 return state;
1605 }
Ted Kremenek8e994a22010-01-11 00:07:44 +00001606
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001607 return state->makeWithStore(Add(B, R, BindingKey::Default, V).getRoot());
Ted Kremenek439a6d12009-11-19 20:20:24 +00001608}
1609
Ted Kremenek609df302009-06-17 22:02:04 +00001610const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001611 const TypedRegion* R,
Ted Kremenek609df302009-06-17 22:02:04 +00001612 SVal Init) {
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001613
1614 ASTContext &Ctx = getContext();
1615 const ArrayType *AT =
1616 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1617 QualType ElementTy = AT->getElementType();
1618 Optional<uint64_t> Size;
1619
1620 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1621 Size = CAT->getSize().getZExtValue();
1622
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001623 // Check if the init expr is a StringLiteral.
1624 if (isa<loc::MemRegionVal>(Init)) {
1625 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1626 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1627 const char* str = S->getStrData();
1628 unsigned len = S->getByteLength();
1629 unsigned j = 0;
1630
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001631 // Copy bytes from the string literal into the target array. Trailing bytes
1632 // in the array that are not covered by the string literal are initialized
1633 // to zero.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001634
1635 // We assume that string constants are bound to
1636 // constant arrays.
Ted Kremenek968999b2010-01-27 16:31:37 +00001637 uint64_t size = Size.getValue();
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001638
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001639 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001640 if (j >= len)
1641 break;
1642
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001643 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001644 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1645 getContext());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001646
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001647 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek609df302009-06-17 22:02:04 +00001648 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001649 }
1650
Ted Kremenek609df302009-06-17 22:02:04 +00001651 return state;
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001652 }
1653
Ted Kremenekfa417142009-08-06 01:20:57 +00001654 // Handle lazy compound values.
1655 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1656 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001657
1658 // Remaining case: explicit compound values.
Ted Kremenek439a6d12009-11-19 20:20:24 +00001659
1660 if (Init.isUnknown())
1661 return setImplicitDefaultValue(state, R, ElementTy);
1662
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001663 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001664 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001665 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001666
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001667 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001668 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001669 if (VI == VE)
1670 break;
1671
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001672 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001673 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001674
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001675 if (ElementTy->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001676 state = BindStruct(state, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001677 else
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001678 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001679 }
1680
Ted Kremenek439a6d12009-11-19 20:20:24 +00001681 // If the init list is shorter than the array length, set the
1682 // array default value.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001683 if (Size.hasValue() && i < Size.getValue())
Ted Kremenek439a6d12009-11-19 20:20:24 +00001684 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001685
Ted Kremenek609df302009-06-17 22:02:04 +00001686 return state;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001687}
1688
Ted Kremenek609df302009-06-17 22:02:04 +00001689const GRState *
1690RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1691 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001692
Ted Kremenek609df302009-06-17 22:02:04 +00001693 if (!Features.supportsFields())
1694 return state;
Mike Stump11289f42009-09-09 15:08:12 +00001695
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001696 QualType T = R->getValueType(getContext());
Zhongxing Xub393b502008-10-31 10:53:01 +00001697 assert(T->isStructureType());
1698
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001699 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001700 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001701
1702 if (!RD->isDefinition())
Ted Kremenek609df302009-06-17 22:02:04 +00001703 return state;
Zhongxing Xub393b502008-10-31 10:53:01 +00001704
Ted Kremenekfa417142009-08-06 01:20:57 +00001705 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001706 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremenekfa417142009-08-06 01:20:57 +00001707 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001708
Ted Kremenek609df302009-06-17 22:02:04 +00001709 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1710 // Ignore them and kill the field values.
1711 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001712 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001713
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001714 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001715 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001716
1717 RecordDecl::field_iterator FI, FE;
1718
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001719 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001720
Zhongxing Xu0442e962009-06-23 05:43:16 +00001721 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001722 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001723
Zhongxing Xub393b502008-10-31 10:53:01 +00001724 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001725 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001726
Ted Kremenek30030012009-09-22 21:19:14 +00001727 if (FTy->isArrayType())
Ted Kremenek609df302009-06-17 22:02:04 +00001728 state = BindArray(state, FR, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001729 else if (FTy->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001730 state = BindStruct(state, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001731 else
1732 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001733 }
1734
Zhongxing Xu0442e962009-06-23 05:43:16 +00001735 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001736 if (FI != FE) {
1737 Store store = state->getStore();
1738 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001739 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001740 state = state->makeWithStore(B.getRoot());
1741 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001742
Ted Kremenek609df302009-06-17 22:02:04 +00001743 return state;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001744}
1745
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001746Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1747 RegionBindings B = GetRegionBindings(store);
1748 llvm::OwningPtr<RegionStoreSubRegionMap>
1749 SubRegions(getRegionStoreSubRegionMap(store));
1750 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001751
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001752 // Set the default value of the struct region to "unknown".
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001753 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001754}
1755
Ted Kremenekfa417142009-08-06 01:20:57 +00001756const GRState*
1757RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1758 const GRState *state,
1759 const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001760
Ted Kremenekfa417142009-08-06 01:20:57 +00001761 // Nuke the old bindings stemming from R.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001762 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekfa417142009-08-06 01:20:57 +00001763
Mike Stump11289f42009-09-09 15:08:12 +00001764 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001765 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenekfa417142009-08-06 01:20:57 +00001766
Mike Stump11289f42009-09-09 15:08:12 +00001767 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001768 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001769
Ted Kremenekfa417142009-08-06 01:20:57 +00001770 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1771 // results in a zero-copy algorithm.
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001772 return state->makeWithStore(Add(B, R, BindingKey::Direct, V).getRoot());
Ted Kremenek8e994a22010-01-11 00:07:44 +00001773}
1774
1775//===----------------------------------------------------------------------===//
1776// "Raw" retrievals and bindings.
1777//===----------------------------------------------------------------------===//
1778
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001779BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekbe909b52010-01-11 02:33:26 +00001780 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1781 const RegionRawOffset &O = ER->getAsRawOffset();
1782
1783 if (O.getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001784 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001785
1786 // FIXME: There are some ElementRegions for which we cannot compute
1787 // raw offsets yet, including regions with symbolic offsets.
1788 }
1789
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001790 return BindingKey(R, 0, k);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001791}
1792
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001793RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001794 return RBFactory.Add(B, K, V);
1795}
1796
1797RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001798 BindingKey::Kind k, SVal V) {
1799 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek8e994a22010-01-11 00:07:44 +00001800}
1801
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001802const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001803 return B.lookup(K);
1804}
1805
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001806const SVal *RegionStoreManager::Lookup(RegionBindings B,
1807 const MemRegion *R,
1808 BindingKey::Kind k) {
1809 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001810}
1811
1812RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1813 return RBFactory.Remove(B, K);
1814}
1815
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001816RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1817 BindingKey::Kind k){
1818 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001819}
1820
1821Store RegionStoreManager::Remove(Store store, BindingKey K) {
1822 RegionBindings B = GetRegionBindings(store);
1823 return Remove(B, K).getRoot();
Ted Kremenekfa417142009-08-06 01:20:57 +00001824}
Mike Stump11289f42009-09-09 15:08:12 +00001825
Ted Kremenek4533a552009-06-16 22:36:44 +00001826//===----------------------------------------------------------------------===//
1827// State pruning.
1828//===----------------------------------------------------------------------===//
Ted Kremenekcc224242009-09-29 06:35:00 +00001829
Mike Stump11289f42009-09-09 15:08:12 +00001830void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenekcee28a42009-08-02 04:45:08 +00001831 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001832 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001833{
Ted Kremenek9f3a6432009-10-17 17:45:11 +00001834 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1835
Ted Kremenekcee28a42009-08-02 04:45:08 +00001836 Store store = state.getStore();
Ted Kremenek2c85f172009-08-06 04:50:20 +00001837 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001838
Ted Kremenek4533a552009-06-16 22:36:44 +00001839 // The backmap from regions to subregions.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001840 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001841 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekcc224242009-09-29 06:35:00 +00001842
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001843 // Do a pass over the regions in the store. For VarRegions we check if
1844 // the variable is still live and if so add it to the list of live roots.
1845 // For other regions we populate our region backmap.
Ted Kremenek4533a552009-06-16 22:36:44 +00001846 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenekcc224242009-09-29 06:35:00 +00001847
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001848 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001849 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00001850 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001851 IntermediateRoots.push_back(R);
Ted Kremenek4533a552009-06-16 22:36:44 +00001852 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001853
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001854 // Process the "intermediate" roots to find if they are referenced by
Mike Stump11289f42009-09-09 15:08:12 +00001855 // real roots.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001856 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001857 llvm::SmallVector<RBDNode, 10> Postponed;
1858
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001859 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001860
Ted Kremenek4533a552009-06-16 22:36:44 +00001861 while (!IntermediateRoots.empty()) {
1862 const MemRegion* R = IntermediateRoots.back();
1863 IntermediateRoots.pop_back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001864
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001865 if (IntermediateVisited.count(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001866 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001867 IntermediateVisited.insert(R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001868
Ted Kremenek4533a552009-06-16 22:36:44 +00001869 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +00001870 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001871 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001872 continue;
1873 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001874
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001875 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001876 llvm::SmallVectorImpl<RBDNode> &Q =
1877 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1878
1879 Q.push_back(std::make_pair(&state, SR));
1880
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001881 continue;
Ted Kremenek4533a552009-06-16 22:36:44 +00001882 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001883
1884 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001885 if (const SubRegion* superR =
Ted Kremenekcc224242009-09-29 06:35:00 +00001886 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001887 IntermediateRoots.push_back(superR);
Ted Kremenek4533a552009-06-16 22:36:44 +00001888 }
Mike Stump11289f42009-09-09 15:08:12 +00001889
Ted Kremenekcc224242009-09-29 06:35:00 +00001890 // Enqueue the RegionRoots onto WorkList.
1891 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1892 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001893 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump11289f42009-09-09 15:08:12 +00001894 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001895 RegionRoots.clear();
1896
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001897 llvm::DenseSet<RBDNode> Visited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001898
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001899tryAgain:
Ted Kremenekcc224242009-09-29 06:35:00 +00001900 while (!WorkList.empty()) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001901 RBDNode N = WorkList.back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001902 WorkList.pop_back();
1903
1904 // Have we visited this node before?
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001905 if (Visited.count(N))
Ted Kremenekcc224242009-09-29 06:35:00 +00001906 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001907 Visited.insert(N);
Mike Stump11289f42009-09-09 15:08:12 +00001908
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001909 const MemRegion *R = N.second;
1910 const GRState *state_N = N.first;
Ted Kremenekcc224242009-09-29 06:35:00 +00001911
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001912 // Enqueue subregions.
1913 RegionStoreSubRegionMap *M;
1914
1915 if (&state == state_N)
1916 M = SubRegions.get();
1917 else {
1918 RegionStoreSubRegionMap *& SM = SC[state_N];
1919 if (!SM)
1920 SM = getRegionStoreSubRegionMap(state_N->getStore());
1921 M = SM;
1922 }
Ted Kremenekb251eb62010-02-02 22:38:47 +00001923
1924 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1925 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1926 I != E; ++I)
1927 WorkList.push_back(std::make_pair(state_N, *I));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001928
Ted Kremenekcc224242009-09-29 06:35:00 +00001929 // Enqueue the super region.
1930 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1931 const MemRegion *superR = SR->getSuperRegion();
1932 if (!isa<MemSpaceRegion>(superR)) {
1933 // If 'R' is a field or an element, we want to keep the bindings
1934 // for the other fields and elements around. The reason is that
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001935 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8b2f5d32009-10-17 07:32:08 +00001936 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1937 || isa<ObjCIvarRegion>(R));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001938 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenekcc224242009-09-29 06:35:00 +00001939 }
1940 }
1941
1942 // Mark the symbol for any live SymbolicRegion as "live". This means we
1943 // should continue to track that symbol.
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001944 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001945 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001946
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001947 // For BlockDataRegions, enqueue the VarRegions for variables marked
1948 // with __block (passed-by-reference).
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001949 // via BlockDeclRefExprs.
1950 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1951 for (BlockDataRegion::referenced_vars_iterator
1952 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001953 RI != RE; ++RI) {
1954 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1955 WorkList.push_back(std::make_pair(state_N, *RI));
1956 }
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001957 // No possible data bindings on a BlockDataRegion. Continue to the
1958 // next region in the worklist.
1959 continue;
1960 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001961
1962 Store store_N = state_N->getStore();
1963 RegionBindings B_N = GetRegionBindings(store_N);
1964
1965 // Get the data binding for R (if any).
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001966 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001967
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001968 if (V) {
1969 // Check for lazy bindings.
1970 if (const nonloc::LazyCompoundVal *LCV =
1971 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001972
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001973 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001974 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001975 }
1976 else {
Ted Kremenekcc224242009-09-29 06:35:00 +00001977 // Update the set of live symbols.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001978 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenekcc224242009-09-29 06:35:00 +00001979 SI!=SE;++SI)
1980 SymReaper.markLive(*SI);
1981
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001982 // If V is a region, then add it to the worklist.
1983 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001984 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenekcc224242009-09-29 06:35:00 +00001985 }
1986 }
1987 }
1988
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001989 // See if any postponed SymbolicRegions are actually live now, after
1990 // having done a scan.
1991 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1992 E = Postponed.end() ; I != E ; ++I) {
1993 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1994 if (SymReaper.isLive(SR->getSymbol())) {
1995 WorkList.push_back(*I);
1996 I->second = NULL;
1997 }
1998 }
1999 }
2000
2001 if (!WorkList.empty())
2002 goto tryAgain;
2003
Ted Kremenek4533a552009-06-16 22:36:44 +00002004 // We have now scanned the store, marking reachable regions and symbols
2005 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00002006 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek2c85f172009-08-06 04:50:20 +00002007 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek8e994a22010-01-11 00:07:44 +00002008 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek4533a552009-06-16 22:36:44 +00002009 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00002010 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek4533a552009-06-16 22:36:44 +00002011 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002012
Ted Kremenek4533a552009-06-16 22:36:44 +00002013 // Remove this dead region from the store.
Ted Kremenek8e994a22010-01-11 00:07:44 +00002014 store = Remove(store, I.getKey());
Mike Stump11289f42009-09-09 15:08:12 +00002015
Ted Kremenek4533a552009-06-16 22:36:44 +00002016 // Mark all non-live symbols that this region references as dead.
2017 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
2018 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00002019
Ted Kremenek64efd0d2010-02-03 03:06:46 +00002020 SVal X = I.getData();
Ted Kremenekf106ab92009-08-02 05:00:15 +00002021 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
2022 for (; SI != SE; ++SI)
2023 SymReaper.maybeDead(*SI);
2024 }
Mike Stump11289f42009-09-09 15:08:12 +00002025
Ted Kremenekcee28a42009-08-02 04:45:08 +00002026 // Write the store back.
2027 state.setStore(store);
Ted Kremenek4533a552009-06-16 22:36:44 +00002028}
2029
Zhongxing Xudaa41762009-10-13 02:24:55 +00002030GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
2031 StackFrameContext const *frame) {
2032 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
2033 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
2034
2035 FunctionDecl::param_const_iterator PI = FD->param_begin();
2036
2037 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
2038
2039 // Copy the arg expression value to the arg variables.
2040 for (; AI != AE; ++AI, ++PI) {
2041 SVal ArgVal = state->getSVal(*AI);
Ted Kremenek721fcc02009-12-04 00:26:31 +00002042 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xudaa41762009-10-13 02:24:55 +00002043 }
2044
2045 return state;
2046}
2047
Ted Kremenek4533a552009-06-16 22:36:44 +00002048//===----------------------------------------------------------------------===//
2049// Utility methods.
2050//===----------------------------------------------------------------------===//
2051
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002052void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00002053 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00002054 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00002055 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002056
Ted Kremenek2c85f172009-08-06 04:50:20 +00002057 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002058 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00002059}