clang 22.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"
19#include "llvm/Support/raw_ostream.h"
20
22
24inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
25 return OS << ID.Value;
26}
27
28/// Represents the storage location being borrowed, e.g., a specific stack
29/// variable.
30/// TODO: Model access paths of other types, e.g., s.field, heap and globals.
31struct AccessPath {
33
35};
36
37/// Information about a single borrow, or "Loan". A loan is created when a
38/// reference or pointer is created.
39struct Loan {
40 /// TODO: Represent opaque loans.
41 /// TODO: Represent nullptr: loans to no path. Accessing it UB! Currently it
42 /// is represented as empty LoanSet
45 /// The expression that creates the loan, e.g., &x.
47
49 : ID(id), Path(path), IssueExpr(IssueExpr) {}
50
51 void dump(llvm::raw_ostream &OS) const;
52};
53
54/// Manages the creation, storage and retrieval of loans.
56public:
57 LoanManager() = default;
58
59 Loan &addLoan(AccessPath Path, const Expr *IssueExpr) {
60 AllLoans.emplace_back(getNextLoanID(), Path, IssueExpr);
61 return AllLoans.back();
62 }
63
64 const Loan &getLoan(LoanID ID) const {
65 assert(ID.Value < AllLoans.size());
66 return AllLoans[ID.Value];
67 }
68 llvm::ArrayRef<Loan> getLoans() const { return AllLoans; }
69
70private:
71 LoanID getNextLoanID() { return NextLoanID++; }
72
73 LoanID NextLoanID{0};
74 /// TODO(opt): Profile and evaluate the usefullness of small buffer
75 /// optimisation.
77};
78} // namespace clang::lifetimes::internal
79
80#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
This represents one expression.
Definition Expr.h:112
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
const Loan & getLoan(LoanID ID) const
Definition Loans.h:64
Loan & addLoan(AccessPath Path, const Expr *IssueExpr)
Definition Loans.h:59
llvm::ArrayRef< Loan > getLoans() const
Definition Loans.h:68
utils::ID< struct LoanTag > LoanID
Definition Loans.h:23
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, LoanID ID)
Definition Loans.h:24
Represents the storage location being borrowed, e.g., a specific stack variable.
Definition Loans.h:31
AccessPath(const clang::ValueDecl *D)
Definition Loans.h:34
const clang::ValueDecl * D
Definition Loans.h:32
Information about a single borrow, or "Loan".
Definition Loans.h:39
Loan(LoanID id, AccessPath path, const Expr *IssueExpr)
Definition Loans.h:48
const Expr * IssueExpr
The expression that creates the loan, e.g., &x.
Definition Loans.h:46
LoanID ID
TODO: Represent opaque loans.
Definition Loans.h:43
void dump(llvm::raw_ostream &OS) const
Definition Loans.cpp:13
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21