blob: af2f365ead96cd1d948b8617723a7e70404ef5f5 [file] [log] [blame]
Ted Kremenek9e240492008-10-04 05:50:14 +00001//== MemRegion.h - 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
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000016#ifndef LLVM_CLANG_GR_MEMREGION_H
17#define LLVM_CLANG_GR_MEMREGION_H
Ted Kremenek9e240492008-10-04 05:50:14 +000018
Benjamin Kramer478851c2012-07-04 17:04:04 +000019#include "clang/AST/ASTContext.h"
Ken Dyck9ff2b132011-01-24 01:55:39 +000020#include "clang/AST/CharUnits.h"
Ted Kremenek993f1c72008-10-17 20:28:54 +000021#include "clang/AST/Decl.h"
Ted Kremenek4c62b552012-02-28 00:56:05 +000022#include "clang/AST/ExprObjC.h"
Chris Lattnerd47d3b02011-07-23 10:35:09 +000023#include "clang/Basic/LLVM.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
Ted Kremenek9e240492008-10-04 05:50:14 +000025#include "llvm/ADT/FoldingSet.h"
Chandler Carruth30a2e162012-12-04 09:18:49 +000026#include "llvm/Support/ErrorHandling.h"
Ted Kremenek9e240492008-10-04 05:50:14 +000027#include <string>
28
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000029namespace llvm {
30class BumpPtrAllocator;
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000031}
Ted Kremenek9e240492008-10-04 05:50:14 +000032
33namespace clang {
Mike Stump1eb44332009-09-09 15:08:12 +000034
Ted Kremenekd17da2b2009-08-21 22:28:32 +000035class LocationContext;
Ted Kremenek67d12872009-12-07 22:05:27 +000036class StackFrameContext;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000037
Ted Kremenek9ef65372010-12-23 07:20:52 +000038namespace ento {
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000039
40class MemRegionManager;
41class MemSpaceRegion;
Ted Kremenekc8413fd2010-12-02 07:49:45 +000042class SValBuilder;
Ted Kremenek42400962009-11-26 02:34:36 +000043class VarRegion;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000044class CodeTextRegion;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Zhongxing Xue8882332010-08-03 04:52:05 +000046/// Represent a region's offset within the top level base region.
47class RegionOffset {
48 /// The base region.
49 const MemRegion *R;
50
51 /// The bit offset within the base region. It shouldn't be negative.
Zhongxing Xue3273e72010-08-03 06:34:25 +000052 int64_t Offset;
Zhongxing Xue8882332010-08-03 04:52:05 +000053
54public:
Aaron Ballman5375d822012-08-14 16:46:35 +000055 // We're using a const instead of an enumeration due to the size required;
56 // Visual Studio will only create enumerations of size int, not long long.
57 static const int64_t Symbolic = INT64_MAX;
Jordan Rose824e07a2012-08-09 22:55:37 +000058
Jordan Rosee0d24eb2012-08-08 18:23:27 +000059 RegionOffset() : R(0) {}
Zhongxing Xue3273e72010-08-03 06:34:25 +000060 RegionOffset(const MemRegion *r, int64_t off) : R(r), Offset(off) {}
Zhongxing Xue8882332010-08-03 04:52:05 +000061
62 const MemRegion *getRegion() const { return R; }
Jordan Rose824e07a2012-08-09 22:55:37 +000063
64 bool hasSymbolicOffset() const { return Offset == Symbolic; }
65
66 int64_t getOffset() const {
67 assert(!hasSymbolicOffset());
68 return Offset;
69 }
Jordan Rosee0d24eb2012-08-08 18:23:27 +000070
71 bool isValid() const { return R; }
Zhongxing Xue8882332010-08-03 04:52:05 +000072};
73
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000074//===----------------------------------------------------------------------===//
75// Base region classes.
76//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremenek9e240492008-10-04 05:50:14 +000078/// MemRegion - The root abstract class for all memory regions.
79class MemRegion : public llvm::FoldingSetNode {
Ted Kremenek42400962009-11-26 02:34:36 +000080 friend class MemRegionManager;
Ted Kremenek9e240492008-10-04 05:50:14 +000081public:
Ted Kremenek67d12872009-12-07 22:05:27 +000082 enum Kind {
83 // Memory spaces.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000084 GenericMemSpaceRegionKind,
Ted Kremenek67d12872009-12-07 22:05:27 +000085 StackLocalsSpaceRegionKind,
86 StackArgumentsSpaceRegionKind,
87 HeapSpaceRegionKind,
Ted Kremenek2b87ae42009-12-11 06:43:27 +000088 UnknownSpaceRegionKind,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000089 StaticGlobalSpaceRegionKind,
Anna Zakseb31a762012-01-04 23:54:01 +000090 GlobalInternalSpaceRegionKind,
91 GlobalSystemSpaceRegionKind,
92 GlobalImmutableSpaceRegionKind,
93 BEG_NON_STATIC_GLOBAL_MEMSPACES = GlobalInternalSpaceRegionKind,
94 END_NON_STATIC_GLOBAL_MEMSPACES = GlobalImmutableSpaceRegionKind,
95 BEG_GLOBAL_MEMSPACES = StaticGlobalSpaceRegionKind,
96 END_GLOBAL_MEMSPACES = GlobalImmutableSpaceRegionKind,
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000097 BEG_MEMSPACES = GenericMemSpaceRegionKind,
Anna Zakseb31a762012-01-04 23:54:01 +000098 END_MEMSPACES = GlobalImmutableSpaceRegionKind,
Ted Kremenek67d12872009-12-07 22:05:27 +000099 // Untyped regions.
100 SymbolicRegionKind,
101 AllocaRegionKind,
102 // Typed regions.
103 BEG_TYPED_REGIONS,
104 FunctionTextRegionKind = BEG_TYPED_REGIONS,
105 BlockTextRegionKind,
Jordan Rose8ecf59a2012-08-17 20:16:34 +0000106 BlockDataRegionKind,
Ted Kremenek96979342011-08-12 20:02:48 +0000107 BEG_TYPED_VALUE_REGIONS,
108 CompoundLiteralRegionKind = BEG_TYPED_VALUE_REGIONS,
Ted Kremenekde0d2632010-01-05 02:18:06 +0000109 CXXThisRegionKind,
Ted Kremenek67d12872009-12-07 22:05:27 +0000110 StringRegionKind,
Ted Kremenek4c62b552012-02-28 00:56:05 +0000111 ObjCStringRegionKind,
Ted Kremenek67d12872009-12-07 22:05:27 +0000112 ElementRegionKind,
113 // Decl Regions.
114 BEG_DECL_REGIONS,
115 VarRegionKind = BEG_DECL_REGIONS,
116 FieldRegionKind,
117 ObjCIvarRegionKind,
Zhongxing Xu4fd56812010-11-26 08:21:53 +0000118 END_DECL_REGIONS = ObjCIvarRegionKind,
Zhongxing Xu02fe28c2010-11-26 08:52:48 +0000119 CXXTempObjectRegionKind,
Zhongxing Xu4fd56812010-11-26 08:21:53 +0000120 CXXBaseObjectRegionKind,
Jordy Rose096aef92011-08-12 21:41:07 +0000121 END_TYPED_VALUE_REGIONS = CXXBaseObjectRegionKind,
122 END_TYPED_REGIONS = CXXBaseObjectRegionKind
Ted Kremenek67d12872009-12-07 22:05:27 +0000123 };
124
Ted Kremenek9e240492008-10-04 05:50:14 +0000125private:
126 const Kind kind;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek9e240492008-10-04 05:50:14 +0000128protected:
129 MemRegion(Kind k) : kind(k) {}
130 virtual ~MemRegion();
131
132public:
Ted Kremeneka6275a52009-07-15 02:31:43 +0000133 ASTContext &getContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenek9e240492008-10-04 05:50:14 +0000135 virtual void Profile(llvm::FoldingSetNodeID& ID) const = 0;
Ted Kremeneka43484a2009-06-23 00:46:41 +0000136
137 virtual MemRegionManager* getMemRegionManager() const = 0;
138
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000139 const MemSpaceRegion *getMemorySpace() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Zhongxing Xuadca2712009-11-10 02:37:53 +0000141 const MemRegion *getBaseRegion() const;
142
Anna Zaks522fc212012-09-12 22:57:30 +0000143 /// Check if the region is a subregion of the given region.
Anna Zaks5f7c0ad2012-09-13 00:37:12 +0000144 virtual bool isSubRegionOf(const MemRegion *R) const;
Anna Zaks522fc212012-09-12 22:57:30 +0000145
Jordan Roseb11a3ad2012-08-13 22:11:34 +0000146 const MemRegion *StripCasts(bool StripBaseCasts = true) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenek15086362009-07-02 18:25:09 +0000148 bool hasGlobalsOrParametersStorage() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenekde0d2632010-01-05 02:18:06 +0000150 bool hasStackStorage() const;
151
152 bool hasStackNonParametersStorage() const;
153
154 bool hasStackParametersStorage() const;
Zhongxing Xub21ff772008-10-24 06:30:07 +0000155
Zhongxing Xue8882332010-08-03 04:52:05 +0000156 /// Compute the offset within the top level memory object.
157 RegionOffset getAsOffset() const;
158
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000159 /// \brief Get a string representation of a region for debug use.
160 std::string getString() const;
161
Ted Kremenek9c378f72011-08-12 23:37:29 +0000162 virtual void dumpToStream(raw_ostream &os) const;
Ted Kremenek7f39d292009-07-02 17:24:10 +0000163
Ted Kremenek8800ad42009-07-13 23:31:04 +0000164 void dump() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Jordan Rose919e8a12012-08-08 18:23:36 +0000166 /// \brief Returns true if this region can be printed in a user-friendly way.
167 virtual bool canPrintPretty() const;
168
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000169 /// \brief Print the region for use in diagnostics.
Jordan Rose919e8a12012-08-08 18:23:36 +0000170 virtual void printPretty(raw_ostream &os) const;
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000171
Mike Stump1eb44332009-09-09 15:08:12 +0000172 Kind getKind() const { return kind; }
173
Ted Kremeneked65d3d2009-03-09 20:28:08 +0000174 template<typename RegionTy> const RegionTy* getAs() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Zhongxing Xuf0f06052009-06-29 05:44:14 +0000176 virtual bool isBoundable() const { return false; }
Ted Kremenek9e240492008-10-04 05:50:14 +0000177};
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Anna Zakseb31a762012-01-04 23:54:01 +0000179/// MemSpaceRegion - A memory region that represents a "memory space";
Ted Kremenek9e240492008-10-04 05:50:14 +0000180/// for example, the set of global variables, the stack frame, etc.
181class MemSpaceRegion : public MemRegion {
Ted Kremeneka43484a2009-06-23 00:46:41 +0000182protected:
Ted Kremenek67d12872009-12-07 22:05:27 +0000183 friend class MemRegionManager;
184
Ted Kremeneka43484a2009-06-23 00:46:41 +0000185 MemRegionManager *Mgr;
186
Ted Kremenek67d12872009-12-07 22:05:27 +0000187 MemSpaceRegion(MemRegionManager *mgr, Kind k = GenericMemSpaceRegionKind)
188 : MemRegion(k), Mgr(mgr) {
189 assert(classof(this));
Ted Kremeneka43484a2009-06-23 00:46:41 +0000190 }
191
Ted Kremenek67d12872009-12-07 22:05:27 +0000192 MemRegionManager* getMemRegionManager() const { return Mgr; }
193
Ted Kremenek9e240492008-10-04 05:50:14 +0000194public:
Ted Kremeneka43484a2009-06-23 00:46:41 +0000195 bool isBoundable() const { return false; }
Ted Kremenek67d12872009-12-07 22:05:27 +0000196
197 void Profile(llvm::FoldingSetNodeID &ID) const;
Ted Kremenek41168ea2009-03-04 02:43:08 +0000198
Ted Kremenek67d12872009-12-07 22:05:27 +0000199 static bool classof(const MemRegion *R) {
200 Kind k = R->getKind();
201 return k >= BEG_MEMSPACES && k <= END_MEMSPACES;
202 }
203};
204
205class GlobalsSpaceRegion : public MemSpaceRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000206 virtual void anchor();
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000207protected:
208 GlobalsSpaceRegion(MemRegionManager *mgr, Kind k)
209 : MemSpaceRegion(mgr, k) {}
Ted Kremenek67d12872009-12-07 22:05:27 +0000210public:
211 static bool classof(const MemRegion *R) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000212 Kind k = R->getKind();
213 return k >= BEG_GLOBAL_MEMSPACES && k <= END_GLOBAL_MEMSPACES;
214 }
215};
Anna Zakseb31a762012-01-04 23:54:01 +0000216
David Blaikie914edfb2012-05-02 21:58:03 +0000217/// \brief The region of the static variables within the current CodeTextRegion
Anna Zakseb31a762012-01-04 23:54:01 +0000218/// scope.
David Blaikie914edfb2012-05-02 21:58:03 +0000219///
Anna Zakseb31a762012-01-04 23:54:01 +0000220/// Currently, only the static locals are placed there, so we know that these
221/// variables do not get invalidated by calls to other functions.
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000222class StaticGlobalSpaceRegion : public GlobalsSpaceRegion {
223 friend class MemRegionManager;
224
225 const CodeTextRegion *CR;
226
227 StaticGlobalSpaceRegion(MemRegionManager *mgr, const CodeTextRegion *cr)
228 : GlobalsSpaceRegion(mgr, StaticGlobalSpaceRegionKind), CR(cr) {}
229
230public:
231 void Profile(llvm::FoldingSetNodeID &ID) const;
232
Ted Kremenek9c378f72011-08-12 23:37:29 +0000233 void dumpToStream(raw_ostream &os) const;
Ted Kremenekfa87d812010-07-06 23:37:21 +0000234
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000235 const CodeTextRegion *getCodeRegion() const { return CR; }
236
237 static bool classof(const MemRegion *R) {
238 return R->getKind() == StaticGlobalSpaceRegionKind;
239 }
240};
Anna Zakseb31a762012-01-04 23:54:01 +0000241
David Blaikie914edfb2012-05-02 21:58:03 +0000242/// \brief The region for all the non-static global variables.
Anna Zakseb31a762012-01-04 23:54:01 +0000243///
244/// This class is further split into subclasses for efficient implementation of
245/// invalidating a set of related global values as is done in
246/// RegionStoreManager::invalidateRegions (instead of finding all the dependent
247/// globals, we invalidate the whole parent region).
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000248class NonStaticGlobalSpaceRegion : public GlobalsSpaceRegion {
249 friend class MemRegionManager;
250
Anna Zakseb31a762012-01-04 23:54:01 +0000251protected:
252 NonStaticGlobalSpaceRegion(MemRegionManager *mgr, Kind k)
253 : GlobalsSpaceRegion(mgr, k) {}
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000254
255public:
Ted Kremenekfa87d812010-07-06 23:37:21 +0000256
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000257 static bool classof(const MemRegion *R) {
Anna Zakseb31a762012-01-04 23:54:01 +0000258 Kind k = R->getKind();
259 return k >= BEG_NON_STATIC_GLOBAL_MEMSPACES &&
260 k <= END_NON_STATIC_GLOBAL_MEMSPACES;
Ted Kremenek67d12872009-12-07 22:05:27 +0000261 }
262};
Anna Zakseb31a762012-01-04 23:54:01 +0000263
David Blaikie914edfb2012-05-02 21:58:03 +0000264/// \brief The region containing globals which are defined in system/external
Anna Zakseb31a762012-01-04 23:54:01 +0000265/// headers and are considered modifiable by system calls (ex: errno).
266class GlobalSystemSpaceRegion : public NonStaticGlobalSpaceRegion {
267 friend class MemRegionManager;
268
269 GlobalSystemSpaceRegion(MemRegionManager *mgr)
270 : NonStaticGlobalSpaceRegion(mgr, GlobalSystemSpaceRegionKind) {}
271
272public:
273
274 void dumpToStream(raw_ostream &os) const;
275
276 static bool classof(const MemRegion *R) {
277 return R->getKind() == GlobalSystemSpaceRegionKind;
278 }
279};
280
David Blaikie914edfb2012-05-02 21:58:03 +0000281/// \brief The region containing globals which are considered not to be modified
Anna Zakseb31a762012-01-04 23:54:01 +0000282/// or point to data which could be modified as a result of a function call
283/// (system or internal). Ex: Const global scalars would be modeled as part of
284/// this region. This region also includes most system globals since they have
285/// low chance of being modified.
286class GlobalImmutableSpaceRegion : public NonStaticGlobalSpaceRegion {
287 friend class MemRegionManager;
288
289 GlobalImmutableSpaceRegion(MemRegionManager *mgr)
290 : NonStaticGlobalSpaceRegion(mgr, GlobalImmutableSpaceRegionKind) {}
291
292public:
293
294 void dumpToStream(raw_ostream &os) const;
295
296 static bool classof(const MemRegion *R) {
297 return R->getKind() == GlobalImmutableSpaceRegionKind;
298 }
299};
300
David Blaikie914edfb2012-05-02 21:58:03 +0000301/// \brief The region containing globals which can be modified by calls to
Anna Zakseb31a762012-01-04 23:54:01 +0000302/// "internally" defined functions - (for now just) functions other then system
303/// calls.
304class GlobalInternalSpaceRegion : public NonStaticGlobalSpaceRegion {
305 friend class MemRegionManager;
306
307 GlobalInternalSpaceRegion(MemRegionManager *mgr)
308 : NonStaticGlobalSpaceRegion(mgr, GlobalInternalSpaceRegionKind) {}
309
310public:
311
312 void dumpToStream(raw_ostream &os) const;
313
314 static bool classof(const MemRegion *R) {
315 return R->getKind() == GlobalInternalSpaceRegionKind;
316 }
317};
318
Ted Kremenek67d12872009-12-07 22:05:27 +0000319class HeapSpaceRegion : public MemSpaceRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000320 virtual void anchor();
Ted Kremenek67d12872009-12-07 22:05:27 +0000321 friend class MemRegionManager;
322
323 HeapSpaceRegion(MemRegionManager *mgr)
324 : MemSpaceRegion(mgr, HeapSpaceRegionKind) {}
325public:
Jordan Rose36397dc2012-06-06 20:47:00 +0000326
327 void dumpToStream(raw_ostream &os) const;
328
Ted Kremenek67d12872009-12-07 22:05:27 +0000329 static bool classof(const MemRegion *R) {
330 return R->getKind() == HeapSpaceRegionKind;
331 }
332};
333
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000334class UnknownSpaceRegion : public MemSpaceRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000335 virtual void anchor();
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000336 friend class MemRegionManager;
337 UnknownSpaceRegion(MemRegionManager *mgr)
338 : MemSpaceRegion(mgr, UnknownSpaceRegionKind) {}
339public:
Jordan Rose36397dc2012-06-06 20:47:00 +0000340
341 void dumpToStream(raw_ostream &os) const;
342
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000343 static bool classof(const MemRegion *R) {
344 return R->getKind() == UnknownSpaceRegionKind;
345 }
346};
347
Ted Kremenek67d12872009-12-07 22:05:27 +0000348class StackSpaceRegion : public MemSpaceRegion {
349private:
350 const StackFrameContext *SFC;
351
352protected:
353 StackSpaceRegion(MemRegionManager *mgr, Kind k, const StackFrameContext *sfc)
354 : MemSpaceRegion(mgr, k), SFC(sfc) {
355 assert(classof(this));
356 }
357
358public:
359 const StackFrameContext *getStackFrame() const { return SFC; }
360
361 void Profile(llvm::FoldingSetNodeID &ID) const;
362
363 static bool classof(const MemRegion *R) {
364 Kind k = R->getKind();
365 return k >= StackLocalsSpaceRegionKind &&
366 k <= StackArgumentsSpaceRegionKind;
367 }
368};
369
370class StackLocalsSpaceRegion : public StackSpaceRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000371 virtual void anchor();
Ted Kremenek67d12872009-12-07 22:05:27 +0000372 friend class MemRegionManager;
373 StackLocalsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc)
374 : StackSpaceRegion(mgr, StackLocalsSpaceRegionKind, sfc) {}
375public:
Jordan Rose36397dc2012-06-06 20:47:00 +0000376
377 void dumpToStream(raw_ostream &os) const;
378
Ted Kremenek67d12872009-12-07 22:05:27 +0000379 static bool classof(const MemRegion *R) {
380 return R->getKind() == StackLocalsSpaceRegionKind;
381 }
382};
383
384class StackArgumentsSpaceRegion : public StackSpaceRegion {
385private:
David Blaikie99ba9e32011-12-20 02:48:34 +0000386 virtual void anchor();
Ted Kremenek67d12872009-12-07 22:05:27 +0000387 friend class MemRegionManager;
388 StackArgumentsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc)
389 : StackSpaceRegion(mgr, StackArgumentsSpaceRegionKind, sfc) {}
390public:
Jordan Rose36397dc2012-06-06 20:47:00 +0000391
392 void dumpToStream(raw_ostream &os) const;
393
Ted Kremenek67d12872009-12-07 22:05:27 +0000394 static bool classof(const MemRegion *R) {
395 return R->getKind() == StackArgumentsSpaceRegionKind;
Ted Kremenek9e240492008-10-04 05:50:14 +0000396 }
397};
398
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000399
Ted Kremenek993f1c72008-10-17 20:28:54 +0000400/// SubRegion - A region that subsets another larger region. Most regions
401/// are subclasses of SubRegion.
402class SubRegion : public MemRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000403private:
404 virtual void anchor();
Ted Kremenek9e240492008-10-04 05:50:14 +0000405protected:
Mike Stump1eb44332009-09-09 15:08:12 +0000406 const MemRegion* superRegion;
Ted Kremenek993f1c72008-10-17 20:28:54 +0000407 SubRegion(const MemRegion* sReg, Kind k) : MemRegion(k), superRegion(sReg) {}
Ted Kremenek9e240492008-10-04 05:50:14 +0000408public:
Ted Kremenek9e240492008-10-04 05:50:14 +0000409 const MemRegion* getSuperRegion() const {
410 return superRegion;
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Jordy Rose32f26562010-07-04 00:00:41 +0000413 /// getExtent - Returns the size of the region in bytes.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000414 virtual DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const {
Jordy Rose32f26562010-07-04 00:00:41 +0000415 return UnknownVal();
416 }
417
Ted Kremeneka43484a2009-06-23 00:46:41 +0000418 MemRegionManager* getMemRegionManager() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Anna Zaks5f7c0ad2012-09-13 00:37:12 +0000420 virtual bool isSubRegionOf(const MemRegion* R) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek9e240492008-10-04 05:50:14 +0000422 static bool classof(const MemRegion* R) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000423 return R->getKind() > END_MEMSPACES;
Ted Kremenek993f1c72008-10-17 20:28:54 +0000424 }
425};
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000427//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428// MemRegion subclasses.
Mike Stump1eb44332009-09-09 15:08:12 +0000429//===----------------------------------------------------------------------===//
Zhongxing Xuec13d922009-04-10 08:45:10 +0000430
Ted Kremenek82bd99f2008-11-13 15:42:31 +0000431/// AllocaRegion - A region that represents an untyped blob of bytes created
432/// by a call to 'alloca'.
433class AllocaRegion : public SubRegion {
434 friend class MemRegionManager;
435protected:
436 unsigned Cnt; // Block counter. Used to distinguish different pieces of
437 // memory allocated by alloca at the same call site.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000438 const Expr *Ex;
Ted Kremenek82bd99f2008-11-13 15:42:31 +0000439
Ted Kremenek9c378f72011-08-12 23:37:29 +0000440 AllocaRegion(const Expr *ex, unsigned cnt, const MemRegion *superRegion)
Ted Kremenek82bd99f2008-11-13 15:42:31 +0000441 : SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremenek82bd99f2008-11-13 15:42:31 +0000443public:
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek9c378f72011-08-12 23:37:29 +0000445 const Expr *getExpr() const { return Ex; }
Zhongxing Xu9852b5b2009-07-06 05:34:56 +0000446
447 bool isBoundable() const { return true; }
448
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000449 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
Jordy Rose32f26562010-07-04 00:00:41 +0000450
Ted Kremenek82bd99f2008-11-13 15:42:31 +0000451 void Profile(llvm::FoldingSetNodeID& ID) const;
452
Ted Kremenek9c378f72011-08-12 23:37:29 +0000453 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Expr *Ex,
Ted Kremenek7ae7ad92009-06-23 00:15:41 +0000454 unsigned Cnt, const MemRegion *superRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek9c378f72011-08-12 23:37:29 +0000456 void dumpToStream(raw_ostream &os) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek82bd99f2008-11-13 15:42:31 +0000458 static bool classof(const MemRegion* R) {
459 return R->getKind() == AllocaRegionKind;
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461};
462
Ted Kremenek993f1c72008-10-17 20:28:54 +0000463/// TypedRegion - An abstract class representing regions that are typed.
464class TypedRegion : public SubRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000465public:
466 virtual void anchor();
Ted Kremenek993f1c72008-10-17 20:28:54 +0000467protected:
468 TypedRegion(const MemRegion* sReg, Kind k) : SubRegion(sReg, k) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremenek993f1c72008-10-17 20:28:54 +0000470public:
Ted Kremenek96979342011-08-12 20:02:48 +0000471 virtual QualType getLocationType() const = 0;
472
473 QualType getDesugaredLocationType(ASTContext &Context) const {
474 return getLocationType().getDesugaredType(Context);
475 }
476
477 bool isBoundable() const { return true; }
478
479 static bool classof(const MemRegion* R) {
480 unsigned k = R->getKind();
481 return k >= BEG_TYPED_REGIONS && k <= END_TYPED_REGIONS;
482 }
483};
484
485/// TypedValueRegion - An abstract class representing regions having a typed value.
486class TypedValueRegion : public TypedRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000487public:
488 virtual void anchor();
Ted Kremenek96979342011-08-12 20:02:48 +0000489protected:
490 TypedValueRegion(const MemRegion* sReg, Kind k) : TypedRegion(sReg, k) {}
491
492public:
Zhongxing Xu018220c2010-08-11 06:10:55 +0000493 virtual QualType getValueType() const = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Zhongxing Xu018220c2010-08-11 06:10:55 +0000495 virtual QualType getLocationType() const {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000496 // FIXME: We can possibly optimize this later to cache this value.
Ted Kremeneka6b0b962010-12-24 08:39:33 +0000497 QualType T = getValueType();
498 ASTContext &ctx = getContext();
499 if (T->getAs<ObjCObjectType>())
500 return ctx.getObjCObjectPointerType(T);
501 return ctx.getPointerType(getValueType());
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
John McCall49f4e1c2010-12-10 11:01:00 +0000504 QualType getDesugaredValueType(ASTContext &Context) const {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000505 QualType T = getValueType();
Douglas Gregor1ab55e92010-12-10 17:03:06 +0000506 return T.getTypePtrOrNull() ? T.getDesugaredType(Context) : T;
Ted Kremenek14553ab2009-01-30 00:08:43 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Jordan Rosee0d24eb2012-08-08 18:23:27 +0000509 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
510
Ted Kremenek993f1c72008-10-17 20:28:54 +0000511 static bool classof(const MemRegion* R) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000512 unsigned k = R->getKind();
Ted Kremenek96979342011-08-12 20:02:48 +0000513 return k >= BEG_TYPED_VALUE_REGIONS && k <= END_TYPED_VALUE_REGIONS;
Ted Kremenek9e240492008-10-04 05:50:14 +0000514 }
515};
516
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000517
Zhongxing Xuec13d922009-04-10 08:45:10 +0000518class CodeTextRegion : public TypedRegion {
David Blaikie99ba9e32011-12-20 02:48:34 +0000519public:
520 virtual void anchor();
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000521protected:
522 CodeTextRegion(const MemRegion *sreg, Kind k) : TypedRegion(sreg, k) {}
Zhongxing Xuec13d922009-04-10 08:45:10 +0000523public:
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000524 bool isBoundable() const { return false; }
525
526 static bool classof(const MemRegion* R) {
527 Kind k = R->getKind();
528 return k >= FunctionTextRegionKind && k <= BlockTextRegionKind;
529 }
530};
Zhongxing Xuec13d922009-04-10 08:45:10 +0000531
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000532/// FunctionTextRegion - A region that represents code texts of function.
533class FunctionTextRegion : public CodeTextRegion {
Anna Zaks5fc1d0c2012-09-17 19:13:56 +0000534 const NamedDecl *FD;
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000535public:
Anna Zaks5fc1d0c2012-09-17 19:13:56 +0000536 FunctionTextRegion(const NamedDecl *fd, const MemRegion* sreg)
537 : CodeTextRegion(sreg, FunctionTextRegionKind), FD(fd) {
538 assert(isa<ObjCMethodDecl>(fd) || isa<FunctionDecl>(fd));
Zhongxing Xuec13d922009-04-10 08:45:10 +0000539 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000540
Anna Zaks5fc1d0c2012-09-17 19:13:56 +0000541 QualType getLocationType() const {
542 const ASTContext &Ctx = getContext();
543 if (const FunctionDecl *D = dyn_cast<FunctionDecl>(FD)) {
544 return Ctx.getPointerType(D->getType());
545 }
546
547 assert(isa<ObjCMethodDecl>(FD));
548 assert(false && "Getting the type of ObjCMethod is not supported yet");
549
550 // TODO: We might want to return a different type here (ex: id (*ty)(...))
551 // depending on how it is used.
552 return QualType();
553 }
554
555 const NamedDecl *getDecl() const {
Ted Kremenekabd46e12009-08-28 04:49:15 +0000556 return FD;
Zhongxing Xu369f4472009-04-20 05:24:46 +0000557 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000558
Ted Kremenek9c378f72011-08-12 23:37:29 +0000559 virtual void dumpToStream(raw_ostream &os) const;
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000560
Zhongxing Xuec13d922009-04-10 08:45:10 +0000561 void Profile(llvm::FoldingSetNodeID& ID) const;
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000562
Anna Zaks5fc1d0c2012-09-17 19:13:56 +0000563 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const NamedDecl *FD,
Ted Kremenekabd46e12009-08-28 04:49:15 +0000564 const MemRegion*);
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000565
Zhongxing Xuec13d922009-04-10 08:45:10 +0000566 static bool classof(const MemRegion* R) {
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000567 return R->getKind() == FunctionTextRegionKind;
568 }
569};
570
571
572/// BlockTextRegion - A region that represents code texts of blocks (closures).
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000573/// Blocks are represented with two kinds of regions. BlockTextRegions
574/// represent the "code", while BlockDataRegions represent instances of blocks,
575/// which correspond to "code+data". The distinction is important, because
576/// like a closure a block captures the values of externally referenced
577/// variables.
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000578class BlockTextRegion : public CodeTextRegion {
Ted Kremenek67d12872009-12-07 22:05:27 +0000579 friend class MemRegionManager;
580
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000581 const BlockDecl *BD;
Ted Kremenek1d26f482011-10-24 01:32:45 +0000582 AnalysisDeclContext *AC;
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000583 CanQualType locTy;
Ted Kremenek67d12872009-12-07 22:05:27 +0000584
585 BlockTextRegion(const BlockDecl *bd, CanQualType lTy,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000586 AnalysisDeclContext *ac, const MemRegion* sreg)
Ted Kremenek67d12872009-12-07 22:05:27 +0000587 : CodeTextRegion(sreg, BlockTextRegionKind), BD(bd), AC(ac), locTy(lTy) {}
588
589public:
Zhongxing Xu018220c2010-08-11 06:10:55 +0000590 QualType getLocationType() const {
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000591 return locTy;
592 }
593
594 const BlockDecl *getDecl() const {
595 return BD;
596 }
Ted Kremenek67d12872009-12-07 22:05:27 +0000597
Ted Kremenek1d26f482011-10-24 01:32:45 +0000598 AnalysisDeclContext *getAnalysisDeclContext() const { return AC; }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000599
Ted Kremenek9c378f72011-08-12 23:37:29 +0000600 virtual void dumpToStream(raw_ostream &os) const;
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000601
602 void Profile(llvm::FoldingSetNodeID& ID) const;
603
604 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const BlockDecl *BD,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000605 CanQualType, const AnalysisDeclContext*,
Ted Kremenek67d12872009-12-07 22:05:27 +0000606 const MemRegion*);
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000607
608 static bool classof(const MemRegion* R) {
609 return R->getKind() == BlockTextRegionKind;
Zhongxing Xuec13d922009-04-10 08:45:10 +0000610 }
611};
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000612
613/// BlockDataRegion - A region that represents a block instance.
614/// Blocks are represented with two kinds of regions. BlockTextRegions
615/// represent the "code", while BlockDataRegions represent instances of blocks,
616/// which correspond to "code+data". The distinction is important, because
617/// like a closure a block captures the values of externally referenced
618/// variables.
Jordan Rose8ecf59a2012-08-17 20:16:34 +0000619class BlockDataRegion : public TypedRegion {
Ted Kremenek67d12872009-12-07 22:05:27 +0000620 friend class MemRegionManager;
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000621 const BlockTextRegion *BC;
Ted Kremenek67d12872009-12-07 22:05:27 +0000622 const LocationContext *LC; // Can be null */
Ted Kremenek42400962009-11-26 02:34:36 +0000623 void *ReferencedVars;
Ted Kremenek85d87df2012-05-04 21:48:42 +0000624 void *OriginalVars;
Ted Kremenek67d12872009-12-07 22:05:27 +0000625
626 BlockDataRegion(const BlockTextRegion *bc, const LocationContext *lc,
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000627 const MemRegion *sreg)
Jordan Rose8ecf59a2012-08-17 20:16:34 +0000628 : TypedRegion(sreg, BlockDataRegionKind), BC(bc), LC(lc),
Ted Kremenek85d87df2012-05-04 21:48:42 +0000629 ReferencedVars(0), OriginalVars(0) {}
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000630
Ted Kremenek85d87df2012-05-04 21:48:42 +0000631public:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000632 const BlockTextRegion *getCodeRegion() const { return BC; }
Ted Kremenek42400962009-11-26 02:34:36 +0000633
Ted Kremenekd02e83a2009-12-04 02:03:51 +0000634 const BlockDecl *getDecl() const { return BC->getDecl(); }
Jordan Rose8ecf59a2012-08-17 20:16:34 +0000635
636 QualType getLocationType() const { return BC->getLocationType(); }
Ted Kremenekd02e83a2009-12-04 02:03:51 +0000637
Ted Kremenek81cef582009-12-03 08:09:21 +0000638 class referenced_vars_iterator {
639 const MemRegion * const *R;
Ted Kremenek85d87df2012-05-04 21:48:42 +0000640 const MemRegion * const *OriginalR;
Ted Kremenek81cef582009-12-03 08:09:21 +0000641 public:
Ted Kremenek85d87df2012-05-04 21:48:42 +0000642 explicit referenced_vars_iterator(const MemRegion * const *r,
643 const MemRegion * const *originalR)
644 : R(r), OriginalR(originalR) {}
Ted Kremenek85d87df2012-05-04 21:48:42 +0000645
Ted Kremeneke3ce2c12012-12-06 07:17:20 +0000646 const VarRegion *getCapturedRegion() const {
Ted Kremenek81cef582009-12-03 08:09:21 +0000647 return cast<VarRegion>(*R);
648 }
Ted Kremeneke3ce2c12012-12-06 07:17:20 +0000649 const VarRegion *getOriginalRegion() const {
650 return cast<VarRegion>(*OriginalR);
651 }
652
Ted Kremenek81cef582009-12-03 08:09:21 +0000653 bool operator==(const referenced_vars_iterator &I) const {
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000654 assert((R == 0) == (I.R == 0));
Ted Kremenek81cef582009-12-03 08:09:21 +0000655 return I.R == R;
656 }
657 bool operator!=(const referenced_vars_iterator &I) const {
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000658 assert((R == 0) == (I.R == 0));
Ted Kremenek81cef582009-12-03 08:09:21 +0000659 return I.R != R;
660 }
Ted Kremenek9c378f72011-08-12 23:37:29 +0000661 referenced_vars_iterator &operator++() {
Ted Kremenek81cef582009-12-03 08:09:21 +0000662 ++R;
Ted Kremenek85d87df2012-05-04 21:48:42 +0000663 ++OriginalR;
Ted Kremenek81cef582009-12-03 08:09:21 +0000664 return *this;
665 }
666 };
Ted Kremenek58467202013-02-05 22:00:19 +0000667
668 /// Return the original region for a captured region, if
669 /// one exists.
670 const VarRegion *getOriginalRegion(const VarRegion *VR) const;
Ted Kremenek81cef582009-12-03 08:09:21 +0000671
Ted Kremenek42400962009-11-26 02:34:36 +0000672 referenced_vars_iterator referenced_vars_begin() const;
673 referenced_vars_iterator referenced_vars_end() const;
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000674
Ted Kremenek9c378f72011-08-12 23:37:29 +0000675 virtual void dumpToStream(raw_ostream &os) const;
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000676
677 void Profile(llvm::FoldingSetNodeID& ID) const;
678
Ted Kremenek67d12872009-12-07 22:05:27 +0000679 static void ProfileRegion(llvm::FoldingSetNodeID&, const BlockTextRegion *,
680 const LocationContext *, const MemRegion *);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000681
682 static bool classof(const MemRegion* R) {
683 return R->getKind() == BlockDataRegionKind;
684 }
Ted Kremenek42400962009-11-26 02:34:36 +0000685private:
686 void LazyInitializeReferencedVars();
Ted Kremenek24570c42012-12-06 07:17:13 +0000687 std::pair<const VarRegion *, const VarRegion *>
688 getCaptureRegions(const VarDecl *VD);
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000689};
Zhongxing Xuec13d922009-04-10 08:45:10 +0000690
Zhongxing Xu026c6632009-02-05 06:57:29 +0000691/// SymbolicRegion - A special, "non-concrete" region. Unlike other region
692/// clases, SymbolicRegion represents a region that serves as an alias for
693/// either a real region, a NULL pointer, etc. It essentially is used to
694/// map the concept of symbolic values into the domain of regions. Symbolic
695/// regions do not need to be typed.
Ted Kremeneke8e86482009-03-30 22:20:54 +0000696class SymbolicRegion : public SubRegion {
Zhongxing Xu026c6632009-02-05 06:57:29 +0000697protected:
698 const SymbolRef sym;
Zhongxing Xu026c6632009-02-05 06:57:29 +0000699
700public:
Mike Stump1eb44332009-09-09 15:08:12 +0000701 SymbolicRegion(const SymbolRef s, const MemRegion* sreg)
Ted Kremeneke8e86482009-03-30 22:20:54 +0000702 : SubRegion(sreg, SymbolicRegionKind), sym(s) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Zhongxing Xu026c6632009-02-05 06:57:29 +0000704 SymbolRef getSymbol() const {
705 return sym;
706 }
707
Zhongxing Xu9852b5b2009-07-06 05:34:56 +0000708 bool isBoundable() const { return true; }
709
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000710 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
Jordy Rose32f26562010-07-04 00:00:41 +0000711
Zhongxing Xu026c6632009-02-05 06:57:29 +0000712 void Profile(llvm::FoldingSetNodeID& ID) const;
713
Ted Kremenek25010132009-06-22 23:13:13 +0000714 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
715 SymbolRef sym,
716 const MemRegion* superRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek9c378f72011-08-12 23:37:29 +0000718 void dumpToStream(raw_ostream &os) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Zhongxing Xu026c6632009-02-05 06:57:29 +0000720 static bool classof(const MemRegion* R) {
721 return R->getKind() == SymbolicRegionKind;
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723};
Zhongxing Xu026c6632009-02-05 06:57:29 +0000724
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000725/// StringRegion - Region associated with a StringLiteral.
Ted Kremenek96979342011-08-12 20:02:48 +0000726class StringRegion : public TypedValueRegion {
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000727 friend class MemRegionManager;
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000728 const StringLiteral* Str;
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000729protected:
730
Ted Kremenek25010132009-06-22 23:13:13 +0000731 StringRegion(const StringLiteral* str, const MemRegion* sreg)
Ted Kremenek96979342011-08-12 20:02:48 +0000732 : TypedValueRegion(sreg, StringRegionKind), Str(str) {}
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000733
734 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
735 const StringLiteral* Str,
736 const MemRegion* superRegion);
737
738public:
Zhongxing Xu6613d082008-11-24 02:18:56 +0000739
740 const StringLiteral* getStringLiteral() const { return Str; }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Zhongxing Xu018220c2010-08-11 06:10:55 +0000742 QualType getValueType() const {
Zhongxing Xuff697822009-05-09 00:50:33 +0000743 return Str->getType();
744 }
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000745
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000746 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
Jordy Rose32f26562010-07-04 00:00:41 +0000747
Zhongxing Xu0a51f1c2009-07-04 11:31:53 +0000748 bool isBoundable() const { return false; }
749
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000750 void Profile(llvm::FoldingSetNodeID& ID) const {
751 ProfileRegion(ID, Str, superRegion);
752 }
753
Ted Kremenek9c378f72011-08-12 23:37:29 +0000754 void dumpToStream(raw_ostream &os) const;
Zhongxing Xucc128b32008-11-10 13:05:26 +0000755
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000756 static bool classof(const MemRegion* R) {
757 return R->getKind() == StringRegionKind;
758 }
759};
Ted Kremenek4c62b552012-02-28 00:56:05 +0000760
761/// The region associated with an ObjCStringLiteral.
762class ObjCStringRegion : public TypedValueRegion {
763 friend class MemRegionManager;
764 const ObjCStringLiteral* Str;
765protected:
766
767 ObjCStringRegion(const ObjCStringLiteral* str, const MemRegion* sreg)
768 : TypedValueRegion(sreg, ObjCStringRegionKind), Str(str) {}
769
770 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
771 const ObjCStringLiteral* Str,
772 const MemRegion* superRegion);
773
774public:
775
776 const ObjCStringLiteral* getObjCStringLiteral() const { return Str; }
777
778 QualType getValueType() const {
779 return Str->getType();
780 }
781
782 bool isBoundable() const { return false; }
783
784 void Profile(llvm::FoldingSetNodeID& ID) const {
785 ProfileRegion(ID, Str, superRegion);
786 }
787
788 void dumpToStream(raw_ostream &os) const;
789
790 static bool classof(const MemRegion* R) {
791 return R->getKind() == ObjCStringRegionKind;
792 }
793};
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000794
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000795/// CompoundLiteralRegion - A memory region representing a compound literal.
796/// Compound literals are essentially temporaries that are stack allocated
797/// or in the global constant pool.
Ted Kremenek96979342011-08-12 20:02:48 +0000798class CompoundLiteralRegion : public TypedValueRegion {
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000799private:
800 friend class MemRegionManager;
Ted Kremenek9c378f72011-08-12 23:37:29 +0000801 const CompoundLiteralExpr *CL;
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000802
Ted Kremenek9c378f72011-08-12 23:37:29 +0000803 CompoundLiteralRegion(const CompoundLiteralExpr *cl, const MemRegion* sReg)
Ted Kremenek96979342011-08-12 20:02:48 +0000804 : TypedValueRegion(sReg, CompoundLiteralRegionKind), CL(cl) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000806 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
Ted Kremenek9c378f72011-08-12 23:37:29 +0000807 const CompoundLiteralExpr *CL,
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000808 const MemRegion* superRegion);
809public:
Zhongxing Xu018220c2010-08-11 06:10:55 +0000810 QualType getValueType() const {
811 return CL->getType();
Zhongxing Xu77cfac62008-10-31 06:30:35 +0000812 }
Zhongxing Xu0a51f1c2009-07-04 11:31:53 +0000813
Zhongxing Xu9a08fee2009-07-04 11:43:03 +0000814 bool isBoundable() const { return !CL->isFileScope(); }
815
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000816 void Profile(llvm::FoldingSetNodeID& ID) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremenek9c378f72011-08-12 23:37:29 +0000818 void dumpToStream(raw_ostream &os) const;
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000819
Ted Kremenek9c378f72011-08-12 23:37:29 +0000820 const CompoundLiteralExpr *getLiteralExpr() const { return CL; }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000822 static bool classof(const MemRegion* R) {
823 return R->getKind() == CompoundLiteralRegionKind;
824 }
825};
Zhongxing Xu17892752008-10-08 02:50:44 +0000826
Ted Kremenek96979342011-08-12 20:02:48 +0000827class DeclRegion : public TypedValueRegion {
Ted Kremenek9e240492008-10-04 05:50:14 +0000828protected:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000829 const Decl *D;
Ted Kremenek9e240492008-10-04 05:50:14 +0000830
Ted Kremenek9c378f72011-08-12 23:37:29 +0000831 DeclRegion(const Decl *d, const MemRegion* sReg, Kind k)
Ted Kremenek96979342011-08-12 20:02:48 +0000832 : TypedValueRegion(sReg, k), D(d) {}
Ted Kremenek9e240492008-10-04 05:50:14 +0000833
Ted Kremenek9c378f72011-08-12 23:37:29 +0000834 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
Ted Kremenek9e240492008-10-04 05:50:14 +0000835 const MemRegion* superRegion, Kind k);
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Zhongxing Xubfb65822008-10-24 09:06:51 +0000837public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000838 const Decl *getDecl() const { return D; }
Ted Kremenek9e240492008-10-04 05:50:14 +0000839 void Profile(llvm::FoldingSetNodeID& ID) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000841 static bool classof(const MemRegion* R) {
842 unsigned k = R->getKind();
Ted Kremenek67d12872009-12-07 22:05:27 +0000843 return k >= BEG_DECL_REGIONS && k <= END_DECL_REGIONS;
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000844 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000845};
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Ted Kremenek9e240492008-10-04 05:50:14 +0000847class VarRegion : public DeclRegion {
848 friend class MemRegionManager;
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000849
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000850 // Constructors and private methods.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000851 VarRegion(const VarDecl *vd, const MemRegion* sReg)
Ted Kremenek67d12872009-12-07 22:05:27 +0000852 : DeclRegion(vd, sReg, VarRegionKind) {}
Ted Kremenek9e240492008-10-04 05:50:14 +0000853
Ted Kremenek9c378f72011-08-12 23:37:29 +0000854 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000855 const MemRegion *superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000856 DeclRegion::ProfileRegion(ID, VD, superRegion, VarRegionKind);
857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000859 void Profile(llvm::FoldingSetNodeID& ID) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
861public:
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000862 const VarDecl *getDecl() const { return cast<VarDecl>(D); }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek5348f942009-12-14 22:15:06 +0000864 const StackFrameContext *getStackFrame() const;
865
Zhongxing Xu018220c2010-08-11 06:10:55 +0000866 QualType getValueType() const {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000867 // FIXME: We can cache this if needed.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000868 return getDecl()->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000869 }
870
Ted Kremenek9c378f72011-08-12 23:37:29 +0000871 void dumpToStream(raw_ostream &os) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenek9e240492008-10-04 05:50:14 +0000873 static bool classof(const MemRegion* R) {
874 return R->getKind() == VarRegionKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000875 }
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000876
Jordan Rose919e8a12012-08-08 18:23:36 +0000877 bool canPrintPretty() const;
878 void printPretty(raw_ostream &os) const;
Ted Kremenek9e240492008-10-04 05:50:14 +0000879};
Ted Kremenekde0d2632010-01-05 02:18:06 +0000880
881/// CXXThisRegion - Represents the region for the implicit 'this' parameter
882/// in a call to a C++ method. This region doesn't represent the object
883/// referred to by 'this', but rather 'this' itself.
Ted Kremenek96979342011-08-12 20:02:48 +0000884class CXXThisRegion : public TypedValueRegion {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000885 friend class MemRegionManager;
886 CXXThisRegion(const PointerType *thisPointerTy,
887 const MemRegion *sReg)
Ted Kremenek96979342011-08-12 20:02:48 +0000888 : TypedValueRegion(sReg, CXXThisRegionKind), ThisPointerTy(thisPointerTy) {}
Ted Kremenekde0d2632010-01-05 02:18:06 +0000889
890 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
891 const PointerType *PT,
892 const MemRegion *sReg);
893
894 void Profile(llvm::FoldingSetNodeID &ID) const;
895
896public:
Zhongxing Xu018220c2010-08-11 06:10:55 +0000897 QualType getValueType() const {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000898 return QualType(ThisPointerTy, 0);
899 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000900
Ted Kremenek9c378f72011-08-12 23:37:29 +0000901 void dumpToStream(raw_ostream &os) const;
Ted Kremenekde0d2632010-01-05 02:18:06 +0000902
903 static bool classof(const MemRegion* R) {
904 return R->getKind() == CXXThisRegionKind;
905 }
906
907private:
908 const PointerType *ThisPointerTy;
909};
Ted Kremenek9e240492008-10-04 05:50:14 +0000910
911class FieldRegion : public DeclRegion {
912 friend class MemRegionManager;
913
Ted Kremenek9c378f72011-08-12 23:37:29 +0000914 FieldRegion(const FieldDecl *fd, const MemRegion* sReg)
Ted Kremenek9e240492008-10-04 05:50:14 +0000915 : DeclRegion(fd, sReg, FieldRegionKind) {}
916
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000917public:
Ted Kremenek9c378f72011-08-12 23:37:29 +0000918 const FieldDecl *getDecl() const { return cast<FieldDecl>(D); }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Zhongxing Xu018220c2010-08-11 06:10:55 +0000920 QualType getValueType() const {
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000921 // FIXME: We can cache this if needed.
Zhongxing Xu018220c2010-08-11 06:10:55 +0000922 return getDecl()->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000923 }
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000924
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000925 DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const;
Jordy Rose32f26562010-07-04 00:00:41 +0000926
Ted Kremenek9c378f72011-08-12 23:37:29 +0000927 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const FieldDecl *FD,
Ted Kremenek6304b082009-06-22 23:34:21 +0000928 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000929 DeclRegion::ProfileRegion(ID, FD, superRegion, FieldRegionKind);
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Ted Kremenek9e240492008-10-04 05:50:14 +0000932 static bool classof(const MemRegion* R) {
933 return R->getKind() == FieldRegionKind;
934 }
Anna Zaks3d7c44e2012-03-21 19:45:08 +0000935
936 void dumpToStream(raw_ostream &os) const;
Jordan Rose919e8a12012-08-08 18:23:36 +0000937
938 bool canPrintPretty() const;
939 void printPretty(raw_ostream &os) const;
Ted Kremenek9e240492008-10-04 05:50:14 +0000940};
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek9e240492008-10-04 05:50:14 +0000942class ObjCIvarRegion : public DeclRegion {
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenek9e240492008-10-04 05:50:14 +0000944 friend class MemRegionManager;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +0000946 ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg);
Ted Kremenek9e240492008-10-04 05:50:14 +0000947
Ted Kremenek9c378f72011-08-12 23:37:29 +0000948 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const ObjCIvarDecl *ivd,
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +0000949 const MemRegion* superRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek9e240492008-10-04 05:50:14 +0000951public:
Benjamin Kramerc35fb7d2012-01-28 12:06:22 +0000952 const ObjCIvarDecl *getDecl() const;
953 QualType getValueType() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Ted Kremenek43b82b82013-02-24 07:21:01 +0000955 bool canPrintPretty() const;
956 void printPretty(raw_ostream &os) const;
957
Ted Kremenek9c378f72011-08-12 23:37:29 +0000958 void dumpToStream(raw_ostream &os) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek9e240492008-10-04 05:50:14 +0000960 static bool classof(const MemRegion* R) {
961 return R->getKind() == ObjCIvarRegionKind;
962 }
963};
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000964//===----------------------------------------------------------------------===//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000965// Auxiliary data classes for use with MemRegions.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000966//===----------------------------------------------------------------------===//
967
968class ElementRegion;
969
970class RegionRawOffset {
971private:
972 friend class ElementRegion;
973
974 const MemRegion *Region;
Ken Dyck9ff2b132011-01-24 01:55:39 +0000975 CharUnits Offset;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000976
Ken Dyck9ff2b132011-01-24 01:55:39 +0000977 RegionRawOffset(const MemRegion* reg, CharUnits offset = CharUnits::Zero())
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000978 : Region(reg), Offset(offset) {}
979
980public:
981 // FIXME: Eventually support symbolic offsets.
Ken Dyck9ff2b132011-01-24 01:55:39 +0000982 CharUnits getOffset() const { return Offset; }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000983 const MemRegion *getRegion() const { return Region; }
984
Ted Kremenek9c378f72011-08-12 23:37:29 +0000985 void dumpToStream(raw_ostream &os) const;
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000986 void dump() const;
987};
Zhongxing Xu511191c2008-10-21 05:27:10 +0000988
Anna Zaks14374252012-01-12 02:22:40 +0000989/// \brief ElementRegin is used to represent both array elements and casts.
Ted Kremenek96979342011-08-12 20:02:48 +0000990class ElementRegion : public TypedValueRegion {
Zhongxing Xu511191c2008-10-21 05:27:10 +0000991 friend class MemRegionManager;
992
Ted Kremenekf936f452009-05-04 06:18:28 +0000993 QualType ElementType;
Ted Kremenek02282ac2010-09-15 03:13:30 +0000994 NonLoc Index;
Zhongxing Xu511191c2008-10-21 05:27:10 +0000995
Ted Kremenek02282ac2010-09-15 03:13:30 +0000996 ElementRegion(QualType elementType, NonLoc Idx, const MemRegion* sReg)
Ted Kremenek96979342011-08-12 20:02:48 +0000997 : TypedValueRegion(sReg, ElementRegionKind),
Ted Kremenekf936f452009-05-04 06:18:28 +0000998 ElementType(elementType), Index(Idx) {
David Blaikie5251abe2013-02-20 05:52:05 +0000999 assert((!Idx.getAs<nonloc::ConcreteInt>() ||
1000 Idx.castAs<nonloc::ConcreteInt>().getValue().isSigned()) &&
Chris Lattner43b28d02008-12-12 07:16:09 +00001001 "The index must be signed");
Zhongxing Xu0395b5d2008-11-29 12:05:04 +00001002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenekf936f452009-05-04 06:18:28 +00001004 static void ProfileRegion(llvm::FoldingSetNodeID& ID, QualType elementType,
1005 SVal Idx, const MemRegion* superRegion);
Zhongxing Xu511191c2008-10-21 05:27:10 +00001006
1007public:
1008
Ted Kremenek02282ac2010-09-15 03:13:30 +00001009 NonLoc getIndex() const { return Index; }
Zhongxing Xu6e78e1b2008-10-23 01:35:34 +00001010
Zhongxing Xu018220c2010-08-11 06:10:55 +00001011 QualType getValueType() const {
Ted Kremenekf936f452009-05-04 06:18:28 +00001012 return ElementType;
1013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Ted Kremenekf936f452009-05-04 06:18:28 +00001015 QualType getElementType() const {
1016 return ElementType;
Ted Kremenekabb042f2008-12-13 19:24:37 +00001017 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +00001018 /// Compute the offset within the array. The array might also be a subobject.
1019 RegionRawOffset getAsArrayOffset() const;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremenek9c378f72011-08-12 23:37:29 +00001021 void dumpToStream(raw_ostream &os) const;
Zhongxing Xub21ff772008-10-24 06:30:07 +00001022
Zhongxing Xu511191c2008-10-21 05:27:10 +00001023 void Profile(llvm::FoldingSetNodeID& ID) const;
1024
1025 static bool classof(const MemRegion* R) {
1026 return R->getKind() == ElementRegionKind;
1027 }
1028};
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001029
Zhongxing Xubc37b8d2010-01-09 09:16:47 +00001030// C++ temporary object associated with an expression.
Ted Kremenek96979342011-08-12 20:02:48 +00001031class CXXTempObjectRegion : public TypedValueRegion {
Zhongxing Xubb141212009-12-16 11:27:52 +00001032 friend class MemRegionManager;
1033
Zhongxing Xubc37b8d2010-01-09 09:16:47 +00001034 Expr const *Ex;
Zhongxing Xubb141212009-12-16 11:27:52 +00001035
Zhongxing Xu02fe28c2010-11-26 08:52:48 +00001036 CXXTempObjectRegion(Expr const *E, MemRegion const *sReg)
Ted Kremenek96979342011-08-12 20:02:48 +00001037 : TypedValueRegion(sReg, CXXTempObjectRegionKind), Ex(E) {}
Zhongxing Xubb141212009-12-16 11:27:52 +00001038
1039 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
Zhongxing Xubc37b8d2010-01-09 09:16:47 +00001040 Expr const *E, const MemRegion *sReg);
Zhongxing Xubb141212009-12-16 11:27:52 +00001041
1042public:
Jeffrey Yasskin782f63e2011-08-26 00:41:31 +00001043 const Expr *getExpr() const { return Ex; }
1044
Zhongxing Xu018220c2010-08-11 06:10:55 +00001045 QualType getValueType() const {
Zhongxing Xubc37b8d2010-01-09 09:16:47 +00001046 return Ex->getType();
Zhongxing Xubb141212009-12-16 11:27:52 +00001047 }
1048
Ted Kremenek9c378f72011-08-12 23:37:29 +00001049 void dumpToStream(raw_ostream &os) const;
Zhongxing Xue1aeb132010-11-25 02:07:24 +00001050
Zhongxing Xubb141212009-12-16 11:27:52 +00001051 void Profile(llvm::FoldingSetNodeID &ID) const;
1052
1053 static bool classof(const MemRegion* R) {
Zhongxing Xu02fe28c2010-11-26 08:52:48 +00001054 return R->getKind() == CXXTempObjectRegionKind;
Zhongxing Xubb141212009-12-16 11:27:52 +00001055 }
1056};
1057
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001058// CXXBaseObjectRegion represents a base object within a C++ object. It is
1059// identified by the base class declaration and the region of its parent object.
Ted Kremenek96979342011-08-12 20:02:48 +00001060class CXXBaseObjectRegion : public TypedValueRegion {
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001061 friend class MemRegionManager;
1062
Jordan Rose4411b422013-02-21 03:12:32 +00001063 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> Data;
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001064
Jordan Rose4411b422013-02-21 03:12:32 +00001065 CXXBaseObjectRegion(const CXXRecordDecl *RD, bool IsVirtual,
1066 const MemRegion *SReg)
1067 : TypedValueRegion(SReg, CXXBaseObjectRegionKind), Data(RD, IsVirtual) {}
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001068
Jordan Rose4411b422013-02-21 03:12:32 +00001069 static void ProfileRegion(llvm::FoldingSetNodeID &ID, const CXXRecordDecl *RD,
1070 bool IsVirtual, const MemRegion *SReg);
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001071
1072public:
Jordan Rose4411b422013-02-21 03:12:32 +00001073 const CXXRecordDecl *getDecl() const { return Data.getPointer(); }
1074 bool isVirtual() const { return Data.getInt(); }
Zhongxing Xud0744412011-01-13 12:30:12 +00001075
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001076 QualType getValueType() const;
1077
Ted Kremenek9c378f72011-08-12 23:37:29 +00001078 void dumpToStream(raw_ostream &os) const;
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001079
1080 void Profile(llvm::FoldingSetNodeID &ID) const;
1081
1082 static bool classof(const MemRegion *region) {
1083 return region->getKind() == CXXBaseObjectRegionKind;
1084 }
1085};
1086
Ted Kremeneked65d3d2009-03-09 20:28:08 +00001087template<typename RegionTy>
1088const RegionTy* MemRegion::getAs() const {
Ted Kremenekf7a0cf42009-07-29 21:43:22 +00001089 if (const RegionTy* RT = dyn_cast<RegionTy>(this))
1090 return RT;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremenekf7a0cf42009-07-29 21:43:22 +00001092 return NULL;
Ted Kremeneked65d3d2009-03-09 20:28:08 +00001093}
Zhongxing Xu511191c2008-10-21 05:27:10 +00001094
Ted Kremenek9e240492008-10-04 05:50:14 +00001095//===----------------------------------------------------------------------===//
1096// MemRegionManager - Factory object for creating regions.
1097//===----------------------------------------------------------------------===//
1098
1099class MemRegionManager {
Ted Kremeneka43484a2009-06-23 00:46:41 +00001100 ASTContext &C;
Ted Kremenek9e240492008-10-04 05:50:14 +00001101 llvm::BumpPtrAllocator& A;
1102 llvm::FoldingSet<MemRegion> Regions;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Anna Zakseb31a762012-01-04 23:54:01 +00001104 GlobalInternalSpaceRegion *InternalGlobals;
1105 GlobalSystemSpaceRegion *SystemGlobals;
1106 GlobalImmutableSpaceRegion *ImmutableGlobals;
1107
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001108
Zhongxing Xuc30470d2010-02-17 08:46:50 +00001109 llvm::DenseMap<const StackFrameContext *, StackLocalsSpaceRegion *>
1110 StackLocalsSpaceRegions;
1111 llvm::DenseMap<const StackFrameContext *, StackArgumentsSpaceRegion *>
1112 StackArgumentsSpaceRegions;
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001113 llvm::DenseMap<const CodeTextRegion *, StaticGlobalSpaceRegion *>
1114 StaticsGlobalSpaceRegions;
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001115
Ted Kremenek67d12872009-12-07 22:05:27 +00001116 HeapSpaceRegion *heap;
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001117 UnknownSpaceRegion *unknown;
Ted Kremenekd05552a2009-07-02 18:14:59 +00001118 MemSpaceRegion *code;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001119
Ted Kremenek9e240492008-10-04 05:50:14 +00001120public:
Ted Kremeneka43484a2009-06-23 00:46:41 +00001121 MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator& a)
Anna Zakseb31a762012-01-04 23:54:01 +00001122 : C(c), A(a), InternalGlobals(0), SystemGlobals(0), ImmutableGlobals(0),
1123 heap(0), unknown(0), code(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Ted Kremenek42400962009-11-26 02:34:36 +00001125 ~MemRegionManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Ted Kremeneka43484a2009-06-23 00:46:41 +00001127 ASTContext &getContext() { return C; }
Ted Kremenek42400962009-11-26 02:34:36 +00001128
1129 llvm::BumpPtrAllocator &getAllocator() { return A; }
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenek67d12872009-12-07 22:05:27 +00001131 /// getStackLocalsRegion - Retrieve the memory region associated with the
1132 /// specified stack frame.
1133 const StackLocalsSpaceRegion *
1134 getStackLocalsRegion(const StackFrameContext *STC);
Ted Kremenekd05552a2009-07-02 18:14:59 +00001135
1136 /// getStackArgumentsRegion - Retrieve the memory region associated with
Ted Kremenek67d12872009-12-07 22:05:27 +00001137 /// function/method arguments of the specified stack frame.
1138 const StackArgumentsSpaceRegion *
1139 getStackArgumentsRegion(const StackFrameContext *STC);
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Ted Kremenek9e240492008-10-04 05:50:14 +00001141 /// getGlobalsRegion - Retrieve the memory region associated with
Ted Kremenekdcee3ce2010-07-01 20:16:50 +00001142 /// global variables.
Anna Zakseb31a762012-01-04 23:54:01 +00001143 const GlobalsSpaceRegion *getGlobalsRegion(
1144 MemRegion::Kind K = MemRegion::GlobalInternalSpaceRegionKind,
1145 const CodeTextRegion *R = 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenek9e240492008-10-04 05:50:14 +00001147 /// getHeapRegion - Retrieve the memory region associated with the
1148 /// generic "heap".
Ted Kremenek67d12872009-12-07 22:05:27 +00001149 const HeapSpaceRegion *getHeapRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +00001150
1151 /// getUnknownRegion - Retrieve the memory region associated with unknown
1152 /// memory space.
Ted Kremenekb48ad642009-12-04 00:26:31 +00001153 const MemSpaceRegion *getUnknownRegion();
Zhongxing Xuc540b262008-10-28 05:15:23 +00001154
Ted Kremenekb48ad642009-12-04 00:26:31 +00001155 const MemSpaceRegion *getCodeRegion();
Zhongxing Xuec13d922009-04-10 08:45:10 +00001156
Ted Kremenek7090ae12008-11-02 00:34:33 +00001157 /// getAllocaRegion - Retrieve a region associated with a call to alloca().
Ted Kremenek9c378f72011-08-12 23:37:29 +00001158 const AllocaRegion *getAllocaRegion(const Expr *Ex, unsigned Cnt,
Ted Kremenek67d12872009-12-07 22:05:27 +00001159 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenek329d6fd2008-10-27 20:57:58 +00001161 /// getCompoundLiteralRegion - Retrieve the region associated with a
1162 /// given CompoundLiteral.
Ted Kremenekb48ad642009-12-04 00:26:31 +00001163 const CompoundLiteralRegion*
Ted Kremenek9c378f72011-08-12 23:37:29 +00001164 getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
Ted Kremenek67d12872009-12-07 22:05:27 +00001165 const LocationContext *LC);
Ted Kremenekde0d2632010-01-05 02:18:06 +00001166
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001167 /// getCXXThisRegion - Retrieve the [artificial] region associated with the
Ted Kremenekde0d2632010-01-05 02:18:06 +00001168 /// parameter 'this'.
1169 const CXXThisRegion *getCXXThisRegion(QualType thisPointerTy,
1170 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Anna Zakse17fdb22012-06-07 03:57:32 +00001172 /// \brief Retrieve or create a "symbolic" memory region.
1173 const SymbolicRegion* getSymbolicRegion(SymbolRef Sym);
1174
1175 /// \brief Return a unique symbolic region belonging to heap memory space.
1176 const SymbolicRegion *getSymbolicHeapRegion(SymbolRef sym);
Zhongxing Xue9f4e542008-10-25 14:13:41 +00001177
Ted Kremenek4c62b552012-02-28 00:56:05 +00001178 const StringRegion *getStringRegion(const StringLiteral* Str);
1179
1180 const ObjCStringRegion *getObjCStringRegion(const ObjCStringLiteral *Str);
Zhongxing Xue9f4e542008-10-25 14:13:41 +00001181
Ted Kremenek9e240492008-10-04 05:50:14 +00001182 /// getVarRegion - Retrieve or create the memory region associated with
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001183 /// a specified VarDecl and LocationContext.
Ted Kremenekb48ad642009-12-04 00:26:31 +00001184 const VarRegion* getVarRegion(const VarDecl *D, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenek67d12872009-12-07 22:05:27 +00001186 /// getVarRegion - Retrieve or create the memory region associated with
1187 /// a specified VarDecl and super region.
1188 const VarRegion* getVarRegion(const VarDecl *D, const MemRegion *superR);
1189
Ted Kremenekf936f452009-05-04 06:18:28 +00001190 /// getElementRegion - Retrieve the memory region associated with the
1191 /// associated element type, index, and super region.
Ted Kremenek02282ac2010-09-15 03:13:30 +00001192 const ElementRegion *getElementRegion(QualType elementType, NonLoc Idx,
Zhongxing Xu856c6bc2010-04-19 11:47:28 +00001193 const MemRegion *superRegion,
1194 ASTContext &Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenekb48ad642009-12-04 00:26:31 +00001196 const ElementRegion *getElementRegionWithSuper(const ElementRegion *ER,
Zhongxing Xu856c6bc2010-04-19 11:47:28 +00001197 const MemRegion *superRegion) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001198 return getElementRegion(ER->getElementType(), ER->getIndex(),
1199 superRegion, ER->getContext());
1200 }
Zhongxing Xu511191c2008-10-21 05:27:10 +00001201
Ted Kremenek9e240492008-10-04 05:50:14 +00001202 /// getFieldRegion - Retrieve or create the memory region associated with
1203 /// a specified FieldDecl. 'superRegion' corresponds to the containing
1204 /// memory region (which typically represents the memory representing
1205 /// a structure or class).
Ted Kremenek9c378f72011-08-12 23:37:29 +00001206 const FieldRegion *getFieldRegion(const FieldDecl *fd,
Ted Kremenekb48ad642009-12-04 00:26:31 +00001207 const MemRegion* superRegion);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Ted Kremenekb48ad642009-12-04 00:26:31 +00001209 const FieldRegion *getFieldRegionWithSuper(const FieldRegion *FR,
1210 const MemRegion *superRegion) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001211 return getFieldRegion(FR->getDecl(), superRegion);
1212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenek9e240492008-10-04 05:50:14 +00001214 /// getObjCIvarRegion - Retrieve or create the memory region associated with
1215 /// a specified Objective-c instance variable. 'superRegion' corresponds
1216 /// to the containing region (which typically represents the Objective-C
1217 /// object).
Ted Kremenek9c378f72011-08-12 23:37:29 +00001218 const ObjCIvarRegion *getObjCIvarRegion(const ObjCIvarDecl *ivd,
Ted Kremenekb48ad642009-12-04 00:26:31 +00001219 const MemRegion* superRegion);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Zhongxing Xu02fe28c2010-11-26 08:52:48 +00001221 const CXXTempObjectRegion *getCXXTempObjectRegion(Expr const *Ex,
1222 LocationContext const *LC);
Zhongxing Xubb141212009-12-16 11:27:52 +00001223
Jordan Rose4411b422013-02-21 03:12:32 +00001224 /// Create a CXXBaseObjectRegion with the given base class for region
1225 /// \p Super.
1226 ///
1227 /// The type of \p Super is assumed be a class deriving from \p BaseClass.
1228 const CXXBaseObjectRegion *
1229 getCXXBaseObjectRegion(const CXXRecordDecl *BaseClass, const MemRegion *Super,
1230 bool IsVirtual);
Zhongxing Xu4fd56812010-11-26 08:21:53 +00001231
Zhongxing Xud0744412011-01-13 12:30:12 +00001232 /// Create a CXXBaseObjectRegion with the same CXXRecordDecl but a different
1233 /// super region.
1234 const CXXBaseObjectRegion *
1235 getCXXBaseObjectRegionWithSuper(const CXXBaseObjectRegion *baseReg,
1236 const MemRegion *superRegion) {
Jordan Rose4411b422013-02-21 03:12:32 +00001237 return getCXXBaseObjectRegion(baseReg->getDecl(), superRegion,
1238 baseReg->isVirtual());
Zhongxing Xud0744412011-01-13 12:30:12 +00001239 }
1240
Anna Zaks5fc1d0c2012-09-17 19:13:56 +00001241 const FunctionTextRegion *getFunctionTextRegion(const NamedDecl *FD);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001242 const BlockTextRegion *getBlockTextRegion(const BlockDecl *BD,
Ted Kremenek67d12872009-12-07 22:05:27 +00001243 CanQualType locTy,
Ted Kremenek1d26f482011-10-24 01:32:45 +00001244 AnalysisDeclContext *AC);
Ted Kremenek67d12872009-12-07 22:05:27 +00001245
1246 /// getBlockDataRegion - Get the memory region associated with an instance
1247 /// of a block. Unlike many other MemRegions, the LocationContext*
1248 /// argument is allowed to be NULL for cases where we have no known
1249 /// context.
Ted Kremenekb48ad642009-12-04 00:26:31 +00001250 const BlockDataRegion *getBlockDataRegion(const BlockTextRegion *bc,
Ted Kremenek67d12872009-12-07 22:05:27 +00001251 const LocationContext *lc = NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenekbcd7f9f2009-12-04 00:05:57 +00001253private:
Ted Kremenek25010132009-06-22 23:13:13 +00001254 template <typename RegionTy, typename A1>
1255 RegionTy* getRegion(const A1 a1);
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek6304b082009-06-22 23:34:21 +00001257 template <typename RegionTy, typename A1>
Ted Kremenekeeea4562009-07-10 16:51:45 +00001258 RegionTy* getSubRegion(const A1 a1, const MemRegion* superRegion);
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenek7ae7ad92009-06-23 00:15:41 +00001260 template <typename RegionTy, typename A1, typename A2>
1261 RegionTy* getRegion(const A1 a1, const A2 a2);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001262
Ted Kremenek0a8112a2009-11-25 23:53:07 +00001263 template <typename RegionTy, typename A1, typename A2>
1264 RegionTy* getSubRegion(const A1 a1, const A2 a2,
1265 const MemRegion* superRegion);
1266
Ted Kremenek67d12872009-12-07 22:05:27 +00001267 template <typename RegionTy, typename A1, typename A2, typename A3>
1268 RegionTy* getSubRegion(const A1 a1, const A2 a2, const A3 a3,
1269 const MemRegion* superRegion);
1270
1271 template <typename REG>
1272 const REG* LazyAllocate(REG*& region);
1273
1274 template <typename REG, typename ARG>
1275 const REG* LazyAllocate(REG*& region, ARG a);
Ted Kremeneked65d3d2009-03-09 20:28:08 +00001276};
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Ted Kremenek25010132009-06-22 23:13:13 +00001278//===----------------------------------------------------------------------===//
Ted Kremeneka43484a2009-06-23 00:46:41 +00001279// Out-of-line member definitions.
Ted Kremenek25010132009-06-22 23:13:13 +00001280//===----------------------------------------------------------------------===//
1281
Ted Kremenek9c378f72011-08-12 23:37:29 +00001282inline ASTContext &MemRegion::getContext() const {
Ted Kremeneka43484a2009-06-23 00:46:41 +00001283 return getMemRegionManager()->getContext();
1284}
Ted Kremenekeb1c7a02009-11-25 01:32:22 +00001285
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +00001286} // end GR namespace
1287
Ted Kremenek9e240492008-10-04 05:50:14 +00001288} // end clang namespace
Ted Kremenekbe912242009-03-05 16:31:07 +00001289
Ted Kremenek25010132009-06-22 23:13:13 +00001290//===----------------------------------------------------------------------===//
1291// Pretty-printing regions.
1292//===----------------------------------------------------------------------===//
1293
Ted Kremenekbe912242009-03-05 16:31:07 +00001294namespace llvm {
Ted Kremenek9c378f72011-08-12 23:37:29 +00001295static inline raw_ostream &operator<<(raw_ostream &os,
Ted Kremenek9ef65372010-12-23 07:20:52 +00001296 const clang::ento::MemRegion* R) {
Ted Kremenek8800ad42009-07-13 23:31:04 +00001297 R->dumpToStream(os);
1298 return os;
Ted Kremenekbe912242009-03-05 16:31:07 +00001299}
1300} // end llvm namespace
1301
Ted Kremenek9e240492008-10-04 05:50:14 +00001302#endif