clang 23.0.0git
Loans.h
Go to the documentation of this file.
1//===- Loans.h - Loan and Access Path Definitions --------------*- 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 the Loan and AccessPath structures, which represent
10// borrows of storage locations, and the LoanManager, which manages the
11// creation and retrieval of loans during lifetime analysis.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
15#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
16
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
21#include "llvm/Support/raw_ostream.h"
22
24
26inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
27 return OS << ID.Value;
28}
29
30/// Represents the storage location being borrowed, e.g., a specific stack
31/// variable.
32/// TODO: Model access paths of other types, e.g., s.field, heap and globals.
33struct AccessPath {
34private:
35 // An access path can be:
36 // - ValueDecl * , to represent the storage location corresponding to the
37 // variable declared in ValueDecl.
38 // - MaterializeTemporaryExpr * , to represent the storage location of the
39 // temporary object materialized via this MaterializeTemporaryExpr.
40 const llvm::PointerUnion<const clang::ValueDecl *,
42 P;
43
44public:
45 AccessPath(const clang::ValueDecl *D) : P(D) {}
47
49 return P.dyn_cast<const clang::ValueDecl *>();
50 }
51
55};
56
57/// An abstract base class for a single "Loan" which represents lending a
58/// storage in memory.
59class Loan {
60 /// TODO: Represent opaque loans.
61 /// TODO: Represent nullptr: loans to no path. Accessing it UB! Currently it
62 /// is represented as empty LoanSet
63public:
64 enum class Kind : uint8_t {
65 /// A loan with an access path to a storage location.
67 /// A non-expiring placeholder loan for a parameter, representing a borrow
68 /// from the function's caller.
70 };
71
72 Loan(Kind K, LoanID ID) : K(K), ID(ID) {}
73 virtual ~Loan() = default;
74
75 Kind getKind() const { return K; }
76 LoanID getID() const { return ID; }
77
78 virtual void dump(llvm::raw_ostream &OS) const = 0;
79
80private:
81 const Kind K;
82 const LoanID ID;
83};
84
85/// PathLoan represents lending a storage location that is visible within the
86/// function's scope (e.g., a local variable on stack).
87class PathLoan : public Loan {
88 AccessPath Path;
89 /// The expression that creates the loan, e.g., &x.
90 const Expr *IssueExpr;
91
92public:
93 PathLoan(LoanID ID, AccessPath Path, const Expr *IssueExpr)
94 : Loan(Kind::Path, ID), Path(Path), IssueExpr(IssueExpr) {}
95
96 const AccessPath &getAccessPath() const { return Path; }
97 const Expr *getIssueExpr() const { return IssueExpr; }
98
99 void dump(llvm::raw_ostream &OS) const override;
100
101 static bool classof(const Loan *L) { return L->getKind() == Kind::Path; }
102};
103
104/// A placeholder loan held by a function parameter or an implicit 'this'
105/// object, representing a borrow from the caller's scope.
106///
107/// Created at function entry for each pointer or reference parameter or for
108/// the implicit 'this' parameter of instance methods, with an
109/// origin. Unlike PathLoan, placeholder loans:
110/// - Have no IssueExpr (created at function entry, not at a borrow site)
111/// - Have no AccessPath (the borrowed object is not visible to the function)
112/// - Do not currently expire, but may in the future when modeling function
113/// invalidations (e.g., vector::push_back)
114///
115/// When a placeholder loan escapes the function (e.g., via return), it
116/// indicates the parameter or method should be marked [[clang::lifetimebound]],
117/// enabling lifetime annotation suggestions.
118class PlaceholderLoan : public Loan {
119 /// The function parameter or method (representing 'this') that holds this
120 /// placeholder loan.
121 llvm::PointerUnion<const ParmVarDecl *, const CXXMethodDecl *> ParamOrMethod;
122
123public:
125 : Loan(Kind::Placeholder, ID), ParamOrMethod(PVD) {}
126
128 : Loan(Kind::Placeholder, ID), ParamOrMethod(MD) {}
129
131 return ParamOrMethod.dyn_cast<const ParmVarDecl *>();
132 }
133
135 return ParamOrMethod.dyn_cast<const CXXMethodDecl *>();
136 }
137
138 void dump(llvm::raw_ostream &OS) const override;
139
140 static bool classof(const Loan *L) {
141 return L->getKind() == Kind::Placeholder;
142 }
143};
144
145/// Manages the creation, storage and retrieval of loans.
147public:
148 LoanManager() = default;
149
150 template <typename LoanType, typename... Args>
151 LoanType *createLoan(Args &&...args) {
152 static_assert(
153 std::is_same_v<LoanType, PathLoan> ||
154 std::is_same_v<LoanType, PlaceholderLoan>,
155 "createLoan can only be used with PathLoan or PlaceholderLoan");
156 void *Mem = LoanAllocator.Allocate<LoanType>();
157 auto *NewLoan =
158 new (Mem) LoanType(getNextLoanID(), std::forward<Args>(args)...);
159 AllLoans.push_back(NewLoan);
160 return NewLoan;
161 }
162
163 const Loan *getLoan(LoanID ID) const {
164 assert(ID.Value < AllLoans.size());
165 return AllLoans[ID.Value];
166 }
167 llvm::ArrayRef<const Loan *> getLoans() const { return AllLoans; }
168
169private:
170 LoanID getNextLoanID() { return NextLoanID++; }
171
172 LoanID NextLoanID{0};
173 /// TODO(opt): Profile and evaluate the usefullness of small buffer
174 /// optimisation.
176 llvm::BumpPtrAllocator LoanAllocator;
177};
178} // namespace clang::lifetimes::internal
179
180#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
This represents one expression.
Definition Expr.h:112
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4920
Represents a parameter to a function.
Definition Decl.h:1790
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
llvm::ArrayRef< const Loan * > getLoans() const
Definition Loans.h:167
const Loan * getLoan(LoanID ID) const
Definition Loans.h:163
LoanType * createLoan(Args &&...args)
Definition Loans.h:151
An abstract base class for a single "Loan" which represents lending a storage in memory.
Definition Loans.h:59
Loan(Kind K, LoanID ID)
Definition Loans.h:72
Kind
TODO: Represent opaque loans.
Definition Loans.h:64
@ Placeholder
A non-expiring placeholder loan for a parameter, representing a borrow from the function's caller.
Definition Loans.h:69
@ Path
A loan with an access path to a storage location.
Definition Loans.h:66
virtual void dump(llvm::raw_ostream &OS) const =0
const Expr * getIssueExpr() const
Definition Loans.h:97
PathLoan(LoanID ID, AccessPath Path, const Expr *IssueExpr)
Definition Loans.h:93
const AccessPath & getAccessPath() const
Definition Loans.h:96
static bool classof(const Loan *L)
Definition Loans.h:101
void dump(llvm::raw_ostream &OS) const override
Definition Loans.cpp:13
PlaceholderLoan(LoanID ID, const CXXMethodDecl *MD)
Definition Loans.h:127
void dump(llvm::raw_ostream &OS) const override
Definition Loans.cpp:26
static bool classof(const Loan *L)
Definition Loans.h:140
PlaceholderLoan(LoanID ID, const ParmVarDecl *PVD)
Definition Loans.h:124
const CXXMethodDecl * getMethodDecl() const
Definition Loans.h:134
const ParmVarDecl * getParmVarDecl() const
Definition Loans.h:130
utils::ID< struct LoanTag > LoanID
Definition Loans.h:25
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, LoanID ID)
Definition Loans.h:26
Represents the storage location being borrowed, e.g., a specific stack variable.
Definition Loans.h:33
AccessPath(const clang::ValueDecl *D)
Definition Loans.h:45
const clang::ValueDecl * getAsValueDecl() const
Definition Loans.h:48
AccessPath(const clang::MaterializeTemporaryExpr *MTE)
Definition Loans.h:46
const clang::MaterializeTemporaryExpr * getAsMaterializeTemporaryExpr() const
Definition Loans.h:52
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21