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