blob: 4414c1a57a6d387b7e50b20c86d73b97fbcdc510 [file] [log] [blame]
Ted Kremenek9e240492008-10-04 05:50:14 +00001//== MemRegion.cpp - Abstract memory regions for static analysis --*- 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 MemRegion and its subclasses. MemRegion defines a
11// partially-typed abstraction of memory useful for path-sensitive dataflow
12// analyses.
13//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/MemRegion.h"
Jordy Rose32f26562010-07-04 00:00:41 +000017#include "clang/Checker/PathSensitive/ValueManager.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000018#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Analysis/Support/BumpVector.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000020#include "clang/AST/CharUnits.h"
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000021#include "clang/AST/RecordLayout.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek9e240492008-10-04 05:50:14 +000023
24using namespace clang;
25
Ted Kremenek25010132009-06-22 23:13:13 +000026//===----------------------------------------------------------------------===//
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000027// MemRegion Construction.
28//===----------------------------------------------------------------------===//
29
30template<typename RegionTy> struct MemRegionManagerTrait;
31
32template <typename RegionTy, typename A1>
33RegionTy* MemRegionManager::getRegion(const A1 a1) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000034
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000035 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
36 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000037
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000038 llvm::FoldingSetNodeID ID;
39 RegionTy::ProfileRegion(ID, a1, superRegion);
40 void* InsertPos;
41 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
42 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000043
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000044 if (!R) {
45 R = (RegionTy*) A.Allocate<RegionTy>();
46 new (R) RegionTy(a1, superRegion);
47 Regions.InsertNode(R, InsertPos);
48 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000049
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000050 return R;
51}
52
53template <typename RegionTy, typename A1>
54RegionTy* MemRegionManager::getSubRegion(const A1 a1,
55 const MemRegion *superRegion) {
56 llvm::FoldingSetNodeID ID;
57 RegionTy::ProfileRegion(ID, a1, superRegion);
58 void* InsertPos;
59 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
60 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000061
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000062 if (!R) {
63 R = (RegionTy*) A.Allocate<RegionTy>();
64 new (R) RegionTy(a1, superRegion);
65 Regions.InsertNode(R, InsertPos);
66 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000067
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000068 return R;
69}
70
71template <typename RegionTy, typename A1, typename A2>
72RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000073
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000074 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
75 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000076
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000077 llvm::FoldingSetNodeID ID;
78 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
79 void* InsertPos;
80 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
81 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000082
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000083 if (!R) {
84 R = (RegionTy*) A.Allocate<RegionTy>();
85 new (R) RegionTy(a1, a2, superRegion);
86 Regions.InsertNode(R, InsertPos);
87 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000088
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000089 return R;
90}
91
92template <typename RegionTy, typename A1, typename A2>
93RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
94 const MemRegion *superRegion) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000095
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000096 llvm::FoldingSetNodeID ID;
97 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
98 void* InsertPos;
99 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
100 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000101
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000102 if (!R) {
103 R = (RegionTy*) A.Allocate<RegionTy>();
104 new (R) RegionTy(a1, a2, superRegion);
105 Regions.InsertNode(R, InsertPos);
106 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000107
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000108 return R;
109}
110
Ted Kremenek67d12872009-12-07 22:05:27 +0000111template <typename RegionTy, typename A1, typename A2, typename A3>
112RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3,
113 const MemRegion *superRegion) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000114
Ted Kremenek67d12872009-12-07 22:05:27 +0000115 llvm::FoldingSetNodeID ID;
116 RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion);
117 void* InsertPos;
118 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
119 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000120
Ted Kremenek67d12872009-12-07 22:05:27 +0000121 if (!R) {
122 R = (RegionTy*) A.Allocate<RegionTy>();
123 new (R) RegionTy(a1, a2, a3, superRegion);
124 Regions.InsertNode(R, InsertPos);
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000125 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000126
Ted Kremenek67d12872009-12-07 22:05:27 +0000127 return R;
128}
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000129
130//===----------------------------------------------------------------------===//
Ted Kremenek42400962009-11-26 02:34:36 +0000131// Object destruction.
Ted Kremenek25010132009-06-22 23:13:13 +0000132//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000133
134MemRegion::~MemRegion() {}
135
Ted Kremenek42400962009-11-26 02:34:36 +0000136MemRegionManager::~MemRegionManager() {
137 // All regions and their data are BumpPtrAllocated. No need to call
138 // their destructors.
139}
140
141//===----------------------------------------------------------------------===//
142// Basic methods.
143//===----------------------------------------------------------------------===//
144
Zhongxing Xu7e5d6ed2009-01-08 13:17:14 +0000145bool SubRegion::isSubRegionOf(const MemRegion* R) const {
146 const MemRegion* r = getSuperRegion();
147 while (r != 0) {
148 if (r == R)
149 return true;
150 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
151 r = sr->getSuperRegion();
152 else
153 break;
154 }
155 return false;
156}
157
Ted Kremeneka43484a2009-06-23 00:46:41 +0000158MemRegionManager* SubRegion::getMemRegionManager() const {
159 const SubRegion* r = this;
160 do {
161 const MemRegion *superRegion = r->getSuperRegion();
162 if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
163 r = sr;
164 continue;
165 }
166 return superRegion->getMemRegionManager();
167 } while (1);
168}
169
Ted Kremenek5348f942009-12-14 22:15:06 +0000170const StackFrameContext *VarRegion::getStackFrame() const {
171 const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
172 return SSR ? SSR->getStackFrame() : NULL;
173}
174
175//===----------------------------------------------------------------------===//
Jordy Rose32f26562010-07-04 00:00:41 +0000176// Region extents.
177//===----------------------------------------------------------------------===//
178
179DefinedOrUnknownSVal DeclRegion::getExtent(ValueManager& ValMgr) const {
180 ASTContext& Ctx = ValMgr.getContext();
Zhongxing Xu018220c2010-08-11 06:10:55 +0000181 QualType T = getDesugaredValueType();
Jordy Rose32f26562010-07-04 00:00:41 +0000182
Jordy Rose52e04c52010-07-05 00:50:15 +0000183 if (isa<VariableArrayType>(T))
184 return nonloc::SymbolVal(ValMgr.getSymbolManager().getExtentSymbol(this));
185 if (isa<IncompleteArrayType>(T))
Jordy Rose32f26562010-07-04 00:00:41 +0000186 return UnknownVal();
187
188 CharUnits Size = Ctx.getTypeSizeInChars(T);
189 QualType SizeTy = Ctx.getSizeType();
190 return ValMgr.makeIntVal(Size.getQuantity(), SizeTy);
191}
192
193DefinedOrUnknownSVal FieldRegion::getExtent(ValueManager& ValMgr) const {
194 DefinedOrUnknownSVal Extent = DeclRegion::getExtent(ValMgr);
195
196 // A zero-length array at the end of a struct often stands for dynamically-
197 // allocated extra memory.
198 if (Extent.isZeroConstant()) {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000199 QualType T = getDesugaredValueType();
Jordy Rose32f26562010-07-04 00:00:41 +0000200
201 if (isa<ConstantArrayType>(T))
202 return UnknownVal();
203 }
204
205 return Extent;
206}
207
208DefinedOrUnknownSVal AllocaRegion::getExtent(ValueManager& ValMgr) const {
209 return nonloc::SymbolVal(ValMgr.getSymbolManager().getExtentSymbol(this));
210}
211
212DefinedOrUnknownSVal SymbolicRegion::getExtent(ValueManager& ValMgr) const {
213 return nonloc::SymbolVal(ValMgr.getSymbolManager().getExtentSymbol(this));
214}
215
216DefinedOrUnknownSVal StringRegion::getExtent(ValueManager& ValMgr) const {
217 QualType SizeTy = ValMgr.getContext().getSizeType();
218 return ValMgr.makeIntVal(getStringLiteral()->getByteLength()+1, SizeTy);
219}
220
221//===----------------------------------------------------------------------===//
Ted Kremenek5348f942009-12-14 22:15:06 +0000222// FoldingSet profiling.
223//===----------------------------------------------------------------------===//
224
Ted Kremenek9e240492008-10-04 05:50:14 +0000225void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
226 ID.AddInteger((unsigned)getKind());
227}
228
Ted Kremenek67d12872009-12-07 22:05:27 +0000229void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
230 ID.AddInteger((unsigned)getKind());
231 ID.AddPointer(getStackFrame());
232}
233
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000234void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
235 ID.AddInteger((unsigned)getKind());
236 ID.AddPointer(getCodeRegion());
237}
238
Mike Stump1eb44332009-09-09 15:08:12 +0000239void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
240 const StringLiteral* Str,
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000241 const MemRegion* superRegion) {
242 ID.AddInteger((unsigned) StringRegionKind);
243 ID.AddPointer(Str);
244 ID.AddPointer(superRegion);
245}
246
Ted Kremenek7090ae12008-11-02 00:34:33 +0000247void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000248 const Expr* Ex, unsigned cnt,
249 const MemRegion *) {
Ted Kremenek7090ae12008-11-02 00:34:33 +0000250 ID.AddInteger((unsigned) AllocaRegionKind);
251 ID.AddPointer(Ex);
252 ID.AddInteger(cnt);
253}
254
255void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000256 ProfileRegion(ID, Ex, Cnt, superRegion);
Ted Kremenek7090ae12008-11-02 00:34:33 +0000257}
258
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000259void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
260 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
261}
262
263void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
264 const CompoundLiteralExpr* CL,
265 const MemRegion* superRegion) {
266 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
267 ID.AddPointer(CL);
268 ID.AddPointer(superRegion);
269}
270
Ted Kremenekde0d2632010-01-05 02:18:06 +0000271void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
272 const PointerType *PT,
273 const MemRegion *sRegion) {
274 ID.AddInteger((unsigned) CXXThisRegionKind);
275 ID.AddPointer(PT);
276 ID.AddPointer(sRegion);
277}
278
279void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
280 CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
281}
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000282
Ted Kremenek9e240492008-10-04 05:50:14 +0000283void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
284 const MemRegion* superRegion, Kind k) {
285 ID.AddInteger((unsigned) k);
286 ID.AddPointer(D);
287 ID.AddPointer(superRegion);
288}
289
290void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
291 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
292}
293
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000294void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000295 VarRegion::ProfileRegion(ID, getDecl(), superRegion);
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000296}
297
Ted Kremenek25010132009-06-22 23:13:13 +0000298void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
299 const MemRegion *sreg) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000300 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremenek6d0e2d22008-12-05 02:39:38 +0000301 ID.Add(sym);
Ted Kremenek25010132009-06-22 23:13:13 +0000302 ID.AddPointer(sreg);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000303}
304
305void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek25010132009-06-22 23:13:13 +0000306 SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000307}
308
Ted Kremenekf936f452009-05-04 06:18:28 +0000309void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Mike Stump1eb44332009-09-09 15:08:12 +0000310 QualType ElementType, SVal Idx,
Zhongxing Xu511191c2008-10-21 05:27:10 +0000311 const MemRegion* superRegion) {
312 ID.AddInteger(MemRegion::ElementRegionKind);
Ted Kremenekf936f452009-05-04 06:18:28 +0000313 ID.Add(ElementType);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000314 ID.AddPointer(superRegion);
315 Idx.Profile(ID);
316}
317
318void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf936f452009-05-04 06:18:28 +0000319 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000320}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000321
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000322void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
323 const FunctionDecl *FD,
324 const MemRegion*) {
325 ID.AddInteger(MemRegion::FunctionTextRegionKind);
Ted Kremenekabd46e12009-08-28 04:49:15 +0000326 ID.AddPointer(FD);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000327}
328
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000329void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
330 FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
331}
332
333void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek67d12872009-12-07 22:05:27 +0000334 const BlockDecl *BD, CanQualType,
335 const AnalysisContext *AC,
336 const MemRegion*) {
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000337 ID.AddInteger(MemRegion::BlockTextRegionKind);
338 ID.AddPointer(BD);
339}
340
341void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000342 BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000343}
344
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000345void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
346 const BlockTextRegion *BC,
347 const LocationContext *LC,
Ted Kremenek67d12872009-12-07 22:05:27 +0000348 const MemRegion *sReg) {
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000349 ID.AddInteger(MemRegion::BlockDataRegionKind);
350 ID.AddPointer(BC);
351 ID.AddPointer(LC);
Ted Kremenek67d12872009-12-07 22:05:27 +0000352 ID.AddPointer(sReg);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000353}
354
355void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000356 BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000357}
358
Zhongxing Xubb141212009-12-16 11:27:52 +0000359void CXXObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000360 Expr const *Ex,
Zhongxing Xubb141212009-12-16 11:27:52 +0000361 const MemRegion *sReg) {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000362 ID.AddPointer(Ex);
Zhongxing Xubb141212009-12-16 11:27:52 +0000363 ID.AddPointer(sReg);
364}
365
366void CXXObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000367 ProfileRegion(ID, Ex, getSuperRegion());
Zhongxing Xubb141212009-12-16 11:27:52 +0000368}
369
Zhongxing Xu026c6632009-02-05 06:57:29 +0000370//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000371// Region pretty-printing.
372//===----------------------------------------------------------------------===//
373
Ted Kremenek8800ad42009-07-13 23:31:04 +0000374void MemRegion::dump() const {
375 dumpToStream(llvm::errs());
Ted Kremenek7f39d292009-07-02 17:24:10 +0000376}
377
Ted Kremenek9e240492008-10-04 05:50:14 +0000378std::string MemRegion::getString() const {
379 std::string s;
380 llvm::raw_string_ostream os(s);
Ted Kremenek8800ad42009-07-13 23:31:04 +0000381 dumpToStream(os);
Ted Kremenek9e240492008-10-04 05:50:14 +0000382 return os.str();
383}
384
Ted Kremenek8800ad42009-07-13 23:31:04 +0000385void MemRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek9e240492008-10-04 05:50:14 +0000386 os << "<Unknown Region>";
387}
388
Ted Kremenek8800ad42009-07-13 23:31:04 +0000389void AllocaRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek7090ae12008-11-02 00:34:33 +0000390 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
391}
392
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000393void FunctionTextRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekabd46e12009-08-28 04:49:15 +0000394 os << "code{" << getDecl()->getDeclName().getAsString() << '}';
Ted Kremenek72e03202009-04-21 19:56:58 +0000395}
396
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000397void BlockTextRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000398 os << "block_code{" << (void*) this << '}';
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000399}
400
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000401void BlockDataRegion::dumpToStream(llvm::raw_ostream& os) const {
402 os << "block_data{" << BC << '}';
403}
404
Ted Kremenek8800ad42009-07-13 23:31:04 +0000405void CompoundLiteralRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000406 // FIXME: More elaborate pretty-printing.
407 os << "{ " << (void*) CL << " }";
408}
409
Zhongxing Xue1aeb132010-11-25 02:07:24 +0000410void CXXObjectRegion::dumpToStream(llvm::raw_ostream &os) const {
411 os << "temp_object";
412}
413
Ted Kremenekde0d2632010-01-05 02:18:06 +0000414void CXXThisRegion::dumpToStream(llvm::raw_ostream &os) const {
415 os << "this";
416}
417
Ted Kremenek8800ad42009-07-13 23:31:04 +0000418void ElementRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000419 os << "element{" << superRegion << ','
420 << Index << ',' << getElementType().getAsString() << '}';
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000421}
422
Ted Kremenek8800ad42009-07-13 23:31:04 +0000423void FieldRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000424 os << superRegion << "->" << getDecl();
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000425}
426
Ted Kremenekfa87d812010-07-06 23:37:21 +0000427void NonStaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
428 os << "NonStaticGlobalSpaceRegion";
429}
430
Ted Kremenekbcfe03a2009-07-19 20:36:24 +0000431void ObjCIvarRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000432 os << "ivar{" << superRegion << ',' << getDecl() << '}';
Ted Kremenekbcfe03a2009-07-19 20:36:24 +0000433}
434
Mike Stump1eb44332009-09-09 15:08:12 +0000435void StringRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekb27ed3d2009-07-19 20:38:24 +0000436 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions()));
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000437}
438
Ted Kremenek8800ad42009-07-13 23:31:04 +0000439void SymbolicRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekaef5d222009-07-13 23:38:57 +0000440 os << "SymRegion{" << sym << '}';
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000441}
442
Ted Kremenek8800ad42009-07-13 23:31:04 +0000443void VarRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000444 os << cast<VarDecl>(D);
Ted Kremenek9e240492008-10-04 05:50:14 +0000445}
446
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447void RegionRawOffset::dump() const {
448 dumpToStream(llvm::errs());
449}
450
451void RegionRawOffset::dumpToStream(llvm::raw_ostream& os) const {
452 os << "raw_offset{" << getRegion() << ',' << getByteOffset() << '}';
453}
454
Ted Kremenekfa87d812010-07-06 23:37:21 +0000455void StaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
456 os << "StaticGlobalsMemSpace{" << CR << '}';
457}
458
Ted Kremenek9e240492008-10-04 05:50:14 +0000459//===----------------------------------------------------------------------===//
460// MemRegionManager methods.
461//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000462
Ted Kremenek67d12872009-12-07 22:05:27 +0000463template <typename REG>
464const REG *MemRegionManager::LazyAllocate(REG*& region) {
Mike Stump1eb44332009-09-09 15:08:12 +0000465 if (!region) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000466 region = (REG*) A.Allocate<REG>();
467 new (region) REG(this);
Ted Kremenek9e240492008-10-04 05:50:14 +0000468 }
Ted Kremeneka43484a2009-06-23 00:46:41 +0000469
Ted Kremenek9e240492008-10-04 05:50:14 +0000470 return region;
471}
472
Ted Kremenek67d12872009-12-07 22:05:27 +0000473template <typename REG, typename ARG>
474const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
475 if (!region) {
476 region = (REG*) A.Allocate<REG>();
477 new (region) REG(this, a);
478 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000479
Ted Kremenek67d12872009-12-07 22:05:27 +0000480 return region;
Ted Kremenek9e240492008-10-04 05:50:14 +0000481}
482
Ted Kremenek67d12872009-12-07 22:05:27 +0000483const StackLocalsSpaceRegion*
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000484MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
485 assert(STC);
Zhongxing Xuc30470d2010-02-17 08:46:50 +0000486 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
487
488 if (R)
489 return R;
490
491 R = A.Allocate<StackLocalsSpaceRegion>();
492 new (R) StackLocalsSpaceRegion(this, STC);
493 return R;
Ted Kremenekd05552a2009-07-02 18:14:59 +0000494}
495
Ted Kremenek67d12872009-12-07 22:05:27 +0000496const StackArgumentsSpaceRegion *
497MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000498 assert(STC);
Zhongxing Xuc30470d2010-02-17 08:46:50 +0000499 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
500
501 if (R)
502 return R;
503
504 R = A.Allocate<StackArgumentsSpaceRegion>();
505 new (R) StackArgumentsSpaceRegion(this, STC);
506 return R;
Ted Kremenek67d12872009-12-07 22:05:27 +0000507}
508
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000509const GlobalsSpaceRegion
510*MemRegionManager::getGlobalsRegion(const CodeTextRegion *CR) {
511 if (!CR)
512 return LazyAllocate(globals);
513
514 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
515 if (R)
516 return R;
517
518 R = A.Allocate<StaticGlobalSpaceRegion>();
519 new (R) StaticGlobalSpaceRegion(this, CR);
520 return R;
Ted Kremenek9e240492008-10-04 05:50:14 +0000521}
522
Ted Kremenek67d12872009-12-07 22:05:27 +0000523const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
Ted Kremenek9e240492008-10-04 05:50:14 +0000524 return LazyAllocate(heap);
525}
526
Ted Kremenekb48ad642009-12-04 00:26:31 +0000527const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
Zhongxing Xu17892752008-10-08 02:50:44 +0000528 return LazyAllocate(unknown);
529}
530
Ted Kremenekb48ad642009-12-04 00:26:31 +0000531const MemSpaceRegion *MemRegionManager::getCodeRegion() {
Zhongxing Xuec13d922009-04-10 08:45:10 +0000532 return LazyAllocate(code);
533}
534
Ted Kremenek25010132009-06-22 23:13:13 +0000535//===----------------------------------------------------------------------===//
536// Constructing regions.
537//===----------------------------------------------------------------------===//
538
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000539const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
Ted Kremenek67d12872009-12-07 22:05:27 +0000540 return getSubRegion<StringRegion>(Str, getGlobalsRegion());
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000541}
542
Ted Kremenekb48ad642009-12-04 00:26:31 +0000543const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
544 const LocationContext *LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000545 const MemRegion *sReg = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000547 if (D->hasGlobalStorage() && !D->isStaticLocal())
548 sReg = getGlobalsRegion();
549 else {
Ted Kremenek67d12872009-12-07 22:05:27 +0000550 // FIXME: Once we implement scope handling, we will need to properly lookup
551 // 'D' to the proper LocationContext.
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000552 const DeclContext *DC = D->getDeclContext();
553 const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000555 if (!STC)
556 sReg = getUnknownRegion();
557 else {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000558 if (D->hasLocalStorage()) {
559 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
560 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
561 : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
562 }
563 else {
564 assert(D->isStaticLocal());
565 const Decl *D = STC->getDecl();
566 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
567 sReg = getGlobalsRegion(getFunctionTextRegion(FD));
568 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
569 const BlockTextRegion *BTR =
570 getBlockTextRegion(BD,
571 C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
572 STC->getAnalysisContext());
573 sReg = getGlobalsRegion(BTR);
574 }
575 else {
576 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now
577 // just use the main global memspace.
578 sReg = getGlobalsRegion();
579 }
580 }
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000581 }
Ted Kremenek67d12872009-12-07 22:05:27 +0000582 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000583
Ted Kremenek67d12872009-12-07 22:05:27 +0000584 return getSubRegion<VarRegion>(D, sReg);
585}
586
587const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
588 const MemRegion *superR) {
589 return getSubRegion<VarRegion>(D, superR);
Ted Kremenek9e240492008-10-04 05:50:14 +0000590}
591
Ted Kremenekb48ad642009-12-04 00:26:31 +0000592const BlockDataRegion *
593MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
594 const LocationContext *LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000595 const MemRegion *sReg = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000596
597 if (LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000598 // FIXME: Once we implement scope handling, we want the parent region
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000599 // to be the scope.
Ted Kremenek67d12872009-12-07 22:05:27 +0000600 const StackFrameContext *STC = LC->getCurrentStackFrame();
601 assert(STC);
602 sReg = getStackLocalsRegion(STC);
603 }
604 else {
605 // We allow 'LC' to be NULL for cases where want BlockDataRegions
606 // without context-sensitivity.
607 sReg = getUnknownRegion();
608 }
609
610 return getSubRegion<BlockDataRegion>(BC, LC, sReg);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000611}
612
Ted Kremenekb48ad642009-12-04 00:26:31 +0000613const CompoundLiteralRegion*
Ted Kremenek67d12872009-12-07 22:05:27 +0000614MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL,
615 const LocationContext *LC) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000616
Ted Kremenek67d12872009-12-07 22:05:27 +0000617 const MemRegion *sReg = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000618
Ted Kremenek67d12872009-12-07 22:05:27 +0000619 if (CL->isFileScope())
620 sReg = getGlobalsRegion();
621 else {
622 const StackFrameContext *STC = LC->getCurrentStackFrame();
623 assert(STC);
624 sReg = getStackLocalsRegion(STC);
625 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000626
Ted Kremenek67d12872009-12-07 22:05:27 +0000627 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000628}
629
Ted Kremenekb48ad642009-12-04 00:26:31 +0000630const ElementRegion*
Ted Kremenek02282ac2010-09-15 03:13:30 +0000631MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
Ted Kremenek46537392009-07-16 01:33:37 +0000632 const MemRegion* superRegion,
633 ASTContext& Ctx){
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000634
Ted Kremenek32f90102010-05-27 00:29:00 +0000635 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
Ted Kremenekabb042f2008-12-13 19:24:37 +0000636
Zhongxing Xu511191c2008-10-21 05:27:10 +0000637 llvm::FoldingSetNodeID ID;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000638 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000639
640 void* InsertPos;
641 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
642 ElementRegion* R = cast_or_null<ElementRegion>(data);
643
644 if (!R) {
645 R = (ElementRegion*) A.Allocate<ElementRegion>();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000646 new (R) ElementRegion(T, Idx, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000647 Regions.InsertNode(R, InsertPos);
648 }
649
650 return R;
651}
652
Ted Kremenekb48ad642009-12-04 00:26:31 +0000653const FunctionTextRegion *
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000654MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000655 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
Zhongxing Xuec13d922009-04-10 08:45:10 +0000656}
657
Ted Kremenekb48ad642009-12-04 00:26:31 +0000658const BlockTextRegion *
Ted Kremenek67d12872009-12-07 22:05:27 +0000659MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
660 AnalysisContext *AC) {
661 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000662}
663
664
Ted Kremenek993f1c72008-10-17 20:28:54 +0000665/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenekb48ad642009-12-04 00:26:31 +0000666const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000667 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000668}
669
Ted Kremenekde0d2632010-01-05 02:18:06 +0000670const FieldRegion*
Ted Kremenekb48ad642009-12-04 00:26:31 +0000671MemRegionManager::getFieldRegion(const FieldDecl* d,
672 const MemRegion* superRegion){
Ted Kremenekeeea4562009-07-10 16:51:45 +0000673 return getSubRegion<FieldRegion>(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000674}
675
Ted Kremenekb48ad642009-12-04 00:26:31 +0000676const ObjCIvarRegion*
Ted Kremenek993f1c72008-10-17 20:28:54 +0000677MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
678 const MemRegion* superRegion) {
Ted Kremenekeeea4562009-07-10 16:51:45 +0000679 return getSubRegion<ObjCIvarRegion>(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000680}
681
Ted Kremenekde0d2632010-01-05 02:18:06 +0000682const CXXObjectRegion*
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000683MemRegionManager::getCXXObjectRegion(Expr const *E,
684 LocationContext const *LC) {
685 const StackFrameContext *SFC = LC->getCurrentStackFrame();
686 assert(SFC);
687 return getSubRegion<CXXObjectRegion>(E, getStackLocalsRegion(SFC));
Zhongxing Xubb141212009-12-16 11:27:52 +0000688}
689
Ted Kremenekde0d2632010-01-05 02:18:06 +0000690const CXXThisRegion*
691MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
692 const LocationContext *LC) {
693 const StackFrameContext *STC = LC->getCurrentStackFrame();
694 assert(STC);
695 const PointerType *PT = thisPointerTy->getAs<PointerType>();
696 assert(PT);
697 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
698}
699
Ted Kremenekb48ad642009-12-04 00:26:31 +0000700const AllocaRegion*
Ted Kremenek67d12872009-12-07 22:05:27 +0000701MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt,
702 const LocationContext *LC) {
703 const StackFrameContext *STC = LC->getCurrentStackFrame();
704 assert(STC);
705 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
Ted Kremenek7090ae12008-11-02 00:34:33 +0000706}
707
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000708const MemSpaceRegion *MemRegion::getMemorySpace() const {
709 const MemRegion *R = this;
Ted Kremenekea20cd72009-06-23 18:05:21 +0000710 const SubRegion* SR = dyn_cast<SubRegion>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek993f1c72008-10-17 20:28:54 +0000712 while (SR) {
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000713 R = SR->getSuperRegion();
714 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000717 return dyn_cast<MemSpaceRegion>(R);
718}
719
720bool MemRegion::hasStackStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000721 return isa<StackSpaceRegion>(getMemorySpace());
Ted Kremenek9e240492008-10-04 05:50:14 +0000722}
Ted Kremenek1670e402009-04-11 00:11:10 +0000723
Ted Kremenekde0d2632010-01-05 02:18:06 +0000724bool MemRegion::hasStackNonParametersStorage() const {
725 return isa<StackLocalsSpaceRegion>(getMemorySpace());
Zhongxing Xudd198f02009-06-23 02:51:21 +0000726}
Ted Kremenek1670e402009-04-11 00:11:10 +0000727
Ted Kremenekde0d2632010-01-05 02:18:06 +0000728bool MemRegion::hasStackParametersStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000729 return isa<StackArgumentsSpaceRegion>(getMemorySpace());
Ted Kremenekdc147262009-07-02 22:02:15 +0000730}
731
Ted Kremenek15086362009-07-02 18:25:09 +0000732bool MemRegion::hasGlobalsOrParametersStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000733 const MemSpaceRegion *MS = getMemorySpace();
734 return isa<StackArgumentsSpaceRegion>(MS) ||
735 isa<GlobalsSpaceRegion>(MS);
Ted Kremenek15086362009-07-02 18:25:09 +0000736}
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000737
Zhongxing Xuadca2712009-11-10 02:37:53 +0000738// getBaseRegion strips away all elements and fields, and get the base region
739// of them.
740const MemRegion *MemRegion::getBaseRegion() const {
741 const MemRegion *R = this;
742 while (true) {
Ted Kremenek68b9a592010-04-06 22:06:03 +0000743 switch (R->getKind()) {
744 case MemRegion::ElementRegionKind:
745 case MemRegion::FieldRegionKind:
746 case MemRegion::ObjCIvarRegionKind:
747 R = cast<SubRegion>(R)->getSuperRegion();
748 continue;
749 default:
750 break;
Zhongxing Xuadca2712009-11-10 02:37:53 +0000751 }
752 break;
753 }
754 return R;
755}
756
Ted Kremenek1670e402009-04-11 00:11:10 +0000757//===----------------------------------------------------------------------===//
758// View handling.
759//===----------------------------------------------------------------------===//
760
Zhongxing Xu479529e2009-11-10 02:17:20 +0000761const MemRegion *MemRegion::StripCasts() const {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000762 const MemRegion *R = this;
763 while (true) {
Mike Stump1eb44332009-09-09 15:08:12 +0000764 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000765 // FIXME: generalize. Essentially we want to strip away ElementRegions
766 // that were layered on a symbolic region because of casts. We only
767 // want to strip away ElementRegions, however, where the index is 0.
768 SVal index = ER->getIndex();
769 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000770 if (CI->getValue().getSExtValue() == 0) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000771 R = ER->getSuperRegion();
772 continue;
773 }
774 }
775 }
776 break;
777 }
778 return R;
779}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000780
781// FIXME: Merge with the implementation of the same method in Store.cpp
782static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
783 if (const RecordType *RT = Ty->getAs<RecordType>()) {
784 const RecordDecl *D = RT->getDecl();
Douglas Gregor952b0172010-02-11 01:04:33 +0000785 if (!D->getDefinition())
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000786 return false;
787 }
788
789 return true;
790}
791
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000792RegionRawOffset ElementRegion::getAsArrayOffset() const {
Ken Dyck199c3d62010-01-11 17:06:35 +0000793 CharUnits offset = CharUnits::Zero();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000794 const ElementRegion *ER = this;
795 const MemRegion *superR = NULL;
796 ASTContext &C = getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000798 // FIXME: Handle multi-dimensional arrays.
799
800 while (ER) {
801 superR = ER->getSuperRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000803 // FIXME: generalize to symbolic offsets.
804 SVal index = ER->getIndex();
805 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
806 // Update the offset.
807 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000809 if (i != 0) {
810 QualType elemType = ER->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000812 // If we are pointing to an incomplete type, go no further.
813 if (!IsCompleteType(C, elemType)) {
814 superR = ER;
815 break;
816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ken Dyck199c3d62010-01-11 17:06:35 +0000818 CharUnits size = C.getTypeSizeInChars(elemType);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000819 offset += (i * size);
820 }
821
822 // Go to the next ElementRegion (if any).
823 ER = dyn_cast<ElementRegion>(superR);
824 continue;
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000827 return NULL;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000830 assert(superR && "super region cannot be NULL");
Ken Dyck199c3d62010-01-11 17:06:35 +0000831 return RegionRawOffset(superR, offset.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000832}
833
Zhongxing Xue8882332010-08-03 04:52:05 +0000834RegionOffset MemRegion::getAsOffset() const {
835 const MemRegion *R = this;
Zhongxing Xue3273e72010-08-03 06:34:25 +0000836 int64_t Offset = 0;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000837
Zhongxing Xue8882332010-08-03 04:52:05 +0000838 while (1) {
839 switch (R->getKind()) {
840 default:
841 return RegionOffset(0);
842 case SymbolicRegionKind:
843 case AllocaRegionKind:
844 case CompoundLiteralRegionKind:
845 case CXXThisRegionKind:
846 case StringRegionKind:
847 case VarRegionKind:
848 case CXXObjectRegionKind:
849 goto Finish;
850 case ElementRegionKind: {
851 const ElementRegion *ER = cast<ElementRegion>(R);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000852 QualType EleTy = ER->getValueType();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000853
Zhongxing Xue8882332010-08-03 04:52:05 +0000854 if (!IsCompleteType(getContext(), EleTy))
855 return RegionOffset(0);
856
857 SVal Index = ER->getIndex();
858 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) {
859 int64_t i = CI->getValue().getSExtValue();
Zhongxing Xue8882332010-08-03 04:52:05 +0000860 CharUnits Size = getContext().getTypeSizeInChars(EleTy);
861 Offset += i * Size.getQuantity() * 8;
862 } else {
863 // We cannot compute offset for non-concrete index.
864 return RegionOffset(0);
865 }
866 R = ER->getSuperRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000867 break;
Zhongxing Xue8882332010-08-03 04:52:05 +0000868 }
869 case FieldRegionKind: {
870 const FieldRegion *FR = cast<FieldRegion>(R);
871 const RecordDecl *RD = FR->getDecl()->getParent();
872 if (!RD->isDefinition())
873 // We cannot compute offset for incomplete type.
874 return RegionOffset(0);
875 // Get the field number.
876 unsigned idx = 0;
877 for (RecordDecl::field_iterator FI = RD->field_begin(),
878 FE = RD->field_end(); FI != FE; ++FI, ++idx)
879 if (FR->getDecl() == *FI)
880 break;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000881
Zhongxing Xue8882332010-08-03 04:52:05 +0000882 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
883 // This is offset in bits.
884 Offset += Layout.getFieldOffset(idx);
885 R = FR->getSuperRegion();
886 break;
887 }
888 }
889 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000890
Zhongxing Xue8882332010-08-03 04:52:05 +0000891 Finish:
892 return RegionOffset(R, Offset);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000893}
894
Ted Kremenek42400962009-11-26 02:34:36 +0000895//===----------------------------------------------------------------------===//
896// BlockDataRegion
897//===----------------------------------------------------------------------===//
898
899void BlockDataRegion::LazyInitializeReferencedVars() {
900 if (ReferencedVars)
901 return;
902
Ted Kremenek67d12872009-12-07 22:05:27 +0000903 AnalysisContext *AC = getCodeRegion()->getAnalysisContext();
Ted Kremenek42400962009-11-26 02:34:36 +0000904 AnalysisContext::referenced_decls_iterator I, E;
905 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000906
Ted Kremenek42400962009-11-26 02:34:36 +0000907 if (I == E) {
908 ReferencedVars = (void*) 0x1;
909 return;
910 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000911
Ted Kremenek42400962009-11-26 02:34:36 +0000912 MemRegionManager &MemMgr = *getMemRegionManager();
913 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
914 BumpVectorContext BC(A);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000915
Ted Kremenek42400962009-11-26 02:34:36 +0000916 typedef BumpVector<const MemRegion*> VarVec;
917 VarVec *BV = (VarVec*) A.Allocate<VarVec>();
Ted Kremenek02b1df62009-12-01 22:12:34 +0000918 new (BV) VarVec(BC, E - I);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000919
Ted Kremenek67d12872009-12-07 22:05:27 +0000920 for ( ; I != E; ++I) {
921 const VarDecl *VD = *I;
922 const VarRegion *VR = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000923
Ted Kremenek85248732010-02-06 00:30:00 +0000924 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
Ted Kremenek67d12872009-12-07 22:05:27 +0000925 VR = MemMgr.getVarRegion(VD, this);
926 else {
927 if (LC)
928 VR = MemMgr.getVarRegion(VD, LC);
929 else {
930 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
931 }
932 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000933
Ted Kremenek67d12872009-12-07 22:05:27 +0000934 assert(VR);
935 BV->push_back(VR, BC);
936 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000937
Ted Kremenek42400962009-11-26 02:34:36 +0000938 ReferencedVars = BV;
939}
940
941BlockDataRegion::referenced_vars_iterator
942BlockDataRegion::referenced_vars_begin() const {
943 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
944
945 BumpVector<const MemRegion*> *Vec =
946 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000947
Ted Kremenek81cef582009-12-03 08:09:21 +0000948 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
949 NULL : Vec->begin());
Ted Kremenek42400962009-11-26 02:34:36 +0000950}
951
952BlockDataRegion::referenced_vars_iterator
953BlockDataRegion::referenced_vars_end() const {
954 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
955
956 BumpVector<const MemRegion*> *Vec =
957 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000958
Ted Kremenek81cef582009-12-03 08:09:21 +0000959 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
960 NULL : Vec->end());
Ted Kremenek42400962009-11-26 02:34:36 +0000961}