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
27
28namespace clang::lifetimes {
29
30/// Enum to track the confidence level of a potential error.
31enum class Confidence : uint8_t {
33 Maybe, // Reported as a potential error (-Wlifetime-safety-strict)
34 Definite // Reported as a definite error (-Wlifetime-safety-permissive)
35};
36
38public:
40 virtual ~LifetimeSafetyReporter() = default;
41
42 virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
43 SourceLocation FreeLoc,
45};
46
47/// The main entry point for the analysis.
49 LifetimeSafetyReporter *Reporter);
50
51namespace internal {
52/// An object to hold the factories for immutable collections, ensuring
53/// that all created states share the same underlying memory management.
55 OriginLoanMap::Factory OriginMapFactory{/*canonicalize=*/false};
56 LoanSet::Factory LoanSetFactory{/*canonicalize=*/false};
57 LivenessMap::Factory LivenessMapFactory{/*canonicalize=*/false};
58};
59
60/// Running the lifetime safety analysis and querying its results. It
61/// encapsulates the various dataflow analyses.
63public:
65 LifetimeSafetyReporter *Reporter);
66
67 void run();
68
69 /// \note These are provided only for testing purposes.
71 return *LoanPropagation;
72 }
73 LiveOriginsAnalysis &getLiveOrigins() const { return *LiveOrigins; }
74 FactManager &getFactManager() { return FactMgr; }
75
76private:
78 LifetimeSafetyReporter *Reporter;
79 LifetimeFactory Factory;
80 FactManager FactMgr;
81 std::unique_ptr<LiveOriginsAnalysis> LiveOrigins;
82 std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
83};
84} // namespace internal
85} // namespace clang::lifetimes
86
87#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
Encodes a location in the source.
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr, SourceLocation FreeLoc, Confidence Confidence)
LoanPropagationAnalysis & getLoanPropagation() const
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
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...