14#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
15#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/Support/Allocator.h"
24#include "llvm/Support/raw_ostream.h"
30 return OS << ID.Value;
61 void dump(llvm::raw_ostream &OS)
const {
63 OS <<
"." << FD->getNameAsString();
89 return dyn_cast<const ParmVarDecl *>(ParamMethodOrField);
93 return dyn_cast<const CXXMethodDecl *>(ParamMethodOrField);
97 return dyn_cast<const FieldDecl *>(ParamMethodOrField);
103 static void Profile(llvm::FoldingSetNodeID &ID,
KeyTy ParamMethodOrField) {
104 ID.AddPointer(ParamMethodOrField.getOpaqueValue());
107 void Profile(llvm::FoldingSetNodeID &ID)
const {
108 Profile(ID, ParamMethodOrField);
152 : Base(
Other.Base), Elements(
Other.Elements) {
153 Elements.push_back(E);
157 return dyn_cast<const clang::ValueDecl *>(Base);
161 return dyn_cast_or_null<clang::MaterializeTemporaryExpr>(
162 dyn_cast<const clang::Expr *>(Base));
166 return dyn_cast<const PlaceholderBase *>(Base);
172 const auto *E = dyn_cast<const clang::Expr *>(Base);
173 return isa_and_nonnull<clang::CXXNewExpr, clang::CallExpr>(E) ? E :
nullptr;
177 return Base == RHS.Base && Elements == RHS.Elements;
187 if (Base !=
Other.Base || Elements.size() >
Other.Elements.size())
189 return std::equal(Elements.begin(), Elements.end(),
Other.Elements.begin());
200 void dump(llvm::raw_ostream &OS)
const;
220 const Expr *IssueExpr;
224 : ID(ID), Path(Path), IssueExpr(IssueExpr) {}
230 void dump(llvm::raw_ostream &OS)
const;
237 explicit LoanManager(llvm::BumpPtrAllocator &Alloc) : LoanAllocator(Alloc) {}
240 void *Mem = LoanAllocator.Allocate<
Loan>();
241 auto *NewLoan =
new (Mem)
Loan(getNextLoanID(), Path, IssueExpr);
242 AllLoans.push_back(NewLoan);
257 assert(ID.Value < AllLoans.size());
258 return AllLoans[ID.Value];
264 LoanID getNextLoanID() {
return NextLoanID++; }
268 const PlaceholderBase *getOrCreatePlaceholderBase(
const ParmVarDecl *PVD);
269 const PlaceholderBase *getOrCreatePlaceholderBase(
const CXXMethodDecl *MD);
270 const PlaceholderBase *getOrCreatePlaceholderBase(
const FieldDecl *FD);
274 llvm::FoldingSet<PlaceholderBase> PlaceholderBases;
279 llvm::BumpPtrAllocator &LoanAllocator;
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.
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
This represents one expression.
Represents a member of a struct/union/class.
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Represents a parameter to a function.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents the storage location being borrowed, e.g., a specific stack variable or a field within it:...
bool isStrictPrefixOf(const AccessPath &Other) const
Returns true if this path is a strict prefix of Other.
AccessPath(const clang::CallExpr *CE)
bool isPrefixOf(const AccessPath &Other) const
Returns true if this path is a prefix of Other (or same as Other).
bool operator!=(const AccessPath &RHS) const
AccessPath(const clang::ValueDecl *D)
const clang::ValueDecl * getAsValueDecl() const
bool operator==(const AccessPath &RHS) const
AccessPath(const clang::MaterializeTemporaryExpr *MTE)
const clang::MaterializeTemporaryExpr * getAsMaterializeTemporaryExpr() const
llvm::ArrayRef< PathElement > getElements() const
const PlaceholderBase * getAsPlaceholderBase() const
void dump(llvm::raw_ostream &OS) const
AccessPath(const PlaceholderBase *PB)
const clang::Expr * getAsAllocation() const
Returns the allocation expression that created this loan: a CXXNewExpr or an allocating CallExpr.
AccessPath(const AccessPath &Other, PathElement E)
Creates an extended access path by appending a path element.
AccessPath(const clang::CXXNewExpr *New)
Loan * createPlaceholderLoan(const ParmVarDecl *PVD)
llvm::ArrayRef< const Loan * > getLoans() const
LoanManager(llvm::BumpPtrAllocator &Alloc)
const Loan * getLoan(LoanID ID) const
Loan * createLoan(AccessPath Path, const Expr *IssueExpr=nullptr)
Loan * createPlaceholderLoan(const FieldDecl *FD)
Loan * createPlaceholderLoan(const CXXMethodDecl *MD)
Represents a component of an access path: either a named field access or an abstract unnamed interior...
const Expr * getIssueExpr() const
Loan(LoanID ID, AccessPath Path, const Expr *IssueExpr=nullptr)
const AccessPath & getAccessPath() const
void dump(llvm::raw_ostream &OS) const
Represents one step in an access path: either a field access or an access to an unnamed interior regi...
static PathElement getInterior()
const FieldDecl * getFieldDecl() const
void dump(llvm::raw_ostream &OS) const
bool operator==(const PathElement &Other) const
bool operator!=(const PathElement &Other) const
static PathElement getField(const FieldDecl &FD)
Represents the base of a placeholder access path, which is a function parameter, the implicit 'this' ...
static void Profile(llvm::FoldingSetNodeID &ID, KeyTy ParamMethodOrField)
PlaceholderBase(const ParmVarDecl *PVD)
llvm::PointerUnion< const ParmVarDecl *, const CXXMethodDecl *, const FieldDecl * > KeyTy
PlaceholderBase(const CXXMethodDecl *MD)
PlaceholderBase(const FieldDecl *FD)
const ParmVarDecl * getParmVarDecl() const
const CXXMethodDecl * getImplicitThisParent() const
const FieldDecl * getFieldDecl() const
void Profile(llvm::FoldingSetNodeID &ID) const
utils::ID< struct LoanTag > LoanID
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, LoanID ID)
A generic, type-safe wrapper for an ID, distinguished by its Tag type.