clang 22.0.0git
Origins.h
Go to the documentation of this file.
1//===- Origins.h - Origin and Origin Management ----------------*- 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 Origins, which represent the set of possible loans a
10// pointer-like object could hold, and the OriginManager, which manages the
11// creation, storage, and retrieval of origins for variables and expressions.
12//
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H
15#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H
16
17#include "clang/AST/Decl.h"
18#include "clang/AST/Expr.h"
20
22
24
25inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
26 return OS << ID.Value;
27}
28
29/// An Origin is a symbolic identifier that represents the set of possible
30/// loans a pointer-like object could hold at any given time.
31/// TODO: Enhance the origin model to handle complex types, pointer
32/// indirection and reborrowing. The plan is to move from a single origin per
33/// variable/expression to a "list of origins" governed by the Type.
34/// For example, the type 'int**' would have two origins.
35/// See discussion:
36/// https://github.com/llvm/llvm-project/pull/142313/commits/0cd187b01e61b200d92ca0b640789c1586075142#r2137644238
37struct Origin {
39 /// A pointer to the AST node that this origin represents. This union
40 /// distinguishes between origins from declarations (variables or parameters)
41 /// and origins from expressions.
42 llvm::PointerUnion<const clang::ValueDecl *, const clang::Expr *> Ptr;
43
44 Origin(OriginID ID, const clang::ValueDecl *D) : ID(ID), Ptr(D) {}
45 Origin(OriginID ID, const clang::Expr *E) : ID(ID), Ptr(E) {}
46
47 const clang::ValueDecl *getDecl() const {
48 return Ptr.dyn_cast<const clang::ValueDecl *>();
49 }
50 const clang::Expr *getExpr() const {
51 return Ptr.dyn_cast<const clang::Expr *>();
52 }
53};
54
55/// Manages the creation, storage, and retrieval of origins for pointer-like
56/// variables and expressions.
58public:
59 OriginManager() = default;
60
62 Origin &addOrigin(OriginID ID, const clang::Expr &E);
63
64 // TODO: Mark this method as const once we remove the call to getOrCreate.
65 OriginID get(const Expr &E);
66
67 OriginID get(const ValueDecl &D);
68
69 OriginID getOrCreate(const Expr &E);
70
71 const Origin &getOrigin(OriginID ID) const;
72
73 llvm::ArrayRef<Origin> getOrigins() const { return AllOrigins; }
74
76
77 void dump(OriginID OID, llvm::raw_ostream &OS) const;
78
79private:
80 OriginID getNextOriginID() { return NextOriginID++; }
81
82 OriginID NextOriginID{0};
83 /// TODO(opt): Profile and evaluate the usefullness of small buffer
84 /// optimisation.
86 llvm::DenseMap<const clang::ValueDecl *, OriginID> DeclToOriginID;
87 llvm::DenseMap<const clang::Expr *, OriginID> ExprToOriginID;
88};
89} // namespace clang::lifetimes::internal
90
91#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_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 Origin & getOrigin(OriginID ID) const
Definition Origins.cpp:74
OriginID getOrCreate(const Expr &E)
Definition Origins.cpp:63
llvm::ArrayRef< Origin > getOrigins() const
Definition Origins.h:73
void dump(OriginID OID, llvm::raw_ostream &OS) const
Definition Origins.cpp:13
Origin & addOrigin(OriginID ID, const clang::ValueDecl &D)
Definition Origins.cpp:25
utils::ID< struct OriginTag > OriginID
Definition Origins.h:23
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, LoanID ID)
Definition Loans.h:24
An Origin is a symbolic identifier that represents the set of possible loans a pointer-like object co...
Definition Origins.h:37
Origin(OriginID ID, const clang::Expr *E)
Definition Origins.h:45
const clang::Expr * getExpr() const
Definition Origins.h:50
const clang::ValueDecl * getDecl() const
Definition Origins.h:47
llvm::PointerUnion< const clang::ValueDecl *, const clang::Expr * > Ptr
A pointer to the AST node that this origin represents.
Definition Origins.h:42
Origin(OriginID ID, const clang::ValueDecl *D)
Definition Origins.h:44
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21