clang 22.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
29
30namespace clang::lifetimes {
31
32/// Enum to track the confidence level of a potential error.
33enum class Confidence : uint8_t {
35 Maybe, // Reported as a potential error (-Wlifetime-safety-strict)
36 Definite // Reported as a definite error (-Wlifetime-safety-permissive)
37};
38
39/// Enum to track functions visible across or within TU.
40enum class SuggestionScope {
41 CrossTU, // For suggestions on declarations visible across Translation Units.
42 IntraTU // For suggestions on definitions local to a Translation Unit.
43};
44
46public:
48 virtual ~LifetimeSafetyReporter() = default;
49
50 virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
51 SourceLocation FreeLoc,
53
54 virtual void reportUseAfterReturn(const Expr *IssueExpr,
55 const Expr *EscapeExpr,
56 SourceLocation ExpiryLoc,
58
59 // Suggests lifetime bound annotations for function paramters
61 const ParmVarDecl *ParmToAnnotate,
62 const Expr *EscapeExpr) {}
63};
64
65/// The main entry point for the analysis.
67 LifetimeSafetyReporter *Reporter,
68 LifetimeSafetyStats &Stats, bool CollectStats);
69
70namespace internal {
71
72void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM,
73 LifetimeSafetyStats &Stats);
74
75/// An object to hold the factories for immutable collections, ensuring
76/// that all created states share the same underlying memory management.
78 OriginLoanMap::Factory OriginMapFactory{/*canonicalize=*/false};
79 LoanSet::Factory LoanSetFactory{/*canonicalize=*/false};
80 LivenessMap::Factory LivenessMapFactory{/*canonicalize=*/false};
81};
82
83/// Running the lifetime safety analysis and querying its results. It
84/// encapsulates the various dataflow analyses.
86public:
88 LifetimeSafetyReporter *Reporter);
89
90 void run();
91
92 /// \note These are provided only for testing purposes.
94 return *LoanPropagation;
95 }
96 LiveOriginsAnalysis &getLiveOrigins() const { return *LiveOrigins; }
97 FactManager &getFactManager() { return *FactMgr; }
98
99private:
101 LifetimeSafetyReporter *Reporter;
102 LifetimeFactory Factory;
103 std::unique_ptr<FactManager> FactMgr;
104 std::unique_ptr<LiveOriginsAnalysis> LiveOrigins;
105 std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
106};
107} // namespace internal
108} // namespace clang::lifetimes
109
110#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.
This represents one expression.
Definition Expr.h:112
Represents a parameter to a function.
Definition Decl.h:1790
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.
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr, SourceLocation FreeLoc, Confidence Confidence)
virtual void suggestAnnotation(SuggestionScope Scope, const ParmVarDecl *ParmToAnnotate, const Expr *EscapeExpr)
virtual void reportUseAfterReturn(const Expr *IssueExpr, const Expr *EscapeExpr, SourceLocation ExpiryLoc, Confidence Confidence)
LoanPropagationAnalysis & getLoanPropagation() const
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM, LifetimeSafetyStats &Stats)
SuggestionScope
Enum to track functions visible across or within TU.
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter, LifetimeSafetyStats &Stats, bool CollectStats)
The main entry point for the analysis.
Confidence
Enum to track the confidence level of a potential error.
An object to hold the factories for immutable collections, ensuring that all created states share the...