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