clang
22.0.0git
include
clang
Analysis
Analyses
LifetimeSafety
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/Analysis/Analyses/LifetimeSafety/Facts.h
"
24
#include "
clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h
"
25
#include "
clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h
"
26
#include "
clang/Analysis/AnalysisDeclContext.h
"
27
28
namespace
clang::lifetimes
{
29
30
/// Enum to track the confidence level of a potential error.
31
enum class
Confidence
: uint8_t {
32
None
,
33
Maybe
,
// Reported as a potential error (-Wlifetime-safety-strict)
34
Definite
// Reported as a definite error (-Wlifetime-safety-permissive)
35
};
36
37
class
LifetimeSafetyReporter
{
38
public
:
39
LifetimeSafetyReporter
() =
default
;
40
virtual
~LifetimeSafetyReporter
() =
default
;
41
42
virtual
void
reportUseAfterFree
(
const
Expr
*IssueExpr,
const
Expr
*UseExpr,
43
SourceLocation
FreeLoc,
44
Confidence
Confidence
) {}
45
};
46
47
/// The main entry point for the analysis.
48
void
runLifetimeSafetyAnalysis
(
AnalysisDeclContext
&AC,
49
LifetimeSafetyReporter *Reporter);
50
51
namespace
internal
{
52
/// An object to hold the factories for immutable collections, ensuring
53
/// that all created states share the same underlying memory management.
54
struct
LifetimeFactory
{
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.
62
class
LifetimeSafetyAnalysis
{
63
public
:
64
LifetimeSafetyAnalysis
(
AnalysisDeclContext
&AC,
65
LifetimeSafetyReporter
*Reporter);
66
67
void
run
();
68
69
/// \note These are provided only for testing purposes.
70
LoanPropagationAnalysis
&
getLoanPropagation
()
const
{
71
return
*LoanPropagation;
72
}
73
LiveOriginsAnalysis
&
getLiveOrigins
()
const
{
return
*LiveOrigins; }
74
FactManager
&
getFactManager
() {
return
FactMgr; }
75
76
private
:
77
AnalysisDeclContext
∾
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
AnalysisDeclContext.h
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Facts.h
LiveOrigins.h
LoanPropagation.h
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Definition
AnalysisDeclContext.h:72
clang::Expr
This represents one expression.
Definition
Expr.h:112
clang::SourceLocation
Encodes a location in the source.
Definition
SourceLocation.h:90
clang::lifetimes::LifetimeSafetyReporter
Definition
LifetimeSafety.h:37
clang::lifetimes::LifetimeSafetyReporter::reportUseAfterFree
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr, SourceLocation FreeLoc, Confidence Confidence)
Definition
LifetimeSafety.h:42
clang::lifetimes::LifetimeSafetyReporter::~LifetimeSafetyReporter
virtual ~LifetimeSafetyReporter()=default
clang::lifetimes::LifetimeSafetyReporter::LifetimeSafetyReporter
LifetimeSafetyReporter()=default
clang::lifetimes::internal::FactManager
Definition
Facts.h:185
clang::lifetimes::internal::LifetimeSafetyAnalysis::getFactManager
FactManager & getFactManager()
Definition
LifetimeSafety.h:74
clang::lifetimes::internal::LifetimeSafetyAnalysis::run
void run()
Definition
LifetimeSafety.cpp:38
clang::lifetimes::internal::LifetimeSafetyAnalysis::getLoanPropagation
LoanPropagationAnalysis & getLoanPropagation() const
Definition
LifetimeSafety.h:70
clang::lifetimes::internal::LifetimeSafetyAnalysis::getLiveOrigins
LiveOriginsAnalysis & getLiveOrigins() const
Definition
LifetimeSafety.h:73
clang::lifetimes::internal::LifetimeSafetyAnalysis::LifetimeSafetyAnalysis
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
Definition
LifetimeSafety.cpp:34
clang::lifetimes::internal::LiveOriginsAnalysis
Definition
LiveOrigins.h:76
clang::lifetimes::internal::LoanPropagationAnalysis
Definition
LoanPropagation.h:32
clang::internal
Definition
IntervalPartition.h:67
clang::lifetimes
Definition
Checker.h:23
clang::lifetimes::runLifetimeSafetyAnalysis
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
The main entry point for the analysis.
Definition
LifetimeSafety.cpp:72
clang::lifetimes::Confidence
Confidence
Enum to track the confidence level of a potential error.
Definition
LifetimeSafety.h:31
clang::lifetimes::Confidence::Definite
@ Definite
Definition
LifetimeSafety.h:34
clang::lifetimes::Confidence::None
@ None
Definition
LifetimeSafety.h:32
clang::lifetimes::Confidence::Maybe
@ Maybe
Definition
LifetimeSafety.h:33
clang::lifetimes::internal::LifetimeFactory
An object to hold the factories for immutable collections, ensuring that all created states share the...
Definition
LifetimeSafety.h:54
clang::lifetimes::internal::LifetimeFactory::LivenessMapFactory
LivenessMap::Factory LivenessMapFactory
Definition
LifetimeSafety.h:57
clang::lifetimes::internal::LifetimeFactory::OriginMapFactory
OriginLoanMap::Factory OriginMapFactory
Definition
LifetimeSafety.h:55
clang::lifetimes::internal::LifetimeFactory::LoanSetFactory
LoanSet::Factory LoanSetFactory
Definition
LifetimeSafety.h:56
Generated on
for clang by
1.14.0