blob: 58006708fa14ac3fa1897bccba706ab5db47bf7e [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();
181 QualType T = getDesugaredValueType(Ctx);
182
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()) {
199 ASTContext& Ctx = ValMgr.getContext();
200 QualType T = getDesugaredValueType(Ctx);
201
202 if (isa<ConstantArrayType>(T))
203 return UnknownVal();
204 }
205
206 return Extent;
207}
208
209DefinedOrUnknownSVal AllocaRegion::getExtent(ValueManager& ValMgr) const {
210 return nonloc::SymbolVal(ValMgr.getSymbolManager().getExtentSymbol(this));
211}
212
213DefinedOrUnknownSVal SymbolicRegion::getExtent(ValueManager& ValMgr) const {
214 return nonloc::SymbolVal(ValMgr.getSymbolManager().getExtentSymbol(this));
215}
216
217DefinedOrUnknownSVal StringRegion::getExtent(ValueManager& ValMgr) const {
218 QualType SizeTy = ValMgr.getContext().getSizeType();
219 return ValMgr.makeIntVal(getStringLiteral()->getByteLength()+1, SizeTy);
220}
221
222//===----------------------------------------------------------------------===//
Ted Kremenek5348f942009-12-14 22:15:06 +0000223// FoldingSet profiling.
224//===----------------------------------------------------------------------===//
225
Ted Kremenek9e240492008-10-04 05:50:14 +0000226void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
227 ID.AddInteger((unsigned)getKind());
228}
229
Ted Kremenek67d12872009-12-07 22:05:27 +0000230void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
231 ID.AddInteger((unsigned)getKind());
232 ID.AddPointer(getStackFrame());
233}
234
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000235void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
236 ID.AddInteger((unsigned)getKind());
237 ID.AddPointer(getCodeRegion());
238}
239
Mike Stump1eb44332009-09-09 15:08:12 +0000240void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
241 const StringLiteral* Str,
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000242 const MemRegion* superRegion) {
243 ID.AddInteger((unsigned) StringRegionKind);
244 ID.AddPointer(Str);
245 ID.AddPointer(superRegion);
246}
247
Ted Kremenek7090ae12008-11-02 00:34:33 +0000248void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000249 const Expr* Ex, unsigned cnt,
250 const MemRegion *) {
Ted Kremenek7090ae12008-11-02 00:34:33 +0000251 ID.AddInteger((unsigned) AllocaRegionKind);
252 ID.AddPointer(Ex);
253 ID.AddInteger(cnt);
254}
255
256void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000257 ProfileRegion(ID, Ex, Cnt, superRegion);
Ted Kremenek7090ae12008-11-02 00:34:33 +0000258}
259
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000260void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
261 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
262}
263
264void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
265 const CompoundLiteralExpr* CL,
266 const MemRegion* superRegion) {
267 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
268 ID.AddPointer(CL);
269 ID.AddPointer(superRegion);
270}
271
Ted Kremenekde0d2632010-01-05 02:18:06 +0000272void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
273 const PointerType *PT,
274 const MemRegion *sRegion) {
275 ID.AddInteger((unsigned) CXXThisRegionKind);
276 ID.AddPointer(PT);
277 ID.AddPointer(sRegion);
278}
279
280void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
281 CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
282}
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000283
Ted Kremenek9e240492008-10-04 05:50:14 +0000284void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
285 const MemRegion* superRegion, Kind k) {
286 ID.AddInteger((unsigned) k);
287 ID.AddPointer(D);
288 ID.AddPointer(superRegion);
289}
290
291void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
292 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
293}
294
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000295void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000296 VarRegion::ProfileRegion(ID, getDecl(), superRegion);
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000297}
298
Ted Kremenek25010132009-06-22 23:13:13 +0000299void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
300 const MemRegion *sreg) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000301 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremenek6d0e2d22008-12-05 02:39:38 +0000302 ID.Add(sym);
Ted Kremenek25010132009-06-22 23:13:13 +0000303 ID.AddPointer(sreg);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000304}
305
306void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek25010132009-06-22 23:13:13 +0000307 SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000308}
309
Ted Kremenekf936f452009-05-04 06:18:28 +0000310void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Mike Stump1eb44332009-09-09 15:08:12 +0000311 QualType ElementType, SVal Idx,
Zhongxing Xu511191c2008-10-21 05:27:10 +0000312 const MemRegion* superRegion) {
313 ID.AddInteger(MemRegion::ElementRegionKind);
Ted Kremenekf936f452009-05-04 06:18:28 +0000314 ID.Add(ElementType);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000315 ID.AddPointer(superRegion);
316 Idx.Profile(ID);
317}
318
319void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf936f452009-05-04 06:18:28 +0000320 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000321}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000322
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000323void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
324 const FunctionDecl *FD,
325 const MemRegion*) {
326 ID.AddInteger(MemRegion::FunctionTextRegionKind);
Ted Kremenekabd46e12009-08-28 04:49:15 +0000327 ID.AddPointer(FD);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000328}
329
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000330void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
331 FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
332}
333
334void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek67d12872009-12-07 22:05:27 +0000335 const BlockDecl *BD, CanQualType,
336 const AnalysisContext *AC,
337 const MemRegion*) {
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000338 ID.AddInteger(MemRegion::BlockTextRegionKind);
339 ID.AddPointer(BD);
340}
341
342void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000343 BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
Zhongxing Xuec13d922009-04-10 08:45:10 +0000344}
345
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000346void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
347 const BlockTextRegion *BC,
348 const LocationContext *LC,
Ted Kremenek67d12872009-12-07 22:05:27 +0000349 const MemRegion *sReg) {
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000350 ID.AddInteger(MemRegion::BlockDataRegionKind);
351 ID.AddPointer(BC);
352 ID.AddPointer(LC);
Ted Kremenek67d12872009-12-07 22:05:27 +0000353 ID.AddPointer(sReg);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000354}
355
356void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000357 BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000358}
359
Zhongxing Xubb141212009-12-16 11:27:52 +0000360void CXXObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000361 Expr const *Ex,
Zhongxing Xubb141212009-12-16 11:27:52 +0000362 const MemRegion *sReg) {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000363 ID.AddPointer(Ex);
Zhongxing Xubb141212009-12-16 11:27:52 +0000364 ID.AddPointer(sReg);
365}
366
367void CXXObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000368 ProfileRegion(ID, Ex, getSuperRegion());
Zhongxing Xubb141212009-12-16 11:27:52 +0000369}
370
Zhongxing Xu026c6632009-02-05 06:57:29 +0000371//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000372// Region pretty-printing.
373//===----------------------------------------------------------------------===//
374
Ted Kremenek8800ad42009-07-13 23:31:04 +0000375void MemRegion::dump() const {
376 dumpToStream(llvm::errs());
Ted Kremenek7f39d292009-07-02 17:24:10 +0000377}
378
Ted Kremenek9e240492008-10-04 05:50:14 +0000379std::string MemRegion::getString() const {
380 std::string s;
381 llvm::raw_string_ostream os(s);
Ted Kremenek8800ad42009-07-13 23:31:04 +0000382 dumpToStream(os);
Ted Kremenek9e240492008-10-04 05:50:14 +0000383 return os.str();
384}
385
Ted Kremenek8800ad42009-07-13 23:31:04 +0000386void MemRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek9e240492008-10-04 05:50:14 +0000387 os << "<Unknown Region>";
388}
389
Ted Kremenek8800ad42009-07-13 23:31:04 +0000390void AllocaRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek7090ae12008-11-02 00:34:33 +0000391 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
392}
393
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000394void FunctionTextRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekabd46e12009-08-28 04:49:15 +0000395 os << "code{" << getDecl()->getDeclName().getAsString() << '}';
Ted Kremenek72e03202009-04-21 19:56:58 +0000396}
397
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000398void BlockTextRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000399 os << "block_code{" << (void*) this << '}';
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000400}
401
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000402void BlockDataRegion::dumpToStream(llvm::raw_ostream& os) const {
403 os << "block_data{" << BC << '}';
404}
405
Ted Kremenek8800ad42009-07-13 23:31:04 +0000406void CompoundLiteralRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000407 // FIXME: More elaborate pretty-printing.
408 os << "{ " << (void*) CL << " }";
409}
410
Ted Kremenekde0d2632010-01-05 02:18:06 +0000411void CXXThisRegion::dumpToStream(llvm::raw_ostream &os) const {
412 os << "this";
413}
414
Ted Kremenek8800ad42009-07-13 23:31:04 +0000415void ElementRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000416 os << "element{" << superRegion << ','
417 << Index << ',' << getElementType().getAsString() << '}';
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000418}
419
Ted Kremenek8800ad42009-07-13 23:31:04 +0000420void FieldRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000421 os << superRegion << "->" << getDecl();
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000422}
423
Ted Kremenekfa87d812010-07-06 23:37:21 +0000424void NonStaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
425 os << "NonStaticGlobalSpaceRegion";
426}
427
Ted Kremenekbcfe03a2009-07-19 20:36:24 +0000428void ObjCIvarRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000429 os << "ivar{" << superRegion << ',' << getDecl() << '}';
Ted Kremenekbcfe03a2009-07-19 20:36:24 +0000430}
431
Mike Stump1eb44332009-09-09 15:08:12 +0000432void StringRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekb27ed3d2009-07-19 20:38:24 +0000433 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions()));
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000434}
435
Ted Kremenek8800ad42009-07-13 23:31:04 +0000436void SymbolicRegion::dumpToStream(llvm::raw_ostream& os) const {
Ted Kremenekaef5d222009-07-13 23:38:57 +0000437 os << "SymRegion{" << sym << '}';
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000438}
439
Ted Kremenek8800ad42009-07-13 23:31:04 +0000440void VarRegion::dumpToStream(llvm::raw_ostream& os) const {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000441 os << cast<VarDecl>(D);
Ted Kremenek9e240492008-10-04 05:50:14 +0000442}
443
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444void RegionRawOffset::dump() const {
445 dumpToStream(llvm::errs());
446}
447
448void RegionRawOffset::dumpToStream(llvm::raw_ostream& os) const {
449 os << "raw_offset{" << getRegion() << ',' << getByteOffset() << '}';
450}
451
Ted Kremenekfa87d812010-07-06 23:37:21 +0000452void StaticGlobalSpaceRegion::dumpToStream(llvm::raw_ostream &os) const {
453 os << "StaticGlobalsMemSpace{" << CR << '}';
454}
455
Ted Kremenek9e240492008-10-04 05:50:14 +0000456//===----------------------------------------------------------------------===//
457// MemRegionManager methods.
458//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000459
Ted Kremenek67d12872009-12-07 22:05:27 +0000460template <typename REG>
461const REG *MemRegionManager::LazyAllocate(REG*& region) {
Mike Stump1eb44332009-09-09 15:08:12 +0000462 if (!region) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000463 region = (REG*) A.Allocate<REG>();
464 new (region) REG(this);
Ted Kremenek9e240492008-10-04 05:50:14 +0000465 }
Ted Kremeneka43484a2009-06-23 00:46:41 +0000466
Ted Kremenek9e240492008-10-04 05:50:14 +0000467 return region;
468}
469
Ted Kremenek67d12872009-12-07 22:05:27 +0000470template <typename REG, typename ARG>
471const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
472 if (!region) {
473 region = (REG*) A.Allocate<REG>();
474 new (region) REG(this, a);
475 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000476
Ted Kremenek67d12872009-12-07 22:05:27 +0000477 return region;
Ted Kremenek9e240492008-10-04 05:50:14 +0000478}
479
Ted Kremenek67d12872009-12-07 22:05:27 +0000480const StackLocalsSpaceRegion*
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000481MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
482 assert(STC);
Zhongxing Xuc30470d2010-02-17 08:46:50 +0000483 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
484
485 if (R)
486 return R;
487
488 R = A.Allocate<StackLocalsSpaceRegion>();
489 new (R) StackLocalsSpaceRegion(this, STC);
490 return R;
Ted Kremenekd05552a2009-07-02 18:14:59 +0000491}
492
Ted Kremenek67d12872009-12-07 22:05:27 +0000493const StackArgumentsSpaceRegion *
494MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000495 assert(STC);
Zhongxing Xuc30470d2010-02-17 08:46:50 +0000496 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
497
498 if (R)
499 return R;
500
501 R = A.Allocate<StackArgumentsSpaceRegion>();
502 new (R) StackArgumentsSpaceRegion(this, STC);
503 return R;
Ted Kremenek67d12872009-12-07 22:05:27 +0000504}
505
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000506const GlobalsSpaceRegion
507*MemRegionManager::getGlobalsRegion(const CodeTextRegion *CR) {
508 if (!CR)
509 return LazyAllocate(globals);
510
511 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
512 if (R)
513 return R;
514
515 R = A.Allocate<StaticGlobalSpaceRegion>();
516 new (R) StaticGlobalSpaceRegion(this, CR);
517 return R;
Ted Kremenek9e240492008-10-04 05:50:14 +0000518}
519
Ted Kremenek67d12872009-12-07 22:05:27 +0000520const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
Ted Kremenek9e240492008-10-04 05:50:14 +0000521 return LazyAllocate(heap);
522}
523
Ted Kremenekb48ad642009-12-04 00:26:31 +0000524const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
Zhongxing Xu17892752008-10-08 02:50:44 +0000525 return LazyAllocate(unknown);
526}
527
Ted Kremenekb48ad642009-12-04 00:26:31 +0000528const MemSpaceRegion *MemRegionManager::getCodeRegion() {
Zhongxing Xuec13d922009-04-10 08:45:10 +0000529 return LazyAllocate(code);
530}
531
Ted Kremenek25010132009-06-22 23:13:13 +0000532//===----------------------------------------------------------------------===//
533// Constructing regions.
534//===----------------------------------------------------------------------===//
535
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000536const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
Ted Kremenek67d12872009-12-07 22:05:27 +0000537 return getSubRegion<StringRegion>(Str, getGlobalsRegion());
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000538}
539
Ted Kremenekb48ad642009-12-04 00:26:31 +0000540const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
541 const LocationContext *LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000542 const MemRegion *sReg = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000544 if (D->hasGlobalStorage() && !D->isStaticLocal())
545 sReg = getGlobalsRegion();
546 else {
Ted Kremenek67d12872009-12-07 22:05:27 +0000547 // FIXME: Once we implement scope handling, we will need to properly lookup
548 // 'D' to the proper LocationContext.
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000549 const DeclContext *DC = D->getDeclContext();
550 const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000552 if (!STC)
553 sReg = getUnknownRegion();
554 else {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000555 if (D->hasLocalStorage()) {
556 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
557 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
558 : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
559 }
560 else {
561 assert(D->isStaticLocal());
562 const Decl *D = STC->getDecl();
563 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
564 sReg = getGlobalsRegion(getFunctionTextRegion(FD));
565 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
566 const BlockTextRegion *BTR =
567 getBlockTextRegion(BD,
568 C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
569 STC->getAnalysisContext());
570 sReg = getGlobalsRegion(BTR);
571 }
572 else {
573 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now
574 // just use the main global memspace.
575 sReg = getGlobalsRegion();
576 }
577 }
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000578 }
Ted Kremenek67d12872009-12-07 22:05:27 +0000579 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000580
Ted Kremenek67d12872009-12-07 22:05:27 +0000581 return getSubRegion<VarRegion>(D, sReg);
582}
583
584const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
585 const MemRegion *superR) {
586 return getSubRegion<VarRegion>(D, superR);
Ted Kremenek9e240492008-10-04 05:50:14 +0000587}
588
Ted Kremenekb48ad642009-12-04 00:26:31 +0000589const BlockDataRegion *
590MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
591 const LocationContext *LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000592 const MemRegion *sReg = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000593
594 if (LC) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000595 // FIXME: Once we implement scope handling, we want the parent region
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000596 // to be the scope.
Ted Kremenek67d12872009-12-07 22:05:27 +0000597 const StackFrameContext *STC = LC->getCurrentStackFrame();
598 assert(STC);
599 sReg = getStackLocalsRegion(STC);
600 }
601 else {
602 // We allow 'LC' to be NULL for cases where want BlockDataRegions
603 // without context-sensitivity.
604 sReg = getUnknownRegion();
605 }
606
607 return getSubRegion<BlockDataRegion>(BC, LC, sReg);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000608}
609
Ted Kremenekb48ad642009-12-04 00:26:31 +0000610const CompoundLiteralRegion*
Ted Kremenek67d12872009-12-07 22:05:27 +0000611MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL,
612 const LocationContext *LC) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000613
Ted Kremenek67d12872009-12-07 22:05:27 +0000614 const MemRegion *sReg = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000615
Ted Kremenek67d12872009-12-07 22:05:27 +0000616 if (CL->isFileScope())
617 sReg = getGlobalsRegion();
618 else {
619 const StackFrameContext *STC = LC->getCurrentStackFrame();
620 assert(STC);
621 sReg = getStackLocalsRegion(STC);
622 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000623
Ted Kremenek67d12872009-12-07 22:05:27 +0000624 return getSubRegion<CompoundLiteralRegion>(CL, sReg);
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000625}
626
Ted Kremenekb48ad642009-12-04 00:26:31 +0000627const ElementRegion*
Ted Kremenekf936f452009-05-04 06:18:28 +0000628MemRegionManager::getElementRegion(QualType elementType, SVal Idx,
Ted Kremenek46537392009-07-16 01:33:37 +0000629 const MemRegion* superRegion,
630 ASTContext& Ctx){
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000631
Ted Kremenek32f90102010-05-27 00:29:00 +0000632 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
Ted Kremenekabb042f2008-12-13 19:24:37 +0000633
Zhongxing Xu511191c2008-10-21 05:27:10 +0000634 llvm::FoldingSetNodeID ID;
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000635 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000636
637 void* InsertPos;
638 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
639 ElementRegion* R = cast_or_null<ElementRegion>(data);
640
641 if (!R) {
642 R = (ElementRegion*) A.Allocate<ElementRegion>();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000643 new (R) ElementRegion(T, Idx, superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +0000644 Regions.InsertNode(R, InsertPos);
645 }
646
647 return R;
648}
649
Ted Kremenekb48ad642009-12-04 00:26:31 +0000650const FunctionTextRegion *
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000651MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000652 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
Zhongxing Xuec13d922009-04-10 08:45:10 +0000653}
654
Ted Kremenekb48ad642009-12-04 00:26:31 +0000655const BlockTextRegion *
Ted Kremenek67d12872009-12-07 22:05:27 +0000656MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
657 AnalysisContext *AC) {
658 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000659}
660
661
Ted Kremenek993f1c72008-10-17 20:28:54 +0000662/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenekb48ad642009-12-04 00:26:31 +0000663const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000664 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000665}
666
Ted Kremenekde0d2632010-01-05 02:18:06 +0000667const FieldRegion*
Ted Kremenekb48ad642009-12-04 00:26:31 +0000668MemRegionManager::getFieldRegion(const FieldDecl* d,
669 const MemRegion* superRegion){
Ted Kremenekeeea4562009-07-10 16:51:45 +0000670 return getSubRegion<FieldRegion>(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000671}
672
Ted Kremenekb48ad642009-12-04 00:26:31 +0000673const ObjCIvarRegion*
Ted Kremenek993f1c72008-10-17 20:28:54 +0000674MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
675 const MemRegion* superRegion) {
Ted Kremenekeeea4562009-07-10 16:51:45 +0000676 return getSubRegion<ObjCIvarRegion>(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000677}
678
Ted Kremenekde0d2632010-01-05 02:18:06 +0000679const CXXObjectRegion*
Zhongxing Xubc37b8d2010-01-09 09:16:47 +0000680MemRegionManager::getCXXObjectRegion(Expr const *E,
681 LocationContext const *LC) {
682 const StackFrameContext *SFC = LC->getCurrentStackFrame();
683 assert(SFC);
684 return getSubRegion<CXXObjectRegion>(E, getStackLocalsRegion(SFC));
Zhongxing Xubb141212009-12-16 11:27:52 +0000685}
686
Ted Kremenekde0d2632010-01-05 02:18:06 +0000687const CXXThisRegion*
688MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
689 const LocationContext *LC) {
690 const StackFrameContext *STC = LC->getCurrentStackFrame();
691 assert(STC);
692 const PointerType *PT = thisPointerTy->getAs<PointerType>();
693 assert(PT);
694 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
695}
696
Ted Kremenekb48ad642009-12-04 00:26:31 +0000697const AllocaRegion*
Ted Kremenek67d12872009-12-07 22:05:27 +0000698MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt,
699 const LocationContext *LC) {
700 const StackFrameContext *STC = LC->getCurrentStackFrame();
701 assert(STC);
702 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
Ted Kremenek7090ae12008-11-02 00:34:33 +0000703}
704
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000705const MemSpaceRegion *MemRegion::getMemorySpace() const {
706 const MemRegion *R = this;
Ted Kremenekea20cd72009-06-23 18:05:21 +0000707 const SubRegion* SR = dyn_cast<SubRegion>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek993f1c72008-10-17 20:28:54 +0000709 while (SR) {
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000710 R = SR->getSuperRegion();
711 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000714 return dyn_cast<MemSpaceRegion>(R);
715}
716
717bool MemRegion::hasStackStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000718 return isa<StackSpaceRegion>(getMemorySpace());
Ted Kremenek9e240492008-10-04 05:50:14 +0000719}
Ted Kremenek1670e402009-04-11 00:11:10 +0000720
Ted Kremenekde0d2632010-01-05 02:18:06 +0000721bool MemRegion::hasStackNonParametersStorage() const {
722 return isa<StackLocalsSpaceRegion>(getMemorySpace());
Zhongxing Xudd198f02009-06-23 02:51:21 +0000723}
Ted Kremenek1670e402009-04-11 00:11:10 +0000724
Ted Kremenekde0d2632010-01-05 02:18:06 +0000725bool MemRegion::hasStackParametersStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000726 return isa<StackArgumentsSpaceRegion>(getMemorySpace());
Ted Kremenekdc147262009-07-02 22:02:15 +0000727}
728
Ted Kremenek15086362009-07-02 18:25:09 +0000729bool MemRegion::hasGlobalsOrParametersStorage() const {
Ted Kremenek67d12872009-12-07 22:05:27 +0000730 const MemSpaceRegion *MS = getMemorySpace();
731 return isa<StackArgumentsSpaceRegion>(MS) ||
732 isa<GlobalsSpaceRegion>(MS);
Ted Kremenek15086362009-07-02 18:25:09 +0000733}
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000734
Zhongxing Xuadca2712009-11-10 02:37:53 +0000735// getBaseRegion strips away all elements and fields, and get the base region
736// of them.
737const MemRegion *MemRegion::getBaseRegion() const {
738 const MemRegion *R = this;
739 while (true) {
Ted Kremenek68b9a592010-04-06 22:06:03 +0000740 switch (R->getKind()) {
741 case MemRegion::ElementRegionKind:
742 case MemRegion::FieldRegionKind:
743 case MemRegion::ObjCIvarRegionKind:
744 R = cast<SubRegion>(R)->getSuperRegion();
745 continue;
746 default:
747 break;
Zhongxing Xuadca2712009-11-10 02:37:53 +0000748 }
749 break;
750 }
751 return R;
752}
753
Ted Kremenek1670e402009-04-11 00:11:10 +0000754//===----------------------------------------------------------------------===//
755// View handling.
756//===----------------------------------------------------------------------===//
757
Zhongxing Xu479529e2009-11-10 02:17:20 +0000758const MemRegion *MemRegion::StripCasts() const {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000759 const MemRegion *R = this;
760 while (true) {
Mike Stump1eb44332009-09-09 15:08:12 +0000761 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000762 // FIXME: generalize. Essentially we want to strip away ElementRegions
763 // that were layered on a symbolic region because of casts. We only
764 // want to strip away ElementRegions, however, where the index is 0.
765 SVal index = ER->getIndex();
766 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000767 if (CI->getValue().getSExtValue() == 0) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000768 R = ER->getSuperRegion();
769 continue;
770 }
771 }
772 }
773 break;
774 }
775 return R;
776}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000777
778// FIXME: Merge with the implementation of the same method in Store.cpp
779static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
780 if (const RecordType *RT = Ty->getAs<RecordType>()) {
781 const RecordDecl *D = RT->getDecl();
Douglas Gregor952b0172010-02-11 01:04:33 +0000782 if (!D->getDefinition())
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000783 return false;
784 }
785
786 return true;
787}
788
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000789RegionRawOffset ElementRegion::getAsArrayOffset() const {
Ken Dyck199c3d62010-01-11 17:06:35 +0000790 CharUnits offset = CharUnits::Zero();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000791 const ElementRegion *ER = this;
792 const MemRegion *superR = NULL;
793 ASTContext &C = getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000795 // FIXME: Handle multi-dimensional arrays.
796
797 while (ER) {
798 superR = ER->getSuperRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000800 // FIXME: generalize to symbolic offsets.
801 SVal index = ER->getIndex();
802 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
803 // Update the offset.
804 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000806 if (i != 0) {
807 QualType elemType = ER->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000809 // If we are pointing to an incomplete type, go no further.
810 if (!IsCompleteType(C, elemType)) {
811 superR = ER;
812 break;
813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ken Dyck199c3d62010-01-11 17:06:35 +0000815 CharUnits size = C.getTypeSizeInChars(elemType);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000816 offset += (i * size);
817 }
818
819 // Go to the next ElementRegion (if any).
820 ER = dyn_cast<ElementRegion>(superR);
821 continue;
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000824 return NULL;
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000827 assert(superR && "super region cannot be NULL");
Ken Dyck199c3d62010-01-11 17:06:35 +0000828 return RegionRawOffset(superR, offset.getQuantity());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000829}
830
Zhongxing Xue8882332010-08-03 04:52:05 +0000831RegionOffset MemRegion::getAsOffset() const {
832 const MemRegion *R = this;
Zhongxing Xue3273e72010-08-03 06:34:25 +0000833 int64_t Offset = 0;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000834
Zhongxing Xue8882332010-08-03 04:52:05 +0000835 while (1) {
836 switch (R->getKind()) {
837 default:
838 return RegionOffset(0);
839 case SymbolicRegionKind:
840 case AllocaRegionKind:
841 case CompoundLiteralRegionKind:
842 case CXXThisRegionKind:
843 case StringRegionKind:
844 case VarRegionKind:
845 case CXXObjectRegionKind:
846 goto Finish;
847 case ElementRegionKind: {
848 const ElementRegion *ER = cast<ElementRegion>(R);
849 QualType EleTy = ER->getValueType(getContext());
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000850
Zhongxing Xue8882332010-08-03 04:52:05 +0000851 if (!IsCompleteType(getContext(), EleTy))
852 return RegionOffset(0);
853
854 SVal Index = ER->getIndex();
855 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) {
856 int64_t i = CI->getValue().getSExtValue();
Zhongxing Xue8882332010-08-03 04:52:05 +0000857 CharUnits Size = getContext().getTypeSizeInChars(EleTy);
858 Offset += i * Size.getQuantity() * 8;
859 } else {
860 // We cannot compute offset for non-concrete index.
861 return RegionOffset(0);
862 }
863 R = ER->getSuperRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000864 break;
Zhongxing Xue8882332010-08-03 04:52:05 +0000865 }
866 case FieldRegionKind: {
867 const FieldRegion *FR = cast<FieldRegion>(R);
868 const RecordDecl *RD = FR->getDecl()->getParent();
869 if (!RD->isDefinition())
870 // We cannot compute offset for incomplete type.
871 return RegionOffset(0);
872 // Get the field number.
873 unsigned idx = 0;
874 for (RecordDecl::field_iterator FI = RD->field_begin(),
875 FE = RD->field_end(); FI != FE; ++FI, ++idx)
876 if (FR->getDecl() == *FI)
877 break;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000878
Zhongxing Xue8882332010-08-03 04:52:05 +0000879 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
880 // This is offset in bits.
881 Offset += Layout.getFieldOffset(idx);
882 R = FR->getSuperRegion();
883 break;
884 }
885 }
886 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000887
Zhongxing Xue8882332010-08-03 04:52:05 +0000888 Finish:
889 return RegionOffset(R, Offset);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000890}
891
Ted Kremenek42400962009-11-26 02:34:36 +0000892//===----------------------------------------------------------------------===//
893// BlockDataRegion
894//===----------------------------------------------------------------------===//
895
896void BlockDataRegion::LazyInitializeReferencedVars() {
897 if (ReferencedVars)
898 return;
899
Ted Kremenek67d12872009-12-07 22:05:27 +0000900 AnalysisContext *AC = getCodeRegion()->getAnalysisContext();
Ted Kremenek42400962009-11-26 02:34:36 +0000901 AnalysisContext::referenced_decls_iterator I, E;
902 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000903
Ted Kremenek42400962009-11-26 02:34:36 +0000904 if (I == E) {
905 ReferencedVars = (void*) 0x1;
906 return;
907 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000908
Ted Kremenek42400962009-11-26 02:34:36 +0000909 MemRegionManager &MemMgr = *getMemRegionManager();
910 llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
911 BumpVectorContext BC(A);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000912
Ted Kremenek42400962009-11-26 02:34:36 +0000913 typedef BumpVector<const MemRegion*> VarVec;
914 VarVec *BV = (VarVec*) A.Allocate<VarVec>();
Ted Kremenek02b1df62009-12-01 22:12:34 +0000915 new (BV) VarVec(BC, E - I);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000916
Ted Kremenek67d12872009-12-07 22:05:27 +0000917 for ( ; I != E; ++I) {
918 const VarDecl *VD = *I;
919 const VarRegion *VR = 0;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000920
Ted Kremenek85248732010-02-06 00:30:00 +0000921 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
Ted Kremenek67d12872009-12-07 22:05:27 +0000922 VR = MemMgr.getVarRegion(VD, this);
923 else {
924 if (LC)
925 VR = MemMgr.getVarRegion(VD, LC);
926 else {
927 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
928 }
929 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000930
Ted Kremenek67d12872009-12-07 22:05:27 +0000931 assert(VR);
932 BV->push_back(VR, BC);
933 }
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000934
Ted Kremenek42400962009-11-26 02:34:36 +0000935 ReferencedVars = BV;
936}
937
938BlockDataRegion::referenced_vars_iterator
939BlockDataRegion::referenced_vars_begin() const {
940 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
941
942 BumpVector<const MemRegion*> *Vec =
943 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000944
Ted Kremenek81cef582009-12-03 08:09:21 +0000945 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
946 NULL : Vec->begin());
Ted Kremenek42400962009-11-26 02:34:36 +0000947}
948
949BlockDataRegion::referenced_vars_iterator
950BlockDataRegion::referenced_vars_end() const {
951 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
952
953 BumpVector<const MemRegion*> *Vec =
954 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000955
Ted Kremenek81cef582009-12-03 08:09:21 +0000956 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
957 NULL : Vec->end());
Ted Kremenek42400962009-11-26 02:34:36 +0000958}