19e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//== MemRegion.h - Abstract memory regions for static analysis --*- C++ -*--==//
29e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//
39e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//                     The LLVM Compiler Infrastructure
49e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//
59e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek// This file is distributed under the University of Illinois Open Source
69e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek// License. See LICENSE.TXT for details.
79e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//
89e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//===----------------------------------------------------------------------===//
99e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//
109e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//  This file defines MemRegion and its subclasses.  MemRegion defines a
119e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//  partially-typed abstraction of memory useful for path-sensitive dataflow
129e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//  analyses.
139e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//
149e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//===----------------------------------------------------------------------===//
159e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
165a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis#ifndef LLVM_CLANG_GR_MEMREGION_H
175a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis#define LLVM_CLANG_GR_MEMREGION_H
189e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
19478851c3ed6bd784e7377dffd8e57b200c1b9ba9Benjamin Kramer#include "clang/AST/ASTContext.h"
209ff2b13aee0f89d23ef4820218f9b88bb5e5c1c1Ken Dyck#include "clang/AST/CharUnits.h"
21993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek#include "clang/AST/Decl.h"
224c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek#include "clang/AST/ExprObjC.h"
23d47d3b0cfeb7e8564ff77f48130fe63282b6d127Chris Lattner#include "clang/Basic/LLVM.h"
249b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
259e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek#include "llvm/ADT/FoldingSet.h"
26651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/Support/Allocator.h"
2730a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/Support/ErrorHandling.h"
289e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek#include <string>
299e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
309e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremeneknamespace clang {
311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32d17da2b99f323fa91b01e1dd119cc32e0ee8197dTed Kremenekclass LocationContext;
3367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekclass StackFrameContext;
345a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis
359ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremeneknamespace ento {
365a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidis
378ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaksclass CodeTextRegion;
385a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidisclass MemRegionManager;
395a4f98ff943e6a501b0fe47ade007c9bbf96cb88Argyrios Kyrtzidisclass MemSpaceRegion;
40c8413fd03f73084a5c93028f8b4db619fc388087Ted Kremenekclass SValBuilder;
418ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaksclass SymbolicRegion;
424240096011a187807058f887eb81df750ffa17feTed Kremenekclass VarRegion;
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu/// Represent a region's offset within the top level base region.
45e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xuclass RegionOffset {
46e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu  /// The base region.
47e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu  const MemRegion *R;
48e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu
49e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu  /// The bit offset within the base region. It shouldn't be negative.
50e3273e78da4716f9c999ae207f6898f376a6e1a4Zhongxing Xu  int64_t Offset;
51e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu
52e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xupublic:
535375d82d1d096ddd8879d8e6641a8f042b0d1d43Aaron Ballman  // We're using a const instead of an enumeration due to the size required;
545375d82d1d096ddd8879d8e6641a8f042b0d1d43Aaron Ballman  // Visual Studio will only create enumerations of size int, not long long.
555375d82d1d096ddd8879d8e6641a8f042b0d1d43Aaron Ballman  static const int64_t Symbolic = INT64_MAX;
56824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose
576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  RegionOffset() : R(nullptr) {}
58e3273e78da4716f9c999ae207f6898f376a6e1a4Zhongxing Xu  RegionOffset(const MemRegion *r, int64_t off) : R(r), Offset(off) {}
59e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu
60e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu  const MemRegion *getRegion() const { return R; }
61824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose
62824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose  bool hasSymbolicOffset() const { return Offset == Symbolic; }
63824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose
64824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose  int64_t getOffset() const {
65824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose    assert(!hasSymbolicOffset());
66824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose    return Offset;
67824e07ac8f5c9efdddb4254de0203b9675b1ef0bJordan Rose  }
68e0d24eb1060a213ec9820dc02c45f26b2d5b348bJordan Rose
69e0d24eb1060a213ec9820dc02c45f26b2d5b348bJordan Rose  bool isValid() const { return R; }
70e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu};
71e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu
7219e1f0ba5cec738ce6cebe3fe0e1edc782206494Ted Kremenek//===----------------------------------------------------------------------===//
7319e1f0ba5cec738ce6cebe3fe0e1edc782206494Ted Kremenek// Base region classes.
7419e1f0ba5cec738ce6cebe3fe0e1edc782206494Ted Kremenek//===----------------------------------------------------------------------===//
751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
769e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek/// MemRegion - The root abstract class for all memory regions.
779e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekclass MemRegion : public llvm::FoldingSetNode {
784240096011a187807058f887eb81df750ffa17feTed Kremenek  friend class MemRegionManager;
799e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekpublic:
8067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  enum Kind {
8167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    // Memory spaces.
82dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    GenericMemSpaceRegionKind,
8367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    StackLocalsSpaceRegionKind,
8467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    StackArgumentsSpaceRegionKind,
8567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    HeapSpaceRegionKind,
862b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek    UnknownSpaceRegionKind,
87dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    StaticGlobalSpaceRegionKind,
88eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    GlobalInternalSpaceRegionKind,
89eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    GlobalSystemSpaceRegionKind,
90eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    GlobalImmutableSpaceRegionKind,
91eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    BEG_NON_STATIC_GLOBAL_MEMSPACES = GlobalInternalSpaceRegionKind,
92eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    END_NON_STATIC_GLOBAL_MEMSPACES = GlobalImmutableSpaceRegionKind,
93eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    BEG_GLOBAL_MEMSPACES = StaticGlobalSpaceRegionKind,
94eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    END_GLOBAL_MEMSPACES = GlobalImmutableSpaceRegionKind,
95dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    BEG_MEMSPACES = GenericMemSpaceRegionKind,
96eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    END_MEMSPACES = GlobalImmutableSpaceRegionKind,
9767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    // Untyped regions.
9867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    SymbolicRegionKind,
9967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    AllocaRegionKind,
10067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    // Typed regions.
10167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    BEG_TYPED_REGIONS,
10267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    FunctionTextRegionKind = BEG_TYPED_REGIONS,
10367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    BlockTextRegionKind,
1048ecf59afbab1dbf184dc4c0c47e7213cbd32ba0aJordan Rose    BlockDataRegionKind,
1059697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    BEG_TYPED_VALUE_REGIONS,
1069697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    CompoundLiteralRegionKind = BEG_TYPED_VALUE_REGIONS,
107de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek    CXXThisRegionKind,
10867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    StringRegionKind,
1094c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek    ObjCStringRegionKind,
11067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    ElementRegionKind,
11167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    // Decl Regions.
11267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    BEG_DECL_REGIONS,
11367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    VarRegionKind = BEG_DECL_REGIONS,
11467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    FieldRegionKind,
11567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    ObjCIvarRegionKind,
1164fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu    END_DECL_REGIONS = ObjCIvarRegionKind,
11702fe28c8a6da29d4ad88d0900c133dcf22d24a75Zhongxing Xu    CXXTempObjectRegionKind,
1184fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu    CXXBaseObjectRegionKind,
119096aef9597b263b4cd6a0feaacf9e7214fa9c75aJordy Rose    END_TYPED_VALUE_REGIONS = CXXBaseObjectRegionKind,
120096aef9597b263b4cd6a0feaacf9e7214fa9c75aJordy Rose    END_TYPED_REGIONS = CXXBaseObjectRegionKind
12167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  };
12267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
1239e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekprivate:
1249e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  const Kind kind;
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1269e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekprotected:
1279e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  MemRegion(Kind k) : kind(k) {}
1289e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  virtual ~MemRegion();
1299e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
1309e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekpublic:
131a6275a534da701f37d19a068e6361e5f10f983a1Ted Kremenek  ASTContext &getContext() const;
1321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1339e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  virtual void Profile(llvm::FoldingSetNodeID& ID) const = 0;
134a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek
135a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek  virtual MemRegionManager* getMemRegionManager() const = 0;
136a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek
137bb7c96f290453104ec35ca17111a5165f68a4697Ted Kremenek  const MemSpaceRegion *getMemorySpace() const;
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
139adca27102ff733c7d42fcbbc2c7e134a7fc026f9Zhongxing Xu  const MemRegion *getBaseRegion() const;
140adca27102ff733c7d42fcbbc2c7e134a7fc026f9Zhongxing Xu
141522fc21f3adc647817edc8017e6928a64c96899bAnna Zaks  /// Check if the region is a subregion of the given region.
1425f7c0add1ea1d8e1d2f920d77fd1a7b6160c2d93Anna Zaks  virtual bool isSubRegionOf(const MemRegion *R) const;
143522fc21f3adc647817edc8017e6928a64c96899bAnna Zaks
144b11a3ada9a22e146c6edd33bcc6301e221fedd7aJordan Rose  const MemRegion *StripCasts(bool StripBaseCasts = true) const;
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1468ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks  /// \brief If this is a symbolic region, returns the region. Otherwise,
1478ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks  /// goes up the base chain looking for the first symbolic base region.
1488ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks  const SymbolicRegion *getSymbolicBase() const;
1498ef064d53fb33b5a8f8743bcbb0a2fd5c3e97be1Anna Zaks
1501508636e99faddf569a57fce82c0fb3aa2124396Ted Kremenek  bool hasGlobalsOrParametersStorage() const;
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  bool hasStackStorage() const;
153de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
154de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  bool hasStackNonParametersStorage() const;
155de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
156de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  bool hasStackParametersStorage() const;
157b21ff77c8126ea628b66d2ffb931fdaa7884f5d2Zhongxing Xu
158e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu  /// Compute the offset within the top level memory object.
159e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu  RegionOffset getAsOffset() const;
160e888233f6b115d3b0dd73bcb5f35e93794408542Zhongxing Xu
1613d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks  /// \brief Get a string representation of a region for debug use.
1623d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks  std::string getString() const;
1633d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks
1649c378f705405d37f49795d5e915989de774fe11fTed Kremenek  virtual void dumpToStream(raw_ostream &os) const;
1657f39d29cb69e7488f994870800d548008e50e1cbTed Kremenek
1668800ad4eaa1621f6d23c8264971063b9f8da6a2eTed Kremenek  void dump() const;
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
168919e8a1c6698bfa6848571d366430126bced727dJordan Rose  /// \brief Returns true if this region can be printed in a user-friendly way.
169919e8a1c6698bfa6848571d366430126bced727dJordan Rose  virtual bool canPrintPretty() const;
170919e8a1c6698bfa6848571d366430126bced727dJordan Rose
1713d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks  /// \brief Print the region for use in diagnostics.
172919e8a1c6698bfa6848571d366430126bced727dJordan Rose  virtual void printPretty(raw_ostream &os) const;
1733d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks
17479d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  /// \brief Returns true if this region's textual representation can be used
17579d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  /// as part of a larger expression.
17679d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  virtual bool canPrintPrettyAsExpr() const;
17779d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks
17879d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  /// \brief Print the region as expression.
17979d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  ///
18079d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  /// When this region represents a subexpression, the method is for printing
18179d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  /// an expression containing it.
18279d0cceb8847bfe6dc9da8eb2ea2f3c6bb73b813Anna Zaks  virtual void printPrettyAsExpr(raw_ostream &os) const;
1839e2f5977a180ae927d05e844c65b8a7873be48a4Anna Zaks
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Kind getKind() const { return kind; }
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
186ed65d3d97132fbcdd124aef4d2478e348dfbd36bTed Kremenek  template<typename RegionTy> const RegionTy* getAs() const;
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
188f0f0605c87739c906861f73d4287798a4969b1e0Zhongxing Xu  virtual bool isBoundable() const { return false; }
1899e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
1901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// MemSpaceRegion - A memory region that represents a "memory space";
1929e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek///  for example, the set of global variables, the stack frame, etc.
1939e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekclass MemSpaceRegion : public MemRegion {
194a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenekprotected:
19567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
19667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
197a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek  MemRegionManager *Mgr;
198a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek
19967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  MemSpaceRegion(MemRegionManager *mgr, Kind k = GenericMemSpaceRegionKind)
20067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : MemRegion(k), Mgr(mgr) {
20167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    assert(classof(this));
20267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  }
20367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
204651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  MemRegionManager* getMemRegionManager() const override { return Mgr; }
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekpublic:
207651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return false; }
208651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID &ID) const override;
21067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
21167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static bool classof(const MemRegion *R) {
21267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    Kind k = R->getKind();
21367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return k >= BEG_MEMSPACES && k <= END_MEMSPACES;
214a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek  }
21567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek};
21667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
21767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekclass GlobalsSpaceRegion : public MemSpaceRegion {
21899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
219dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenekprotected:
220dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  GlobalsSpaceRegion(MemRegionManager *mgr, Kind k)
221dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    : MemSpaceRegion(mgr, k) {}
222dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenekpublic:
223dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  static bool classof(const MemRegion *R) {
224dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    Kind k = R->getKind();
225dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    return k >= BEG_GLOBAL_MEMSPACES && k <= END_GLOBAL_MEMSPACES;
226dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  }
227dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek};
228eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
229914edfbb07c34d8cad8d0451193b4f9dd02a2d5aDavid Blaikie/// \brief The region of the static variables within the current CodeTextRegion
230eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// scope.
231914edfbb07c34d8cad8d0451193b4f9dd02a2d5aDavid Blaikie///
232eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// Currently, only the static locals are placed there, so we know that these
233eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// variables do not get invalidated by calls to other functions.
234dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenekclass StaticGlobalSpaceRegion : public GlobalsSpaceRegion {
23567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
236a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek
237dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  const CodeTextRegion *CR;
238dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek
239dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  StaticGlobalSpaceRegion(MemRegionManager *mgr, const CodeTextRegion *cr)
240dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    : GlobalsSpaceRegion(mgr, StaticGlobalSpaceRegionKind), CR(cr) {}
241dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek
242dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenekpublic:
243651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID &ID) const override;
244651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
245651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
246fa87d812d7c78d7f0cd1c5636e21e07c23c85341Ted Kremenek
247dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  const CodeTextRegion *getCodeRegion() const { return CR; }
248dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek
249dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  static bool classof(const MemRegion *R) {
250dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    return R->getKind() == StaticGlobalSpaceRegionKind;
251dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  }
252dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek};
253eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
254914edfbb07c34d8cad8d0451193b4f9dd02a2d5aDavid Blaikie/// \brief The region for all the non-static global variables.
255eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks///
256eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// This class is further split into subclasses for efficient implementation of
257eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// invalidating a set of related global values as is done in
258eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// RegionStoreManager::invalidateRegions (instead of finding all the dependent
259eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// globals, we invalidate the whole parent region).
260dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenekclass NonStaticGlobalSpaceRegion : public GlobalsSpaceRegion {
261dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  friend class MemRegionManager;
262dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek
263eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaksprotected:
264eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  NonStaticGlobalSpaceRegion(MemRegionManager *mgr, Kind k)
265eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    : GlobalsSpaceRegion(mgr, k) {}
266dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek
2679e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekpublic:
268fa87d812d7c78d7f0cd1c5636e21e07c23c85341Ted Kremenek
26967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static bool classof(const MemRegion *R) {
270eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    Kind k = R->getKind();
271eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    return k >= BEG_NON_STATIC_GLOBAL_MEMSPACES &&
272eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks           k <= END_NON_STATIC_GLOBAL_MEMSPACES;
27367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  }
27467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek};
275eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
276914edfbb07c34d8cad8d0451193b4f9dd02a2d5aDavid Blaikie/// \brief The region containing globals which are defined in system/external
277eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// headers and are considered modifiable by system calls (ex: errno).
278eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaksclass GlobalSystemSpaceRegion : public NonStaticGlobalSpaceRegion {
279eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  friend class MemRegionManager;
280eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
281eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  GlobalSystemSpaceRegion(MemRegionManager *mgr)
282eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    : NonStaticGlobalSpaceRegion(mgr, GlobalSystemSpaceRegionKind) {}
283eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
284eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zakspublic:
285eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
286651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
287eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
288eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  static bool classof(const MemRegion *R) {
289eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    return R->getKind() == GlobalSystemSpaceRegionKind;
290eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  }
291eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks};
292eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
293914edfbb07c34d8cad8d0451193b4f9dd02a2d5aDavid Blaikie/// \brief The region containing globals which are considered not to be modified
294eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// or point to data which could be modified as a result of a function call
295eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// (system or internal). Ex: Const global scalars would be modeled as part of
296eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// this region. This region also includes most system globals since they have
297eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// low chance of being modified.
298eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaksclass GlobalImmutableSpaceRegion : public NonStaticGlobalSpaceRegion {
299eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  friend class MemRegionManager;
300eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
301eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  GlobalImmutableSpaceRegion(MemRegionManager *mgr)
302eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    : NonStaticGlobalSpaceRegion(mgr, GlobalImmutableSpaceRegionKind) {}
303eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
304eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zakspublic:
305eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
307eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
308eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  static bool classof(const MemRegion *R) {
309eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    return R->getKind() == GlobalImmutableSpaceRegionKind;
310eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  }
311eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks};
312eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
313914edfbb07c34d8cad8d0451193b4f9dd02a2d5aDavid Blaikie/// \brief The region containing globals which can be modified by calls to
314eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// "internally" defined functions - (for now just) functions other then system
315eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks/// calls.
316eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaksclass GlobalInternalSpaceRegion : public NonStaticGlobalSpaceRegion {
317eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  friend class MemRegionManager;
318eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
319eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  GlobalInternalSpaceRegion(MemRegionManager *mgr)
320eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    : NonStaticGlobalSpaceRegion(mgr, GlobalInternalSpaceRegionKind) {}
321eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
322eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zakspublic:
323eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
324651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
325eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
326eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  static bool classof(const MemRegion *R) {
327eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks    return R->getKind() == GlobalInternalSpaceRegionKind;
328eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  }
329eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks};
330eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
33167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekclass HeapSpaceRegion : public MemSpaceRegion {
33299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
33367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
33467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
33567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  HeapSpaceRegion(MemRegionManager *mgr)
33667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : MemSpaceRegion(mgr, HeapSpaceRegionKind) {}
33767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekpublic:
33836397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
339651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
34036397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
34167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static bool classof(const MemRegion *R) {
34267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return R->getKind() == HeapSpaceRegionKind;
34367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  }
34467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek};
34567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
3462b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenekclass UnknownSpaceRegion : public MemSpaceRegion {
34799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
3482b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek  friend class MemRegionManager;
3492b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek  UnknownSpaceRegion(MemRegionManager *mgr)
3502b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek    : MemSpaceRegion(mgr, UnknownSpaceRegionKind) {}
3512b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenekpublic:
35236397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
353651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
35436397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
3552b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek  static bool classof(const MemRegion *R) {
3562b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek    return R->getKind() == UnknownSpaceRegionKind;
3572b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek  }
3582b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek};
3592b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek
36067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekclass StackSpaceRegion : public MemSpaceRegion {
36167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekprivate:
36267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const StackFrameContext *SFC;
3639e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
36467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekprotected:
36567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  StackSpaceRegion(MemRegionManager *mgr, Kind k, const StackFrameContext *sfc)
36667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : MemSpaceRegion(mgr, k), SFC(sfc) {
36767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    assert(classof(this));
36867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  }
36941168eac256fed59ec5406a75fce91c59cd5dd91Ted Kremenek
37067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekpublic:
37167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const StackFrameContext *getStackFrame() const { return SFC; }
37267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
373651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID &ID) const override;
37467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
37567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static bool classof(const MemRegion *R) {
37667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    Kind k = R->getKind();
37767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return k >= StackLocalsSpaceRegionKind &&
37867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek           k <= StackArgumentsSpaceRegionKind;
37967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  }
38067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek};
38167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
38267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekclass StackLocalsSpaceRegion : public StackSpaceRegion {
38399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
38467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
38567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  StackLocalsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc)
38667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : StackSpaceRegion(mgr, StackLocalsSpaceRegionKind, sfc) {}
38767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekpublic:
38836397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
389651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
39036397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
39167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static bool classof(const MemRegion *R) {
39267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return R->getKind() == StackLocalsSpaceRegionKind;
39367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  }
39467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek};
39567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
39667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekclass StackArgumentsSpaceRegion : public StackSpaceRegion {
39767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekprivate:
39899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
39967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
40067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  StackArgumentsSpaceRegion(MemRegionManager *mgr, const StackFrameContext *sfc)
40167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : StackSpaceRegion(mgr, StackArgumentsSpaceRegionKind, sfc) {}
40267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekpublic:
40336397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
404651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
40536397dc6c1bf1513a3bac4eabe9209e5b2295a55Jordan Rose
40667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static bool classof(const MemRegion *R) {
40767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return R->getKind() == StackArgumentsSpaceRegionKind;
4089e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
4099e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
4109e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
4117caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
412993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek/// SubRegion - A region that subsets another larger region.  Most regions
413993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek///  are subclasses of SubRegion.
414993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenekclass SubRegion : public MemRegion {
41599ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikieprivate:
41699ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
4179e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekprotected:
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const MemRegion* superRegion;
419993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek  SubRegion(const MemRegion* sReg, Kind k) : MemRegion(k), superRegion(sReg) {}
4209e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekpublic:
4219e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  const MemRegion* getSuperRegion() const {
4229e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    return superRegion;
4239e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42532f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose  /// getExtent - Returns the size of the region in bytes.
426c8413fd03f73084a5c93028f8b4db619fc388087Ted Kremenek  virtual DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const {
42732f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose    return UnknownVal();
42832f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose  }
42932f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose
430651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  MemRegionManager* getMemRegionManager() const override;
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
432651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isSubRegionOf(const MemRegion* R) const override;
4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4349e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  static bool classof(const MemRegion* R) {
43567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return R->getKind() > END_MEMSPACES;
436993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek  }
437993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek};
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43919e1f0ba5cec738ce6cebe3fe0e1edc782206494Ted Kremenek//===----------------------------------------------------------------------===//
44019e1f0ba5cec738ce6cebe3fe0e1edc782206494Ted Kremenek// MemRegion subclasses.
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump//===----------------------------------------------------------------------===//
442ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu
44382bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek/// AllocaRegion - A region that represents an untyped blob of bytes created
44482bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek///  by a call to 'alloca'.
44582bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenekclass AllocaRegion : public SubRegion {
44682bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek  friend class MemRegionManager;
44782bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenekprotected:
44882bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek  unsigned Cnt; // Block counter.  Used to distinguish different pieces of
44982bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek                // memory allocated by alloca at the same call site.
4509c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const Expr *Ex;
45182bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek
4529c378f705405d37f49795d5e915989de774fe11fTed Kremenek  AllocaRegion(const Expr *ex, unsigned cnt, const MemRegion *superRegion)
45382bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek    : SubRegion(superRegion, AllocaRegionKind), Cnt(cnt), Ex(ex) {}
4541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45582bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenekpublic:
4561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4579c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const Expr *getExpr() const { return Ex; }
4589852b5bf94d4934de63da6356c651c61e81f58d9Zhongxing Xu
459651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return true; }
4609852b5bf94d4934de63da6356c651c61e81f58d9Zhongxing Xu
461651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override;
46232f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose
463651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
46482bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek
4659c378f705405d37f49795d5e915989de774fe11fTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Expr *Ex,
4667ae7ad9951f032d0a33b64c964f7cdcb9cc6f59bTed Kremenek                            unsigned Cnt, const MemRegion *superRegion);
4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
468651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47082bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek  static bool classof(const MemRegion* R) {
47182bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek    return R->getKind() == AllocaRegionKind;
47282bd99f4db2454cc6e1b7bfaac6db25cb3444ddcTed Kremenek  }
4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump};
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
475993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek/// TypedRegion - An abstract class representing regions that are typed.
476993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenekclass TypedRegion : public SubRegion {
47799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
478651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void anchor() override;
479993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenekprotected:
480993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek  TypedRegion(const MemRegion* sReg, Kind k) : SubRegion(sReg, k) {}
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
482993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenekpublic:
4839697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek  virtual QualType getLocationType() const = 0;
4849697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek
4859697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek  QualType getDesugaredLocationType(ASTContext &Context) const {
4869697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    return getLocationType().getDesugaredType(Context);
4879697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek  }
4889697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek
489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return true; }
4909697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek
4919697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek  static bool classof(const MemRegion* R) {
4929697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    unsigned k = R->getKind();
4939697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    return k >= BEG_TYPED_REGIONS && k <= END_TYPED_REGIONS;
4949697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek  }
4959697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek};
4969697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek
4979697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek/// TypedValueRegion - An abstract class representing regions having a typed value.
4989697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass TypedValueRegion : public TypedRegion {
49999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
500651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void anchor() override;
5019697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekprotected:
5029697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek  TypedValueRegion(const MemRegion* sReg, Kind k) : TypedRegion(sReg, k) {}
5039697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek
5049697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekpublic:
505018220c343c103b7dfaa117a7a474c7a7fd6d068Zhongxing Xu  virtual QualType getValueType() const = 0;
5061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
507651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getLocationType() const override {
5086eddeb153415049c7b62de4b45385a759a6906c6Ted Kremenek    // FIXME: We can possibly optimize this later to cache this value.
509a6b0b96e5376cd9cf182a3e240e0537feed43cdeTed Kremenek    QualType T = getValueType();
510a6b0b96e5376cd9cf182a3e240e0537feed43cdeTed Kremenek    ASTContext &ctx = getContext();
511a6b0b96e5376cd9cf182a3e240e0537feed43cdeTed Kremenek    if (T->getAs<ObjCObjectType>())
512a6b0b96e5376cd9cf182a3e240e0537feed43cdeTed Kremenek      return ctx.getObjCObjectPointerType(T);
513a6b0b96e5376cd9cf182a3e240e0537feed43cdeTed Kremenek    return ctx.getPointerType(getValueType());
5146eddeb153415049c7b62de4b45385a759a6906c6Ted Kremenek  }
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51649f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall  QualType getDesugaredValueType(ASTContext &Context) const {
517018220c343c103b7dfaa117a7a474c7a7fd6d068Zhongxing Xu    QualType T = getValueType();
5181ab55e9bb87d98bff1d42c7a0ee502c64755d9f5Douglas Gregor    return T.getTypePtrOrNull() ? T.getDesugaredType(Context) : T;
51914553abd17d303b0b310b3ab1523eb0d30d8121cTed Kremenek  }
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override;
522e0d24eb1060a213ec9820dc02c45f26b2d5b348bJordan Rose
523993f1c72913417be7c534ec7a634363cdfc84fa5Ted Kremenek  static bool classof(const MemRegion* R) {
5249e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    unsigned k = R->getKind();
5259697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    return k >= BEG_TYPED_VALUE_REGIONS && k <= END_TYPED_VALUE_REGIONS;
5269e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
5279e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
5289e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
529ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu
530eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenekclass CodeTextRegion : public TypedRegion {
53199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
532651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void anchor() override;
533eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenekprotected:
534eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  CodeTextRegion(const MemRegion *sreg, Kind k) : TypedRegion(sreg, k) {}
535ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xupublic:
536651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return false; }
537651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
538eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  static bool classof(const MemRegion* R) {
539eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek    Kind k = R->getKind();
540eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek    return k >= FunctionTextRegionKind && k <= BlockTextRegionKind;
541eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  }
542eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek};
543ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu
544eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek/// FunctionTextRegion - A region that represents code texts of function.
545eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenekclass FunctionTextRegion : public CodeTextRegion {
5465fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks  const NamedDecl *FD;
547eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenekpublic:
5485fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks  FunctionTextRegion(const NamedDecl *fd, const MemRegion* sreg)
5495fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    : CodeTextRegion(sreg, FunctionTextRegionKind), FD(fd) {
5505fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    assert(isa<ObjCMethodDecl>(fd) || isa<FunctionDecl>(fd));
5515fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks  }
552651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
553651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getLocationType() const override {
5545fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    const ASTContext &Ctx = getContext();
5555fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    if (const FunctionDecl *D = dyn_cast<FunctionDecl>(FD)) {
5565fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks      return Ctx.getPointerType(D->getType());
5575fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    }
5585fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks
5595fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    assert(isa<ObjCMethodDecl>(FD));
5605fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    assert(false && "Getting the type of ObjCMethod is not supported yet");
5615fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks
5625fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    // TODO: We might want to return a different type here (ex: id (*ty)(...))
5635fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    //       depending on how it is used.
5645fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks    return QualType();
565ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu  }
5665fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks
5675fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks  const NamedDecl *getDecl() const {
568abd46e13cfd48f2c9bf26d9759edb4366aaa6d5bTed Kremenek    return FD;
56972e032004b0d2c2c298e8e4f7027f23a21c0cc7dTed Kremenek  }
570651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
571651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
572651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
573651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
574651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
5755fc1d0c4532c55cc47ba6628f296bf5b86d2eaf0Anna Zaks  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const NamedDecl *FD,
576abd46e13cfd48f2c9bf26d9759edb4366aaa6d5bTed Kremenek                            const MemRegion*);
577eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek
578ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu  static bool classof(const MemRegion* R) {
579eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek    return R->getKind() == FunctionTextRegionKind;
580eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  }
581eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek};
582eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek
583eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek
584eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek/// BlockTextRegion - A region that represents code texts of blocks (closures).
5850a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  Blocks are represented with two kinds of regions.  BlockTextRegions
5860a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  represent the "code", while BlockDataRegions represent instances of blocks,
5870a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  which correspond to "code+data".  The distinction is important, because
5880a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  like a closure a block captures the values of externally referenced
5890a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  variables.
590eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenekclass BlockTextRegion : public CodeTextRegion {
59167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
59267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
593eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  const BlockDecl *BD;
5941d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek  AnalysisDeclContext *AC;
595eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  CanQualType locTy;
59667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
59767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  BlockTextRegion(const BlockDecl *bd, CanQualType lTy,
5981d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek                  AnalysisDeclContext *ac, const MemRegion* sreg)
59967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : CodeTextRegion(sreg, BlockTextRegionKind), BD(bd), AC(ac), locTy(lTy) {}
60067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
60167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenekpublic:
602651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getLocationType() const override {
603eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek    return locTy;
604eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  }
605eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek
606eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  const BlockDecl *getDecl() const {
607eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek    return BD;
608eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  }
60967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
6101d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek  AnalysisDeclContext *getAnalysisDeclContext() const { return AC; }
611651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
612651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  virtual void dumpToStream(raw_ostream &os) const override;
613651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
614651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
615651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
616eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const BlockDecl *BD,
6171d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek                            CanQualType, const AnalysisDeclContext*,
61867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek                            const MemRegion*);
619eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek
620eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek  static bool classof(const MemRegion* R) {
621eb1c7a04509f5d25c09005a6d46bd8bbb3ca88cbTed Kremenek    return R->getKind() == BlockTextRegionKind;
622ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu  }
623ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu};
6240a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek
6250a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek/// BlockDataRegion - A region that represents a block instance.
6260a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  Blocks are represented with two kinds of regions.  BlockTextRegions
6270a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  represent the "code", while BlockDataRegions represent instances of blocks,
6280a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  which correspond to "code+data".  The distinction is important, because
6290a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  like a closure a block captures the values of externally referenced
6300a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek///  variables.
6318ecf59afbab1dbf184dc4c0c47e7213cbd32ba0aJordan Roseclass BlockDataRegion : public TypedRegion {
63267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  friend class MemRegionManager;
6330a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek  const BlockTextRegion *BC;
63467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const LocationContext *LC; // Can be null */
6353eb52bb5d791630f926ff2226dae25012315ad9aBill Wendling  unsigned BlockCount;
6364240096011a187807058f887eb81df750ffa17feTed Kremenek  void *ReferencedVars;
63785d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek  void *OriginalVars;
63867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
63967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  BlockDataRegion(const BlockTextRegion *bc, const LocationContext *lc,
6403eb52bb5d791630f926ff2226dae25012315ad9aBill Wendling                  unsigned count, const MemRegion *sreg)
6418ecf59afbab1dbf184dc4c0c47e7213cbd32ba0aJordan Rose  : TypedRegion(sreg, BlockDataRegionKind), BC(bc), LC(lc),
6423eb52bb5d791630f926ff2226dae25012315ad9aBill Wendling     BlockCount(count),
6436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ReferencedVars(nullptr), OriginalVars(nullptr) {}
6440a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek
64585d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenekpublic:
6460a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek  const BlockTextRegion *getCodeRegion() const { return BC; }
6474240096011a187807058f887eb81df750ffa17feTed Kremenek
648d02e83a548359df978f40cc1a52ffd78b7ef371bTed Kremenek  const BlockDecl *getDecl() const { return BC->getDecl(); }
6498ecf59afbab1dbf184dc4c0c47e7213cbd32ba0aJordan Rose
650651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getLocationType() const override { return BC->getLocationType(); }
651651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
65281cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek  class referenced_vars_iterator {
65381cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    const MemRegion * const *R;
65485d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek    const MemRegion * const *OriginalR;
65581cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek  public:
65685d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek    explicit referenced_vars_iterator(const MemRegion * const *r,
65785d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek                                      const MemRegion * const *originalR)
65885d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek      : R(r), OriginalR(originalR) {}
65985d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek
660e3ce2c10c3f6ae7b26700d758de909deab190d42Ted Kremenek    const VarRegion *getCapturedRegion() const {
66181cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek      return cast<VarRegion>(*R);
66281cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    }
663e3ce2c10c3f6ae7b26700d758de909deab190d42Ted Kremenek    const VarRegion *getOriginalRegion() const {
664e3ce2c10c3f6ae7b26700d758de909deab190d42Ted Kremenek      return cast<VarRegion>(*OriginalR);
665e3ce2c10c3f6ae7b26700d758de909deab190d42Ted Kremenek    }
666e3ce2c10c3f6ae7b26700d758de909deab190d42Ted Kremenek
66781cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    bool operator==(const referenced_vars_iterator &I) const {
6686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      assert((R == nullptr) == (I.R == nullptr));
66981cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek      return I.R == R;
67081cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    }
67181cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    bool operator!=(const referenced_vars_iterator &I) const {
6726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      assert((R == nullptr) == (I.R == nullptr));
67381cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek      return I.R != R;
67481cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    }
6759c378f705405d37f49795d5e915989de774fe11fTed Kremenek    referenced_vars_iterator &operator++() {
67681cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek      ++R;
67785d87df66a50a15a1957f7213802000b451a8ec9Ted Kremenek      ++OriginalR;
67881cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek      return *this;
67981cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek    }
68081cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek  };
6815846720f08a6b225484bfe663599c2b057a99bc8Ted Kremenek
6825846720f08a6b225484bfe663599c2b057a99bc8Ted Kremenek  /// Return the original region for a captured region, if
6835846720f08a6b225484bfe663599c2b057a99bc8Ted Kremenek  /// one exists.
6845846720f08a6b225484bfe663599c2b057a99bc8Ted Kremenek  const VarRegion *getOriginalRegion(const VarRegion *VR) const;
68581cef5889027d0f96b24afa7a0bb28d9023474edTed Kremenek
6864240096011a187807058f887eb81df750ffa17feTed Kremenek  referenced_vars_iterator referenced_vars_begin() const;
6874240096011a187807058f887eb81df750ffa17feTed Kremenek  referenced_vars_iterator referenced_vars_end() const;
688651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
689651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
690651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
691651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
692651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
69367d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID&, const BlockTextRegion *,
6943eb52bb5d791630f926ff2226dae25012315ad9aBill Wendling                            const LocationContext *, unsigned,
6953eb52bb5d791630f926ff2226dae25012315ad9aBill Wendling                            const MemRegion *);
6960a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek
6970a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek  static bool classof(const MemRegion* R) {
6980a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek    return R->getKind() == BlockDataRegionKind;
6990a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek  }
7004240096011a187807058f887eb81df750ffa17feTed Kremenekprivate:
7014240096011a187807058f887eb81df750ffa17feTed Kremenek  void LazyInitializeReferencedVars();
70224570c4c258545f8310e4bc96503a5668982cf67Ted Kremenek  std::pair<const VarRegion *, const VarRegion *>
70324570c4c258545f8310e4bc96503a5668982cf67Ted Kremenek  getCaptureRegions(const VarDecl *VD);
7040a8112a78d00b5140c5b7f16c3b34c2c0c13c1a0Ted Kremenek};
705ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu
706026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu/// SymbolicRegion - A special, "non-concrete" region. Unlike other region
707026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu///  clases, SymbolicRegion represents a region that serves as an alias for
708026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu///  either a real region, a NULL pointer, etc.  It essentially is used to
709026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu///  map the concept of symbolic values into the domain of regions.  Symbolic
710026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu///  regions do not need to be typed.
711e8e86482da4c1872673bbb9c237649229d19793bTed Kremenekclass SymbolicRegion : public SubRegion {
712026c66395b88a09437319139a43b090093f7e1ddZhongxing Xuprotected:
713026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu  const SymbolRef sym;
714026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu
715026c66395b88a09437319139a43b090093f7e1ddZhongxing Xupublic:
7161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SymbolicRegion(const SymbolRef s, const MemRegion* sreg)
717e8e86482da4c1872673bbb9c237649229d19793bTed Kremenek    : SubRegion(sreg, SymbolicRegionKind), sym(s) {}
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
719026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu  SymbolRef getSymbol() const {
720026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu    return sym;
721026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu  }
722026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu
723651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return true; }
7249852b5bf94d4934de63da6356c651c61e81f58d9Zhongxing Xu
725651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override;
72632f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose
727651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
728026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu
729250101353b711a409b075f1bc11070dddec7100bTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID,
730250101353b711a409b075f1bc11070dddec7100bTed Kremenek                            SymbolRef sym,
731250101353b711a409b075f1bc11070dddec7100bTed Kremenek                            const MemRegion* superRegion);
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
733651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
7341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
735026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu  static bool classof(const MemRegion* R) {
736026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu    return R->getKind() == SymbolicRegionKind;
737026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu  }
7381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump};
739026c66395b88a09437319139a43b090093f7e1ddZhongxing Xu
740e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu/// StringRegion - Region associated with a StringLiteral.
7419697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass StringRegion : public TypedValueRegion {
742e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  friend class MemRegionManager;
743e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  const StringLiteral* Str;
744e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xuprotected:
745e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
746250101353b711a409b075f1bc11070dddec7100bTed Kremenek  StringRegion(const StringLiteral* str, const MemRegion* sreg)
7479697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    : TypedValueRegion(sreg, StringRegionKind), Str(str) {}
748e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
749e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  static void ProfileRegion(llvm::FoldingSetNodeID& ID,
750e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu                            const StringLiteral* Str,
751e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu                            const MemRegion* superRegion);
752e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
753e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xupublic:
7546613d08a19aa6ce9b6330487f3bfac841d4b8a4dZhongxing Xu
7556613d08a19aa6ce9b6330487f3bfac841d4b8a4dZhongxing Xu  const StringLiteral* getStringLiteral() const { return Str; }
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
757651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
758ff6978263439e21d795b0602fabcb38488ef8441Zhongxing Xu    return Str->getType();
759ff6978263439e21d795b0602fabcb38488ef8441Zhongxing Xu  }
760e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
761651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override;
76232f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose
763651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return false; }
7640a51f1c6cec6032bcf64ce205bc4edfdc3cb3a2fZhongxing Xu
765651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override {
766e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu    ProfileRegion(ID, Str, superRegion);
767e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  }
768e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
769651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
770cc128b32429494fe04ed36d7ba30c011cb4e173aZhongxing Xu
771e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  static bool classof(const MemRegion* R) {
772e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu    return R->getKind() == StringRegionKind;
773e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  }
774e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu};
7754c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek
7764c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek/// The region associated with an ObjCStringLiteral.
7774c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenekclass ObjCStringRegion : public TypedValueRegion {
7784c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  friend class MemRegionManager;
7794c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  const ObjCStringLiteral* Str;
7804c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenekprotected:
7814c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek
7824c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  ObjCStringRegion(const ObjCStringLiteral* str, const MemRegion* sreg)
7834c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  : TypedValueRegion(sreg, ObjCStringRegionKind), Str(str) {}
7844c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek
7854c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID,
7864c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek                            const ObjCStringLiteral* Str,
7874c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek                            const MemRegion* superRegion);
7884c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek
7894c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenekpublic:
7904c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek
7914c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  const ObjCStringLiteral* getObjCStringLiteral() const { return Str; }
792651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
793651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
7944c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek    return Str->getType();
7954c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  }
796651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
797651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return false; }
798651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
799651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override {
8004c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek    ProfileRegion(ID, Str, superRegion);
8014c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  }
802651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
803651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
804651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
8054c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  static bool classof(const MemRegion* R) {
8064c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek    return R->getKind() == ObjCStringRegionKind;
8074c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  }
8084c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek};
809e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
810329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek/// CompoundLiteralRegion - A memory region representing a compound literal.
811329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek///   Compound literals are essentially temporaries that are stack allocated
812329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek///   or in the global constant pool.
8139697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass CompoundLiteralRegion : public TypedValueRegion {
814329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenekprivate:
815329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek  friend class MemRegionManager;
8169c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const CompoundLiteralExpr *CL;
817329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek
8189c378f705405d37f49795d5e915989de774fe11fTed Kremenek  CompoundLiteralRegion(const CompoundLiteralExpr *cl, const MemRegion* sReg)
8199697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    : TypedValueRegion(sReg, CompoundLiteralRegionKind), CL(cl) {}
8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
821329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID,
8229c378f705405d37f49795d5e915989de774fe11fTed Kremenek                            const CompoundLiteralExpr *CL,
823329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek                            const MemRegion* superRegion);
824329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenekpublic:
825651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
826018220c343c103b7dfaa117a7a474c7a7fd6d068Zhongxing Xu    return CL->getType();
82777cfac623178d0c16e16e2f171d20b0fea8fde30Zhongxing Xu  }
8280a51f1c6cec6032bcf64ce205bc4edfdc3cb3a2fZhongxing Xu
829651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool isBoundable() const override { return !CL->isFileScope(); }
8309a08fee80a34938b8c11c7166e009a89ced4c2b4Zhongxing Xu
831651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
833651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
834329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek
8359c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const CompoundLiteralExpr *getLiteralExpr() const { return CL; }
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
837329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek  static bool classof(const MemRegion* R) {
838329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek    return R->getKind() == CompoundLiteralRegionKind;
839329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek  }
840329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek};
841178927517fa09ddbb04dc8ef725b5716c18aae21Zhongxing Xu
8429697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass DeclRegion : public TypedValueRegion {
8439e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekprotected:
8449c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const Decl *D;
8459e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
8469c378f705405d37f49795d5e915989de774fe11fTed Kremenek  DeclRegion(const Decl *d, const MemRegion* sReg, Kind k)
8479697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    : TypedValueRegion(sReg, k), D(d) {}
8489e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
8499c378f705405d37f49795d5e915989de774fe11fTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
8509e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek                      const MemRegion* superRegion, Kind k);
8511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
852bfb6582ef46dfb33672d9621f879fc262339d704Zhongxing Xupublic:
8539c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const Decl *getDecl() const { return D; }
854651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
8551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
856e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  static bool classof(const MemRegion* R) {
857e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu    unsigned k = R->getKind();
85867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    return k >= BEG_DECL_REGIONS && k <= END_DECL_REGIONS;
859e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu  }
8609e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8629e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekclass VarRegion : public DeclRegion {
8639e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  friend class MemRegionManager;
864d17da2b99f323fa91b01e1dd119cc32e0ee8197dTed Kremenek
865d17da2b99f323fa91b01e1dd119cc32e0ee8197dTed Kremenek  // Constructors and private methods.
8669c378f705405d37f49795d5e915989de774fe11fTed Kremenek  VarRegion(const VarDecl *vd, const MemRegion* sReg)
86767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek    : DeclRegion(vd, sReg, VarRegionKind) {}
8689e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
8699c378f705405d37f49795d5e915989de774fe11fTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const VarDecl *VD,
870d17da2b99f323fa91b01e1dd119cc32e0ee8197dTed Kremenek                            const MemRegion *superRegion) {
8719e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    DeclRegion::ProfileRegion(ID, VD, superRegion, VarRegionKind);
8729e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
874651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumppublic:
877d17da2b99f323fa91b01e1dd119cc32e0ee8197dTed Kremenek  const VarDecl *getDecl() const { return cast<VarDecl>(D); }
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8795348f94abd0e9d3945da8d059b55b156967e8ff9Ted Kremenek  const StackFrameContext *getStackFrame() const;
880651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
881651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
8826eddeb153415049c7b62de4b45385a759a6906c6Ted Kremenek    // FIXME: We can cache this if needed.
883018220c343c103b7dfaa117a7a474c7a7fd6d068Zhongxing Xu    return getDecl()->getType();
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
886651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8889e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  static bool classof(const MemRegion* R) {
8899e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    return R->getKind() == VarRegionKind;
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
8913d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks
892651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool canPrintPrettyAsExpr() const override;
8939e2f5977a180ae927d05e844c65b8a7873be48a4Anna Zaks
894651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void printPrettyAsExpr(raw_ostream &os) const override;
8959e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
896de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
897de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek/// CXXThisRegion - Represents the region for the implicit 'this' parameter
898de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek///  in a call to a C++ method.  This region doesn't represent the object
899de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek///  referred to by 'this', but rather 'this' itself.
9009697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass CXXThisRegion : public TypedValueRegion {
901de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  friend class MemRegionManager;
902de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  CXXThisRegion(const PointerType *thisPointerTy,
903de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek                const MemRegion *sReg)
9049697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    : TypedValueRegion(sReg, CXXThisRegionKind), ThisPointerTy(thisPointerTy) {}
905de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
906de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID &ID,
907de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek                            const PointerType *PT,
908de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek                            const MemRegion *sReg);
909de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
910651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID &ID) const override;
911de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
912651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinespublic:
913651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
914de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek    return QualType(ThisPointerTy, 0);
915de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  }
9167caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
917651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
918651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
919de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  static bool classof(const MemRegion* R) {
920de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek    return R->getKind() == CXXThisRegionKind;
921de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  }
922de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
923de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenekprivate:
924de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  const PointerType *ThisPointerTy;
925de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek};
9269e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
9279e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekclass FieldRegion : public DeclRegion {
9289e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  friend class MemRegionManager;
9299e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
9309c378f705405d37f49795d5e915989de774fe11fTed Kremenek  FieldRegion(const FieldDecl *fd, const MemRegion* sReg)
9319e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    : DeclRegion(fd, sReg, FieldRegionKind) {}
9329e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
9334bd1eefd48c70ebef185e524d0484c00f16000cfTed Kremenekpublic:
9349c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const FieldDecl *getDecl() const { return cast<FieldDecl>(D); }
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
936651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
9376eddeb153415049c7b62de4b45385a759a6906c6Ted Kremenek    // FIXME: We can cache this if needed.
938018220c343c103b7dfaa117a7a474c7a7fd6d068Zhongxing Xu    return getDecl()->getType();
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
9404bd1eefd48c70ebef185e524d0484c00f16000cfTed Kremenek
941651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override;
94232f2656b90900ac04c4b50e87c16749d0ceb9ef2Jordy Rose
9439c378f705405d37f49795d5e915989de774fe11fTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const FieldDecl *FD,
9446304b08daee27b25ae2e2bdb8bffd67dfad74b3cTed Kremenek                            const MemRegion* superRegion) {
9459e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    DeclRegion::ProfileRegion(ID, FD, superRegion, FieldRegionKind);
9469e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
9471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9489e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  static bool classof(const MemRegion* R) {
9499e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    return R->getKind() == FieldRegionKind;
9509e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
9513d7c44e01d568e5d5c0fac9c6ccb3f080157ba19Anna Zaks
952651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
953919e8a1c6698bfa6848571d366430126bced727dJordan Rose
954651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool canPrintPretty() const override;
955651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void printPretty(raw_ostream &os) const override;
956651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool canPrintPrettyAsExpr() const override;
957651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void printPrettyAsExpr(raw_ostream &os) const override;
9589e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9609e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekclass ObjCIvarRegion : public DeclRegion {
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9629e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  friend class MemRegionManager;
9631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
964c35fb7d67d515659ad2325b4f6ec97c9fe64fb63Benjamin Kramer  ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg);
9659e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
9669c378f705405d37f49795d5e915989de774fe11fTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, const ObjCIvarDecl *ivd,
967c35fb7d67d515659ad2325b4f6ec97c9fe64fb63Benjamin Kramer                            const MemRegion* superRegion);
9681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9699e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekpublic:
970c35fb7d67d515659ad2325b4f6ec97c9fe64fb63Benjamin Kramer  const ObjCIvarDecl *getDecl() const;
971651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override;
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
973651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool canPrintPrettyAsExpr() const override;
974651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void printPrettyAsExpr(raw_ostream &os) const override;
97543b82b823a6113fdbee54243b280db9c55ef72cbTed Kremenek
976651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9789e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  static bool classof(const MemRegion* R) {
9799e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek    return R->getKind() == ObjCIvarRegionKind;
9809e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  }
9819e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek};
9827caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu//===----------------------------------------------------------------------===//
983fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// Auxiliary data classes for use with MemRegions.
9847caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu//===----------------------------------------------------------------------===//
9857caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
9867caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xuclass ElementRegion;
9877caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
9887caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xuclass RegionRawOffset {
9897caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xuprivate:
9907caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  friend class ElementRegion;
9917caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
9927caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  const MemRegion *Region;
9939ff2b13aee0f89d23ef4820218f9b88bb5e5c1c1Ken Dyck  CharUnits Offset;
9947caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
9959ff2b13aee0f89d23ef4820218f9b88bb5e5c1c1Ken Dyck  RegionRawOffset(const MemRegion* reg, CharUnits offset = CharUnits::Zero())
9967caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu    : Region(reg), Offset(offset) {}
9977caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
9987caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xupublic:
9997caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  // FIXME: Eventually support symbolic offsets.
10009ff2b13aee0f89d23ef4820218f9b88bb5e5c1c1Ken Dyck  CharUnits getOffset() const { return Offset; }
10017caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  const MemRegion *getRegion() const { return Region; }
10027caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu
10039c378f705405d37f49795d5e915989de774fe11fTed Kremenek  void dumpToStream(raw_ostream &os) const;
10047caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  void dump() const;
10057caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu};
1006511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
10071437425a62dbf7bdb0a855d3ed3b05ed2019ec1eAnna Zaks/// \brief ElementRegin is used to represent both array elements and casts.
10089697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass ElementRegion : public TypedValueRegion {
1009511191ce8920160525611be2be754c32a0724c3eZhongxing Xu  friend class MemRegionManager;
1010511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
1011f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek  QualType ElementType;
101202282acd7a42d06a3178e3102d34a585bd82dd9fTed Kremenek  NonLoc Index;
1013511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
101402282acd7a42d06a3178e3102d34a585bd82dd9fTed Kremenek  ElementRegion(QualType elementType, NonLoc Idx, const MemRegion* sReg)
10159697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    : TypedValueRegion(sReg, ElementRegionKind),
1016f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek      ElementType(elementType), Index(Idx) {
10175251abea41b446c26e3239c8dd6c7edea6fc335dDavid Blaikie    assert((!Idx.getAs<nonloc::ConcreteInt>() ||
10185251abea41b446c26e3239c8dd6c7edea6fc335dDavid Blaikie            Idx.castAs<nonloc::ConcreteInt>().getValue().isSigned()) &&
101943b28d07019bc78447ecbbb721526de4ffd83f20Chris Lattner           "The index must be signed");
10200395b5d4987fe5baa818015e9d294c128619e4ecZhongxing Xu  }
10211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1022f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek  static void ProfileRegion(llvm::FoldingSetNodeID& ID, QualType elementType,
1023f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek                            SVal Idx, const MemRegion* superRegion);
1024511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
1025511191ce8920160525611be2be754c32a0724c3eZhongxing Xupublic:
1026511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
102702282acd7a42d06a3178e3102d34a585bd82dd9fTed Kremenek  NonLoc getIndex() const { return Index; }
10286e78e1b22f3b16bb2ef76950b9b75f060bdba7bfZhongxing Xu
1029651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
1030f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek    return ElementType;
1031f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek  }
10321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1033f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek  QualType getElementType() const {
1034f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek    return ElementType;
1035abb042f33ea8e6107a7dc8efc51d2ace329f9f48Ted Kremenek  }
10367caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  /// Compute the offset within the array. The array might also be a subobject.
10377caf9b369cba6edaf6eac25121cbc65ee938f14dZhongxing Xu  RegionRawOffset getAsArrayOffset() const;
10381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1039651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
1040b21ff77c8126ea628b66d2ffb931fdaa7884f5d2Zhongxing Xu
1041651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID& ID) const override;
1042511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
1043511191ce8920160525611be2be754c32a0724c3eZhongxing Xu  static bool classof(const MemRegion* R) {
1044511191ce8920160525611be2be754c32a0724c3eZhongxing Xu    return R->getKind() == ElementRegionKind;
1045511191ce8920160525611be2be754c32a0724c3eZhongxing Xu  }
1046511191ce8920160525611be2be754c32a0724c3eZhongxing Xu};
104719e1f0ba5cec738ce6cebe3fe0e1edc782206494Ted Kremenek
1048bc37b8dd9914e02580f531fa6e5e72be34d9675eZhongxing Xu// C++ temporary object associated with an expression.
10499697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass CXXTempObjectRegion : public TypedValueRegion {
1050bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu  friend class MemRegionManager;
1051bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
1052bc37b8dd9914e02580f531fa6e5e72be34d9675eZhongxing Xu  Expr const *Ex;
1053bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
105402fe28c8a6da29d4ad88d0900c133dcf22d24a75Zhongxing Xu  CXXTempObjectRegion(Expr const *E, MemRegion const *sReg)
10559697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenek    : TypedValueRegion(sReg, CXXTempObjectRegionKind), Ex(E) {}
1056bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
1057bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu  static void ProfileRegion(llvm::FoldingSetNodeID &ID,
1058bc37b8dd9914e02580f531fa6e5e72be34d9675eZhongxing Xu                            Expr const *E, const MemRegion *sReg);
1059bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
1060bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xupublic:
1061782f63ecd124f9384f988dc7e0cf4ae1540c15f6Jeffrey Yasskin  const Expr *getExpr() const { return Ex; }
1062782f63ecd124f9384f988dc7e0cf4ae1540c15f6Jeffrey Yasskin
1063651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override {
1064bc37b8dd9914e02580f531fa6e5e72be34d9675eZhongxing Xu    return Ex->getType();
1065bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu  }
1066bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
1067651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
1068e1aeb13251a4858724d2b7ad1925d25076f23d08Zhongxing Xu
1069651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID &ID) const override;
1070bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
1071bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu  static bool classof(const MemRegion* R) {
107202fe28c8a6da29d4ad88d0900c133dcf22d24a75Zhongxing Xu    return R->getKind() == CXXTempObjectRegionKind;
1073bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu  }
1074bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu};
1075bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
10764fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu// CXXBaseObjectRegion represents a base object within a C++ object. It is
10774fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu// identified by the base class declaration and the region of its parent object.
10789697934650354bed2e509d8e7e44f21a1fb00f76Ted Kremenekclass CXXBaseObjectRegion : public TypedValueRegion {
10794fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu  friend class MemRegionManager;
10804fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
10814411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> Data;
10824fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
10834411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  CXXBaseObjectRegion(const CXXRecordDecl *RD, bool IsVirtual,
10844411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose                      const MemRegion *SReg)
10854411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose    : TypedValueRegion(SReg, CXXBaseObjectRegionKind), Data(RD, IsVirtual) {}
10864fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
10874411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  static void ProfileRegion(llvm::FoldingSetNodeID &ID, const CXXRecordDecl *RD,
10884411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose                            bool IsVirtual, const MemRegion *SReg);
10894fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
10904fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xupublic:
10914411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  const CXXRecordDecl *getDecl() const { return Data.getPointer(); }
10924411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  bool isVirtual() const { return Data.getInt(); }
1093d074441e027471a914cbb909a7aad1d43224950fZhongxing Xu
1094651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  QualType getValueType() const override;
10954fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
1096651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void dumpToStream(raw_ostream &os) const override;
10974fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
1098651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Profile(llvm::FoldingSetNodeID &ID) const override;
10994fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
11004fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu  static bool classof(const MemRegion *region) {
11014fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu    return region->getKind() == CXXBaseObjectRegionKind;
11024fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu  }
110307d8470effc0b0364801adddb6ff92bd22334402Anna Zaks
1104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool canPrintPrettyAsExpr() const override;
1105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void printPrettyAsExpr(raw_ostream &os) const override;
11074fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu};
11084fd56816e0925c04f2c92e75399f5c9018d5d6fbZhongxing Xu
1109ed65d3d97132fbcdd124aef4d2478e348dfbd36bTed Kremenektemplate<typename RegionTy>
1110ed65d3d97132fbcdd124aef4d2478e348dfbd36bTed Kremenekconst RegionTy* MemRegion::getAs() const {
1111f7a0cf426eddae76e1a71dd2295631a2cf0560afTed Kremenek  if (const RegionTy* RT = dyn_cast<RegionTy>(this))
1112f7a0cf426eddae76e1a71dd2295631a2cf0560afTed Kremenek    return RT;
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return nullptr;
1115ed65d3d97132fbcdd124aef4d2478e348dfbd36bTed Kremenek}
1116511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
11179e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//===----------------------------------------------------------------------===//
11189e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek// MemRegionManager - Factory object for creating regions.
11199e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek//===----------------------------------------------------------------------===//
11209e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek
11219e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekclass MemRegionManager {
1122a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek  ASTContext &C;
11239e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  llvm::BumpPtrAllocator& A;
11249e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  llvm::FoldingSet<MemRegion> Regions;
11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1126eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  GlobalInternalSpaceRegion *InternalGlobals;
1127eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  GlobalSystemSpaceRegion *SystemGlobals;
1128eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  GlobalImmutableSpaceRegion *ImmutableGlobals;
1129eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks
11302b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek
1131c30470dd245e2c3b367d3d806e75bf6cc20fc18cZhongxing Xu  llvm::DenseMap<const StackFrameContext *, StackLocalsSpaceRegion *>
1132c30470dd245e2c3b367d3d806e75bf6cc20fc18cZhongxing Xu    StackLocalsSpaceRegions;
1133c30470dd245e2c3b367d3d806e75bf6cc20fc18cZhongxing Xu  llvm::DenseMap<const StackFrameContext *, StackArgumentsSpaceRegion *>
1134c30470dd245e2c3b367d3d806e75bf6cc20fc18cZhongxing Xu    StackArgumentsSpaceRegions;
1135dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  llvm::DenseMap<const CodeTextRegion *, StaticGlobalSpaceRegion *>
1136dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek    StaticsGlobalSpaceRegions;
11372b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek
113867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  HeapSpaceRegion *heap;
11392b87ae45e129b941d0a4d221c9d4842385a119bdTed Kremenek  UnknownSpaceRegion *unknown;
1140d05552a21377f493c882298c59e8829040b01d34Ted Kremenek  MemSpaceRegion *code;
1141dc0a25d9bff956cdbe54ea0bfc8fbbe3ceb4eb92Zhongxing Xu
11429e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenekpublic:
11436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  MemRegionManager(ASTContext &c, llvm::BumpPtrAllocator &a)
11446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : C(c), A(a), InternalGlobals(nullptr), SystemGlobals(nullptr),
11456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      ImmutableGlobals(nullptr), heap(nullptr), unknown(nullptr),
11466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      code(nullptr) {}
11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11484240096011a187807058f887eb81df750ffa17feTed Kremenek  ~MemRegionManager();
11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1150a43484afda4c4fb4b23a53a2dc91d985d39dc2c4Ted Kremenek  ASTContext &getContext() { return C; }
11514240096011a187807058f887eb81df750ffa17feTed Kremenek
11524240096011a187807058f887eb81df750ffa17feTed Kremenek  llvm::BumpPtrAllocator &getAllocator() { return A; }
11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115467d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  /// getStackLocalsRegion - Retrieve the memory region associated with the
115567d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  ///  specified stack frame.
115667d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const StackLocalsSpaceRegion *
115767d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  getStackLocalsRegion(const StackFrameContext *STC);
1158d05552a21377f493c882298c59e8829040b01d34Ted Kremenek
1159d05552a21377f493c882298c59e8829040b01d34Ted Kremenek  /// getStackArgumentsRegion - Retrieve the memory region associated with
116067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  ///  function/method arguments of the specified stack frame.
116167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const StackArgumentsSpaceRegion *
116267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  getStackArgumentsRegion(const StackFrameContext *STC);
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11649e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  /// getGlobalsRegion - Retrieve the memory region associated with
1165dcee3ce97fc76f20ce8f5a7451071e3dec537073Ted Kremenek  ///  global variables.
1166eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks  const GlobalsSpaceRegion *getGlobalsRegion(
1167eb31a76d1cdaaf8874c549dc6bd964ff270d3822Anna Zaks      MemRegion::Kind K = MemRegion::GlobalInternalSpaceRegionKind,
11686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      const CodeTextRegion *R = nullptr);
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11709e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  /// getHeapRegion - Retrieve the memory region associated with the
11719e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///  generic "heap".
117267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const HeapSpaceRegion *getHeapRegion();
1173178927517fa09ddbb04dc8ef725b5716c18aae21Zhongxing Xu
1174178927517fa09ddbb04dc8ef725b5716c18aae21Zhongxing Xu  /// getUnknownRegion - Retrieve the memory region associated with unknown
1175178927517fa09ddbb04dc8ef725b5716c18aae21Zhongxing Xu  /// memory space.
1176b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek  const MemSpaceRegion *getUnknownRegion();
1177c540b261ac553b91840146eaa3fee3f11b1013a7Zhongxing Xu
1178b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek  const MemSpaceRegion *getCodeRegion();
1179ec13d9206645af07ef7c571405893b8d901de151Zhongxing Xu
11807090ae1a984eb446a5fb4da7b1f2573830653799Ted Kremenek  /// getAllocaRegion - Retrieve a region associated with a call to alloca().
11819c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const AllocaRegion *getAllocaRegion(const Expr *Ex, unsigned Cnt,
118267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek                                      const LocationContext *LC);
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1184329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek  /// getCompoundLiteralRegion - Retrieve the region associated with a
1185329d6fde79254503b14724e1231a9d70fa6b387fTed Kremenek  ///  given CompoundLiteral.
1186b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek  const CompoundLiteralRegion*
11879c378f705405d37f49795d5e915989de774fe11fTed Kremenek  getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
118867d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek                           const LocationContext *LC);
1189de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek
1190fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  /// getCXXThisRegion - Retrieve the [artificial] region associated with the
1191de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  ///  parameter 'this'.
1192de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek  const CXXThisRegion *getCXXThisRegion(QualType thisPointerTy,
1193de0d26310191215a6d1d189dc419f87af18ce6beTed Kremenek                                        const LocationContext *LC);
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1195e17fdb2d5dbf0ffefd417587003eebbe5baf5984Anna Zaks  /// \brief Retrieve or create a "symbolic" memory region.
1196e17fdb2d5dbf0ffefd417587003eebbe5baf5984Anna Zaks  const SymbolicRegion* getSymbolicRegion(SymbolRef Sym);
1197e17fdb2d5dbf0ffefd417587003eebbe5baf5984Anna Zaks
1198e17fdb2d5dbf0ffefd417587003eebbe5baf5984Anna Zaks  /// \brief Return a unique symbolic region belonging to heap memory space.
1199e17fdb2d5dbf0ffefd417587003eebbe5baf5984Anna Zaks  const SymbolicRegion *getSymbolicHeapRegion(SymbolRef sym);
1200e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
12014c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  const StringRegion *getStringRegion(const StringLiteral* Str);
12024c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek
12034c62b557e269a27515dfca1f754ae936c8fdb824Ted Kremenek  const ObjCStringRegion *getObjCStringRegion(const ObjCStringLiteral *Str);
1204e9f4e5420895a75dd788e9891921e7781c1823b9Zhongxing Xu
12059e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  /// getVarRegion - Retrieve or create the memory region associated with
1206d17da2b99f323fa91b01e1dd119cc32e0ee8197dTed Kremenek  ///  a specified VarDecl and LocationContext.
1207b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek  const VarRegion* getVarRegion(const VarDecl *D, const LocationContext *LC);
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120967d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  /// getVarRegion - Retrieve or create the memory region associated with
121067d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  ///  a specified VarDecl and super region.
121167d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek  const VarRegion* getVarRegion(const VarDecl *D, const MemRegion *superR);
121267d1287035767f4f6c8ca0c2bb755990012a44caTed Kremenek
1213f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek  /// getElementRegion - Retrieve the memory region associated with the
1214f936f4568700d799e7d92eecef67b0e2b822ae7eTed Kremenek  ///  associated element type, index, and super region.
121502282acd7a42d06a3178e3102d34a585bd82dd9fTed Kremenek  const ElementRegion *getElementRegion(QualType elementType, NonLoc Idx,
1216856c6bcaea56e05255e9f3997ddd56b5c18a14f0Zhongxing Xu                                        const MemRegion *superRegion,
1217856c6bcaea56e05255e9f3997ddd56b5c18a14f0Zhongxing Xu                                        ASTContext &Ctx);
12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1219b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek  const ElementRegion *getElementRegionWithSuper(const ElementRegion *ER,
1220856c6bcaea56e05255e9f3997ddd56b5c18a14f0Zhongxing Xu                                                 const MemRegion *superRegion) {
1221a5e81f1240bcc5b9b0721fc6275075ad7cadaf5eTed Kremenek    return getElementRegion(ER->getElementType(), ER->getIndex(),
1222a5e81f1240bcc5b9b0721fc6275075ad7cadaf5eTed Kremenek                            superRegion, ER->getContext());
1223a5e81f1240bcc5b9b0721fc6275075ad7cadaf5eTed Kremenek  }
1224511191ce8920160525611be2be754c32a0724c3eZhongxing Xu
12259e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  /// getFieldRegion - Retrieve or create the memory region associated with
12269e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///  a specified FieldDecl.  'superRegion' corresponds to the containing
12279e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///  memory region (which typically represents the memory representing
12289e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///  a structure or class).
12299c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const FieldRegion *getFieldRegion(const FieldDecl *fd,
1230b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek                                    const MemRegion* superRegion);
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1232b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek  const FieldRegion *getFieldRegionWithSuper(const FieldRegion *FR,
1233b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek                                             const MemRegion *superRegion) {
1234a5e81f1240bcc5b9b0721fc6275075ad7cadaf5eTed Kremenek    return getFieldRegion(FR->getDecl(), superRegion);
1235a5e81f1240bcc5b9b0721fc6275075ad7cadaf5eTed Kremenek  }
12361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12379e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  /// getObjCIvarRegion - Retrieve or create the memory region associated with
12389e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///   a specified Objective-c instance variable.  'superRegion' corresponds
12399e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///   to the containing region (which typically represents the Objective-C
12409e24049bef26b6289cce9ac9b483c5cbb096e3aeTed Kremenek  ///   object).
12419c378f705405d37f49795d5e915989de774fe11fTed Kremenek  const ObjCIvarRegion *getObjCIvarRegion(const ObjCIvarDecl *ivd,
1242b48ad64a0e9e8d6488adebe597b6af32c1eb84f4Ted Kremenek                                          const MemRegion* superRegion);
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124402fe28c8a6da29d4ad88d0900c133dcf22d24a75Zhongxing Xu  const CXXTempObjectRegion *getCXXTempObjectRegion(Expr const *Ex,
124502fe28c8a6da29d4ad88d0900c133dcf22d24a75Zhongxing Xu                                                    LocationContext const *LC);
1246bb141217871e93767aa3f2de1b9946fa6d37066aZhongxing Xu
12474411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  /// Create a CXXBaseObjectRegion with the given base class for region
12484411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  /// \p Super.
12494411b423e91da0a2c879b70c0222aeba35f72044Jordan Rose  ///
1250