Skip to content

Commit 5af65da

Browse files
committed
move dataflow to lib
1 parent 146a15c commit 5af65da

File tree

14 files changed

+417
-412
lines changed

14 files changed

+417
-412
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_CHECKER_H
1616

1717
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
18+
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
1819
#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
1920
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
20-
#include "clang/Analysis/Analyses/LifetimeSafety/Reporter.h"
2121

2222
namespace clang::lifetimes::internal {
2323

2424
/// Runs the lifetime checker, which detects use-after-free errors by
2525
/// examining loan expiration points and checking if any live origins hold
2626
/// the expired loan.
27-
void runLifetimeChecker(LoanPropagationAnalysis &LoanPropagation,
28-
LiveOriginAnalysis &LiveOrigins, FactManager &FactMgr,
29-
AnalysisDeclContext &ADC,
27+
void runLifetimeChecker(const LoanPropagation &LP, const LiveOrigins &LO,
28+
const FactManager &FactMgr, AnalysisDeclContext &ADC,
3029
LifetimeSafetyReporter *Reporter);
3130

3231
} // namespace clang::lifetimes::internal

clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
#include "clang/AST/DeclCXX.h"
1414

15-
namespace clang {
16-
namespace lifetimes {
15+
namespace clang ::lifetimes {
1716

1817
/// Returns the most recent declaration of the method to ensure all
1918
/// lifetime-bound attributes from redeclarations are considered.
@@ -38,7 +37,7 @@ bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD);
3837
/// lifetimebound, either due to an explicit lifetimebound attribute on the
3938
/// method or because it's a normal assignment operator.
4039
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
41-
} // namespace lifetimes
42-
} // namespace clang
40+
41+
} // namespace clang::lifetimes
4342

4443
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H

clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,29 @@
2121
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
2222
#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
2323
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
24-
#include "clang/Analysis/Analyses/LifetimeSafety/Reporter.h"
2524
#include "clang/Analysis/AnalysisDeclContext.h"
2625
#include "clang/Analysis/CFG.h"
2726
#include "llvm/ADT/StringMap.h"
2827

