Skip to content

Commit 98bb91d

Browse files
committed
Apply changes from code browser
Apply changes from code browser
1 parent 52464b3 commit 98bb91d

File tree

4 files changed

+26
-38
lines changed

4 files changed

+26
-38
lines changed

.github/new-prs-labeler.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,8 @@ clang:openmp:
10961096
- llvm/test/Transforms/OpenMP/**
10971097

10981098
clang:temporal-safety:
1099-
- clang/include/clang/Analysis/Analyses/LifetimeSafety*
1100-
- clang/lib/Analysis/LifetimeSafety*
1099+
- clang/include/clang/Analysis/Analyses/LifetimeSafety/**
1100+
- clang/lib/Analysis/LifetimeSafety/**
11011101
- clang/unittests/Analysis/LifetimeSafety*
11021102
- clang/test/Sema/*lifetime-safety*
11031103
- clang/test/Sema/*lifetime-analysis*

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- Facts.h - Lifetime Analysis Facts and Fact Generation ---*- C++ -*-===//
1+
//===- Facts.h - Lifetime Analysis Facts and Fact Manager ------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// This file defines Facts, which are atomic lifetime-relevant events (such as
10-
// loan issuance, loan expiration, origin flow, and use), and the FactGenerator,
11-
// which traverses the AST to generate these facts from CFG statements.
10+
// loan issuance, loan expiration, origin flow, and use), and the FactManager,
11+
// which manages the storage and retrieval of facts for each CFG block.
1212
//
1313
//===----------------------------------------------------------------------===//
1414
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H
@@ -255,7 +255,9 @@ class FactManager {
255255
}
256256

257257
LoanManager &getLoanMgr() { return LoanMgr; }
258+
const LoanManager &getLoanMgr() const { return LoanMgr; }
258259
OriginManager &getOriginMgr() { return OriginMgr; }
260+
const OriginManager &getOriginMgr() const { return OriginMgr; }
259261

260262
private:
261263
LoanManager LoanMgr;

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "clang/Analysis/AnalysisDeclContext.h"
2626
#include "clang/Analysis/CFG.h"
2727
#include "llvm/ADT/StringMap.h"
28-
#include <memory>
2928

3029
namespace clang::lifetimes {
3130

@@ -34,8 +33,14 @@ void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC,
3433
LifetimeSafetyReporter *Reporter);
3534

3635
namespace internal {
37-
// Forward declarations of internal types.
38-
struct LifetimeFactory;
36+
/// An object to hold the factories for immutable collections, ensuring
37+
/// that all created states share the same underlying memory management.
38+
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};
43+
};
3944

4045
/// Running the lifetime safety analysis and querying its results. It
4146
/// encapsulates the various dataflow analyses.
@@ -83,8 +88,8 @@ class LifetimeSafetyAnalysis {
8388
private:
8489
AnalysisDeclContext &AC;
8590
LifetimeSafetyReporter *Reporter;
86-
std::unique_ptr<LifetimeFactory> Factory;
87-
std::unique_ptr<FactManager> FactMgr;
91+
LifetimeFactory Factory;
92+
FactManager FactMgr;
8893
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
8994
std::unique_ptr<LiveOriginAnalysis> LiveOrigins;
9095
};

clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
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"
@@ -37,26 +35,12 @@
3735
namespace clang::lifetimes {
3836
namespace 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.
5439
LifetimeSafetyAnalysis::~LifetimeSafetyAnalysis() = default;
5540

5641
LifetimeSafetyAnalysis::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

6145
void 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

9680
LoanSet LifetimeSafetyAnalysis::getLoansAtPoint(OriginID OID,
@@ -101,18 +85,16 @@ LoanSet LifetimeSafetyAnalysis::getLoansAtPoint(OriginID OID,
10185

10286
std::optional<OriginID>
10387
LifetimeSafetyAnalysis::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

11194
std::vector<LoanID>
11295
LifetimeSafetyAnalysis::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

130112
llvm::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

Comments
 (0)