2626#include " clang/Analysis/AnalysisDeclContext.h"
2727#include " clang/Analysis/CFG.h"
2828#include " llvm/ADT/FoldingSet.h"
29- #include " llvm/ADT/ImmutableMap.h"
30- #include " llvm/ADT/ImmutableSet.h"
3129#include " llvm/Support/Debug.h"
3230#include " llvm/Support/ErrorHandling.h"
3331#include " llvm/Support/TimeProfiler.h"
3735namespace clang ::lifetimes {
3836namespace internal {
3937
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-
5338// We need this here for unique_ptr with forward declared class.
5439LifetimeSafetyAnalysis::~LifetimeSafetyAnalysis () = default ;
5540
5641LifetimeSafetyAnalysis::LifetimeSafetyAnalysis (AnalysisDeclContext &AC,
5742 LifetimeSafetyReporter *Reporter)
58- : AC(AC), Reporter(Reporter), Factory(std::make_unique<LifetimeFactory>()),
59- FactMgr (std::make_unique<FactManager>()) {}
43+ : AC(AC), Reporter(Reporter) {}
6044
6145void LifetimeSafetyAnalysis::run () {
6246 llvm::TimeTraceScope TimeProfile (" LifetimeSafetyAnalysis" );
@@ -65,9 +49,9 @@ void LifetimeSafetyAnalysis::run() {
6549 DEBUG_WITH_TYPE (" PrintCFG" , Cfg.dump (AC.getASTContext ().getLangOpts (),
6650 /* ShowColors=*/ true ));
6751
68- FactsGenerator FactGen (* FactMgr, AC);
52+ FactsGenerator FactGen (FactMgr, AC);
6953 FactGen.run ();
70- DEBUG_WITH_TYPE (" LifetimeFacts" , FactMgr-> dump (Cfg, AC));
54+ DEBUG_WITH_TYPE (" LifetimeFacts" , FactMgr. dump (Cfg, AC));
7155
7256 // / TODO(opt): Consider optimizing individual blocks before running the
7357 // / dataflow analysis.
@@ -81,16 +65,16 @@ void LifetimeSafetyAnalysis::run() {
8165 // / 3. Collapse ExpireFacts belonging to same source location into a single
8266 // / Fact.
8367 LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
84- Cfg, AC, * FactMgr, Factory-> OriginMapFactory , Factory-> LoanSetFactory );
68+ Cfg, AC, FactMgr, Factory. OriginMapFactory , Factory. LoanSetFactory );
8569 LoanPropagation->run ();
8670
8771 LiveOrigins = std::make_unique<LiveOriginAnalysis>(
88- Cfg, AC, * FactMgr, Factory-> LivenessMapFactory );
72+ Cfg, AC, FactMgr, Factory. LivenessMapFactory );
8973 LiveOrigins->run ();
9074 DEBUG_WITH_TYPE (" LiveOrigins" ,
9175 LiveOrigins->dump (llvm::dbgs (), getTestPoints ()));
9276
93- runLifetimeChecker (*LoanPropagation, *LiveOrigins, * FactMgr, AC, Reporter);
77+ runLifetimeChecker (*LoanPropagation, *LiveOrigins, FactMgr, AC, Reporter);
9478}
9579
9680LoanSet LifetimeSafetyAnalysis::getLoansAtPoint (OriginID OID,
@@ -101,18 +85,16 @@ LoanSet LifetimeSafetyAnalysis::getLoansAtPoint(OriginID OID,
10185
10286std::optional<OriginID>
10387LifetimeSafetyAnalysis::getOriginIDForDecl (const ValueDecl *D) const {
104- assert (FactMgr && " FactManager not initialized" );
10588 // This assumes the OriginManager's `get` can find an existing origin.
10689 // We might need a `find` method on OriginManager to avoid `getOrCreate` logic
10790 // 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);
10992}
11093
11194std::vector<LoanID>
11295LifetimeSafetyAnalysis::getLoanIDForVar (const VarDecl *VD) const {
113- assert (FactMgr && " FactManager not initialized" );
11496 std::vector<LoanID> Result;
115- for (const Loan &L : FactMgr-> getLoanMgr ().getLoans ())
97+ for (const Loan &L : FactMgr. getLoanMgr ().getLoans ())
11698 if (L.Path .D == VD)
11799 Result.push_back (L.ID );
118100 return Result;
@@ -128,10 +110,9 @@ LifetimeSafetyAnalysis::getLiveOriginsAtPoint(ProgramPoint PP) const {
128110}
129111
130112llvm::StringMap<ProgramPoint> LifetimeSafetyAnalysis::getTestPoints () const {
131- assert (FactMgr && " FactManager not initialized" );
132113 llvm::StringMap<ProgramPoint> AnnotationToPointMap;
133114 for (const CFGBlock *Block : *AC.getCFG ()) {
134- for (const Fact *F : FactMgr-> getFacts (Block)) {
115+ for (const Fact *F : FactMgr. getFacts (Block)) {
135116 if (const auto *TPF = F->getAs <TestPointFact>()) {
136117 StringRef PointName = TPF->getAnnotation ();
137118 assert (AnnotationToPointMap.find (PointName) ==
0 commit comments