Skip to content

Commit c6e7bf0

Browse files
committed
more useful names and anonymous namespaces
1 parent 5af65da commit c6e7bf0

File tree

14 files changed

+195
-228
lines changed

14 files changed

+195
-228
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ namespace clang::lifetimes::internal {
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(const LoanPropagation &LP, const LiveOrigins &LO,
27+
void runLifetimeChecker(const LoanPropagationAnalysis &LoanPropagation,
28+
const LiveOriginsAnalysis &LiveOrigins,
2829
const FactManager &FactMgr, AnalysisDeclContext &ADC,
2930
LifetimeSafetyReporter *Reporter);
3031

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ class FactManager {
204204

205205
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const;
206206

207+
/// Retrieves program points that were specially marked in the source code
208+
/// for testing.
209+
///
210+
/// The analysis recognizes special function calls of the form
211+
/// `void("__lifetime_test_point_<name>")` as test points. This method returns
212+
/// a map from the annotation string (<name>) to the corresponding
213+
/// `ProgramPoint`. This allows test harnesses to query the analysis state at
214+
/// user-defined locations in the code.
215+
/// \note This is intended for testing only.
216+
llvm::StringMap<ProgramPoint> getTestPoints() const;
217+
207218
LoanManager &getLoanMgr() { return LoanMgr; }
208219
const LoanManager &getLoanMgr() const { return LoanMgr; }
209220
OriginManager &getOriginMgr() { return OriginMgr; }

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

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
2323
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
2424
#include "clang/Analysis/AnalysisDeclContext.h"
25-
#include "clang/Analysis/CFG.h"
26-
#include "llvm/ADT/StringMap.h"
2725

2826
namespace clang::lifetimes {
2927

@@ -52,7 +50,6 @@ namespace internal {
5250
/// An object to hold the factories for immutable collections, ensuring
5351
/// that all created states share the same underlying memory management.
5452
struct LifetimeFactory {
55-
// llvm::BumpPtrAllocator Allocator;
5653
OriginLoanMap::Factory OriginMapFactory{/*canonicalize=*/false};
5754
LoanSet::Factory LoanSetFactory{/*canonicalize=*/false};
5855
LivenessMap::Factory LivenessMapFactory{/*canonicalize=*/false};
@@ -67,57 +64,20 @@ class LifetimeSafetyAnalysis {
6764

6865
void run();
6966

70-
/// Returns the loan propagation analysis object.
71-
/// \note This is intended for testing only.
72-
LoanPropagation &getLoanPropagation() const {
73-
assert(LP && "Analysis has not been run.");
74-
return *LP;
67+
/// \note These are provided only for testing purposes.
68+
LoanPropagationAnalysis &getLoanPropagation() const {
69+
return *LoanPropagation;
7570
}
76-
77-
/// Returns the live origin analysis object.
78-
/// \note This is intended for testing only.
79-
LiveOrigins &getLiveOriginAnalysis() const {
80-
assert(LO && "Analysis has not been run.");
81-
return *LO;
82-
}
83-
84-
/// Returns the set of origins that are live at a specific program point,
85-
/// along with the confidence level of their liveness.
86-
///
87-
/// An origin is considered live if there are potential future uses of that
88-
/// origin after the given program point. The confidence level indicates
89-
/// whether the origin is definitely live (Definite) due to being domintated
90-
/// by a set of uses or only possibly live (Maybe) only on some but not all
91-
/// control flow paths.
92-
std::vector<std::pair<OriginID, LivenessKind>>
93-
getLiveOriginsAtPoint(ProgramPoint PP) const;
94-
95-
/// Finds the OriginID for a given declaration.
96-
/// Returns a null optional if not found.
97-
std::optional<OriginID> getOriginIDForDecl(const ValueDecl *D) const;
98-
99-
/// Finds the LoanID's for the loan created with the specific variable as
100-
/// their Path.
101-
std::vector<LoanID> getLoanIDForVar(const VarDecl *VD) const;
102-
103-
/// Retrieves program points that were specially marked in the source code
104-
/// for testing.
105-
///
106-
/// The analysis recognizes special function calls of the form
107-
/// `void("__lifetime_test_point_<name>")` as test points. This method returns
108-
/// a map from the annotation string (<name>) to the corresponding
109-
/// `ProgramPoint`. This allows test harnesses to query the analysis state at
110-
/// user-defined locations in the code.
111-
/// \note This is intended for testing only.
112-
llvm::StringMap<ProgramPoint> getTestPoints() const;
71+
LiveOriginsAnalysis &getLiveOrigins() const { return *LiveOrigins; }
72+
FactManager &getFactManager() { return FactMgr; }
11373

11474
private:
11575
AnalysisDeclContext &AC;
11676
LifetimeSafetyReporter *Reporter;
11777
LifetimeFactory Factory;
11878
FactManager FactMgr;
119-
std::unique_ptr<LiveOrigins> LO;
120-
std::unique_ptr<LoanPropagation> LP;
79+
std::unique_ptr<LiveOriginsAnalysis> LiveOrigins;
80+
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation;
12181
};
12282
} // namespace internal
12383
} // namespace clang::lifetimes

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ struct LivenessInfo {
7272

7373
using LivenessMap = llvm::ImmutableMap<OriginID, LivenessInfo>;
7474

75-
class LiveOrigins {
75+
class LiveOriginsAnalysis {
7676
public:
77-
LiveOrigins(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
78-
LivenessMap::Factory &SF);
79-
~LiveOrigins();
77+
LiveOriginsAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
78+
LivenessMap::Factory &SF);
79+
~LiveOriginsAnalysis();
80+
81+
/// Returns the set of origins that are live at a specific program point,
82+
/// along with the confidence level of their liveness.
83+
LivenessMap getLiveOriginsAt(ProgramPoint P) const;
8084

81-
LivenessMap getLiveOrigins(ProgramPoint P) const;
8285
// Dump liveness values on all test points in the program.
8386
void dump(llvm::raw_ostream &OS,
8487
llvm::StringMap<ProgramPoint> TestPoints) const;

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ namespace clang::lifetimes::internal {
2929
using LoanSet = llvm::ImmutableSet<LoanID>;
3030
using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>;
3131

32-
class LoanPropagation {
32+
class LoanPropagationAnalysis {
3333
public:
34-
LoanPropagation(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
35-
OriginLoanMap::Factory &OriginLoanMapFactory,
36-
LoanSet::Factory &LoanSetFactory);
37-
~LoanPropagation();
34+
LoanPropagationAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
35+
OriginLoanMap::Factory &OriginLoanMapFactory,
36+
LoanSet::Factory &LoanSetFactory);
37+
~LoanPropagationAnalysis();
3838

3939
LoanSet getLoans(OriginID OID, ProgramPoint P) const;
4040

4141
private:
4242
class Impl;
43-
44-
public:
4543
std::unique_ptr<Impl> PImpl;
4644
};
4745

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

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,70 +58,21 @@ class OriginManager {
5858
public:
5959
OriginManager() = default;
6060

61-
Origin &addOrigin(OriginID ID, const clang::ValueDecl &D) {
62-
AllOrigins.emplace_back(ID, &D);
63-
return AllOrigins.back();
64-
}
65-
Origin &addOrigin(OriginID ID, const clang::Expr &E) {
66-
AllOrigins.emplace_back(ID, &E);
67-
return AllOrigins.back();
68-
}
61+
Origin &addOrigin(OriginID ID, const clang::ValueDecl &D);
62+
Origin &addOrigin(OriginID ID, const clang::Expr &E);
6963

7064
// TODO: Mark this method as const once we remove the call to getOrCreate.
71-
OriginID get(const Expr &E) {
72-
auto It = ExprToOriginID.find(&E);
73-
if (It != ExprToOriginID.end())
74-
return It->second;
75-
// If the expression itself has no specific origin, and it's a reference
76-
// to a declaration, its origin is that of the declaration it refers to.
77-
// For pointer types, where we don't pre-emptively create an origin for the
78-
// DeclRefExpr itself.
79-
if (const auto *DRE = dyn_cast<DeclRefExpr>(&E))
80-
return get(*DRE->getDecl());
81-
// TODO: This should be an assert(It != ExprToOriginID.end()). The current
82-
// implementation falls back to getOrCreate to avoid crashing on
83-
// yet-unhandled pointer expressions, creating an empty origin for them.
84-
return getOrCreate(E);
85-
}
65+
OriginID get(const Expr &E);
8666

87-
OriginID get(const ValueDecl &D) {
88-
auto It = DeclToOriginID.find(&D);
89-
// TODO: This should be an assert(It != DeclToOriginID.end()). The current
90-
// implementation falls back to getOrCreate to avoid crashing on
91-
// yet-unhandled pointer expressions, creating an empty origin for them.
92-
if (It == DeclToOriginID.end())
93-
return getOrCreate(D);
67+
OriginID get(const ValueDecl &D);
9468

95-
return It->second;
96-
}
69+
OriginID getOrCreate(const Expr &E);
9770

98-
OriginID getOrCreate(const Expr &E) {
99-
auto It = ExprToOriginID.find(&E);
100-
if (It != ExprToOriginID.end())
101-
return It->second;
102-
103-
OriginID NewID = getNextOriginID();
104-
addOrigin(NewID, E);
105-
ExprToOriginID[&E] = NewID;
106-
return NewID;
107-
}
108-
109-
const Origin &getOrigin(OriginID ID) const {
110-
assert(ID.Value < AllOrigins.size());
111-
return AllOrigins[ID.Value];
112-
}
71+
const Origin &getOrigin(OriginID ID) const;
11372

11473
llvm::ArrayRef<Origin> getOrigins() const { return AllOrigins; }
11574

116-
OriginID getOrCreate(const ValueDecl &D) {
117-
auto It = DeclToOriginID.find(&D);
118-
if (It != DeclToOriginID.end())
119-
return It->second;
120-
OriginID NewID = getNextOriginID();
121-
addOrigin(NewID, D);
122-
DeclToOriginID[&D] = NewID;
123-
return NewID;
124-
}
75+
OriginID getOrCreate(const ValueDecl &D);
12576

12677
void dump(OriginID OID, llvm::raw_ostream &OS) const;
12778

clang/lib/Analysis/LifetimeSafety/Checker.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@
2626

2727
namespace clang::lifetimes::internal {
2828

29-
/// Struct to store the complete context for a potential lifetime violation.
30-
struct PendingWarning {
31-
SourceLocation ExpiryLoc; // Where the loan expired.
32-
const Expr *UseExpr; // Where the origin holding this loan was used.
33-
Confidence ConfidenceLevel;
34-
};
35-
3629
static Confidence livenessKindToConfidence(LivenessKind K) {
3730
switch (K) {
3831
case LivenessKind::Must:
@@ -45,22 +38,29 @@ static Confidence livenessKindToConfidence(LivenessKind K) {
4538
llvm_unreachable("unknown liveness kind");
4639
}
4740

41+
namespace {
42+
43+
/// Struct to store the complete context for a potential lifetime violation.
44+
struct PendingWarning {
45+
SourceLocation ExpiryLoc; // Where the loan expired.
46+
const Expr *UseExpr; // Where the origin holding this loan was used.
47+
Confidence ConfidenceLevel;
48+
};
49+
4850
class LifetimeChecker {
4951
private:
5052
llvm::DenseMap<LoanID, PendingWarning> FinalWarningsMap;
51-
const LoanPropagation &LP;
52-
const LiveOrigins &LO;
53+
const LoanPropagationAnalysis &LoanPropagation;
54+
const LiveOriginsAnalysis &LiveOrigins;
5355
const FactManager &FactMgr;
54-
AnalysisDeclContext &ADC;
5556
LifetimeSafetyReporter *Reporter;
5657

5758
public:
58-
LifetimeChecker(const LoanPropagation &LP, const LiveOrigins &LO,
59-
const FactManager &FM, AnalysisDeclContext &ADC,
60-
LifetimeSafetyReporter *Reporter)
61-
: LP(LP), LO(LO), FactMgr(FM), ADC(ADC), Reporter(Reporter) {}
62-
63-
void run() {
59+
LifetimeChecker(const LoanPropagationAnalysis &LoanPropagation,
60+
const LiveOriginsAnalysis &LiveOrigins, const FactManager &FM,
61+
AnalysisDeclContext &ADC, LifetimeSafetyReporter *Reporter)
62+
: LoanPropagation(LoanPropagation), LiveOrigins(LiveOrigins), FactMgr(FM),
63+
Reporter(Reporter) {
6464
for (const CFGBlock *B : *ADC.getAnalysis<PostOrderCFGView>())
6565
for (const Fact *F : FactMgr.getFacts(B))
6666
if (const auto *EF = F->getAs<ExpireFact>())
@@ -81,11 +81,11 @@ class LifetimeChecker {
8181
/// propagation (e.g., a loan may only be held on some execution paths).
8282
void checkExpiry(const ExpireFact *EF) {
8383
LoanID ExpiredLoan = EF->getLoanID();
84-
LivenessMap Origins = LO.getLiveOrigins(EF);
84+
LivenessMap Origins = LiveOrigins.getLiveOriginsAt(EF);
8585
Confidence CurConfidence = Confidence::None;
8686
const UseFact *BadUse = nullptr;
8787
for (auto &[OID, LiveInfo] : Origins) {
88-
LoanSet HeldLoans = LP.getLoans(OID, EF);
88+
LoanSet HeldLoans = LoanPropagation.getLoans(OID, EF);
8989
if (!HeldLoans.contains(ExpiredLoan))
9090
continue;
9191
// Loan is defaulted.
@@ -117,13 +117,14 @@ class LifetimeChecker {
117117
}
118118
}
119119
};
120+
} // namespace
120121

121-
void runLifetimeChecker(const LoanPropagation &LP, const LiveOrigins &LO,
122+
void runLifetimeChecker(const LoanPropagationAnalysis &LP,
123+
const LiveOriginsAnalysis &LO,
122124
const FactManager &FactMgr, AnalysisDeclContext &ADC,
123125
LifetimeSafetyReporter *Reporter) {
124126
llvm::TimeTraceScope TimeProfile("LifetimeChecker");
125127
LifetimeChecker Checker(LP, LO, FactMgr, ADC, Reporter);
126-
Checker.run();
127128
}
128129

129130
} // namespace clang::lifetimes::internal

clang/lib/Analysis/LifetimeSafety/Dataflow.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class DataflowAnalysis {
7575
static constexpr bool isForward() { return Dir == Direction::Forward; }
7676

7777
protected:
78-
FactManager &AllFacts;
78+
FactManager &FactMgr;
7979

80-
explicit DataflowAnalysis(const CFG &C, AnalysisDeclContext &AC,
81-
FactManager &F)
82-
: Cfg(C), AC(AC), AllFacts(F) {}
80+
explicit DataflowAnalysis(const CFG &Cfg, AnalysisDeclContext &AC,
81+
FactManager &FactMgr)
82+
: Cfg(Cfg), AC(AC), FactMgr(FactMgr) {}
8383

8484
public:
8585
void run() {
@@ -140,7 +140,7 @@ class DataflowAnalysis {
140140
/// Computes the state at one end of a block by applying all its facts
141141
/// sequentially to a given state from the other end.
142142
Lattice transferBlock(const CFGBlock *Block, Lattice State) {
143-
auto Facts = AllFacts.getFacts(Block);
143+
auto Facts = FactMgr.getFacts(Block);
144144
if constexpr (isForward()) {
145145
for (const Fact *F : Facts) {
146146
State = transferFact(State, F);

clang/lib/Analysis/LifetimeSafety/Facts.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ void TestPointFact::dump(llvm::raw_ostream &OS, const LoanManager &,
6262
OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
6363
}
6464

65+
llvm::StringMap<ProgramPoint> FactManager::getTestPoints() const {
66+
llvm::StringMap<ProgramPoint> AnnotationToPointMap;
67+
for (const CFGBlock *Block : BlockToFactsMap.keys()) {
68+
for (const Fact *F : getFacts(Block)) {
69+
if (const auto *TPF = F->getAs<TestPointFact>()) {
70+
StringRef PointName = TPF->getAnnotation();
71+
assert(AnnotationToPointMap.find(PointName) ==
72+
AnnotationToPointMap.end() &&
73+
"more than one test points with the same name");
74+
AnnotationToPointMap[PointName] = F;
75+
}
76+
}
77+
}
78+
return AnnotationToPointMap;
79+
}
80+
6581
void FactManager::dump(const CFG &Cfg, AnalysisDeclContext &AC) const {
6682
llvm::dbgs() << "==========================================\n";
6783
llvm::dbgs() << " Lifetime Analysis Facts:\n";

0 commit comments

Comments
 (0)