blob: eca7619d3fc49eb0c5810eb9558e53a2fea89b45 [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 Kremenek21142582010-12-23 19:38:26 +000016#include "clang/StaticAnalyzer/PathSensitive/MemRegion.h"
17#include "clang/StaticAnalyzer/PathSensitive/SValBuilder.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;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenek9e240492008-10-04 05:50:14 +000026
Ted Kremenek25010132009-06-22 23:13:13 +000027//===----------------------------------------------------------------------===//
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000028// MemRegion Construction.
29//===----------------------------------------------------------------------===//
30
31template<typename RegionTy> struct MemRegionManagerTrait;
32
33template <typename RegionTy, typename A1>
34RegionTy* MemRegionManager::getRegion(const A1 a1) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000035
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000036 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
37 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000038
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000039 llvm::FoldingSetNodeID ID;
40 RegionTy::ProfileRegion(ID, a1, superRegion);
41 void* InsertPos;
42 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
43 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000044
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000045 if (!R) {
46 R = (RegionTy*) A.Allocate<RegionTy>();
47 new (R) RegionTy(a1, superRegion);
48 Regions.InsertNode(R, InsertPos);
49 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000050
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000051 return R;
52}
53
54template <typename RegionTy, typename A1>
55RegionTy* MemRegionManager::getSubRegion(const A1 a1,
56 const MemRegion *superRegion) {
57 llvm::FoldingSetNodeID ID;
58 RegionTy::ProfileRegion(ID, a1, superRegion);
59 void* InsertPos;
60 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
61 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000062
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000063 if (!R) {
64 R = (RegionTy*) A.Allocate<RegionTy>();
65 new (R) RegionTy(a1, superRegion);
66 Regions.InsertNode(R, InsertPos);
67 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000068
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000069 return R;
70}
71
72template <typename RegionTy, typename A1, typename A2>
73RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000074
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000075 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
76 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000077
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000078 llvm::FoldingSetNodeID ID;
79 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
80 void* InsertPos;
81 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
82 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000083
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000084 if (!R) {
85 R = (RegionTy*) A.Allocate<RegionTy>();
86 new (R) RegionTy(a1, a2, superRegion);
87 Regions.InsertNode(R, InsertPos);
88 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000089
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000090 return R;
91}
92
93template <typename RegionTy, typename A1, typename A2>
94RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
95 const MemRegion *superRegion) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000096
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +000097 llvm::FoldingSetNodeID ID;
98 RegionTy::ProfileRegion(ID, a1, a2, superRegion);
99 void* InsertPos;
100 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
101 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000102
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000103 if (!R) {
104 R = (RegionTy*) A.Allocate<RegionTy>();
105 new (R) RegionTy(a1, a2, superRegion);
106 Regions.InsertNode(R, InsertPos);
107 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000108
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000109 return R;
110}
111
Ted Kremenek67d12872009-12-07 22:05:27 +0000112template <typename RegionTy, typename A1, typename A2, typename A3>
113RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3,
114 const MemRegion *superRegion) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000115
Ted Kremenek67d12872009-12-07 22:05:27 +0000116 llvm::FoldingSetNodeID ID;
117 RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion);
118 void* InsertPos;
119 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
120 InsertPos));
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000121
Ted Kremenek67d12872009-12-07 22:05:27 +0000122 if (!R) {
123 R = (RegionTy*) A.Allocate<RegionTy>();
124 new (R) RegionTy(a1, a2, a3, superRegion);
125 Regions.InsertNode(R, InsertPos);
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000126 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000127
Ted Kremenek67d12872009-12-07 22:05:27 +0000128 return R;
129}
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +0000130
131//===----------------------------------------------------------------------===//
Ted Kremenek42400962009-11-26 02:34:36 +0000132// Object destruction.
Ted Kremenek25010132009-06-22 23:13:13 +0000133//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000134
135MemRegion::~MemRegion() {}
136
Ted Kremenek42400962009-11-26 02:34:36 +0000137MemRegionManager::~MemRegionManager() {
138 // All regions and their data are BumpPtrAllocated. No need to call
139 // their destructors.
140}
141
142//===----------------------------------------------------------------------===//
143// Basic methods.
144//===----------------------------------------------------------------------===//
145
Zhongxing Xu7e5d6ed2009-01-08 13:17:14 +0000146bool SubRegion::isSubRegionOf(const MemRegion* R) const {
147 const MemRegion* r = getSuperRegion();
148 while (r != 0) {
149 if (r == R)
150 return true;
151 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
152 r = sr->getSuperRegion();
153 else
154 break;
155 }
156 return false;
157}
158
Ted Kremeneka43484a2009-06-23 00:46:41 +0000159MemRegionManager* SubRegion::getMemRegionManager() const {
160 const SubRegion* r = this;
161 do {
162 const MemRegion *superRegion = r->getSuperRegion();
163 if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
164 r = sr;
165 continue;
166 }
167 return superRegion->getMemRegionManager();
168 } while (1);
169}
170
Ted Kremenek5348f942009-12-14 22:15:06 +0000171const StackFrameContext *VarRegion::getStackFrame() const {
172 const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
173 return SSR ? SSR->getStackFrame() : NULL;
174}
175
176//===----------------------------------------------------------------------===//
Jordy Rose32f26562010-07-04 00:00:41 +0000177// Region extents.
178//===----------------------------------------------------------------------===//
179
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000180DefinedOrUnknownSVal DeclRegion::getExtent(SValBuilder &svalBuilder) const {
181 ASTContext& Ctx = svalBuilder.getContext();
John McCall49f4e1c2010-12-10 11:01:00 +0000182 QualType T = getDesugaredValueType(Ctx);
Jordy Rose32f26562010-07-04 00:00:41 +0000183
Jordy Rose52e04c52010-07-05 00:50:15 +0000184 if (isa<VariableArrayType>(T))
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000185 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
Jordy Rose52e04c52010-07-05 00:50:15 +0000186 if (isa<IncompleteArrayType>(T))
Jordy Rose32f26562010-07-04 00:00:41 +0000187 return UnknownVal();
188
Ted Kremenek99c330d2010-12-23 02:42:36 +0000189 CharUnits size = Ctx.getTypeSizeInChars(T);
190 QualType sizeTy = svalBuilder.getArrayIndexType();
191 return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
Jordy Rose32f26562010-07-04 00:00:41 +0000192}
193
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000194DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
195 DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
Jordy Rose32f26562010-07-04 00:00:41 +0000196
197 // A zero-length array at the end of a struct often stands for dynamically-
198 // allocated extra memory.
199 if (Extent.isZeroConstant()) {
John McCall49f4e1c2010-12-10 11:01:00 +0000200 QualType T = getDesugaredValueType(svalBuilder.getContext());
Jordy Rose32f26562010-07-04 00:00:41 +0000201
202 if (isa<ConstantArrayType>(T))
203 return UnknownVal();
204 }
205
206 return Extent;
207}
208
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000209DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
210 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
Jordy Rose32f26562010-07-04 00:00:41 +0000211}
212
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000213DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
214 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
Jordy Rose32f26562010-07-04 00:00:41 +0000215}
216
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000217DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
Ted Kremenek99c330d2010-12-23 02:42:36 +0000218 return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
219 svalBuilder.getArrayIndexType());
Jordy Rose32f26562010-07-04 00:00:41 +0000220}
221
Zhongxing Xu4fd56812010-11-26 08:21:53 +0000222QualType CXXBaseObjectRegion::getValueType() const {
223 return QualType(decl->getTypeForDecl(), 0);
224}
225
Jordy Rose32f26562010-07-04 00:00:41 +0000226//===----------------------------------------------------------------------===//
Ted Kremenek5348f942009-12-14 22:15:06 +0000227// FoldingSet profiling.
228//===----------------------------------------------------------------------===//
229
Ted Kremenek9e240492008-10-04 05:50:14 +0000230void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
231 ID.AddInteger((unsigned)getKind());
232}
233
Ted Kremenek67d12872009-12-07 22:05:27 +0000234void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
235 ID.AddInteger((unsigned)getKind());
236 ID.AddPointer(getStackFrame());
237}
238
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000239void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
240 ID.AddInteger((unsigned)getKind());
241 ID.AddPointer(getCodeRegion());
242}
243
Mike Stump1eb44332009-09-09 15:08:12 +0000244void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
245 const StringLiteral* Str,
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000246 const MemRegion* superRegion) {
247 ID.AddInteger((unsigned) StringRegionKind);
248 ID.AddPointer(Str);
249 ID.AddPointer(superRegion);
250}
251
Ted Kremenek7090ae12008-11-02 00:34:33 +0000252void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000253 const Expr* Ex, unsigned cnt,
254 const MemRegion *) {
Ted Kremenek7090ae12008-11-02 00:34:33 +0000255 ID.AddInteger((unsigned) AllocaRegionKind);
256 ID.AddPointer(Ex);
257 ID.AddInteger(cnt);
258}
259
260void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000261 ProfileRegion(ID, Ex, Cnt, superRegion);
Ted Kremenek7090ae12008-11-02 00:34:33 +0000262}
263
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000264void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
265 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
266}
267
268void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
269 const CompoundLiteralExpr* CL,
270 const MemRegion* superRegion) {
271 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
272 ID.AddPointer(CL);
273 ID.AddPointer(superRegion);
274}
275
Ted Kremenekde0d2632010-01-05 02:18:06 +0000276void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
277 const PointerType *PT,
278 const MemRegion *sRegion) {
279 ID.AddInteger((unsigned) CXXThisRegionKind);
280 ID.AddPointer(PT);
281 ID.AddPointer(sRegion);
282}
283
284void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
285 CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
286}
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000287
Ted Kremenek9e240492008-10-04 05:50:14 +0000288void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
289 const MemRegion* superRegion, Kind k) {
290 ID.AddInteger((unsigned) k);
291 ID.AddPointer(D);
292 ID.AddPointer(superRegion);
293}
294
295void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
296 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
297}
298
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000299void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000300 VarRegion::ProfileRegion(ID, getDecl(), superRegion);
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000301}
302
Ted Kremenek25010132009-06-22 23:13:13 +0000303void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
304 const MemRegion *sreg) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000305 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremenek6d0e2d22008-12-05 02:39:38 +0000306 ID.Add(sym);
Ted Kremenek25010132009-06-22 23:13:13 +0000307 ID.AddPointer(sreg);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000308}
309
310void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek25010132009-06-22 23:13:13 +0000311 SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000312}
313
Ted Kremenekf936f452009-05-04 06:18:28 +0000314void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Mike Stump1eb44332009-09-09 15:08:12 +0000315 QualType ElementType, SVal Idx,
Zhongxing Xu511191c2008-10-21 05:27:10 +0000316 const MemRegion* superRegion) {
317 ID.AddInteger(MemRegion::ElementRegionKind);
Ted Kremenekf936f452009-05-04 06:18:28 +0000318 ID.Add(ElementType);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000319 ID.AddPointer(superRegion);
320 Idx.Profile(ID);
321}
322
323void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf936f452009-05-04 06:18:28 +0000324 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000325}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000326
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000327void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
328 const FunctionDecl *FD,
329 const MemRegion*) {
330 ID.AddInteger(MemRegion::FunctionTextRegionKind);
Ted Kremenekabd46e12009-08-28 04:49:15 +0000331 ID.AddPointer(FD);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000332}
333
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000334void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
335 FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
336}
337
338void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek67d12872009-12-07 22:05:27 +0000339 const BlockDecl *BD, CanQualType,
340 const AnalysisContext *AC,
341 const MemRegion*) {
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000342 ID.AddInteger(MemRegion::BlockTextRegionKind);
343 ID.AddPointer(BD);
344}
345
346void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000347 BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000348}
349
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000350void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
351 const BlockTextRegion *BC,
352 const LocationContext *LC,
Ted Kremenek67d12872009-12-07 22:05:27 +0000353 const MemRegion *sReg) {
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000354 ID.AddInteger(MemRegion::BlockDataRegionKind);
355 ID.AddPointer(BC);
356 ID.AddPointer(LC);
Ted Kremenek67d12872009-12-07 22:05:27 +0000357 ID.AddPointer(sReg);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000358}
359
360void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000361 BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000362}
363
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000364void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
365 Expr const *Ex,
366 const MemRegion *sReg) {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000367 ID.AddPointer(Ex);
Zhongxing Xubb141212009-12-16 11:27:52 +0000368 ID.AddPointer(sReg);
369}
370
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000371void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000372 ProfileRegion(ID, Ex, getSuperRegion());
Zhongxing Xubb141212009-12-16 11:27:52 +0000373}
374
Zhongxing Xu4fd56812010-11-26 08:21:53 +0000375void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
376 const CXXRecordDecl *decl,
377 const MemRegion *sReg) {
378 ID.AddPointer(decl);
379 ID.AddPointer(sReg);
380}
381
382void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
383 ProfileRegion(ID, decl, superRegion);
384}
385
Zhongxing Xu026c6632009-02-05 06:57:29 +0000386//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000387// Region pretty-printing.
388//===----------------------------------------------------------------------===//
389
Ted Kremenek8800ad42009-07-13 23:31:04 +0000390void MemRegion::dump() const {
391 dumpToStream(llvm::errs());
Ted Kremenek7f39d292009-07-02 17:24:10 +0000392}
393
Ted Kremenek9e240492008-10-04 05:50:14 +0000394std::string MemRegion::getString() const {
395 std::string s;
396 llvm::raw_string_ostream os(s);
Ted Kremenek8800ad42009-07-13 23:31:04 +0000397 dumpToStream(os);
Ted Kremenek9e240492008-10-04 05:50:14 +0000398 return os.str();
399}
400
Ted Kremenek8800ad42009-07-13 23:31:04 +0000401void MemRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek9e240492008-10-04 05:50:14 +0000402 os << "<Unknown Region>";
403}
404
Ted Kremenek8800ad42009-07-13 23:31:04 +0000405void AllocaRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek7090ae12008-11-02 00:34:33 +0000406 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
407}
408
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000409void FunctionTextRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekabd46e12009-08-28 04:49:15 +0000410 os << "code{" << getDecl()->getDeclName().getAsString() << '}';
Ted Kremenek72e03202009-04-21 19:56:58 +0000411}
412
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000413void BlockTextRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000414 os << "block_code{" << (void*) this << '}';
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000415}
416
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000417void BlockDataRegion::dumpToStream(llvm::raw_ostream& os) const {
418 os << "block_data{" << BC << '}';
419}
420
Ted Kremenek8800ad42009-07-13 23:31:04 +0000421void CompoundLiteralRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000422 // FIXME: More elaborate pretty-printing.
423 os << "{ " << (void*) CL << " }";
424}
425
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000426void CXXTempObjectRegion::dumpToStream(llvm::raw_ostream &os) const {
Zhongxing Xue1aeb132010-11-25 02:07:24 +0000427 os << "temp_object";
428}
429
Zhongxing Xu4fd56812010-11-26 08:21:53 +0000430void CXXBaseObjectRegion::dumpToStream(llvm::raw_ostream &os) const {
431 os << "base " << decl->getName();
432}
433
Ted Kremenekde0d2632010-01-05 02:18:06 +0000434void CXXThisRegion::dumpToStream(llvm::raw_ostream &os) const {
435 os << "this";
436}
437
Ted Kremenek8800ad42009-07-13 23:31:04 +0000438void ElementRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000439 os << "element{" << superRegion << ','
440 << Index << ',' << getElementType().getAsString() << '}';
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000441}
442
Ted Kremenek8800ad42009-07-13 23:31:04 +0000443void FieldRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000444 os << superRegion << "->" << getDecl();
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000445}
446
Ted Kremenekfa87d812010-07-06 23:37:21 +0000447void NonStaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
448 os << "NonStaticGlobalSpaceRegion";
449}
450
Ted Kremenekbcfe03a2009-07-19 20:36:24 +0000451void ObjCIvarRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000452 os << "ivar{" << superRegion << ',' << getDecl() << '}';
Ted Kremenekbcfe03a2009-07-19 20:36:24 +0000453}
454
Mike Stump1eb44332009-09-09 15:08:12 +0000455void StringRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekb27ed3d2009-07-19 20:38:24 +0000456 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions()));
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000457}
458
Ted Kremenek8800ad42009-07-13 23:31:04 +0000459void SymbolicRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekaef5d222009-07-13 23:38:57 +0000460 os << "SymRegion{" << sym << '}';
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000461}
462
Ted Kremenek8800ad42009-07-13 23:31:04 +0000463void VarRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000464 os << cast<VarDecl>(D);
Ted Kremenek9e240492008-10-04 05:50:14 +0000465}
466
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000467void RegionRawOffset::dump() const {
468 dumpToStream(llvm::errs());
469}
470
471void RegionRawOffset::dumpToStream(llvm::raw_ostream& os) const {
Ken Dyck9ff2b132011-01-24 01:55:39 +0000472 os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000473}
474
Ted Kremenekfa87d812010-07-06 23:37:21 +0000475void StaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
476 os << "StaticGlobalsMemSpace{" << CR << '}';
477}
478
Ted Kremenek9e240492008-10-04 05:50:14 +0000479//===----------------------------------------------------------------------===//
480// MemRegionManager methods.
481//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000482
Ted Kremenek67d12872009-12-07 22:05:27 +0000483template <typename REG>
484const REG *MemRegionManager::LazyAllocate(REG*& region) {
Mike Stump1eb44332009-09-09 15:08:12 +0000485 if (!region) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000486 region = (REG*) A.Allocate<REG>();
487 new (region) REG(this);
Ted Kremenek9e240492008-10-04 05:50:14 +0000488 }
Ted Kremeneka43484a2009-06-23 00:46:41 +0000489
Ted Kremenek9e240492008-10-04 05:50:14 +0000490 return region;
491}
492
Ted Kremenek67d12872009-12-07 22:05:27 +0000493template <typename REG, typename ARG>
494const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
495 if (!region) {
496 region = (REG*) A.Allocate<REG>();
497 new (region) REG(this, a);
498 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000499
Ted Kremenek67d12872009-12-07 22:05:27 +0000500 return region;
Ted Kremenek9e240492008-10-04 05:50:14 +0000501}
502
Ted Kremenek67d12872009-12-07 22:05:27 +0000503const StackLocalsSpaceRegion*
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000504MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
505 assert(STC);
Zhongxing Xuc30470d2010-02-17 08:46:50 +0000506 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
507
508 if (R)
509 return R;
510
511 R = A.Allocate<StackLocalsSpaceRegion>();
512 new (R) StackLocalsSpaceRegion(this, STC);
513 return R;
Ted Kremenekd05552a2009-07-02 18:14:59 +0000514}
515
Ted Kremenek67d12872009-12-07 22:05:27 +0000516const StackArgumentsSpaceRegion *
517MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000518 assert(STC);
Zhongxing Xuc30470d2010-02-17 08:46:50 +0000519 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
520
521 if (R)
522 return R;
523
524 R = A.Allocate<StackArgumentsSpaceRegion>();
525 new (R) StackArgumentsSpaceRegion(this, STC);
526 return R;
Ted Kremenek67d12872009-12-07 22:05:27 +0000527}
528
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000529const GlobalsSpaceRegion
530*MemRegionManager::getGlobalsRegion(const CodeTextRegion *CR) {
531 if (!CR)
532 return LazyAllocate(globals);
533
534 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
535 if (R)
536 return R;
537
538 R = A.Allocate<StaticGlobalSpaceRegion>();
539 new (R) StaticGlobalSpaceRegion(this, CR);
540 return R;
Ted Kremenek9e240492008-10-04 05:50:14 +0000541}
542
Ted Kremenek67d12872009-12-07 22:05:27 +0000543const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
Ted Kremenek9e240492008-10-04 05:50:14 +0000544 return LazyAllocate(heap);
545}
546
Ted Kremenekb48ad642009-12-04 00:26:31 +0000547const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
Zhongxing Xu17892752008-10-08 02:50:44 +0000548 return LazyAllocate(unknown);
549}
550
Ted Kremenekb48ad642009-12-04 00:26:31 +0000551const MemSpaceRegion *MemRegionManager::getCodeRegion() {
Zhongxing Xuec13d922009-04-10 08:45:10 +0000552 return LazyAllocate(code);
553}
554
Ted Kremenek25010132009-06-22 23:13:13 +0000555//===----------------------------------------------------------------------===//
556// Constructing regions.
557//===----------------------------------------------------------------------===//
558
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000559const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
Ted Kremenek67d12872009-12-07 22:05:27 +0000560 return getSubRegion<StringRegion>(Str, getGlobalsRegion());
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000561}
562
Ted Kremenekb48ad642009-12-04 00:26:31 +0000563const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
564 const LocationContext *LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000565 const MemRegion *sReg = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000567 if (D->hasGlobalStorage() && !D->isStaticLocal())
568 sReg = getGlobalsRegion();
569 else {
Ted Kremenek67d12872009-12-07 22:05:27 +0000570 // FIXME: Once we implement scope handling, we will need to properly lookup
571 // 'D' to the proper LocationContext.
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000572 const DeclContext *DC = D->getDeclContext();
573 const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000575 if (!STC)
576 sReg = getUnknownRegion();
577 else {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000578 if (D->hasLocalStorage()) {
579 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
580 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
581 : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
582 }
583 else {
584 assert(D->isStaticLocal());
585 const Decl *D = STC->getDecl();
586 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
587 sReg = getGlobalsRegion(getFunctionTextRegion(FD));
588 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
589 const BlockTextRegion *BTR =
590 getBlockTextRegion(BD,
591 C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
592 STC->getAnalysisContext());
593 sReg = getGlobalsRegion(BTR);
594 }
595 else {
596 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now
597 // just use the main global memspace.
598 sReg = getGlobalsRegion();
599 }
600 }
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000601 }
Ted Kremenek67d12872009-12-07 22:05:27 +0000602 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000603
Ted Kremenek67d12872009-12-07 22:05:27 +0000604 return getSubRegion<VarRegion>(D, sReg);
605}
606
607const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
608 const MemRegion *superR) {
609 return getSubRegion<VarRegion>(D, superR);
Ted Kremenek9e240492008-10-04 05:50:14 +0000610}
611
Ted Kremenekb48ad642009-12-04 00:26:31 +0000612const BlockDataRegion *
613MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
614 const LocationContext *LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000615 const MemRegion *sReg = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000616
617 if (LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000618 // FIXME: Once we implement scope handling, we want the parent region
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000619 // to be the scope.
Ted Kremenek67d12872009-12-07 22:05:27 +0000620 const StackFrameContext *STC = LC->getCurrentStackFrame();
621 assert(STC);
622 sReg = getStackLocalsRegion(STC);
623 }
624 else {
625 // We allow 'LC' to be NULL for cases where want BlockDataRegions
626 // without context-sensitivity.
627 sReg = getUnknownRegion();
628 }
629
630 return getSubRegion<BlockDataRegion>(BC, LC, sReg);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000631}
632
Ted Kremenekb48ad642009-12-04 00:26:31 +0000633const CompoundLiteralRegion*
Ted Kremenek67d12872009-12-07 22:05:27 +0000634MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL,
635 const LocationContext *LC) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000636
Ted Kremenek67d12872009-12-07 22:05:27 +0000637 const MemRegion *sReg = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000638
Ted Kremenek67d12872009-12-07 22:05:27 +0000639 if (CL->isFileScope())
640 sReg = getGlobalsRegion();
641 else {
642 const StackFrameContext *STC = LC->getCurrentStackFrame();
643 assert(STC);
644 sReg = getStackLocalsRegion(STC);
645 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000646
Ted Kremenek67d12872009-12-07 22:05:27 +0000647 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000648}
649
Ted Kremenekb48ad642009-12-04 00:26:31 +0000650const ElementRegion*
Ted Kremenek02282ac2010-09-15 03:13:30 +0000651MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
Ted Kremenek46537392009-07-16 01:33:37 +0000652 const MemRegion* superRegion,
653 ASTContext& Ctx){
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000654
Ted Kremenek32f90102010-05-27 00:29:00 +0000655 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
Ted Kremenekabb042f2008-12-13 19:24:37 +0000656
Zhongxing Xu511191c2008-10-21 05:27:10 +0000657 llvm::FoldingSetNodeID ID;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000658 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000659
660 void* InsertPos;
661 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
662 ElementRegion* R = cast_or_null<ElementRegion>(data);
663
664 if (!R) {
665 R = (ElementRegion*) A.Allocate<ElementRegion>();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000666 new (R) ElementRegion(T, Idx, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000667 Regions.InsertNode(R, InsertPos);
668 }
669
670 return R;
671}
672
Ted Kremenekb48ad642009-12-04 00:26:31 +0000673const FunctionTextRegion *
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000674MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000675 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
Zhongxing Xuec13d922009-04-10 08:45:10 +0000676}
677
Ted Kremenekb48ad642009-12-04 00:26:31 +0000678const BlockTextRegion *
Ted Kremenek67d12872009-12-07 22:05:27 +0000679MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
680 AnalysisContext *AC) {
681 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000682}
683
684
Ted Kremenek993f1c72008-10-17 20:28:54 +0000685/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenekb48ad642009-12-04 00:26:31 +0000686const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000687 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000688}
689
Ted Kremenekde0d2632010-01-05 02:18:06 +0000690const FieldRegion*
Ted Kremenekb48ad642009-12-04 00:26:31 +0000691MemRegionManager::getFieldRegion(const FieldDecl* d,
692 const MemRegion* superRegion){
Ted Kremenekeeea4562009-07-10 16:51:45 +0000693 return getSubRegion<FieldRegion>(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000694}
695
Ted Kremenekb48ad642009-12-04 00:26:31 +0000696const ObjCIvarRegion*
Ted Kremenek993f1c72008-10-17 20:28:54 +0000697MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
698 const MemRegion* superRegion) {
Ted Kremenekeeea4562009-07-10 16:51:45 +0000699 return getSubRegion<ObjCIvarRegion>(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000700}
701
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000702const CXXTempObjectRegion*
703MemRegionManager::getCXXTempObjectRegion(Expr const *E,
704 LocationContext const *LC) {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000705 const StackFrameContext *SFC = LC->getCurrentStackFrame();
706 assert(SFC);
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000707 return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
Zhongxing Xubb141212009-12-16 11:27:52 +0000708}
709
Zhongxing Xu4fd56812010-11-26 08:21:53 +0000710const CXXBaseObjectRegion *
711MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *decl,
712 const MemRegion *superRegion) {
713 return getSubRegion<CXXBaseObjectRegion>(decl, superRegion);
714}
715
Ted Kremenekde0d2632010-01-05 02:18:06 +0000716const CXXThisRegion*
717MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
718 const LocationContext *LC) {
719 const StackFrameContext *STC = LC->getCurrentStackFrame();
720 assert(STC);
721 const PointerType *PT = thisPointerTy->getAs<PointerType>();
722 assert(PT);
723 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
724}
725
Ted Kremenekb48ad642009-12-04 00:26:31 +0000726const AllocaRegion*
Ted Kremenek67d12872009-12-07 22:05:27 +0000727MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt,
728 const LocationContext *LC) {
729 const StackFrameContext *STC = LC->getCurrentStackFrame();
730 assert(STC);
731 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
Ted Kremenek7090ae12008-11-02 00:34:33 +0000732}
733
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000734const MemSpaceRegion *MemRegion::getMemorySpace() const {
735 const MemRegion *R = this;
Ted Kremenekea20cd72009-06-23 18:05:21 +0000736 const SubRegion* SR = dyn_cast<SubRegion>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek993f1c72008-10-17 20:28:54 +0000738 while (SR) {
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000739 R = SR->getSuperRegion();
740 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000743 return dyn_cast<MemSpaceRegion>(R);
744}
745
746bool MemRegion::hasStackStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000747 return isa<StackSpaceRegion>(getMemorySpace());
Ted Kremenek9e240492008-10-04 05:50:14 +0000748}
Ted Kremenek1670e402009-04-11 00:11:10 +0000749
Ted Kremenekde0d2632010-01-05 02:18:06 +0000750bool MemRegion::hasStackNonParametersStorage() const {
751 return isa<StackLocalsSpaceRegion>(getMemorySpace());
Zhongxing Xudd198f02009-06-23 02:51:21 +0000752}
Ted Kremenek1670e402009-04-11 00:11:10 +0000753
Ted Kremenekde0d2632010-01-05 02:18:06 +0000754bool MemRegion::hasStackParametersStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000755 return isa<StackArgumentsSpaceRegion>(getMemorySpace());
Ted Kremenekdc147262009-07-02 22:02:15 +0000756}
757
Ted Kremenek15086362009-07-02 18:25:09 +0000758bool MemRegion::hasGlobalsOrParametersStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000759 const MemSpaceRegion *MS = getMemorySpace();
760 return isa<StackArgumentsSpaceRegion>(MS) ||
761 isa<GlobalsSpaceRegion>(MS);
Ted Kremenek15086362009-07-02 18:25:09 +0000762}
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000763
Zhongxing Xuadca2712009-11-10 02:37:53 +0000764// getBaseRegion strips away all elements and fields, and get the base region
765// of them.
766const MemRegion *MemRegion::getBaseRegion() const {
767 const MemRegion *R = this;
768 while (true) {
Ted Kremenek68b9a592010-04-06 22:06:03 +0000769 switch (R->getKind()) {
770 case MemRegion::ElementRegionKind:
771 case MemRegion::FieldRegionKind:
772 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xue17da652011-01-13 12:46:31 +0000773 case MemRegion::CXXBaseObjectRegionKind:
Ted Kremenek68b9a592010-04-06 22:06:03 +0000774 R = cast<SubRegion>(R)->getSuperRegion();
775 continue;
776 default:
777 break;
Zhongxing Xuadca2712009-11-10 02:37:53 +0000778 }
779 break;
780 }
781 return R;
782}
783
Ted Kremenek1670e402009-04-11 00:11:10 +0000784//===----------------------------------------------------------------------===//
785// View handling.
786//===----------------------------------------------------------------------===//
787
Zhongxing Xu479529e2009-11-10 02:17:20 +0000788const MemRegion *MemRegion::StripCasts() const {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000789 const MemRegion *R = this;
790 while (true) {
Mike Stump1eb44332009-09-09 15:08:12 +0000791 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000792 // FIXME: generalize. Essentially we want to strip away ElementRegions
793 // that were layered on a symbolic region because of casts. We only
794 // want to strip away ElementRegions, however, where the index is 0.
795 SVal index = ER->getIndex();
796 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000797 if (CI->getValue().getSExtValue() == 0) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000798 R = ER->getSuperRegion();
799 continue;
800 }
801 }
802 }
803 break;
804 }
805 return R;
806}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000807
808// FIXME: Merge with the implementation of the same method in Store.cpp
809static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
810 if (const RecordType *RT = Ty->getAs<RecordType>()) {
811 const RecordDecl *D = RT->getDecl();
Douglas Gregor952b0172010-02-11 01:04:33 +0000812 if (!D->getDefinition())
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000813 return false;
814 }
815
816 return true;
817}
818
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000819RegionRawOffset ElementRegion::getAsArrayOffset() const {
Ken Dyck199c3d62010-01-11 17:06:35 +0000820 CharUnits offset = CharUnits::Zero();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000821 const ElementRegion *ER = this;
822 const MemRegion *superR = NULL;
823 ASTContext &C = getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000825 // FIXME: Handle multi-dimensional arrays.
826
827 while (ER) {
828 superR = ER->getSuperRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000830 // FIXME: generalize to symbolic offsets.
831 SVal index = ER->getIndex();
832 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
833 // Update the offset.
834 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000836 if (i != 0) {
837 QualType elemType = ER->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000839 // If we are pointing to an incomplete type, go no further.
840 if (!IsCompleteType(C, elemType)) {
841 superR = ER;
842 break;
843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ken Dyck199c3d62010-01-11 17:06:35 +0000845 CharUnits size = C.getTypeSizeInChars(elemType);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000846 offset += (i * size);
847 }
848
849 // Go to the next ElementRegion (if any).
850 ER = dyn_cast<ElementRegion>(superR);
851 continue;
852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000854 return NULL;
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000857 assert(superR && "super region cannot be NULL");
Ken Dyck9ff2b132011-01-24 01:55:39 +0000858 return RegionRawOffset(superR, offset);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000859}
860
Zhongxing Xue8882332010-08-03 04:52:05 +0000861RegionOffset MemRegion::getAsOffset() const {
862 const MemRegion *R = this;
Zhongxing Xue3273e72010-08-03 06:34:25 +0000863 int64_t Offset = 0;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000864
Zhongxing Xue8882332010-08-03 04:52:05 +0000865 while (1) {
866 switch (R->getKind()) {
867 default:
868 return RegionOffset(0);
869 case SymbolicRegionKind:
870 case AllocaRegionKind:
871 case CompoundLiteralRegionKind:
872 case CXXThisRegionKind:
873 case StringRegionKind:
874 case VarRegionKind:
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000875 case CXXTempObjectRegionKind:
Zhongxing Xue8882332010-08-03 04:52:05 +0000876 goto Finish;
877 case ElementRegionKind: {
878 const ElementRegion *ER = cast<ElementRegion>(R);
Zhongxing Xu018220c2010-08-11 06:10:55 +0000879 QualType EleTy = ER->getValueType();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000880
Zhongxing Xue8882332010-08-03 04:52:05 +0000881 if (!IsCompleteType(getContext(), EleTy))
882 return RegionOffset(0);
883
884 SVal Index = ER->getIndex();
885 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) {
886 int64_t i = CI->getValue().getSExtValue();
Zhongxing Xue8882332010-08-03 04:52:05 +0000887 CharUnits Size = getContext().getTypeSizeInChars(EleTy);
888 Offset += i * Size.getQuantity() * 8;
889 } else {
890 // We cannot compute offset for non-concrete index.
891 return RegionOffset(0);
892 }
893 R = ER->getSuperRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000894 break;
Zhongxing Xue8882332010-08-03 04:52:05 +0000895 }
896 case FieldRegionKind: {
897 const FieldRegion *FR = cast<FieldRegion>(R);
898 const RecordDecl *RD = FR->getDecl()->getParent();
899 if (!RD->isDefinition())
900 // We cannot compute offset for incomplete type.
901 return RegionOffset(0);
902 // Get the field number.
903 unsigned idx = 0;
904 for (RecordDecl::field_iterator FI = RD->field_begin(),
905 FE = RD->field_end(); FI != FE; ++FI, ++idx)
906 if (FR->getDecl() == *FI)
907 break;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000908
Zhongxing Xue8882332010-08-03 04:52:05 +0000909 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
910 // This is offset in bits.
911 Offset += Layout.getFieldOffset(idx);
912 R = FR->getSuperRegion();
913 break;
914 }
915 }
916 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000917
Zhongxing Xue8882332010-08-03 04:52:05 +0000918 Finish:
919 return RegionOffset(R, Offset);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000920}
921
Ted Kremenek42400962009-11-26 02:34:36 +0000922//===----------------------------------------------------------------------===//
923// BlockDataRegion
924//===----------------------------------------------------------------------===//
925
926void BlockDataRegion::LazyInitializeReferencedVars() {
927 if (ReferencedVars)
928 return;
929
Ted Kremenek67d12872009-12-07 22:05:27 +0000930 AnalysisContext *AC = getCodeRegion()->getAnalysisContext();
Ted Kremenek42400962009-11-26 02:34:36 +0000931 AnalysisContext::referenced_decls_iterator I, E;
932 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000933
Ted Kremenek42400962009-11-26 02:34:36 +0000934 if (I == E) {
935 ReferencedVars = (void*) 0x1;
936 return;
937 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000938
Ted Kremenek42400962009-11-26 02:34:36 +0000939 MemRegionManager &MemMgr = *getMemRegionManager();
940 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
941 BumpVectorContext BC(A);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000942
Ted Kremenek42400962009-11-26 02:34:36 +0000943 typedef BumpVector<const MemRegion*> VarVec;
944 VarVec *BV = (VarVec*) A.Allocate<VarVec>();
Ted Kremenek02b1df62009-12-01 22:12:34 +0000945 new (BV) VarVec(BC, E - I);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000946
Ted Kremenek67d12872009-12-07 22:05:27 +0000947 for ( ; I != E; ++I) {
948 const VarDecl *VD = *I;
949 const VarRegion *VR = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000950
Ted Kremenek85248732010-02-06 00:30:00 +0000951 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
Ted Kremenek67d12872009-12-07 22:05:27 +0000952 VR = MemMgr.getVarRegion(VD, this);
953 else {
954 if (LC)
955 VR = MemMgr.getVarRegion(VD, LC);
956 else {
957 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
958 }
959 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000960
Ted Kremenek67d12872009-12-07 22:05:27 +0000961 assert(VR);
962 BV->push_back(VR, BC);
963 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000964
Ted Kremenek42400962009-11-26 02:34:36 +0000965 ReferencedVars = BV;
966}
967
968BlockDataRegion::referenced_vars_iterator
969BlockDataRegion::referenced_vars_begin() const {
970 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
971
972 BumpVector<const MemRegion*> *Vec =
973 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000974
Ted Kremenek81cef582009-12-03 08:09:21 +0000975 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
976 NULL : Vec->begin());
Ted Kremenek42400962009-11-26 02:34:36 +0000977}
978
979BlockDataRegion::referenced_vars_iterator
980BlockDataRegion::referenced_vars_end() const {
981 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
982
983 BumpVector<const MemRegion*> *Vec =
984 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000985
Ted Kremenek81cef582009-12-03 08:09:21 +0000986 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
987 NULL : Vec->end());
Ted Kremenek42400962009-11-26 02:34:36 +0000988}