Skip to content

Commit 5b17e4d

Browse files
committed
Change BorrowLoan to PathLoan
1 parent aad82e1 commit 5b17e4d

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ struct AccessPath {
3434
AccessPath(const clang::ValueDecl *D) : D(D) {}
3535
};
3636

37-
/// An abstract base class for a single "Loan" which represents a lifetime
38-
/// dependency.
37+
/// An abstract base class for a single "Loan" which represents lending a
38+
/// storage in memory.
3939
class Loan {
4040
/// TODO: Represent opaque loans.
4141
/// TODO: Represent nullptr: loans to no path. Accessing it UB! Currently it
4242
/// is represented as empty LoanSet
4343
public:
4444
enum class Kind : uint8_t {
45-
/// A regular borrow of a variable within the function that has a path and
46-
/// can expire.
47-
Borrow,
45+
/// A loan with an access path to a storage location.
46+
Path,
4847
/// A non-expiring placeholder loan for a parameter, representing a borrow
4948
/// from the function's caller.
5049
Placeholder
@@ -63,30 +62,30 @@ class Loan {
6362
const LoanID ID;
6463
};
6564

66-
/// Information about a single borrow loan. A borrow loan is created when a
67-
/// reference or pointer is created.
68-
class BorrowLoan : public Loan {
65+
/// PathLoan represents lending a storage location that is visible within the
66+
/// function's scope (e.g., a local variable on stack).
67+
class PathLoan : public Loan {
6968
AccessPath Path;
7069
/// The expression that creates the loan, e.g., &x.
7170
const Expr *IssueExpr;
7271

7372
public:
74-
BorrowLoan(LoanID ID, AccessPath Path, const Expr *IssueExpr)
75-
: Loan(Kind::Borrow, ID), Path(Path), IssueExpr(IssueExpr) {}
73+
PathLoan(LoanID ID, AccessPath Path, const Expr *IssueExpr)
74+
: Loan(Kind::Path, ID), Path(Path), IssueExpr(IssueExpr) {}
7675

7776
const AccessPath &getAccessPath() const { return Path; }
7877
const Expr *getIssueExpr() const { return IssueExpr; }
7978

8079
void dump(llvm::raw_ostream &OS) const override;
8180

82-
static bool classof(const Loan *L) { return L->getKind() == Kind::Borrow; }
81+
static bool classof(const Loan *L) { return L->getKind() == Kind::Path; }
8382
};
8483

8584
/// A placeholder loan held by a function parameter, representing a borrow from
8685
/// the caller's scope.
8786
///
8887
/// Created at function entry for each pointer or reference parameter with an
89-
/// origin. Unlike BorrowLoan, placeholder loans:
88+
/// origin. Unlike PathLoan, placeholder loans:
9089
/// - Have no IssueExpr (created at function entry, not at a borrow site)
9190
/// - Have no AccessPath (the borrowed object is not visible to the function)
9291
/// - Do not currently expire, but may in the future when modeling function
@@ -120,9 +119,9 @@ class LoanManager {
120119
template <typename LoanType, typename... Args>
121120
LoanType *createLoan(Args &&...args) {
122121
static_assert(
123-
std::is_same_v<LoanType, BorrowLoan> ||
122+
std::is_same_v<LoanType, PathLoan> ||
124123
std::is_same_v<LoanType, PlaceholderLoan>,
125-
"createLoan can only be used with BorrowLoan or PlaceholderLoan");
124+
"createLoan can only be used with PathLoan or PlaceholderLoan");
126125
void *Mem = LoanAllocator.Allocate<LoanType>();
127126
auto *NewLoan =
128127
new (Mem) LoanType(getNextLoanID(), std::forward<Args>(args)...);

clang/lib/Analysis/LifetimeSafety/Checker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class LifetimeChecker {
135135
return;
136136
for (const auto &[LID, Warning] : FinalWarningsMap) {
137137
const Loan *L = FactMgr.getLoanMgr().getLoan(LID);
138-
const auto *BL = cast<BorrowLoan>(L);
138+
const auto *BL = cast<PathLoan>(L);
139139
const Expr *IssueExpr = BL->getIssueExpr();
140140
llvm::PointerUnion<const UseFact *, const OriginEscapesFact *>
141141
CausingFact = Warning.CausingFact;

clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ static bool hasOrigin(const VarDecl *VD) {
3131
/// This function should be called whenever a DeclRefExpr represents a borrow.
3232
/// \param DRE The declaration reference expression that initiates the borrow.
3333
/// \return The new Loan on success, nullptr otherwise.
34-
static const BorrowLoan *createLoan(FactManager &FactMgr,
35-
const DeclRefExpr *DRE) {
34+
static const PathLoan *createLoan(FactManager &FactMgr,
35+
const DeclRefExpr *DRE) {
3636
if (const auto *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3737
AccessPath Path(VD);
3838
// The loan is created at the location of the DeclRefExpr.
39-
return FactMgr.getLoanMgr().createLoan<BorrowLoan>(Path, DRE);
39+
return FactMgr.getLoanMgr().createLoan<PathLoan>(Path, DRE);
4040
}
4141
return nullptr;
4242
}
@@ -230,7 +230,7 @@ void FactsGenerator::handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds) {
230230
return;
231231
// Iterate through all loans to see if any expire.
232232
for (const auto *Loan : FactMgr.getLoanMgr().getLoans()) {
233-
if (const auto *BL = dyn_cast<BorrowLoan>(Loan)) {
233+
if (const auto *BL = dyn_cast<PathLoan>(Loan)) {
234234
// Check if the loan is for a stack variable and if that variable
235235
// is the one being destructed.
236236
if (BL->getAccessPath().D == LifetimeEndsVD)

clang/lib/Analysis/LifetimeSafety/Loans.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace clang::lifetimes::internal {
1212

13-
void BorrowLoan::dump(llvm::raw_ostream &OS) const {
13+
void PathLoan::dump(llvm::raw_ostream &OS) const {
1414
OS << getID() << " (Path: ";
1515
OS << Path.D->getNameAsString() << ")";
1616
}

clang/unittests/Analysis/LifetimeSafetyTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class LifetimeTestHelper {
115115
}
116116
std::vector<LoanID> LID;
117117
for (const Loan *L : Analysis.getFactManager().getLoanMgr().getLoans())
118-
if (const auto *BL = dyn_cast<BorrowLoan>(L))
118+
if (const auto *BL = dyn_cast<PathLoan>(L))
119119
if (BL->getAccessPath().D == VD)
120120
LID.push_back(L->getID());
121121
if (LID.empty()) {

0 commit comments

Comments
 (0)