clang 24.0.0git
MemRegion.h
Go to the documentation of this file.
1//==- MemRegion.h - Abstract memory regions for static analysis -*- C++ -*--==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines MemRegion and its subclasses. MemRegion defines a
10// partially-typed abstraction of memory useful for path-sensitive dataflow
11// analyses.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H
16#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H
17
19#include "clang/AST/CharUnits.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
23#include "clang/AST/Expr.h"
24#include "clang/AST/ExprObjC.h"
25#include "clang/AST/Type.h"
27#include "clang/Basic/LLVM.h"
32#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/FoldingSet.h"
34#include "llvm/ADT/PointerIntPair.h"
35#include "llvm/ADT/iterator_range.h"
36#include "llvm/Support/Allocator.h"
37#include "llvm/Support/Casting.h"
38#include "llvm/Support/ErrorHandling.h"
39#include <cassert>
40#include <cstdint>
41#include <limits>
42#include <optional>
43#include <string>
44#include <utility>
45
46namespace clang {
47
49class CXXRecordDecl;
50class Decl;
51class StackFrame;
52
53namespace ento {
54
55class CodeTextRegion;
56class MemRegion;
58class MemSpaceRegion;
59class SValBuilder;
60class SymbolicRegion;
61class VarRegion;
62
63/// Represent a region's offset within the top level base region.
65 /// The base region.
66 const MemRegion *R = nullptr;
67
68 /// The bit offset within the base region. Can be negative.
69 int64_t Offset;
70
71public:
72 // We're using a const instead of an enumeration due to the size required;
73 // Visual Studio will only create enumerations of size int, not long long.
74 static const int64_t Symbolic = std::numeric_limits<int64_t>::max();
75
76 RegionOffset() = default;
77 RegionOffset(const MemRegion *r, int64_t off) : R(r), Offset(off) {}
78
79 /// It might return null.
80 const MemRegion *getRegion() const { return R; }
81
82 bool hasSymbolicOffset() const { return Offset == Symbolic; }
83
84 int64_t getOffset() const {
85 assert(!hasSymbolicOffset());
86 return Offset;
87 }
88
89 bool isValid() const { return R; }
90};
91
92//===----------------------------------------------------------------------===//
93// Base region classes.
94//===----------------------------------------------------------------------===//
95
96/// MemRegion - The root abstract class for all memory regions.
97class MemRegion : public llvm::FoldingSetNode {
98public:
99 enum Kind {
100#define REGION(Id, Parent) Id ## Kind,
101#define REGION_RANGE(Id, First, Last) BEGIN_##Id = First, END_##Id = Last,
102#include "clang/StaticAnalyzer/Core/PathSensitive/Regions.def"
103#undef REGION
104#undef REGION_RANGE
105 };
106
107private:
108 const Kind kind;
109 mutable std::optional<RegionOffset> cachedOffset;
110
111protected:
112 MemRegion(Kind k) : kind(k) {}
113 virtual ~MemRegion();
114
115public:
116 ASTContext &getContext() const;
117
118 virtual void Profile(llvm::FoldingSetNodeID& ID) const = 0;
119
121
122 /// Deprecated. Gets the 'raw' memory space of a memory region's base region.
123 /// If the MemRegion is originally associated with Unknown memspace, then the
124 /// State may have a more accurate memspace for this region.
125 /// Use getMemorySpace(ProgramStateRef) instead.
126 [[nodiscard]] LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion *
127 getRawMemorySpace() const;
128
129 /// Deprecated. Use getMemorySpace(ProgramStateRef) instead.
130 template <class MemSpace>
131 [[nodiscard]] const MemSpace *getRawMemorySpaceAs() const {
132 return dyn_cast<MemSpace>(getRawMemorySpace());
133 }
134
135 /// Returns the most specific memory space for this memory region in the given
136 /// ProgramStateRef. We may infer a more accurate memory space for unknown
137 /// space regions and associate this in the State.
138 [[nodiscard]] LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion *
139 getMemorySpace(ProgramStateRef State) const;
140
141 template <class MemSpace>
142 [[nodiscard]] const MemSpace *getMemorySpaceAs(ProgramStateRef State) const {
143 return dyn_cast<MemSpace>(getMemorySpace(State));
144 }
145
146 template <typename... MemorySpaces>
147 [[nodiscard]] bool hasMemorySpace(ProgramStateRef State) const {
148 static_assert(sizeof...(MemorySpaces));
149 return isa<MemorySpaces...>(getMemorySpace(State));
150 }
151
152 /// Set the dynamically deduced memory space of a MemRegion that currently has
153 /// UnknownSpaceRegion. \p Space shouldn't be UnknownSpaceRegion.
154 [[nodiscard]] ProgramStateRef
155 setMemorySpace(ProgramStateRef State, const MemSpaceRegion *Space) const;
156
157 LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion *getBaseRegion() const;
158
159 /// Recursively retrieve the region of the most derived class instance of
160 /// regions of C++ base class instances.
161 LLVM_ATTRIBUTE_RETURNS_NONNULL
163
164 /// Check if the region is a subregion of the given region.
165 /// Each region is a subregion of itself.
166 virtual bool isSubRegionOf(const MemRegion *R) const;
167
168 LLVM_ATTRIBUTE_RETURNS_NONNULL
169 const MemRegion *StripCasts(bool StripBaseAndDerivedCasts = true) const;
170
171 /// If this is a symbolic region, returns the region. Otherwise,
172 /// goes up the base chain looking for the first symbolic base region.
173 /// It might return null.
174 const SymbolicRegion *getSymbolicBase() const;
175
176 /// Compute the offset within the top level memory object.
178
179 /// Get a string representation of a region for debug use.
180 std::string getString() const;
181
182 virtual void dumpToStream(raw_ostream &os) const;
183
184 void dump() const;
185
186 /// Returns true if this region can be printed in a user-friendly way.
187 virtual bool canPrintPretty() const;
188
189 /// Print the region for use in diagnostics.
190 virtual void printPretty(raw_ostream &os) const;
191
192 /// Returns true if this region's textual representation can be used
193 /// as part of a larger expression.
194 virtual bool canPrintPrettyAsExpr() const;
195
196 /// Print the region as expression.
197 ///
198 /// When this region represents a subexpression, the method is for printing
199 /// an expression containing it.
200 virtual void printPrettyAsExpr(raw_ostream &os) const;
201
202 Kind getKind() const { return kind; }
203
204 StringRef getKindStr() const;
205
206 template<typename RegionTy> const RegionTy* getAs() const;
207 template <typename RegionTy>
208 LLVM_ATTRIBUTE_RETURNS_NONNULL const RegionTy *castAs() const;
209
210 virtual bool isBoundable() const { return false; }
211
212 /// Get descriptive name for memory region. The name is obtained from
213 /// the variable/field declaration retrieved from the memory region.
214 /// Regions that point to an element of an array are returned as: "arr[0]".
215 /// Regions that point to a struct are returned as: "st.var".
216 /// Returns an empty string for regions that don't have a clear descriptive
217 /// name (e.g. a heap are allocated by 'malloc').
218 //
219 /// \param UseQuotes Set if the name should be quoted.
220 ///
221 /// \param AllowFallback When true, always retursn a non-empty string, using
222 /// vague descriptions like "the heap area", "the string literal" (or "the
223 /// region" as a catch-all) when there is nothing better.
224 ///
225 /// \returns variable name for memory region
226 std::string getDescriptiveName(bool UseQuotes = true,
227 bool AllowFallback = false) const;
228
229 /// Retrieve source range from memory region. The range retrieval
230 /// is based on the decl obtained from the memory region.
231 /// For a VarRegion the range of the base region is returned.
232 /// For a FieldRegion the range of the field is returned.
233 /// If no declaration is found, an empty source range is returned.
234 /// The client is responsible for checking if the returned range is valid.
235 ///
236 /// \returns source range for declaration retrieved from memory region
237 SourceRange sourceRange() const;
238};
239
240/// MemSpaceRegion - A memory region that represents a "memory space";
241/// for example, the set of global variables, the stack frame, etc.
242class MemSpaceRegion : public MemRegion {
243protected:
245
247 assert(classof(this));
248 }
249
250 MemRegionManager &getMemRegionManager() const override { return Mgr; }
251
252public:
253 bool isBoundable() const override { return false; }
254
255 void Profile(llvm::FoldingSetNodeID &ID) const override;
256
257 static bool classof(const MemRegion *R) {
258 Kind k = R->getKind();
259 return k >= BEGIN_MEMSPACES && k <= END_MEMSPACES;
260 }
261};
262
263/// CodeSpaceRegion - The memory space that holds the executable code of
264/// functions and blocks.
265class CodeSpaceRegion : public MemSpaceRegion {
266 friend class MemRegionManager;
267
268 CodeSpaceRegion(MemRegionManager &mgr)
269 : MemSpaceRegion(mgr, CodeSpaceRegionKind) {}
270
271public:
272 void dumpToStream(raw_ostream &os) const override;
273
274 static bool classof(const MemRegion *R) {
275 return R->getKind() == CodeSpaceRegionKind;
276 }
277};
278
280 virtual void anchor();
281
282protected:
284 assert(classof(this));
285 }
286
287public:
288 static bool classof(const MemRegion *R) {
289 Kind k = R->getKind();
290 return k >= BEGIN_GLOBAL_MEMSPACES && k <= END_GLOBAL_MEMSPACES;
291 }
292};
293
294/// The region of the static variables within the current CodeTextRegion
295/// scope.
296///
297/// Currently, only the static locals are placed there, so we know that these
298/// variables do not get invalidated by calls to other functions.
299class StaticGlobalSpaceRegion : public GlobalsSpaceRegion {
300 friend class MemRegionManager;
301
302 const CodeTextRegion *CR;
303
304 StaticGlobalSpaceRegion(MemRegionManager &mgr, const CodeTextRegion *cr)
305 : GlobalsSpaceRegion(mgr, StaticGlobalSpaceRegionKind), CR(cr) {
306 assert(cr);
307 }
308
309public:
310 void Profile(llvm::FoldingSetNodeID &ID) const override;
311
312 void dumpToStream(raw_ostream &os) const override;
313
314 LLVM_ATTRIBUTE_RETURNS_NONNULL
315 const CodeTextRegion *getCodeRegion() const { return CR; }
316
317 static bool classof(const MemRegion *R) {
318 return R->getKind() == StaticGlobalSpaceRegionKind;
319 }
320};
321
322/// The region for all the non-static global variables.
323///
324/// This class is further split into subclasses for efficient implementation of
325/// invalidating a set of related global values as is done in
326/// RegionStoreManager::invalidateRegions (instead of finding all the dependent
327/// globals, we invalidate the whole parent region).
329 void anchor() override;
330
331protected:
333 : GlobalsSpaceRegion(mgr, k) {
334 assert(classof(this));
335 }
336
337public:
338 static bool classof(const MemRegion *R) {
339 Kind k = R->getKind();
340 return k >= BEGIN_NON_STATIC_GLOBAL_MEMSPACES &&
341 k <= END_NON_STATIC_GLOBAL_MEMSPACES;
342 }
343};
344
345/// The region containing globals which are defined in system/external
346/// headers and are considered modifiable by system calls (ex: errno).
347class GlobalSystemSpaceRegion : public NonStaticGlobalSpaceRegion {
348 friend class MemRegionManager;
349
350 GlobalSystemSpaceRegion(MemRegionManager &mgr)
351 : NonStaticGlobalSpaceRegion(mgr, GlobalSystemSpaceRegionKind) {}
352
353public:
354 void dumpToStream(raw_ostream &os) const override;
355
356 static bool classof(const MemRegion *R) {
357 return R->getKind() == GlobalSystemSpaceRegionKind;
358 }
359};
360
361/// The region containing globals which are considered not to be modified
362/// or point to data which could be modified as a result of a function call
363/// (system or internal). Ex: Const global scalars would be modeled as part of
364/// this region. This region also includes most system globals since they have
365/// low chance of being modified.
366class GlobalImmutableSpaceRegion : public NonStaticGlobalSpaceRegion {
367 friend class MemRegionManager;
368
369 GlobalImmutableSpaceRegion(MemRegionManager &mgr)
370 : NonStaticGlobalSpaceRegion(mgr, GlobalImmutableSpaceRegionKind) {}
371
372public:
373 void dumpToStream(raw_ostream &os) const override;
374
375 static bool classof(const MemRegion *R) {
376 return R->getKind() == GlobalImmutableSpaceRegionKind;
377 }
378};
379
380/// The region containing globals which can be modified by calls to
381/// "internally" defined functions - (for now just) functions other than system
382/// calls.
383class GlobalInternalSpaceRegion : public NonStaticGlobalSpaceRegion {
384 friend class MemRegionManager;
385
386 GlobalInternalSpaceRegion(MemRegionManager &mgr)
387 : NonStaticGlobalSpaceRegion(mgr, GlobalInternalSpaceRegionKind) {}
388
389public:
390 void dumpToStream(raw_ostream &os) const override;
391
392 static bool classof(const MemRegion *R) {
393 return R->getKind() == GlobalInternalSpaceRegionKind;
394 }
395};
396
397class HeapSpaceRegion : public MemSpaceRegion {
398 friend class MemRegionManager;
399
400 HeapSpaceRegion(MemRegionManager &mgr)
401 : MemSpaceRegion(mgr, HeapSpaceRegionKind) {}
402
403public:
404 void dumpToStream(raw_ostream &os) const override;
405
406 static bool classof(const MemRegion *R) {
407 return R->getKind() == HeapSpaceRegionKind;
408 }
409};
410
411class UnknownSpaceRegion : public MemSpaceRegion {
412 friend class MemRegionManager;
413
414 UnknownSpaceRegion(MemRegionManager &mgr)
415 : MemSpaceRegion(mgr, UnknownSpaceRegionKind) {}
416
417public:
418 void dumpToStream(raw_ostream &os) const override;
419
420 static bool classof(const MemRegion *R) {
421 return R->getKind() == UnknownSpaceRegionKind;
422 }
423};
424
426 virtual void anchor();
427
428 const StackFrame *SF;
429
430protected:
432 : MemSpaceRegion(mgr, k), SF(SF) {
433 assert(classof(this));
434 assert(SF);
435 }
436
437public:
438 LLVM_ATTRIBUTE_RETURNS_NONNULL
439 const StackFrame *getStackFrame() const { return SF; }
440
441 void Profile(llvm::FoldingSetNodeID &ID) const override;
442
443 static bool classof(const MemRegion *R) {
444 Kind k = R->getKind();
445 return k >= BEGIN_STACK_MEMSPACES && k <= END_STACK_MEMSPACES;
446 }
447};
448
449class StackLocalsSpaceRegion : public StackSpaceRegion {
450 friend class MemRegionManager;
451
452 StackLocalsSpaceRegion(MemRegionManager &mgr, const StackFrame *SF)
453 : StackSpaceRegion(mgr, StackLocalsSpaceRegionKind, SF) {}
454
455public:
456 void dumpToStream(raw_ostream &os) const override;
457
458 static bool classof(const MemRegion *R) {
459 return R->getKind() == StackLocalsSpaceRegionKind;
460 }
461};
462
463class StackArgumentsSpaceRegion : public StackSpaceRegion {
464private:
465 friend class MemRegionManager;
466
467 StackArgumentsSpaceRegion(MemRegionManager &mgr, const StackFrame *SF)
468 : StackSpaceRegion(mgr, StackArgumentsSpaceRegionKind, SF) {}
469
470public:
471 void dumpToStream(raw_ostream &os) const override;
472
473 static bool classof(const MemRegion *R) {
474 return R->getKind() == StackArgumentsSpaceRegionKind;
475 }
476};
477
478/// SubRegion - A region that subsets another larger region. Most regions
479/// are subclasses of SubRegion.
480class SubRegion : public MemRegion {
481 virtual void anchor();
482
483protected:
485
486 SubRegion(const MemRegion *sReg, Kind k) : MemRegion(k), superRegion(sReg) {
487 assert(classof(this));
488 assert(sReg);
489 }
490
491public:
492 LLVM_ATTRIBUTE_RETURNS_NONNULL
493 const MemRegion* getSuperRegion() const {
494 return superRegion;
495 }
496
497 MemRegionManager &getMemRegionManager() const override;
498
499 bool isSubRegionOf(const MemRegion* R) const override;
500
501 static bool classof(const MemRegion* R) {
502 return R->getKind() > END_MEMSPACES;
503 }
504};
505
506//===----------------------------------------------------------------------===//
507// MemRegion subclasses.
508//===----------------------------------------------------------------------===//
509
510/// AllocaRegion - A region that represents an untyped blob of bytes created
511/// by a call to 'alloca'.
512class AllocaRegion : public SubRegion {
513 friend class MemRegionManager;
514
515 // Block counter. Used to distinguish different pieces of memory allocated by
516 // alloca at the same call site.
517 unsigned Cnt;
518
519 const Expr *Ex;
520
521 AllocaRegion(const Expr *ex, unsigned cnt, const MemSpaceRegion *superRegion)
522 : SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {
523 assert(Ex);
524 }
525
526 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Expr *Ex,
527 unsigned Cnt, const MemRegion *superRegion);
528
529public:
530 LLVM_ATTRIBUTE_RETURNS_NONNULL
531 const Expr *getExpr() const { return Ex; }
532
533 bool isBoundable() const override { return true; }
534
535 void Profile(llvm::FoldingSetNodeID& ID) const override;
536
537 void dumpToStream(raw_ostream &os) const override;
538
539 static bool classof(const MemRegion* R) {
540 return R->getKind() == AllocaRegionKind;
541 }
542};
543
544/// TypedRegion - An abstract class representing regions that are typed.
545class TypedRegion : public SubRegion {
546 void anchor() override;
547
548protected:
549 TypedRegion(const MemRegion *sReg, Kind k) : SubRegion(sReg, k) {
550 assert(classof(this));
551 }
552
553public:
554 virtual QualType getLocationType() const = 0;
555
557 return getLocationType().getDesugaredType(Context);
558 }
559
560 bool isBoundable() const override { return true; }
561
562 static bool classof(const MemRegion* R) {
563 unsigned k = R->getKind();
564 return k >= BEGIN_TYPED_REGIONS && k <= END_TYPED_REGIONS;
565 }
566};
567
568/// TypedValueRegion - An abstract class representing regions having a typed value.
570 void anchor() override;
571
572protected:
573 TypedValueRegion(const MemRegion* sReg, Kind k) : TypedRegion(sReg, k) {
574 assert(classof(this));
575 }
576
577public:
578 virtual QualType getValueType() const = 0;
579
580 QualType getLocationType() const override {
581 // FIXME: We can possibly optimize this later to cache this value.
583 ASTContext &ctx = getContext();
584 if (T->getAs<ObjCObjectType>())
585 return ctx.getObjCObjectPointerType(T);
586 return ctx.getPointerType(getValueType());
587 }
588
591 return T.getTypePtrOrNull() ? T.getDesugaredType(Context) : T;
592 }
593
594 static bool classof(const MemRegion* R) {
595 unsigned k = R->getKind();
596 return k >= BEGIN_TYPED_VALUE_REGIONS && k <= END_TYPED_VALUE_REGIONS;
597 }
598};
599
601 void anchor() override;
602
603protected:
604 CodeTextRegion(const MemSpaceRegion *sreg, Kind k) : TypedRegion(sreg, k) {
605 assert(classof(this));
606 }
607
608public:
609 bool isBoundable() const override { return false; }
610
611 static bool classof(const MemRegion* R) {
612 Kind k = R->getKind();
613 return k >= BEGIN_CODE_TEXT_REGIONS && k <= END_CODE_TEXT_REGIONS;
614 }
615};
616
617/// FunctionCodeRegion - A region that represents code texts of function.
618class FunctionCodeRegion : public CodeTextRegion {
619 friend class MemRegionManager;
620
621 const NamedDecl *FD;
622
623 FunctionCodeRegion(const NamedDecl *fd, const CodeSpaceRegion* sreg)
624 : CodeTextRegion(sreg, FunctionCodeRegionKind), FD(fd) {
625 assert(isa<ObjCMethodDecl>(fd) || isa<FunctionDecl>(fd));
626 }
627
628 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const NamedDecl *FD,
629 const MemRegion*);
630
631public:
632 QualType getLocationType() const override {
633 const ASTContext &Ctx = getContext();
634 if (const auto *D = dyn_cast<FunctionDecl>(FD)) {
635 return Ctx.getPointerType(D->getType());
636 }
637
638 assert(isa<ObjCMethodDecl>(FD));
639 assert(false && "Getting the type of ObjCMethod is not supported yet");
640
641 // TODO: We might want to return a different type here (ex: id (*ty)(...))
642 // depending on how it is used.
643 return {};
644 }
645
646 const NamedDecl *getDecl() const {
647 return FD;
648 }
649
650 void dumpToStream(raw_ostream &os) const override;
651
652 void Profile(llvm::FoldingSetNodeID& ID) const override;
653
654 static bool classof(const MemRegion* R) {
655 return R->getKind() == FunctionCodeRegionKind;
656 }
657};
658
659/// BlockCodeRegion - A region that represents code texts of blocks (closures).
660/// Blocks are represented with two kinds of regions. BlockCodeRegions
661/// represent the "code", while BlockDataRegions represent instances of blocks,
662/// which correspond to "code+data". The distinction is important, because
663/// like a closure a block captures the values of externally referenced
664/// variables.
665class BlockCodeRegion : public CodeTextRegion {
666 friend class MemRegionManager;
667
668 const BlockDecl *BD;
670 CanQualType locTy;
671
672 BlockCodeRegion(const BlockDecl *bd, CanQualType lTy,
673 AnalysisDeclContext *ac, const CodeSpaceRegion* sreg)
674 : CodeTextRegion(sreg, BlockCodeRegionKind), BD(bd), AC(ac), locTy(lTy) {
675 assert(bd);
676 assert(ac);
677 assert(lTy->getTypePtr()->isBlockPointerType());
678 }
679
680 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const BlockDecl *BD,
682 const MemRegion*);
683
684public:
685 QualType getLocationType() const override {
686 return locTy;
687 }
688
689 LLVM_ATTRIBUTE_RETURNS_NONNULL
690 const BlockDecl *getDecl() const {
691 return BD;
692 }
693
694 LLVM_ATTRIBUTE_RETURNS_NONNULL
696
697 void dumpToStream(raw_ostream &os) const override;
698
699 void Profile(llvm::FoldingSetNodeID& ID) const override;
700
701 static bool classof(const MemRegion* R) {
702 return R->getKind() == BlockCodeRegionKind;
703 }
704};
705
706/// BlockDataRegion - A region that represents a block instance.
707/// Blocks are represented with two kinds of regions. BlockCodeRegions
708/// represent the "code", while BlockDataRegions represent instances of blocks,
709/// which correspond to "code+data". The distinction is important, because
710/// like a closure a block captures the values of externally referenced
711/// variables.
712class BlockDataRegion : public TypedRegion {
713 friend class MemRegionManager;
714
715 const BlockCodeRegion *BC;
716 const StackFrame *SF;
717 unsigned BlockCount;
718 void *ReferencedVars = nullptr;
719 void *OriginalVars = nullptr;
720
721 BlockDataRegion(const BlockCodeRegion *bc, const StackFrame *SF,
722 unsigned count, const MemSpaceRegion *sreg)
723 : TypedRegion(sreg, BlockDataRegionKind), BC(bc), SF(SF),
724 BlockCount(count) {
725 assert(bc);
726 assert(bc->getDecl());
727 assert(SF);
728 assert(isa<GlobalImmutableSpaceRegion>(sreg) ||
731 }
732
733 static void ProfileRegion(llvm::FoldingSetNodeID &, const BlockCodeRegion *,
734 const StackFrame *, unsigned, const MemRegion *);
735
736public:
737 LLVM_ATTRIBUTE_RETURNS_NONNULL
738 const BlockCodeRegion *getCodeRegion() const { return BC; }
739
740 LLVM_ATTRIBUTE_RETURNS_NONNULL
741 const BlockDecl *getDecl() const { return BC->getDecl(); }
742
743 QualType getLocationType() const override { return BC->getLocationType(); }
744
746 const MemRegion * const *R;
747 const MemRegion * const *OriginalR;
748
749 public:
750 explicit referenced_vars_iterator(const MemRegion * const *r,
751 const MemRegion * const *originalR)
752 : R(r), OriginalR(originalR) {}
753
754 LLVM_ATTRIBUTE_RETURNS_NONNULL
756 return cast<VarRegion>(*R);
757 }
758
759 LLVM_ATTRIBUTE_RETURNS_NONNULL
761 return cast<VarRegion>(*OriginalR);
762 }
763
765 assert((R == nullptr) == (I.R == nullptr));
766 return I.R == R;
767 }
768
770 assert((R == nullptr) == (I.R == nullptr));
771 return I.R != R;
772 }
773
775 ++R;
776 ++OriginalR;
777 return *this;
778 }
779
780 // This isn't really a conventional iterator.
781 // We just implement the deref as a no-op for now to make range-based for
782 // loops work.
783 const referenced_vars_iterator &operator*() const { return *this; }
784 };
785
786 /// Return the original region for a captured region, if
787 /// one exists. It might return null.
788 const VarRegion *getOriginalRegion(const VarRegion *VR) const;
789
790 referenced_vars_iterator referenced_vars_begin() const;
791 referenced_vars_iterator referenced_vars_end() const;
792 llvm::iterator_range<referenced_vars_iterator> referenced_vars() const;
793
794 void dumpToStream(raw_ostream &os) const override;
795
796 void Profile(llvm::FoldingSetNodeID& ID) const override;
797
798 static bool classof(const MemRegion* R) {
799 return R->getKind() == BlockDataRegionKind;
800 }
801
802private:
803 void LazyInitializeReferencedVars();
804 std::pair<const VarRegion *, const VarRegion *>
805 getCaptureRegions(const VarDecl *VD);
806};
807
808/// SymbolicRegion - A special, "non-concrete" region. Unlike other region
809/// classes, SymbolicRegion represents a region that serves as an alias for
810/// either a real region, a NULL pointer, etc. It essentially is used to
811/// map the concept of symbolic values into the domain of regions. Symbolic
812/// regions do not need to be typed.
813class SymbolicRegion : public SubRegion {
814 friend class MemRegionManager;
815
816 const SymbolRef sym;
817
818 SymbolicRegion(const SymbolRef s, const MemSpaceRegion *sreg)
819 : SubRegion(sreg, SymbolicRegionKind), sym(s) {
820 // Because pointer arithmetic is represented by ElementRegion layers,
821 // the base symbol here should not contain any arithmetic.
822 assert(isa_and_nonnull<SymbolData>(s));
823 assert(s->getType()->isAnyPointerType() ||
824 s->getType()->isReferenceType() ||
826 assert(isa<UnknownSpaceRegion>(sreg) || isa<HeapSpaceRegion>(sreg) ||
828 }
829
830public:
831 /// It might return null.
832 SymbolRef getSymbol() const { return sym; }
833
834 /// Gets the type of the wrapped symbol.
835 /// This type might not be accurate at all times - it's just our best guess.
836 /// Consider these cases:
837 /// void foo(void *data, char *str, base *obj) {...}
838 /// The type of the pointee of `data` is of course not `void`, yet that's our
839 /// best guess. `str` might point to any object and `obj` might point to some
840 /// derived instance. `TypedRegions` other hand are representing the cases
841 /// when we actually know their types.
843 return sym->getType()->getPointeeType();
844 }
845
846 bool isBoundable() const override { return true; }
847
848 void Profile(llvm::FoldingSetNodeID& ID) const override;
849
850 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
851 SymbolRef sym,
852 const MemRegion* superRegion);
853
854 void dumpToStream(raw_ostream &os) const override;
855
856 static bool classof(const MemRegion* R) {
857 return R->getKind() == SymbolicRegionKind;
858 }
859};
860
861/// StringRegion - Region associated with a StringLiteral.
862class StringRegion : public TypedValueRegion {
863 friend class MemRegionManager;
864
865 const StringLiteral *Str;
866
867 StringRegion(const StringLiteral *str, const GlobalInternalSpaceRegion *sreg)
868 : TypedValueRegion(sreg, StringRegionKind), Str(str) {
869 assert(str);
870 }
871
872 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
873 const StringLiteral *Str,
874 const MemRegion *superRegion);
875
876public:
877 LLVM_ATTRIBUTE_RETURNS_NONNULL
878 const StringLiteral *getStringLiteral() const { return Str; }
879
880 QualType getValueType() const override { return Str->getType(); }
881
882 bool isBoundable() const override { return false; }
883
884 void Profile(llvm::FoldingSetNodeID& ID) const override {
885 ProfileRegion(ID, Str, superRegion);
886 }
887
888 void dumpToStream(raw_ostream &os) const override;
889
890 static bool classof(const MemRegion* R) {
891 return R->getKind() == StringRegionKind;
892 }
893};
894
895/// The region associated with an ObjCStringLiteral.
896class ObjCStringRegion : public TypedValueRegion {
897 friend class MemRegionManager;
898
899 const ObjCStringLiteral *Str;
900
901 ObjCStringRegion(const ObjCStringLiteral *str,
902 const GlobalInternalSpaceRegion *sreg)
903 : TypedValueRegion(sreg, ObjCStringRegionKind), Str(str) {
904 assert(str);
905 }
906
907 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
908 const ObjCStringLiteral *Str,
909 const MemRegion *superRegion);
910
911public:
912 LLVM_ATTRIBUTE_RETURNS_NONNULL
913 const ObjCStringLiteral *getObjCStringLiteral() const { return Str; }
914
915 QualType getValueType() const override { return Str->getType(); }
916
917 bool isBoundable() const override { return false; }
918
919 void Profile(llvm::FoldingSetNodeID& ID) const override {
920 ProfileRegion(ID, Str, superRegion);
921 }
922
923 void dumpToStream(raw_ostream &os) const override;
924
925 static bool classof(const MemRegion* R) {
926 return R->getKind() == ObjCStringRegionKind;
927 }
928};
929
930/// CompoundLiteralRegion - A memory region representing a compound literal.
931/// Compound literals are essentially temporaries that are stack allocated
932/// or in the global constant pool.
933class CompoundLiteralRegion : public TypedValueRegion {
934 friend class MemRegionManager;
935
936 const CompoundLiteralExpr *CL;
937
938 CompoundLiteralRegion(const CompoundLiteralExpr *cl,
939 const MemSpaceRegion *sReg)
940 : TypedValueRegion(sReg, CompoundLiteralRegionKind), CL(cl) {
941 assert(cl);
942 assert(isa<GlobalInternalSpaceRegion>(sReg) ||
944 }
945
946 static void ProfileRegion(llvm::FoldingSetNodeID& ID,
947 const CompoundLiteralExpr *CL,
948 const MemRegion* superRegion);
949
950public:
951 QualType getValueType() const override { return CL->getType(); }
952
953 bool isBoundable() const override { return !CL->isFileScope(); }
954
955 void Profile(llvm::FoldingSetNodeID& ID) const override;
956
957 void dumpToStream(raw_ostream &os) const override;
958
959 LLVM_ATTRIBUTE_RETURNS_NONNULL
960 const CompoundLiteralExpr *getLiteralExpr() const { return CL; }
961
962 static bool classof(const MemRegion* R) {
963 return R->getKind() == CompoundLiteralRegionKind;
964 }
965};
966
968protected:
969 DeclRegion(const MemRegion *sReg, Kind k) : TypedValueRegion(sReg, k) {
970 assert(classof(this));
971 }
972
973public:
974 // TODO what does this return?
975 virtual const ValueDecl *getDecl() const = 0;
976
977 static bool classof(const MemRegion* R) {
978 unsigned k = R->getKind();
979 return k >= BEGIN_DECL_REGIONS && k <= END_DECL_REGIONS;
980 }
981};
982
983class VarRegion : public DeclRegion {
984 friend class MemRegionManager;
985
986protected:
987 // Constructors and protected methods.
988 VarRegion(const MemRegion *sReg, Kind k) : DeclRegion(sReg, k) {
989 // VarRegion appears in unknown space when it's a block variable as seen
990 // from a block using it, when this block is analyzed at top-level.
991 // Other block variables appear within block data regions,
992 // which, unlike everything else on this list, are not memory spaces.
993 assert(isa<GlobalsSpaceRegion>(sReg) || isa<StackSpaceRegion>(sReg) ||
995 }
996
997public:
998 // TODO what does this return?
999 const VarDecl *getDecl() const override = 0;
1000
1001 /// It might return null.
1002 const StackFrame *getStackFrame() const;
1003
1004 QualType getValueType() const override {
1005 // FIXME: We can cache this if needed.
1006 return getDecl()->getType();
1007 }
1008
1009 static bool classof(const MemRegion *R) {
1010 unsigned k = R->getKind();
1011 return k >= BEGIN_VAR_REGIONS && k <= END_VAR_REGIONS;
1012 }
1013};
1014
1015// TODO: Currently MemRegionManager::getVarRegion returns NonParamVarRegion
1016// instances to represent the parameters of the entrypoint stack frame and
1017// parameters of outer stack frames that appear as captured within a lambda or
1018// a block. This should be overhauled.
1019class NonParamVarRegion : public VarRegion {
1020 friend class MemRegionManager;
1021
1022 const VarDecl *VD;
1023
1024 // Constructors and private methods.
1025 NonParamVarRegion(const VarDecl *vd, const MemRegion *sReg)
1026 : VarRegion(sReg, NonParamVarRegionKind), VD(vd) {
1027 // VarRegion appears in unknown space when it's a block variable as seen
1028 // from a block using it, when this block is analyzed at top-level.
1029 // Other block variables appear within block data regions,
1030 // which, unlike everything else on this list, are not memory spaces.
1031 assert(isa<GlobalsSpaceRegion>(sReg) || isa<StackSpaceRegion>(sReg) ||
1033 assert(vd);
1034 }
1035
1036 static void ProfileRegion(llvm::FoldingSetNodeID &ID, const VarDecl *VD,
1037 const MemRegion *superRegion);
1038
1039public:
1040 void Profile(llvm::FoldingSetNodeID &ID) const override;
1041
1042 LLVM_ATTRIBUTE_RETURNS_NONNULL
1043 const VarDecl *getDecl() const override { return VD; }
1044
1045 QualType getValueType() const override {
1046 // FIXME: We can cache this if needed.
1047 return getDecl()->getType();
1048 }
1049
1050 void dumpToStream(raw_ostream &os) const override;
1051
1052 bool canPrintPrettyAsExpr() const override;
1053
1054 void printPrettyAsExpr(raw_ostream &os) const override;
1055
1056 static bool classof(const MemRegion* R) {
1057 return R->getKind() == NonParamVarRegionKind;
1058 }
1059};
1060
1061/// ParamVarRegion - Represents a region for parameters. Only parameters of the
1062/// function in the current stack frame are represented as `ParamVarRegion`s.
1063/// Parameters of top-level analyzed functions as well as captured paremeters
1064/// by lambdas and blocks are repesented as `NonParamVarRegion`s.
1065/// TODO: It would be nice to make this more consistent.
1066
1067// FIXME: `ParamVarRegion` only supports parameters of functions, C++
1068// constructors, blocks and Objective-C methods with existing `Decl`. Upon
1069// implementing stack frame creations for functions without decl (functions
1070// passed by unknown function pointer) methods of `ParamVarRegion` must be
1071// updated.
1072class ParamVarRegion : public VarRegion {
1073 friend class MemRegionManager;
1074
1075 const Expr *OriginExpr;
1076 unsigned Index;
1077
1078 ParamVarRegion(const Expr *OE, unsigned Idx, const MemRegion *SReg)
1079 : VarRegion(SReg, ParamVarRegionKind), OriginExpr(OE), Index(Idx) {
1080 assert(!cast<StackSpaceRegion>(SReg)->getStackFrame()->inTopFrame());
1081 assert(OriginExpr);
1082 }
1083
1084 static void ProfileRegion(llvm::FoldingSetNodeID &ID, const Expr *OE,
1085 unsigned Idx, const MemRegion *SReg);
1086
1087public:
1088 LLVM_ATTRIBUTE_RETURNS_NONNULL
1089 const Expr *getOriginExpr() const { return OriginExpr; }
1090 unsigned getIndex() const { return Index; }
1091
1092 void Profile(llvm::FoldingSetNodeID& ID) const override;
1093
1094 void dumpToStream(raw_ostream &os) const override;
1095
1096 QualType getValueType() const override;
1097
1098 /// TODO: What does this return?
1099 const ParmVarDecl *getDecl() const override;
1100
1101 bool canPrintPrettyAsExpr() const override;
1102 void printPrettyAsExpr(raw_ostream &os) const override;
1103
1104 static bool classof(const MemRegion *R) {
1105 return R->getKind() == ParamVarRegionKind;
1106 }
1107};
1108
1109/// CXXThisRegion - Represents the region for the implicit 'this' parameter
1110/// in a call to a C++ method. This region doesn't represent the object
1111/// referred to by 'this', but rather 'this' itself.
1112class CXXThisRegion : public TypedValueRegion {
1113 friend class MemRegionManager;
1114
1115 CXXThisRegion(const PointerType *thisPointerTy,
1116 const StackArgumentsSpaceRegion *sReg)
1117 : TypedValueRegion(sReg, CXXThisRegionKind),
1118 ThisPointerTy(thisPointerTy) {
1119 assert(ThisPointerTy->getPointeeType()->getAsCXXRecordDecl() &&
1120 "Invalid region type!");
1121 }
1122
1123 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
1124 const PointerType *PT,
1125 const MemRegion *sReg);
1126
1127public:
1128 void Profile(llvm::FoldingSetNodeID &ID) const override;
1129
1130 QualType getValueType() const override {
1131 return QualType(ThisPointerTy, 0);
1132 }
1133
1134 void dumpToStream(raw_ostream &os) const override;
1135
1136 static bool classof(const MemRegion* R) {
1137 return R->getKind() == CXXThisRegionKind;
1138 }
1139
1140private:
1141 const PointerType *ThisPointerTy;
1142};
1143
1144class FieldRegion : public DeclRegion {
1145 friend class MemRegionManager;
1146
1147 const FieldDecl *FD;
1148
1149 FieldRegion(const FieldDecl *fd, const SubRegion *sReg)
1150 : DeclRegion(sReg, FieldRegionKind), FD(fd) {
1151 assert(FD);
1152 }
1153
1154 static void ProfileRegion(llvm::FoldingSetNodeID &ID, const FieldDecl *FD,
1155 const MemRegion* superRegion) {
1156 ID.AddInteger(static_cast<unsigned>(FieldRegionKind));
1157 ID.AddPointer(FD);
1158 ID.AddPointer(superRegion);
1159 }
1160
1161public:
1162 LLVM_ATTRIBUTE_RETURNS_NONNULL
1163 const FieldDecl *getDecl() const override { return FD; }
1164
1165 void Profile(llvm::FoldingSetNodeID &ID) const override;
1166
1167 QualType getValueType() const override {
1168 // FIXME: We can cache this if needed.
1169 return getDecl()->getType();
1170 }
1171
1172 void dumpToStream(raw_ostream &os) const override;
1173
1174 bool canPrintPretty() const override;
1175 void printPretty(raw_ostream &os) const override;
1176 bool canPrintPrettyAsExpr() const override;
1177 void printPrettyAsExpr(raw_ostream &os) const override;
1178
1179 static bool classof(const MemRegion* R) {
1180 return R->getKind() == FieldRegionKind;
1181 }
1182};
1183
1184class ObjCIvarRegion : public DeclRegion {
1185 friend class MemRegionManager;
1186
1187 const ObjCIvarDecl *IVD;
1188
1189 ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg);
1190
1191 static void ProfileRegion(llvm::FoldingSetNodeID& ID, const ObjCIvarDecl *ivd,
1192 const MemRegion* superRegion);
1193
1194public:
1195 LLVM_ATTRIBUTE_RETURNS_NONNULL
1196 const ObjCIvarDecl *getDecl() const override;
1197
1198 void Profile(llvm::FoldingSetNodeID& ID) const override;
1199
1200 QualType getValueType() const override;
1201
1202 bool canPrintPrettyAsExpr() const override;
1203 void printPrettyAsExpr(raw_ostream &os) const override;
1204
1205 void dumpToStream(raw_ostream &os) const override;
1206
1207 static bool classof(const MemRegion* R) {
1208 return R->getKind() == ObjCIvarRegionKind;
1209 }
1210};
1211
1212//===----------------------------------------------------------------------===//
1213// Auxiliary data classes for use with MemRegions.
1214//===----------------------------------------------------------------------===//
1215
1216class RegionRawOffset {
1217 friend class ElementRegion;
1218
1219 const MemRegion *Region;
1220 CharUnits Offset;
1221
1222 RegionRawOffset(const MemRegion* reg, CharUnits offset = CharUnits::Zero())
1223 : Region(reg), Offset(offset) {}
1224
1225public:
1226 // FIXME: Eventually support symbolic offsets.
1227 CharUnits getOffset() const { return Offset; }
1228
1229 // It might return null.
1230 const MemRegion *getRegion() const { return Region; }
1231
1232 void dumpToStream(raw_ostream &os) const;
1233 void dump() const;
1234};
1235
1236/// ElementRegion is used to represent both array elements and casts.
1237class ElementRegion : public TypedValueRegion {
1238 friend class MemRegionManager;
1239
1240 QualType ElementType;
1241 NonLoc Index;
1242
1243 ElementRegion(QualType elementType, NonLoc Idx, const SubRegion *sReg)
1244 : TypedValueRegion(sReg, ElementRegionKind), ElementType(elementType),
1245 Index(Idx) {
1246 assert((!isa<nonloc::ConcreteInt>(Idx) ||
1247 Idx.castAs<nonloc::ConcreteInt>().getValue()->isSigned()) &&
1248 "The index must be signed");
1249 assert(!elementType.isNull() && !elementType->isVoidType() &&
1250 "Invalid region type!");
1251 }
1252
1253 static void ProfileRegion(llvm::FoldingSetNodeID& ID, QualType elementType,
1254 SVal Idx, const MemRegion* superRegion);
1255
1256public:
1257 NonLoc getIndex() const { return Index; }
1258
1259 QualType getValueType() const override { return ElementType; }
1260
1261 QualType getElementType() const { return ElementType; }
1262
1263 /// Compute the offset within the array. The array might also be a subobject.
1265
1266 void dumpToStream(raw_ostream &os) const override;
1267
1268 void Profile(llvm::FoldingSetNodeID& ID) const override;
1269
1270 static bool classof(const MemRegion* R) {
1271 return R->getKind() == ElementRegionKind;
1272 }
1273};
1274
1275// C++ temporary object associated with an expression.
1276class CXXTempObjectRegion : public TypedValueRegion {
1277 friend class MemRegionManager;
1278
1279 Expr const *Ex;
1280
1281 CXXTempObjectRegion(Expr const *E, MemSpaceRegion const *sReg)
1282 : TypedValueRegion(sReg, CXXTempObjectRegionKind), Ex(E) {
1283 assert(E);
1284 assert(isa<StackLocalsSpaceRegion>(sReg));
1285 }
1286
1287 static void ProfileRegion(llvm::FoldingSetNodeID &ID,
1288 Expr const *E, const MemRegion *sReg);
1289
1290public:
1291 LLVM_ATTRIBUTE_RETURNS_NONNULL
1292 const Expr *getExpr() const { return Ex; }
1293
1294 LLVM_ATTRIBUTE_RETURNS_NONNULL
1295 const StackFrame *getStackFrame() const;
1296
1297 QualType getValueType() const override { return Ex->getType(); }
1298
1299 void dumpToStream(raw_ostream &os) const override;
1300
1301 void Profile(llvm::FoldingSetNodeID &ID) const override;
1302
1303 static bool classof(const MemRegion* R) {
1304 return R->getKind() == CXXTempObjectRegionKind;
1305 }
1306};
1307
1308// C++ temporary object that have lifetime extended to lifetime of the
1309// variable. Usually they represent temporary bounds to reference variables.
1310class CXXLifetimeExtendedObjectRegion : public TypedValueRegion {
1311 friend class MemRegionManager;
1312
1313 Expr const *Ex;
1314 ValueDecl const *ExD;
1315
1316 CXXLifetimeExtendedObjectRegion(Expr const *E, ValueDecl const *D,
1317 MemSpaceRegion const *sReg)
1318 : TypedValueRegion(sReg, CXXLifetimeExtendedObjectRegionKind), Ex(E),
1319 ExD(D) {
1320 assert(E);
1321 assert(D);
1323 }
1324
1325 static void ProfileRegion(llvm::FoldingSetNodeID &ID, Expr const *E,
1326 ValueDecl const *D, const MemRegion *sReg);
1327
1328public:
1329 LLVM_ATTRIBUTE_RETURNS_NONNULL
1330 const Expr *getExpr() const { return Ex; }
1331 LLVM_ATTRIBUTE_RETURNS_NONNULL
1332 const ValueDecl *getExtendingDecl() const { return ExD; }
1333 /// It might return null.
1334 const StackFrame *getStackFrame() const;
1335
1336 QualType getValueType() const override { return Ex->getType(); }
1337
1338 void dumpToStream(raw_ostream &os) const override;
1339
1340 void Profile(llvm::FoldingSetNodeID &ID) const override;
1341
1342 static bool classof(const MemRegion *R) {
1343 return R->getKind() == CXXLifetimeExtendedObjectRegionKind;
1344 }
1345};
1346
1347// CXXBaseObjectRegion represents a base object within a C++ object. It is
1348// identified by the base class declaration and the region of its parent object.
1349class CXXBaseObjectRegion : public TypedValueRegion {
1350 friend class MemRegionManager;
1351
1352 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> Data;
1353
1354 CXXBaseObjectRegion(const CXXRecordDecl *RD, bool IsVirtual,
1355 const SubRegion *SReg)
1356 : TypedValueRegion(SReg, CXXBaseObjectRegionKind), Data(RD, IsVirtual) {
1357 assert(RD);
1358 }
1359
1360 static void ProfileRegion(llvm::FoldingSetNodeID &ID, const CXXRecordDecl *RD,
1361 bool IsVirtual, const MemRegion *SReg);
1362
1363public:
1364 LLVM_ATTRIBUTE_RETURNS_NONNULL
1365 const CXXRecordDecl *getDecl() const { return Data.getPointer(); }
1366 bool isVirtual() const { return Data.getInt(); }
1367
1368 QualType getValueType() const override;
1369
1370 void dumpToStream(raw_ostream &os) const override;
1371
1372 void Profile(llvm::FoldingSetNodeID &ID) const override;
1373
1374 bool canPrintPrettyAsExpr() const override;
1375
1376 void printPrettyAsExpr(raw_ostream &os) const override;
1377
1378 static bool classof(const MemRegion *region) {
1379 return region->getKind() == CXXBaseObjectRegionKind;
1380 }
1381};
1382
1383// CXXDerivedObjectRegion represents a derived-class object that surrounds
1384// a C++ object. It is identified by the derived class declaration and the
1385// region of its parent object. It is a bit counter-intuitive (but not otherwise
1386// unseen) that this region represents a larger segment of memory that its
1387// super-region.
1388class CXXDerivedObjectRegion : public TypedValueRegion {
1389 friend class MemRegionManager;
1390
1391 const CXXRecordDecl *DerivedD;
1392
1393 CXXDerivedObjectRegion(const CXXRecordDecl *DerivedD, const SubRegion *SReg)
1394 : TypedValueRegion(SReg, CXXDerivedObjectRegionKind), DerivedD(DerivedD) {
1395 assert(DerivedD);
1396 // In case of a concrete region, it should always be possible to model
1397 // the base-to-derived cast by undoing a previous derived-to-base cast,
1398 // otherwise the cast is most likely ill-formed.
1399 assert(SReg->getSymbolicBase() &&
1400 "Should have unwrapped a base region instead!");
1401 }
1402
1403 static void ProfileRegion(llvm::FoldingSetNodeID &ID, const CXXRecordDecl *RD,
1404 const MemRegion *SReg);
1405
1406public:
1407 LLVM_ATTRIBUTE_RETURNS_NONNULL
1408 const CXXRecordDecl *getDecl() const { return DerivedD; }
1409
1410 QualType getValueType() const override;
1411
1412 void dumpToStream(raw_ostream &os) const override;
1413
1414 void Profile(llvm::FoldingSetNodeID &ID) const override;
1415
1416 bool canPrintPrettyAsExpr() const override;
1417
1418 void printPrettyAsExpr(raw_ostream &os) const override;
1419
1420 static bool classof(const MemRegion *region) {
1421 return region->getKind() == CXXDerivedObjectRegionKind;
1422 }
1423};
1424
1425template<typename RegionTy>
1426const RegionTy* MemRegion::getAs() const {
1427 if (const auto *RT = dyn_cast<RegionTy>(this))
1428 return RT;
1429
1430 return nullptr;
1431}
1432
1433template <typename RegionTy>
1434LLVM_ATTRIBUTE_RETURNS_NONNULL const RegionTy *MemRegion::castAs() const {
1435 return cast<RegionTy>(this);
1436}
1437
1438//===----------------------------------------------------------------------===//
1439// MemRegionManager - Factory object for creating regions.
1440//===----------------------------------------------------------------------===//
1441
1443 ASTContext &Ctx;
1444 llvm::BumpPtrAllocator& A;
1445
1446 llvm::FoldingSet<MemRegion> Regions;
1447
1448 GlobalInternalSpaceRegion *InternalGlobals = nullptr;
1449 GlobalSystemSpaceRegion *SystemGlobals = nullptr;
1450 GlobalImmutableSpaceRegion *ImmutableGlobals = nullptr;
1451
1452 llvm::DenseMap<const StackFrame *, StackLocalsSpaceRegion *>
1453 StackLocalsSpaceRegions;
1454 llvm::DenseMap<const StackFrame *, StackArgumentsSpaceRegion *>
1455 StackArgumentsSpaceRegions;
1456 llvm::DenseMap<const CodeTextRegion *, StaticGlobalSpaceRegion *>
1457 StaticsGlobalSpaceRegions;
1458
1459 HeapSpaceRegion *heap = nullptr;
1460 UnknownSpaceRegion *unknown = nullptr;
1461 CodeSpaceRegion *code = nullptr;
1462
1463public:
1464 MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a) : Ctx(c), A(a) {}
1466
1467 ASTContext &getContext() { return Ctx; }
1468 const ASTContext &getContext() const { return Ctx; }
1469
1470 llvm::BumpPtrAllocator &getAllocator() { return A; }
1471
1472 /// \returns The static size in bytes of the region \p MR.
1473 /// \note The region \p MR must be a 'SubRegion'.
1475 SValBuilder &SVB) const;
1476
1477 /// getStackLocalsRegion - Retrieve the memory region associated with the
1478 /// specified stack frame.
1480
1481 /// getStackArgumentsRegion - Retrieve the memory region associated with
1482 /// function/method arguments of the specified stack frame.
1485
1486 /// getGlobalsRegion - Retrieve the memory region associated with
1487 /// global variables.
1489 MemRegion::Kind K = MemRegion::GlobalInternalSpaceRegionKind,
1490 const CodeTextRegion *R = nullptr);
1491
1492 /// getHeapRegion - Retrieve the memory region associated with the
1493 /// generic "heap".
1495
1496 /// getUnknownRegion - Retrieve the memory region associated with unknown
1497 /// memory space.
1499
1501
1502 /// getAllocaRegion - Retrieve a region associated with a call to alloca().
1503 const AllocaRegion *getAllocaRegion(const Expr *Ex, unsigned Cnt,
1504 const StackFrame *SF);
1505
1506 /// getCompoundLiteralRegion - Retrieve the region associated with a
1507 /// given CompoundLiteral.
1508 const CompoundLiteralRegion *
1510
1511 /// getCXXThisRegion - Retrieve the [artificial] region associated with the
1512 /// parameter 'this'.
1513 const CXXThisRegion *getCXXThisRegion(QualType thisPointerTy,
1514 const StackFrame *SF);
1515
1516 /// Retrieve or create a "symbolic" memory region.
1517 /// If no memory space is specified, `UnknownSpaceRegion` will be used.
1518 const SymbolicRegion *
1519 getSymbolicRegion(SymbolRef Sym, const MemSpaceRegion *MemSpace = nullptr);
1520
1521 /// Return a unique symbolic region belonging to heap memory space.
1523
1524 const StringRegion *getStringRegion(const StringLiteral *Str);
1525
1527
1528 /// getVarRegion - Retrieve or create the memory region associated with
1529 /// a specified VarDecl and StackFrame.
1530 const VarRegion *getVarRegion(const VarDecl *VD, const StackFrame *SF);
1531
1532 /// getVarRegion - Retrieve or create the memory region associated with
1533 /// a specified VarDecl and StackFrame.
1535 const MemRegion *superR);
1536
1537 /// getParamVarRegion - Retrieve or create the memory region
1538 /// associated with a specified CallExpr, Index and StackFrame.
1539 const ParamVarRegion *getParamVarRegion(const Expr *OriginExpr,
1540 unsigned Index, const StackFrame *SF);
1541
1542 /// getElementRegion - Retrieve the memory region associated with the
1543 /// associated element type, index, and super region.
1544 const ElementRegion *getElementRegion(QualType elementType, NonLoc Idx,
1545 const SubRegion *superRegion,
1546 const ASTContext &Ctx);
1547
1549 const SubRegion *superRegion) {
1550 return getElementRegion(ER->getElementType(), ER->getIndex(),
1551 superRegion, ER->getContext());
1552 }
1553
1554 /// getFieldRegion - Retrieve or create the memory region associated with
1555 /// a specified FieldDecl. 'superRegion' corresponds to the containing
1556 /// memory region (which typically represents the memory representing
1557 /// a structure or class).
1558 const FieldRegion *getFieldRegion(const FieldDecl *FD,
1559 const SubRegion *SuperRegion);
1560
1562 const SubRegion *superRegion) {
1563 return getFieldRegion(FR->getDecl(), superRegion);
1564 }
1565
1566 /// getObjCIvarRegion - Retrieve or create the memory region associated with
1567 /// a specified Objective-c instance variable. 'superRegion' corresponds
1568 /// to the containing region (which typically represents the Objective-C
1569 /// object).
1571 const SubRegion* superRegion);
1572
1574 StackFrame const *SF);
1575
1576 /// Create a CXXLifetimeExtendedObjectRegion for temporaries which are
1577 /// lifetime-extended by local references.
1580 StackFrame const *SF);
1581
1582 /// Create a CXXLifetimeExtendedObjectRegion for temporaries which are
1583 /// lifetime-extended by *static* references.
1584 /// This differs from \ref getCXXLifetimeExtendedObjectRegion(Expr const *,
1585 /// ValueDecl const *, StackFrame const *) in the super-region used.
1588
1589 /// Create a CXXBaseObjectRegion with the given base class for region
1590 /// \p Super.
1591 ///
1592 /// The type of \p Super is assumed be a class deriving from \p BaseClass.
1593 const CXXBaseObjectRegion *
1594 getCXXBaseObjectRegion(const CXXRecordDecl *BaseClass, const SubRegion *Super,
1595 bool IsVirtual);
1596
1597 /// Create a CXXBaseObjectRegion with the same CXXRecordDecl but a different
1598 /// super region.
1599 const CXXBaseObjectRegion *
1601 const SubRegion *superRegion) {
1602 return getCXXBaseObjectRegion(baseReg->getDecl(), superRegion,
1603 baseReg->isVirtual());
1604 }
1605
1606 /// Create a CXXDerivedObjectRegion with the given derived class for region
1607 /// \p Super. This should not be used for casting an existing
1608 /// CXXBaseObjectRegion back to the derived type; instead, CXXBaseObjectRegion
1609 /// should be removed.
1612 const SubRegion *Super);
1613
1616 CanQualType locTy,
1618
1619 /// getBlockDataRegion - Get the memory region associated with an instance
1620 /// of a block. Unlike many other MemRegions, the StackFrame * argument
1621 /// is allowed to be NULL for cases where we have no known stack frame.
1623 const StackFrame *SF,
1624 unsigned blockCount);
1625
1626private:
1627 template <typename RegionTy, typename SuperTy,
1628 typename Arg1Ty>
1629 RegionTy* getSubRegion(const Arg1Ty arg1,
1630 const SuperTy* superRegion);
1631
1632 template <typename RegionTy, typename SuperTy,
1633 typename Arg1Ty, typename Arg2Ty>
1634 RegionTy* getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
1635 const SuperTy* superRegion);
1636
1637 template <typename RegionTy, typename SuperTy,
1638 typename Arg1Ty, typename Arg2Ty, typename Arg3Ty>
1639 RegionTy* getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
1640 const Arg3Ty arg3,
1641 const SuperTy* superRegion);
1642
1643 template <typename REG>
1644 const REG* LazyAllocate(REG*& region);
1645
1646 template <typename REG, typename ARG>
1647 const REG* LazyAllocate(REG*& region, ARG a);
1648};
1649
1650//===----------------------------------------------------------------------===//
1651// Out-of-line member definitions.
1652//===----------------------------------------------------------------------===//
1653
1656}
1657
1658//===----------------------------------------------------------------------===//
1659// Means for storing region/symbol handling traits.
1660//===----------------------------------------------------------------------===//
1661
1662/// Information about invalidation for a particular region/symbol.
1664 using StorageTypeForKinds = unsigned char;
1665
1666 llvm::DenseMap<const MemRegion *, StorageTypeForKinds> MRTraitsMap;
1667 llvm::DenseMap<SymbolRef, StorageTypeForKinds> SymTraitsMap;
1668
1669 using const_region_iterator =
1670 llvm::DenseMap<const MemRegion *, StorageTypeForKinds>::const_iterator;
1671 using const_symbol_iterator =
1672 llvm::DenseMap<SymbolRef, StorageTypeForKinds>::const_iterator;
1673
1674public:
1675 /// Describes different invalidation traits.
1677 /// Tells that a region's contents is not changed.
1679
1680 /// Suppress pointer-escaping of a region.
1682
1683 // Do not invalidate super region.
1685
1686 /// When applied to a MemSpaceRegion, indicates the entire memory space
1687 /// should be invalidated.
1689
1690 // Do not forget to extend StorageTypeForKinds if number of traits exceed
1691 // the number of bits StorageTypeForKinds can store.
1692 };
1693
1694 void setTrait(SymbolRef Sym, InvalidationKinds IK);
1695 void setTrait(const MemRegion *MR, InvalidationKinds IK);
1696 bool hasTrait(SymbolRef Sym, InvalidationKinds IK) const;
1697 bool hasTrait(const MemRegion *MR, InvalidationKinds IK) const;
1698};
1699
1700//===----------------------------------------------------------------------===//
1701// Pretty-printing regions.
1702//===----------------------------------------------------------------------===//
1703inline raw_ostream &operator<<(raw_ostream &os, const MemRegion *R) {
1704 R->dumpToStream(os);
1705 return os;
1706}
1707
1708} // namespace ento
1709
1710} // namespace clang
1711
1712#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_MEMREGION_H
Defines the clang::ASTContext interface.
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
QualType getObjCObjectPointerType(QualType OIT) const
Return a ObjCObjectPointerType type for the given ObjCObjectType.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4806
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
CompoundLiteralExpr - [C99 6.5.2.5].
Definition Expr.h:3616
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:112
Represents a member of a struct/union/class.
Definition Decl.h:3294
This represents a decl that may have a name.
Definition Decl.h:274
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1958
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition ExprObjC.h:84
Represents a parameter to a function.
Definition Decl.h:1819
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3408
A (possibly-)qualified type.
Definition TypeBase.h:938
QualType getDesugaredType(const ASTContext &Context) const
Return the specified type with any "sugar" removed from the type.
Definition TypeBase.h:1312
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
A trivial tuple used to represent a source range.
It represents a stack frame of the call stack.
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1810
bool isBlockPointerType() const
Definition TypeBase.h:8761
bool isVoidType() const
Definition TypeBase.h:9113
bool isReferenceType() const
Definition TypeBase.h:8765
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isAnyPointerType() const
Definition TypeBase.h:8749
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:932
AllocaRegion - A region that represents an untyped blob of bytes created by a call to 'alloca'.
Definition MemRegion.h:512
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:539
void Profile(llvm::FoldingSetNodeID &ID) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const Expr * getExpr() const
Definition MemRegion.h:531
bool isBoundable() const override
Definition MemRegion.h:533
friend class MemRegionManager
Definition MemRegion.h:513
BlockCodeRegion - A region that represents code texts of blocks (closures).
Definition MemRegion.h:665
QualType getLocationType() const override
Definition MemRegion.h:685
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
Definition MemRegion.h:695
void dumpToStream(raw_ostream &os) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const BlockDecl * getDecl() const
Definition MemRegion.h:690
static bool classof(const MemRegion *R)
Definition MemRegion.h:701
void Profile(llvm::FoldingSetNodeID &ID) const override
bool operator==(const referenced_vars_iterator &I) const
Definition MemRegion.h:764
const referenced_vars_iterator & operator*() const
Definition MemRegion.h:783
bool operator!=(const referenced_vars_iterator &I) const
Definition MemRegion.h:769
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarRegion * getCapturedRegion() const
Definition MemRegion.h:755
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarRegion * getOriginalRegion() const
Definition MemRegion.h:760
referenced_vars_iterator(const MemRegion *const *r, const MemRegion *const *originalR)
Definition MemRegion.h:750
BlockDataRegion - A region that represents a block instance.
Definition MemRegion.h:712
const VarRegion * getOriginalRegion(const VarRegion *VR) const
Return the original region for a captured region, if one exists.
QualType getLocationType() const override
Definition MemRegion.h:743
LLVM_ATTRIBUTE_RETURNS_NONNULL const BlockDecl * getDecl() const
Definition MemRegion.h:741
static bool classof(const MemRegion *R)
Definition MemRegion.h:798
referenced_vars_iterator referenced_vars_begin() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const BlockCodeRegion * getCodeRegion() const
Definition MemRegion.h:738
void Profile(llvm::FoldingSetNodeID &ID) const override
referenced_vars_iterator referenced_vars_end() const
void dumpToStream(raw_ostream &os) const override
llvm::iterator_range< referenced_vars_iterator > referenced_vars() const
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
LLVM_ATTRIBUTE_RETURNS_NONNULL const CXXRecordDecl * getDecl() const
Definition MemRegion.h:1365
bool canPrintPrettyAsExpr() const override
Returns true if this region's textual representation can be used as part of a larger expression.
void Profile(llvm::FoldingSetNodeID &ID) const override
static bool classof(const MemRegion *region)
Definition MemRegion.h:1378
void dumpToStream(raw_ostream &os) const override
QualType getValueType() const override
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
void Profile(llvm::FoldingSetNodeID &ID) const override
QualType getValueType() const override
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *region)
Definition MemRegion.h:1420
bool canPrintPrettyAsExpr() const override
Returns true if this region's textual representation can be used as part of a larger expression.
LLVM_ATTRIBUTE_RETURNS_NONNULL const CXXRecordDecl * getDecl() const
Definition MemRegion.h:1408
static bool classof(const MemRegion *R)
Definition MemRegion.h:1342
LLVM_ATTRIBUTE_RETURNS_NONNULL const Expr * getExpr() const
Definition MemRegion.h:1330
void Profile(llvm::FoldingSetNodeID &ID) const override
const StackFrame * getStackFrame() const
It might return null.
LLVM_ATTRIBUTE_RETURNS_NONNULL const ValueDecl * getExtendingDecl() const
Definition MemRegion.h:1332
void dumpToStream(raw_ostream &os) const override
QualType getValueType() const override
Definition MemRegion.h:1297
LLVM_ATTRIBUTE_RETURNS_NONNULL const StackFrame * getStackFrame() const
void Profile(llvm::FoldingSetNodeID &ID) const override
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:1303
LLVM_ATTRIBUTE_RETURNS_NONNULL const Expr * getExpr() const
Definition MemRegion.h:1292
CXXThisRegion - Represents the region for the implicit 'this' parameter in a call to a C++ method.
Definition MemRegion.h:1112
QualType getValueType() const override
Definition MemRegion.h:1130
static bool classof(const MemRegion *R)
Definition MemRegion.h:1136
void Profile(llvm::FoldingSetNodeID &ID) const override
void dumpToStream(raw_ostream &os) const override
friend class MemRegionManager
Definition MemRegion.h:1113
CodeSpaceRegion - The memory space that holds the executable code of functions and blocks.
Definition MemRegion.h:265
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:274
CodeTextRegion(const MemSpaceRegion *sreg, Kind k)
Definition MemRegion.h:604
static bool classof(const MemRegion *R)
Definition MemRegion.h:611
bool isBoundable() const override
Definition MemRegion.h:609
CompoundLiteralRegion - A memory region representing a compound literal.
Definition MemRegion.h:933
LLVM_ATTRIBUTE_RETURNS_NONNULL const CompoundLiteralExpr * getLiteralExpr() const
Definition MemRegion.h:960
QualType getValueType() const override
Definition MemRegion.h:951
bool isBoundable() const override
Definition MemRegion.h:953
void Profile(llvm::FoldingSetNodeID &ID) const override
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:962
DeclRegion(const MemRegion *sReg, Kind k)
Definition MemRegion.h:969
virtual const ValueDecl * getDecl() const =0
static bool classof(const MemRegion *R)
Definition MemRegion.h:977
ElementRegion is used to represent both array elements and casts.
Definition MemRegion.h:1237
static bool classof(const MemRegion *R)
Definition MemRegion.h:1270
QualType getValueType() const override
Definition MemRegion.h:1259
QualType getElementType() const
Definition MemRegion.h:1261
void Profile(llvm::FoldingSetNodeID &ID) const override
RegionRawOffset getAsArrayOffset() const
Compute the offset within the array. The array might also be a subobject.
void dumpToStream(raw_ostream &os) const override
friend class MemRegionManager
Definition MemRegion.h:1238
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
static bool classof(const MemRegion *R)
Definition MemRegion.h:1179
bool canPrintPretty() const override
Returns true if this region can be printed in a user-friendly way.
bool canPrintPrettyAsExpr() const override
Returns true if this region's textual representation can be used as part of a larger expression.
void dumpToStream(raw_ostream &os) const override
QualType getValueType() const override
Definition MemRegion.h:1167
void printPretty(raw_ostream &os) const override
Print the region for use in diagnostics.
void Profile(llvm::FoldingSetNodeID &ID) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const FieldDecl * getDecl() const override
Definition MemRegion.h:1163
friend class MemRegionManager
Definition MemRegion.h:1145
FunctionCodeRegion - A region that represents code texts of function.
Definition MemRegion.h:618
static bool classof(const MemRegion *R)
Definition MemRegion.h:654
QualType getLocationType() const override
Definition MemRegion.h:632
const NamedDecl * getDecl() const
Definition MemRegion.h:646
void dumpToStream(raw_ostream &os) const override
void Profile(llvm::FoldingSetNodeID &ID) const override
The region containing globals which are considered not to be modified or point to data which could be...
Definition MemRegion.h:366
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:375
The region containing globals which can be modified by calls to "internally" defined functions - (for...
Definition MemRegion.h:383
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:392
The region containing globals which are defined in system/external headers and are considered modifia...
Definition MemRegion.h:347
static bool classof(const MemRegion *R)
Definition MemRegion.h:356
void dumpToStream(raw_ostream &os) const override
GlobalsSpaceRegion(MemRegionManager &mgr, Kind k)
Definition MemRegion.h:283
static bool classof(const MemRegion *R)
Definition MemRegion.h:288
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:406
const HeapSpaceRegion * getHeapRegion()
getHeapRegion - Retrieve the memory region associated with the generic "heap".
llvm::BumpPtrAllocator & getAllocator()
Definition MemRegion.h:1470
const StackLocalsSpaceRegion * getStackLocalsRegion(const StackFrame *SF)
getStackLocalsRegion - Retrieve the memory region associated with the specified stack frame.
const FieldRegion * getFieldRegion(const FieldDecl *FD, const SubRegion *SuperRegion)
getFieldRegion - Retrieve or create the memory region associated with a specified FieldDecl.
const ParamVarRegion * getParamVarRegion(const Expr *OriginExpr, unsigned Index, const StackFrame *SF)
getParamVarRegion - Retrieve or create the memory region associated with a specified CallExpr,...
const StackArgumentsSpaceRegion * getStackArgumentsRegion(const StackFrame *SF)
getStackArgumentsRegion - Retrieve the memory region associated with function/method arguments of the...
const BlockCodeRegion * getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy, AnalysisDeclContext *AC)
const ASTContext & getContext() const
Definition MemRegion.h:1468
const UnknownSpaceRegion * getUnknownRegion()
getUnknownRegion - Retrieve the memory region associated with unknown memory space.
const CXXDerivedObjectRegion * getCXXDerivedObjectRegion(const CXXRecordDecl *BaseClass, const SubRegion *Super)
Create a CXXDerivedObjectRegion with the given derived class for region Super.
const CXXLifetimeExtendedObjectRegion * getCXXLifetimeExtendedObjectRegion(Expr const *Ex, ValueDecl const *VD, StackFrame const *SF)
Create a CXXLifetimeExtendedObjectRegion for temporaries which are lifetime-extended by local referen...
const CompoundLiteralRegion * getCompoundLiteralRegion(const CompoundLiteralExpr *CL, const StackFrame *SF)
getCompoundLiteralRegion - Retrieve the region associated with a given CompoundLiteral.
const ElementRegion * getElementRegion(QualType elementType, NonLoc Idx, const SubRegion *superRegion, const ASTContext &Ctx)
getElementRegion - Retrieve the memory region associated with the associated element type,...
const NonParamVarRegion * getNonParamVarRegion(const VarDecl *VD, const MemRegion *superR)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and StackFram...
const FieldRegion * getFieldRegionWithSuper(const FieldRegion *FR, const SubRegion *superRegion)
Definition MemRegion.h:1561
const ObjCIvarRegion * getObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *superRegion)
getObjCIvarRegion - Retrieve or create the memory region associated with a specified Objective-c inst...
const VarRegion * getVarRegion(const VarDecl *VD, const StackFrame *SF)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and StackFram...
const AllocaRegion * getAllocaRegion(const Expr *Ex, unsigned Cnt, const StackFrame *SF)
getAllocaRegion - Retrieve a region associated with a call to alloca().
const SymbolicRegion * getSymbolicHeapRegion(SymbolRef sym)
Return a unique symbolic region belonging to heap memory space.
const CXXTempObjectRegion * getCXXTempObjectRegion(Expr const *Ex, StackFrame const *SF)
const ObjCStringRegion * getObjCStringRegion(const ObjCStringLiteral *Str)
MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a)
Definition MemRegion.h:1464
const StringRegion * getStringRegion(const StringLiteral *Str)
DefinedOrUnknownSVal getStaticSize(const MemRegion *MR, SValBuilder &SVB) const
const CodeSpaceRegion * getCodeRegion()
const GlobalsSpaceRegion * getGlobalsRegion(MemRegion::Kind K=MemRegion::GlobalInternalSpaceRegionKind, const CodeTextRegion *R=nullptr)
getGlobalsRegion - Retrieve the memory region associated with global variables.
const CXXThisRegion * getCXXThisRegion(QualType thisPointerTy, const StackFrame *SF)
getCXXThisRegion - Retrieve the [artificial] region associated with the parameter 'this'.
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym, const MemSpaceRegion *MemSpace=nullptr)
Retrieve or create a "symbolic" memory region.
const ElementRegion * getElementRegionWithSuper(const ElementRegion *ER, const SubRegion *superRegion)
Definition MemRegion.h:1548
const FunctionCodeRegion * getFunctionCodeRegion(const NamedDecl *FD)
const CXXBaseObjectRegion * getCXXBaseObjectRegion(const CXXRecordDecl *BaseClass, const SubRegion *Super, bool IsVirtual)
Create a CXXBaseObjectRegion with the given base class for region Super.
const CXXLifetimeExtendedObjectRegion * getCXXStaticLifetimeExtendedObjectRegion(const Expr *Ex, ValueDecl const *VD)
Create a CXXLifetimeExtendedObjectRegion for temporaries which are lifetime-extended by static refere...
const BlockDataRegion * getBlockDataRegion(const BlockCodeRegion *bc, const StackFrame *SF, unsigned blockCount)
getBlockDataRegion - Get the memory region associated with an instance of a block.
const CXXBaseObjectRegion * getCXXBaseObjectRegionWithSuper(const CXXBaseObjectRegion *baseReg, const SubRegion *superRegion)
Create a CXXBaseObjectRegion with the same CXXRecordDecl but a different super region.
Definition MemRegion.h:1600
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
virtual bool canPrintPrettyAsExpr() const
Returns true if this region's textual representation can be used as part of a larger expression.
std::string getDescriptiveName(bool UseQuotes=true, bool AllowFallback=false) const
Get descriptive name for memory region.
virtual void Profile(llvm::FoldingSetNodeID &ID) const =0
LLVM_ATTRIBUTE_RETURNS_NONNULL const RegionTy * castAs() const
Definition MemRegion.h:1434
const MemSpace * getMemorySpaceAs(ProgramStateRef State) const
Definition MemRegion.h:142
virtual bool isBoundable() const
Definition MemRegion.h:210
StringRef getKindStr() const
RegionOffset getAsOffset() const
Compute the offset within the top level memory object.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * StripCasts(bool StripBaseAndDerivedCasts=true) const
bool hasMemorySpace(ProgramStateRef State) const
Definition MemRegion.h:147
const MemSpace * getRawMemorySpaceAs() const
Deprecated. Use getMemorySpace(ProgramStateRef) instead.
Definition MemRegion.h:131
ProgramStateRef setMemorySpace(ProgramStateRef State, const MemSpaceRegion *Space) const
Set the dynamically deduced memory space of a MemRegion that currently has UnknownSpaceRegion.
ASTContext & getContext() const
Definition MemRegion.h:1654
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace(ProgramStateRef State) const
Returns the most specific memory space for this memory region in the given ProgramStateRef.
virtual bool isSubRegionOf(const MemRegion *R) const
Check if the region is a subregion of the given region.
virtual void dumpToStream(raw_ostream &os) const
const SymbolicRegion * getSymbolicBase() const
If this is a symbolic region, returns the region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
virtual void printPretty(raw_ostream &os) const
Print the region for use in diagnostics.
virtual void printPrettyAsExpr(raw_ostream &os) const
Print the region as expression.
std::string getString() const
Get a string representation of a region for debug use.
const RegionTy * getAs() const
Definition MemRegion.h:1426
Kind getKind() const
Definition MemRegion.h:202
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getMostDerivedObjectRegion() const
Recursively retrieve the region of the most derived class instance of regions of C++ base class insta...
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getRawMemorySpace() const
Deprecated.
virtual MemRegionManager & getMemRegionManager() const =0
virtual bool canPrintPretty() const
Returns true if this region can be printed in a user-friendly way.
SourceRange sourceRange() const
Retrieve source range from memory region.
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
Definition MemRegion.h:242
MemRegionManager & getMemRegionManager() const override
Definition MemRegion.h:250
static bool classof(const MemRegion *R)
Definition MemRegion.h:257
void Profile(llvm::FoldingSetNodeID &ID) const override
MemRegionManager & Mgr
Definition MemRegion.h:244
bool isBoundable() const override
Definition MemRegion.h:253
MemSpaceRegion(MemRegionManager &mgr, Kind k)
Definition MemRegion.h:246
QualType getValueType() const override
Definition MemRegion.h:1045
bool canPrintPrettyAsExpr() const override
Returns true if this region's textual representation can be used as part of a larger expression.
void Profile(llvm::FoldingSetNodeID &ID) const override
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
void dumpToStream(raw_ostream &os) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarDecl * getDecl() const override
Definition MemRegion.h:1043
static bool classof(const MemRegion *R)
Definition MemRegion.h:1056
NonStaticGlobalSpaceRegion(MemRegionManager &mgr, Kind k)
Definition MemRegion.h:332
static bool classof(const MemRegion *R)
Definition MemRegion.h:338
bool canPrintPrettyAsExpr() const override
Returns true if this region's textual representation can be used as part of a larger expression.
void Profile(llvm::FoldingSetNodeID &ID) const override
QualType getValueType() const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:1207
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
LLVM_ATTRIBUTE_RETURNS_NONNULL const ObjCIvarDecl * getDecl() const override
void dumpToStream(raw_ostream &os) const override
The region associated with an ObjCStringLiteral.
Definition MemRegion.h:896
QualType getValueType() const override
Definition MemRegion.h:915
bool isBoundable() const override
Definition MemRegion.h:917
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:925
LLVM_ATTRIBUTE_RETURNS_NONNULL const ObjCStringLiteral * getObjCStringLiteral() const
Definition MemRegion.h:913
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition MemRegion.h:919
ParamVarRegion - Represents a region for parameters.
Definition MemRegion.h:1072
bool canPrintPrettyAsExpr() const override
Returns true if this region's textual representation can be used as part of a larger expression.
LLVM_ATTRIBUTE_RETURNS_NONNULL const Expr * getOriginExpr() const
Definition MemRegion.h:1089
static bool classof(const MemRegion *R)
Definition MemRegion.h:1104
const ParmVarDecl * getDecl() const override
TODO: What does this return?
unsigned getIndex() const
Definition MemRegion.h:1090
void Profile(llvm::FoldingSetNodeID &ID) const override
QualType getValueType() const override
void dumpToStream(raw_ostream &os) const override
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
Information about invalidation for a particular region/symbol.
Definition MemRegion.h:1663
InvalidationKinds
Describes different invalidation traits.
Definition MemRegion.h:1676
@ TK_PreserveContents
Tells that a region's contents is not changed.
Definition MemRegion.h:1678
@ TK_EntireMemSpace
When applied to a MemSpaceRegion, indicates the entire memory space should be invalidated.
Definition MemRegion.h:1688
@ TK_SuppressEscape
Suppress pointer-escaping of a region.
Definition MemRegion.h:1681
bool hasTrait(SymbolRef Sym, InvalidationKinds IK) const
void setTrait(SymbolRef Sym, InvalidationKinds IK)
Represent a region's offset within the top level base region.
Definition MemRegion.h:64
static const int64_t Symbolic
Definition MemRegion.h:74
bool hasSymbolicOffset() const
Definition MemRegion.h:82
const MemRegion * getRegion() const
It might return null.
Definition MemRegion.h:80
RegionOffset(const MemRegion *r, int64_t off)
Definition MemRegion.h:77
int64_t getOffset() const
Definition MemRegion.h:84
CharUnits getOffset() const
Definition MemRegion.h:1227
void dumpToStream(raw_ostream &os) const
const MemRegion * getRegion() const
Definition MemRegion.h:1230
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:84
static bool classof(const MemRegion *R)
Definition MemRegion.h:473
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:458
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:443
LLVM_ATTRIBUTE_RETURNS_NONNULL const StackFrame * getStackFrame() const
Definition MemRegion.h:439
void Profile(llvm::FoldingSetNodeID &ID) const override
StackSpaceRegion(MemRegionManager &mgr, Kind k, const StackFrame *SF)
Definition MemRegion.h:431
void Profile(llvm::FoldingSetNodeID &ID) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:317
void dumpToStream(raw_ostream &os) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const CodeTextRegion * getCodeRegion() const
Definition MemRegion.h:315
StringRegion - Region associated with a StringLiteral.
Definition MemRegion.h:862
static bool classof(const MemRegion *R)
Definition MemRegion.h:890
QualType getValueType() const override
Definition MemRegion.h:880
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition MemRegion.h:884
bool isBoundable() const override
Definition MemRegion.h:882
void dumpToStream(raw_ostream &os) const override
LLVM_ATTRIBUTE_RETURNS_NONNULL const StringLiteral * getStringLiteral() const
Definition MemRegion.h:878
friend class MemRegionManager
Definition MemRegion.h:863
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:480
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getSuperRegion() const
Definition MemRegion.h:493
static bool classof(const MemRegion *R)
Definition MemRegion.h:501
bool isSubRegionOf(const MemRegion *R) const override
Check if the region is a subregion of the given region.
SubRegion(const MemRegion *sReg, Kind k)
Definition MemRegion.h:486
const MemRegion * superRegion
Definition MemRegion.h:484
MemRegionManager & getMemRegionManager() const override
virtual QualType getType() const =0
SymbolicRegion - A special, "non-concrete" region.
Definition MemRegion.h:813
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:856
bool isBoundable() const override
Definition MemRegion.h:846
void Profile(llvm::FoldingSetNodeID &ID) const override
SymbolRef getSymbol() const
It might return null.
Definition MemRegion.h:832
static void ProfileRegion(llvm::FoldingSetNodeID &ID, SymbolRef sym, const MemRegion *superRegion)
friend class MemRegionManager
Definition MemRegion.h:814
QualType getPointeeStaticType() const
Gets the type of the wrapped symbol.
Definition MemRegion.h:842
QualType getDesugaredLocationType(ASTContext &Context) const
Definition MemRegion.h:556
bool isBoundable() const override
Definition MemRegion.h:560
virtual QualType getLocationType() const =0
TypedRegion(const MemRegion *sReg, Kind k)
Definition MemRegion.h:549
static bool classof(const MemRegion *R)
Definition MemRegion.h:562
virtual QualType getValueType() const =0
QualType getLocationType() const override
Definition MemRegion.h:580
static bool classof(const MemRegion *R)
Definition MemRegion.h:594
QualType getDesugaredValueType(ASTContext &Context) const
Definition MemRegion.h:589
TypedValueRegion(const MemRegion *sReg, Kind k)
Definition MemRegion.h:573
void dumpToStream(raw_ostream &os) const override
static bool classof(const MemRegion *R)
Definition MemRegion.h:420
QualType getValueType() const override
Definition MemRegion.h:1004
const VarDecl * getDecl() const override=0
VarRegion(const MemRegion *sReg, Kind k)
Definition MemRegion.h:988
const StackFrame * getStackFrame() const
It might return null.
static bool classof(const MemRegion *R)
Definition MemRegion.h:1009
friend class MemRegionManager
Definition MemRegion.h:984
Value representing integer constant.
Definition SVals.h:306
APSIntPtr getValue() const
Definition SVals.h:310
Definition SPIR.cpp:35
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
raw_ostream & operator<<(raw_ostream &os, const MemRegion *R)
Definition MemRegion.h:1703
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
const FunctionProtoType * T
U cast(CodeGen::Address addr)
Definition Address.h:327