26
26
#include " clang/Analysis/AnalysisDeclContext.h"
27
27
#include " clang/Analysis/CFG.h"
28
28
#include " llvm/ADT/FoldingSet.h"
29
- #include " llvm/ADT/ImmutableMap.h"
30
- #include " llvm/ADT/ImmutableSet.h"
31
29
#include " llvm/Support/Debug.h"
32
30
#include " llvm/Support/ErrorHandling.h"
33
31
#include " llvm/Support/TimeProfiler.h"
37
35
namespace clang ::lifetimes {
38
36
namespace internal {
39
37
40
- // ========================================================================= //
41
- // LifetimeSafetyAnalysis Class Implementation
42
- // ========================================================================= //
43
-
44
- // / An object to hold the factories for immutable collections, ensuring
45
- // / that all created states share the same underlying memory management.
46
- struct LifetimeFactory {
47
- llvm::BumpPtrAllocator Allocator;
48
- OriginLoanMap::Factory OriginMapFactory{Allocator, /* canonicalize=*/ false };
49
- LoanSet::Factory LoanSetFactory{Allocator, /* canonicalize=*/ false };
50
- LivenessMap::Factory LivenessMapFactory{Allocator, /* canonicalize=*/ false };
51
- };
52
-
53
38
// We need this here for unique_ptr with forward declared class.
54
39
LifetimeSafetyAnalysis::~LifetimeSafetyAnalysis () = default ;
55
40
56
41
LifetimeSafetyAnalysis::LifetimeSafetyAnalysis (AnalysisDeclContext &AC,
57
42
LifetimeSafetyReporter *Reporter)
58
- : AC(AC), Reporter(Reporter), Factory(std::make_unique<LifetimeFactory>()),
59
- FactMgr (std::make_unique<FactManager>()) {}
43
+ : AC(AC), Reporter(Reporter) {}
60
44
61
45
void LifetimeSafetyAnalysis::run () {
62
46
llvm::TimeTraceScope TimeProfile (" LifetimeSafetyAnalysis" );
@@ -65,9 +49,9 @@ void LifetimeSafetyAnalysis::run() {
65
49
DEBUG_WITH_TYPE (" PrintCFG" , Cfg.dump (AC.getASTContext ().getLangOpts (),
66
50
/* ShowColors=*/ true ));
67
51
68
- FactsGenerator FactGen (* FactMgr, AC);
52
+ FactsGenerator FactGen (FactMgr, AC);
69
53
FactGen.run ();
70
- DEBUG_WITH_TYPE (" LifetimeFacts" , FactMgr-> dump (Cfg, AC));
54
+ DEBUG_WITH_TYPE (" LifetimeFacts" , FactMgr. dump (Cfg, AC));
71
55
72
56
// / TODO(opt): Consider optimizing individual blocks before running the
73
57
// / dataflow analysis.
@@ -81,16 +65,16 @@ void LifetimeSafetyAnalysis::run() {
81
65
// / 3. Collapse ExpireFacts belonging to same source location into a single
82
66
// / Fact.
83
67
LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
84
- Cfg, AC, * FactMgr, Factory-> OriginMapFactory , Factory-> LoanSetFactory );
68
+ Cfg, AC, FactMgr, Factory. OriginMapFactory , Factory. LoanSetFactory );
85
69
LoanPropagation->run ();
86
70
87
71
LiveOrigins = std::make_unique<LiveOriginAnalysis>(
88
- Cfg, AC, * FactMgr, Factory-> LivenessMapFactory );
72
+ Cfg, AC, FactMgr, Factory. LivenessMapFactory );
89
73
LiveOrigins->run ();
90
74
DEBUG_WITH_TYPE (" LiveOrigins" ,
91
75
LiveOrigins->dump (llvm::dbgs (), getTestPoints ()));
92
76
93
- runLifetimeChecker (*LoanPropagation, *LiveOrigins, * FactMgr, AC, Reporter);
77
+ runLifetimeChecker (*LoanPropagation, *LiveOrigins, FactMgr, AC, Reporter);
94
78
}
95
79
96
80
LoanSet LifetimeSafetyAnalysis::getLoansAtPoint (OriginID OID,
@@ -101,18 +85,16 @@ LoanSet LifetimeSafetyAnalysis::getLoansAtPoint(OriginID OID,
101
85
102
86
std::optional<OriginID>
103
87
LifetimeSafetyAnalysis::getOriginIDForDecl (const ValueDecl *D) const {
104
- assert (FactMgr && " FactManager not initialized" );
105
88
// This assumes the OriginManager's `get` can find an existing origin.
106
89
// We might need a `find` method on OriginManager to avoid `getOrCreate` logic
107
90
// in a const-query context if that becomes an issue.
108
- return FactMgr-> getOriginMgr ().get (*D);
91
+ return const_cast <OriginManager &>(FactMgr. getOriginMgr () ).get (*D);
109
92
}
110
93
111
94
std::vector<LoanID>
112
95
LifetimeSafetyAnalysis::getLoanIDForVar (const VarDecl *VD) const {
113
- assert (FactMgr && " FactManager not initialized" );
114
96
std::vector<LoanID> Result;
115
- for (const Loan &L : FactMgr-> getLoanMgr ().getLoans ())
97
+ for (const Loan &L : FactMgr. getLoanMgr ().getLoans ())
116
98
if (L.Path .D == VD)
117
99
Result.push_back (L.ID );
118
100
return Result;
@@ -128,10 +110,9 @@ LifetimeSafetyAnalysis::getLiveOriginsAtPoint(ProgramPoint PP) const {
128
110
}
129
111
130
112
llvm::StringMap<ProgramPoint> LifetimeSafetyAnalysis::getTestPoints () const {
131
- assert (FactMgr && " FactManager not initialized" );
132
113
llvm::StringMap<ProgramPoint> AnnotationToPointMap;
133
114
for (const CFGBlock *Block : *AC.getCFG ()) {
134
- for (const Fact *F : FactMgr-> getFacts (Block)) {
115
+ for (const Fact *F : FactMgr. getFacts (Block)) {
135
116
if (const auto *TPF = F->getAs <TestPointFact>()) {
136
117
StringRef PointName = TPF->getAnnotation ();
137
118
assert (AnnotationToPointMap.find (PointName) ==
0 commit comments