clang 23.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/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/TypeBase.h"
24#include "llvm/Support/raw_ostream.h"
25
27
29
30inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
31 return OS << ID.Value;
32}
33
34/// An Origin is a symbolic identifier that represents the set of possible
35/// loans a pointer-like object could hold at any given time.
36///
37/// Each Origin corresponds to a single level of indirection. For complex types
38/// with multiple levels of indirection (e.g., `int**`), multiple Origins are
39/// organized into an OriginList structure (see below).
40struct Origin {
42 /// A pointer to the AST node that this origin represents. This union
43 /// distinguishes between origins from declarations (variables or parameters)
44 /// and origins from expressions.
45 llvm::PointerUnion<const clang::ValueDecl *, const clang::Expr *> Ptr;
46
47 /// The type at this indirection level.
48 ///
49 /// For `int** pp`:
50 /// Root origin: QT = `int**` (what pp points to)
51 /// Pointee origin: QT = `int*` (what *pp points to)
52 ///
53 /// Null for synthetic lvalue origins (e.g., outer origin of DeclRefExpr).
54 const Type *Ty;
55
56 Origin(OriginID ID, const clang::ValueDecl *D, const Type *QT)
57 : ID(ID), Ptr(D), Ty(QT) {}
58 Origin(OriginID ID, const clang::Expr *E, const Type *QT)
59 : ID(ID), Ptr(E), Ty(QT) {}
60
61 const clang::ValueDecl *getDecl() const {
62 return Ptr.dyn_cast<const clang::ValueDecl *>();
63 }
64 const clang::Expr *getExpr() const {
65 return Ptr.dyn_cast<const clang::Expr *>();
66 }
67};
68
69/// A list of origins representing levels of indirection for pointer-like types.
70///
71/// Each node in the list contains an OriginID representing a level of
72/// indirection. The list structure captures the multi-level nature of
73/// pointer and reference types in the lifetime analysis.
74///
75/// Examples:
76/// - For `int& x`, the list has size 2:
77/// * Outer: origin for the reference storage itself (the lvalue `x`)
78/// * Inner: origin for what `x` refers to
79///
80/// - For `int* p`, the list has size 2:
81/// * Outer: origin for the pointer variable `p`
82/// * Inner: origin for what `p` points to
83///
84/// - For `View v` (where View is gsl::Pointer), the list has size 2:
85/// * Outer: origin for the view object itself
86/// * Inner: origin for what the view refers to
87///
88/// - For `int** pp`, the list has size 3:
89/// * Outer: origin for `pp` itself
90/// * Inner: origin for `*pp` (what `pp` points to)
91/// * Inner->Inner: origin for `**pp` (what `*pp` points to)
92///
93/// The list structure enables the analysis to track how loans flow through
94/// different levels of indirection when assignments and dereferences occur.
96public:
97 OriginList(OriginID OID) : OuterOID(OID) {}
98
99 OriginList *peelOuterOrigin() const { return InnerList; }
100 OriginID getOuterOriginID() const { return OuterOID; }
101
102 void setInnerOriginList(OriginList *Inner) { InnerList = Inner; }
103
104 // Used for assertion checks only (to ensure origin lists have matching
105 // lengths).
106 size_t getLength() const {
107 size_t Length = 1;
108 const OriginList *T = this;
109 while (T->InnerList) {
110 T = T->InnerList;
111 Length++;
112 }
113 return Length;
114 }
115
116private:
117 OriginID OuterOID;
118 OriginList *InnerList = nullptr;
119};
120
121bool doesDeclHaveStorage(const ValueDecl *D);
122
123/// Manages the creation, storage, and retrieval of origins for pointer-like
124/// variables and expressions.
126public:
127 explicit OriginManager(const AnalysisDeclContext &AC);
128
129 /// Gets or creates the OriginList for a given ValueDecl.
130 ///
131 /// Creates a list structure mirroring the levels of indirection in the
132 /// declaration's type (e.g., `int** p` creates list of size 2).
133 ///
134 /// \returns The OriginList, or nullptr if the type is not pointer-like.
136
137 /// Gets or creates the OriginList for a given Expr.
138 ///
139 /// Creates a list based on the expression's type and value category:
140 /// - Lvalues get an implicit reference level (modeling addressability)
141 /// - Rvalues of non-pointer type return nullptr (no trackable origin)
142 /// - DeclRefExpr may reuse the underlying declaration's list
143 ///
144 /// \returns The OriginList, or nullptr for non-pointer rvalues.
146
147 /// Wraps an existing OriginID in a new single-element OriginList, so a fact
148 /// can refer to a single level of an existing OriginList.
150
151 /// Returns the OriginList for the implicit 'this' parameter if the current
152 /// declaration is an instance method.
153 std::optional<OriginList *> getThisOrigins() const { return ThisOrigins; }
154
155 const Origin &getOrigin(OriginID ID) const;
156
157 llvm::ArrayRef<Origin> getOrigins() const { return AllOrigins; }
158
159 unsigned getNumOrigins() const { return NextOriginID.Value; }
160
161 bool hasOrigins(QualType QT) const;
162 bool hasOrigins(const Expr *E) const;
163
164 void dump(OriginID OID, llvm::raw_ostream &OS) const;
165
166 /// Collects statistics about expressions that lack associated origins.
167 void collectMissingOrigins(Stmt &FunctionBody, LifetimeSafetyStats &LSStats);
168
169private:
170 OriginID getNextOriginID() { return NextOriginID++; }
171
172 OriginList *createNode(const ValueDecl *D, QualType QT);
173 OriginList *createNode(const Expr *E, QualType QT);
174
175 template <typename T>
176 OriginList *buildListForType(QualType QT, const T *Node);
177
178 void initializeThisOrigins(const Decl *D);
179
180 /// Pre-scans the function body (and constructor init lists) to discover
181 /// return types of lifetime-annotated calls (currently
182 /// [[clang::lifetimebound]]), registering them for origin tracking.
183 void collectLifetimeAnnotatedOriginTypes(const AnalysisDeclContext &AC);
184 void registerLifetimeAnnotatedOriginType(QualType QT);
185
186 ASTContext &AST;
187 OriginID NextOriginID{0};
188 /// TODO(opt): Profile and evaluate the usefulness of small buffer
189 /// optimisation.
190 llvm::SmallVector<Origin> AllOrigins;
191 llvm::BumpPtrAllocator ListAllocator;
192 llvm::DenseMap<const clang::ValueDecl *, OriginList *> DeclToList;
193 llvm::DenseMap<const clang::Expr *, OriginList *> ExprToList;
194 std::optional<OriginList *> ThisOrigins;
195 /// Types that are not inherently pointer-like but require origin tracking
196 /// because of lifetime annotations (currently [[clang::lifetimebound]]) on
197 /// functions that return them.
198 llvm::DenseSet<const Type *> LifetimeAnnotatedOriginTypes;
199};
200} // namespace clang::lifetimes::internal
201
202#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:112
A (possibly-)qualified type.
Definition TypeBase.h:937
Stmt - This represents one statement.
Definition Stmt.h:86
The base class of the type hierarchy.
Definition TypeBase.h:1866
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
A list of origins representing levels of indirection for pointer-like types.
Definition Origins.h:95
OriginList * peelOuterOrigin() const
Definition Origins.h:99
void setInnerOriginList(OriginList *Inner)
Definition Origins.h:102
std::optional< OriginList * > getThisOrigins() const
Returns the OriginList for the implicit 'this' parameter if the current declaration is an instance me...
Definition Origins.h:153
OriginList * getOrCreateList(const ValueDecl *D)
Gets or creates the OriginList for a given ValueDecl.
Definition Origins.cpp:208
OriginList * createSingleOriginList(OriginID OID)
Wraps an existing OriginID in a new single-element OriginList, so a fact can refer to a single level ...
Definition Origins.cpp:189
const Origin & getOrigin(OriginID ID) const
Definition Origins.cpp:295
llvm::ArrayRef< Origin > getOrigins() const
Definition Origins.h:157
void collectMissingOrigins(Stmt &FunctionBody, LifetimeSafetyStats &LSStats)
Collects statistics about expressions that lack associated origins.
Definition Origins.cpp:300
OriginManager(const AnalysisDeclContext &AC)
Definition Origins.cpp:160
void dump(OriginID OID, llvm::raw_ostream &OS) const
Definition Origins.cpp:276
utils::ID< struct OriginTag > OriginID
Definition Origins.h:28
bool doesDeclHaveStorage(const ValueDecl *D)
Returns true if the declaration has its own storage that can be borrowed.
Definition Origins.cpp:156
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, LoanID ID)
Definition Loans.h:26
A structure to hold the statistics related to LifetimeAnalysis.
An Origin is a symbolic identifier that represents the set of possible loans a pointer-like object co...
Definition Origins.h:40
const clang::Expr * getExpr() const
Definition Origins.h:64
const clang::ValueDecl * getDecl() const
Definition Origins.h:61
llvm::PointerUnion< const clang::ValueDecl *, const clang::Expr * > Ptr
A pointer to the AST node that this origin represents.
Definition Origins.h:45
const Type * Ty
The type at this indirection level.
Definition Origins.h:54
Origin(OriginID ID, const clang::ValueDecl *D, const Type *QT)
Definition Origins.h:56
Origin(OriginID ID, const clang::Expr *E, const Type *QT)
Definition Origins.h:58
A generic, type-safe wrapper for an ID, distinguished by its Tag type.
Definition Utils.h:21