clang  15.0.0git
StorageLocation.h
Go to the documentation of this file.
1 //===-- StorageLocation.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines classes that represent elements of the local variable store
10 // and of the heap during dataflow analysis.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_STORAGELOCATION_H
15 #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_STORAGELOCATION_H
16 
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/ADT/DenseMap.h"
20 
21 namespace clang {
22 namespace dataflow {
23 
24 /// Base class for elements of the local variable store and of the heap.
25 ///
26 /// Each storage location holds a value. The mapping from storage locations to
27 /// values is stored in the environment.
29 public:
30  enum class Kind { Scalar, Aggregate };
31 
32  StorageLocation(Kind LocKind, QualType Type) : LocKind(LocKind), Type(Type) {}
33 
34  virtual ~StorageLocation() = default;
35 
36  Kind getKind() const { return LocKind; }
37 
38  QualType getType() const { return Type; }
39 
40 private:
41  Kind LocKind;
42  QualType Type;
43 };
44 
45 /// A storage location that is not subdivided further for the purposes of
46 /// abstract interpretation. For example: `int`, `int*`, `int&`.
47 class ScalarStorageLocation final : public StorageLocation {
48 public:
50  : StorageLocation(Kind::Scalar, Type) {}
51 
52  static bool classof(const StorageLocation *Loc) {
53  return Loc->getKind() == Kind::Scalar;
54  }
55 };
56 
57 /// A storage location which is subdivided into smaller storage locations that
58 /// can be traced independently by abstract interpretation. For example: a
59 /// struct with public members. The child map is flat, so when used for a struct
60 /// or class type, all accessible members of base struct and class types are
61 /// directly accesible as children of this location.
63 public:
66  Type, llvm::DenseMap<const ValueDecl *, StorageLocation *>()) {}
67 
69  QualType Type,
70  llvm::DenseMap<const ValueDecl *, StorageLocation *> Children)
71  : StorageLocation(Kind::Aggregate, Type), Children(std::move(Children)) {}
72 
73  static bool classof(const StorageLocation *Loc) {
74  return Loc->getKind() == Kind::Aggregate;
75  }
76 
77  /// Returns the child storage location for `D`.
78  StorageLocation &getChild(const ValueDecl &D) const {
79  auto It = Children.find(&D);
80  assert(It != Children.end());
81  return *It->second;
82  }
83 
84 private:
85  llvm::DenseMap<const ValueDecl *, StorageLocation *> Children;
86 };
87 
88 } // namespace dataflow
89 } // namespace clang
90 
91 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_STORAGELOCATION_H
llvm
YAML serialization mapping.
Definition: Dominators.h:30
clang::dataflow::StorageLocation::getKind
Kind getKind() const
Definition: StorageLocation.h:36
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:675
clang::dataflow::StorageLocation
Base class for elements of the local variable store and of the heap.
Definition: StorageLocation.h:28
clang::dataflow::AggregateStorageLocation::AggregateStorageLocation
AggregateStorageLocation(QualType Type)
Definition: StorageLocation.h:64
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1500
Decl.h
clang::dataflow::StorageLocation::Kind::Scalar
@ Scalar
clang::dataflow::ScalarStorageLocation
A storage location that is not subdivided further for the purposes of abstract interpretation.
Definition: StorageLocation.h:47
clang::dataflow::StorageLocation::Kind
Kind
Definition: StorageLocation.h:30
Type.h
clang::dataflow::AggregateStorageLocation
A storage location which is subdivided into smaller storage locations that can be traced independentl...
Definition: StorageLocation.h:62
clang::dataflow::StorageLocation::getType
QualType getType() const
Definition: StorageLocation.h:38
clang::dataflow::AggregateStorageLocation::AggregateStorageLocation
AggregateStorageLocation(QualType Type, llvm::DenseMap< const ValueDecl *, StorageLocation * > Children)
Definition: StorageLocation.h:68
clang::dataflow::AggregateStorageLocation::classof
static bool classof(const StorageLocation *Loc)
Definition: StorageLocation.h:73
clang::ValueDecl
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:674
clang::dataflow::StorageLocation::StorageLocation
StorageLocation(Kind LocKind, QualType Type)
Definition: StorageLocation.h:32
clang::dataflow::ScalarStorageLocation::classof
static bool classof(const StorageLocation *Loc)
Definition: StorageLocation.h:52
clang::dataflow::ScalarStorageLocation::ScalarStorageLocation
ScalarStorageLocation(QualType Type)
Definition: StorageLocation.h:49
std
Definition: Format.h:4296
clang
Definition: CalledOnceCheck.h:17
clang::dataflow::StorageLocation::~StorageLocation
virtual ~StorageLocation()=default
clang::dataflow::StorageLocation::Kind::Aggregate
@ Aggregate
clang::dataflow::AggregateStorageLocation::getChild
StorageLocation & getChild(const ValueDecl &D) const
Returns the child storage location for D.
Definition: StorageLocation.h:78