clang 23.0.0git
LifetimeSafety.h
Go to the documentation of this file.
1//===- LifetimeSafety.h - C++ Lifetime Safety Analysis -*----------- 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 main entry point and orchestrator for the C++ Lifetime
10// Safety Analysis. It coordinates the entire analysis pipeline: fact
11// generation, loan propagation, live origins analysis, and enforcement of
12// lifetime safety policy.
13//
14// The analysis is based on the concepts of "origins" and "loans" to track
15// pointer lifetimes and detect issues like use-after-free and dangling
16// pointers. See the RFC for more details:
17// https://discourse.llvm.org/t/rfc-intra-procedural-lifetime-analysis-in-clang/86291
18//
19//===----------------------------------------------------------------------===//
20#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
21#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
22
23#include "clang/AST/Decl.h"
31#include "llvm/ADT/PointerUnion.h"
32#include <cstddef>
33#include <memory>
34
35namespace clang::lifetimes {
36
38 /// Maximum number of CFG blocks to analyze. Functions with larger CFGs will
39 /// be skipped.
41
42 /// Whether to suggest lifetime annotations.
44};
45
46/// Enum to track functions visible across or within TU.
47enum class WarningScope {
48 CrossTU, // For warnings on declarations visible across Translation Units.
49 IntraTU // For warnings on functions local to a Translation Unit.
50};
51
52/// Abstract interface for operations requiring Sema access.
53///
54/// This class exists to break a circular dependency: the LifetimeSafety
55/// analysis target cannot directly depend on clangSema (which would create the
56/// cycle: clangSema -> clangAnalysis -> clangAnalysisLifetimeSafety ->
57/// clangSema).
58///
59/// Instead, this interface is implemented in AnalysisBasedWarnings.cpp (part of
60/// clangSema), allowing the analysis to report diagnostics and modify the AST
61/// through Sema without introducing a circular dependency.
63public:
65 virtual ~LifetimeSafetySemaHelper() = default;
66
67 virtual void reportUseAfterScope(const Expr *IssueExpr, const Expr *UseExpr,
68 const Expr *MovedExpr,
69 SourceLocation FreeLoc,
71
72 virtual void reportUseAfterReturn(const Expr *IssueExpr,
73 const Expr *ReturnExpr,
74 const Expr *MovedExpr) {}
75
76 virtual void reportDanglingField(const Expr *IssueExpr,
77 const FieldDecl *Field,
78 const Expr *MovedExpr,
79 SourceLocation ExpiryLoc) {}
80
81 virtual void reportDanglingGlobal(const Expr *IssueExpr,
82 const VarDecl *DanglingGlobal,
83 const Expr *MovedExpr,
84 SourceLocation ExpiryLoc) {}
85
86 // Reports when a reference/iterator is used after the container operation
87 // that invalidated it.
88 virtual void reportUseAfterInvalidation(const Expr *IssueExpr,
89 const Expr *UseExpr,
90 const Expr *InvalidationExpr) {}
91 virtual void reportUseAfterInvalidation(const ParmVarDecl *PVD,
92 const Expr *UseExpr,
93 const Expr *InvalidationExpr) {}
94 virtual void reportInvalidatedField(const Expr *IssueExpr,
95 const FieldDecl *Field,
96 const Expr *InvalidationExpr) {}
97 virtual void reportInvalidatedField(const ParmVarDecl *PVD,
98 const FieldDecl *Field,
99 const Expr *InvalidationExpr) {}
100 virtual void reportInvalidatedGlobal(const Expr *IssueExpr,
101 const VarDecl *Global,
102 const Expr *InvalidationExpr) {}
103 virtual void reportInvalidatedGlobal(const ParmVarDecl *PVD,
104 const VarDecl *Global,
105 const Expr *InvalidationExpr) {}
106
108 llvm::PointerUnion<const Expr *, const FieldDecl *, const VarDecl *>;
109
110 // Suggests lifetime bound annotations for function parameters.
112 const ParmVarDecl *ParmToAnnotate,
114
115 // Reports misuse of [[clang::noescape]] when parameter escapes through return
116 virtual void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape,
117 const Expr *EscapeExpr) {}
118 // Reports misuse of [[clang::noescape]] when parameter escapes through field
119 virtual void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape,
120 const FieldDecl *EscapeField) {}
121 // Reports misuse of [[clang::noescape]] when parameter escapes through
122 // assignment to a global variable
123 virtual void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape,
124 const VarDecl *EscapeGlobal) {}
125
126 // Reports misuse of [[clang::lifetimebound]] when parameter doesn't escape
127 // through return.
128 virtual void
129 reportLifetimeboundViolation(const ParmVarDecl *ParmWithLifetimebound) {}
130
131 // Reports misuse of [[clang::lifetimebound]] when implicit this parameter
132 // doesn't escape through return.
133 virtual void
134 reportLifetimeboundViolation(const CXXMethodDecl *MDWithLifetimebound) {}
135
136 // Reports a member function definition that has [[clang::lifetimebound]] on
137 // the implicit this parameter when the canonical declaration does not.
139 const CXXMethodDecl *FDef,
140 const CXXMethodDecl *FDecl) {}
141
142 // Reports a function definition parameter that has [[clang::lifetimebound]]
143 // when the corresponding parameter in the canonical declaration.
145 const ParmVarDecl *PVDDef,
146 const ParmVarDecl *PVDDecl) {}
147
149
150 // Suggests lifetime bound annotations for implicit this.
152 const CXXMethodDecl *MD,
153 const Expr *EscapeExpr) {}
154
155 // Adds inferred lifetime bound attribute for implicit this to its
156 // TypeSourceInfo.
158};
159
160/// The main entry point for the analysis.
162 LifetimeSafetySemaHelper *SemaHelper,
163 const LifetimeSafetyOpts &Opts,
164 LifetimeSafetyStats &Stats, bool CollectStats);
165
166namespace internal {
167
168void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM,
169 LifetimeSafetyStats &Stats);
170
171/// An object to hold the factories for immutable collections, ensuring
172/// that all created states share the same underlying memory management.
174 OriginLoanMap::Factory OriginMapFactory{/*canonicalize=*/false};
175 LoanSet::Factory LoanSetFactory{/*canonicalize=*/false};
176 MovedLoansMap::Factory MovedLoansMapFactory{/*canonicalize=*/false};
177 LivenessMap::Factory LivenessMapFactory{/*canonicalize=*/false};
178};
179
180/// Running the lifetime safety analysis and querying its results. It
181/// encapsulates the various dataflow analyses.
183public:
185 LifetimeSafetySemaHelper *SemaHelper,
186 const LifetimeSafetyOpts &LSOpts);
187
188 void run();
189
190 /// \note These are provided only for testing purposes.
192 return *LoanPropagation;
193 }
194 LiveOriginsAnalysis &getLiveOrigins() const { return *LiveOrigins; }
195 FactManager &getFactManager() { return *FactMgr; }
196
197private:
199 LifetimeSafetySemaHelper *SemaHelper;
200 const LifetimeSafetyOpts LSOpts;
201 LifetimeFactory Factory;
202 std::unique_ptr<FactManager> FactMgr;
203 std::unique_ptr<LiveOriginsAnalysis> LiveOrigins;
204 std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
205 std::unique_ptr<MovedLoansAnalysis> MovedLoans;
206};
207} // namespace internal
208} // namespace clang::lifetimes
209
210#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
This represents one expression.
Definition Expr.h:112
Represents a member of a struct/union/class.
Definition Decl.h:3191
Represents a parameter to a function.
Definition Decl.h:1817
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
Encodes a location in the source.
Represents a variable declaration or definition.
Definition Decl.h:932
Abstract interface for operations requiring Sema access.
virtual void reportInapplicableLifetimebound(const ParmVarDecl *PVD)
virtual void reportUseAfterInvalidation(const ParmVarDecl *PVD, const Expr *UseExpr, const Expr *InvalidationExpr)
llvm::PointerUnion< const Expr *, const FieldDecl *, const VarDecl * > EscapingTarget
virtual void suggestLifetimeboundToParmVar(WarningScope Scope, const ParmVarDecl *ParmToAnnotate, EscapingTarget Target)
virtual void reportDanglingGlobal(const Expr *IssueExpr, const VarDecl *DanglingGlobal, const Expr *MovedExpr, SourceLocation ExpiryLoc)
virtual void reportInvalidatedField(const ParmVarDecl *PVD, const FieldDecl *Field, const Expr *InvalidationExpr)
virtual void reportMisplacedLifetimebound(WarningScope Scope, const ParmVarDecl *PVDDef, const ParmVarDecl *PVDDecl)
virtual void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const VarDecl *EscapeGlobal)
virtual void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const FieldDecl *EscapeField)
virtual void reportDanglingField(const Expr *IssueExpr, const FieldDecl *Field, const Expr *MovedExpr, SourceLocation ExpiryLoc)
virtual void reportLifetimeboundViolation(const ParmVarDecl *ParmWithLifetimebound)
virtual void reportUseAfterScope(const Expr *IssueExpr, const Expr *UseExpr, const Expr *MovedExpr, SourceLocation FreeLoc, llvm::ArrayRef< const Expr * > ExprChain)
virtual void addLifetimeBoundToImplicitThis(const CXXMethodDecl *MD)
virtual void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const Expr *EscapeExpr)
virtual void reportMisplacedLifetimebound(WarningScope Scope, const CXXMethodDecl *FDef, const CXXMethodDecl *FDecl)
virtual void suggestLifetimeboundToImplicitThis(WarningScope Scope, const CXXMethodDecl *MD, const Expr *EscapeExpr)
virtual void reportLifetimeboundViolation(const CXXMethodDecl *MDWithLifetimebound)
virtual void reportUseAfterInvalidation(const Expr *IssueExpr, const Expr *UseExpr, const Expr *InvalidationExpr)
virtual void reportUseAfterReturn(const Expr *IssueExpr, const Expr *ReturnExpr, const Expr *MovedExpr)
virtual void reportInvalidatedGlobal(const ParmVarDecl *PVD, const VarDecl *Global, const Expr *InvalidationExpr)
virtual void reportInvalidatedGlobal(const Expr *IssueExpr, const VarDecl *Global, const Expr *InvalidationExpr)
virtual void reportInvalidatedField(const Expr *IssueExpr, const FieldDecl *Field, const Expr *InvalidationExpr)
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, const LifetimeSafetyOpts &LSOpts)
LoanPropagationAnalysis & getLoanPropagation() const
void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM, LifetimeSafetyStats &Stats)
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, const LifetimeSafetyOpts &Opts, LifetimeSafetyStats &Stats, bool CollectStats)
The main entry point for the analysis.
WarningScope
Enum to track functions visible across or within TU.
bool SuggestAnnotations
Whether to suggest lifetime annotations.
size_t MaxCFGBlocks
Maximum number of CFG blocks to analyze.
An object to hold the factories for immutable collections, ensuring that all created states share the...