2928
namespace clang::lifetimes {
3029

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+
3147
/// The main entry point for the analysis.
3248
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC,
3349
LifetimeSafetyReporter *Reporter);
@@ -36,10 +52,10 @@ namespace internal {
3652
/// An object to hold the factories for immutable collections, ensuring
3753
/// that all created states share the same underlying memory management.
3854
struct LifetimeFactory {
39-
llvm::BumpPtrAllocator Allocator;
40-
OriginLoanMap::Factory OriginMapFactory{Allocator, /*canonicalize=*/false};
41-
LoanSet::Factory LoanSetFactory{Allocator, /*canonicalize=*/false};
42-
LivenessMap::Factory LivenessMapFactory{Allocator, /*canonicalize=*/false};
55+
// llvm::BumpPtrAllocator Allocator;
56+
OriginLoanMap::Factory OriginMapFactory{/*canonicalize=*/false};
57+
LoanSet::Factory LoanSetFactory{/*canonicalize=*/false};
58+
LivenessMap::Factory LivenessMapFactory{/*canonicalize=*/false};
4359
};
4460

4561
/// Running the lifetime safety analysis and querying its results. It
@@ -48,22 +64,21 @@ class LifetimeSafetyAnalysis {
4864
public:
4965
LifetimeSafetyAnalysis(AnalysisDeclContext &AC,
5066
LifetimeSafetyReporter *Reporter);
51-
~LifetimeSafetyAnalysis();
5267

5368
void run();
5469

5570
/// Returns the loan propagation analysis object.
5671
/// \note This is intended for testing only.
57-
LoanPropagationAnalysis &getLoanPropagationAnalysis() const {
58-
assert(LoanPropagation && "Analysis has not been run.");
59-
return *LoanPropagation;
72+
LoanPropagation &getLoanPropagation() const {
73+
assert(LP && "Analysis has not been run.");
74+
return *LP;
6075
}
6176

6277
/// Returns the live origin analysis object.
6378
/// \note This is intended for testing only.
64-
LiveOriginAnalysis &getLiveOriginAnalysis() const {
65-
assert(LiveOrigins && "Analysis has not been run.");
66-
return *LiveOrigins;
79+
LiveOrigins &getLiveOriginAnalysis() const {
80+
assert(LO && "Analysis has not been run.");
81+
return *LO;
6782
}
6883

6984
/// Returns the set of origins that are live at a specific program point,
@@ -101,8 +116,8 @@ class LifetimeSafetyAnalysis {
101116
LifetimeSafetyReporter *Reporter;
102117
LifetimeFactory Factory;
103118
FactManager FactMgr;
104-
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
105-
std::unique_ptr<LiveOriginAnalysis> LiveOrigins;
119+
std::unique_ptr<LiveOrigins> LO;
120+
std::unique_ptr<LoanPropagation> LP;
106121
};
107122
} // namespace internal
108123
} // namespace clang::lifetimes

clang/include/clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
2121
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
2222

23-
#include "clang/Analysis/Analyses/LifetimeSafety/Dataflow.h"
2423
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
2524
#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"
2625
#include "clang/Analysis/AnalysisDeclContext.h"
@@ -31,8 +30,6 @@
3130

3231
namespace clang::lifetimes::internal {
3332

34-
using OriginSet = llvm::ImmutableSet<OriginID>;
35-
3633
enum class LivenessKind : uint8_t {
3734
Dead, // Not alive
3835
Maybe, // Live on some path but not all paths (may-be-live)
@@ -45,6 +42,7 @@ struct LivenessInfo {
4542
/// multiple uses along different paths, this will point to the use appearing
4643
/// earlier in the translation unit.
4744
/// This is 'null' when the origin is not live.
45+
4846
const UseFact *CausingUseFact;
4947
/// The kind of liveness of the origin.
5048
/// `Must`: The origin is live on all control-flow paths from the current
@@ -74,63 +72,22 @@ struct LivenessInfo {
7472

7573
using LivenessMap = llvm::ImmutableMap<OriginID, LivenessInfo>;
7674

77-
/// The dataflow lattice for origin liveness analysis.
78-
/// It tracks which origins are live, why they're live (which UseFact),
79-
/// and the confidence level of that liveness.
80-
struct LivenessLattice {
81-
LivenessMap LiveOrigins;
82-
83-
LivenessLattice() : LiveOrigins(nullptr) {};
84-
85-
explicit LivenessLattice(LivenessMap L) : LiveOrigins(L) {}
86-
87-
bool operator==(const LivenessLattice &Other) const {
88-
return LiveOrigins == Other.LiveOrigins;
89-
}
90-
91-
bool operator!=(const LivenessLattice &Other) const {
92-
return !(*this == Other);
93-
}
94-
95-
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const;
96-
};
97-
98-
/// The analysis that tracks which origins are live, with granular information
99-
/// about the causing use fact and confidence level. This is a backward
100-
/// analysis.
101-
class LiveOriginAnalysis
102-
: public DataflowAnalysis<LiveOriginAnalysis, LivenessLattice,
103-
Direction::Backward> {
104-
75+
class LiveOrigins {
10576
public:
106-
LiveOriginAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
107-
LivenessMap::Factory &SF)
108-
: DataflowAnalysis(C, AC, F), FactMgr(F), Factory(SF) {}
109-
using DataflowAnalysis<LiveOriginAnalysis, Lattice,
110-
Direction::Backward>::transfer;
111-
112-
StringRef getAnalysisName() const { return "LiveOrigins"; }
113-
114-
Lattice getInitialState() { return Lattice(Factory.getEmptyMap()); }
115-
116-
Lattice join(Lattice L1, Lattice L2) const;
117-
118-
Lattice transfer(Lattice In, const UseFact &UF);
119-
Lattice transfer(Lattice In, const IssueFact &IF);
120-
Lattice transfer(Lattice In, const OriginFlowFact &OF);
121-
122-
LivenessMap getLiveOrigins(ProgramPoint P) const {
123-
return getState(P).LiveOrigins;
124-
}
77+
LiveOrigins(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
78+
LivenessMap::Factory &SF);
79+
~LiveOrigins();
12580

81+
LivenessMap getLiveOrigins(ProgramPoint P) const;
12682
// Dump liveness values on all test points in the program.
12783
void dump(llvm::raw_ostream &OS,
12884
llvm::StringMap<ProgramPoint> TestPoints) const;
12985

13086
private:
131-
FactManager &FactMgr;
132-
LivenessMap::Factory &Factory;
87+
class Impl;
88+
std::unique_ptr<Impl> PImpl;
13389
};
90+
13491
} // namespace clang::lifetimes::internal
13592

13693
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H

clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOAN_PROPAGATION_H
1616
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOAN_PROPAGATION_H
1717

18-
#include "clang/Analysis/Analyses/LifetimeSafety/Dataflow.h"
1918
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
2019
#include "clang/Analysis/AnalysisDeclContext.h"
2120
#include "clang/Analysis/CFG.h"
2221
#include "llvm/ADT/ImmutableMap.h"
2322
#include "llvm/ADT/ImmutableSet.h"
24-
#include "llvm/Support/Debug.h"
2523

2624
namespace clang::lifetimes::internal {
2725

@@ -31,65 +29,22 @@ namespace clang::lifetimes::internal {
3129
using LoanSet = llvm::ImmutableSet<LoanID>;
3230
using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>;
3331

34-
/// Represents the dataflow lattice for loan propagation.
35-
///
36-
/// This lattice tracks which loans each origin may hold at a given program
37-
/// point.The lattice has a finite height: An origin's loan set is bounded by
38-
/// the total number of loans in the function.
39-
/// TODO(opt): To reduce the lattice size, propagate origins of declarations,
40-
/// not expressions, because expressions are not visible across blocks.
41-
struct LoanPropagationLattice {
42-
/// The map from an origin to the set of loans it contains.
43-
OriginLoanMap Origins = OriginLoanMap(nullptr);
44-
45-
explicit LoanPropagationLattice(const OriginLoanMap &S) : Origins(S) {}
46-
LoanPropagationLattice() = default;
47-
48-
bool operator==(const LoanPropagationLattice &Other) const {
49-
return Origins == Other.Origins;
50-
}
51-
bool operator!=(const LoanPropagationLattice &Other) const {
52-
return !(*this == Other);
53-
}
54-
55-
void dump(llvm::raw_ostream &OS) const;
56-
};
57-
58-
class LoanPropagationAnalysis
59-
: public DataflowAnalysis<LoanPropagationAnalysis, LoanPropagationLattice,
60-
Direction::Forward> {
61-
OriginLoanMap::Factory &OriginLoanMapFactory;
62-
LoanSet::Factory &LoanSetFactory;
63-
32+
class LoanPropagation {
6433
public:
65-
LoanPropagationAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
66-
OriginLoanMap::Factory &OriginLoanMapFactory,
67-
LoanSet::Factory &LoanSetFactory)
68-
: DataflowAnalysis(C, AC, F), OriginLoanMapFactory(OriginLoanMapFactory),
69-
LoanSetFactory(LoanSetFactory) {}
70-
71-
using Base::transfer;
34+
LoanPropagation(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
35+
OriginLoanMap::Factory &OriginLoanMapFactory,
36+
LoanSet::Factory &LoanSetFactory);
37+
~LoanPropagation();
7238

73-
StringRef getAnalysisName() const { return "LoanPropagation"; }
74-
75-
Lattice getInitialState() { return Lattice{}; }
76-
77-
Lattice join(Lattice A, Lattice B);
78-
79-
Lattice transfer(Lattice In, const IssueFact &F);
80-
Lattice transfer(Lattice In, const OriginFlowFact &F);
81-
82-
LoanSet getLoans(OriginID OID, ProgramPoint P) const {
83-
return getLoans(getState(P), OID);
84-
}
39+
LoanSet getLoans(OriginID OID, ProgramPoint P) const;
8540

8641
private:
87-
LoanSet getLoans(Lattice L, OriginID OID) const {
88-
if (auto *Loans = L.Origins.lookup(OID))
89-
return *Loans;
90-
return LoanSetFactory.getEmptySet();
91-
}
42+
class Impl;
43+
44+
public:
45+
std::unique_ptr<Impl> PImpl;
9246
};
47+
9348
} // namespace clang::lifetimes::internal
9449

9550
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOAN_PROPAGATION_H

clang/include/clang/Analysis/Analyses/LifetimeSafety/Origins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace clang::lifetimes::internal {
2222

2323
using OriginID = utils::ID<struct OriginTag>;
24+
2425
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
2526
return OS << ID.Value;
2627
}

clang/include/clang/Analysis/Analyses/LifetimeSafety/Reporter.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)