Skip to content

Commit 1131b15

Browse files
authored
Apply changes from code browser
Apply changes from code browser
1 parent 98bb91d commit 1131b15

File tree

6 files changed

+112
-66
lines changed

6 files changed

+112
-66
lines changed

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

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ class Fact {
6565
}
6666

6767
virtual void dump(llvm::raw_ostream &OS, const LoanManager &,
68-
const OriginManager &) const {
69-
OS << "Fact (Kind: " << static_cast<int>(K) << ")\n";
70-
}
68+
const OriginManager &) const;
7169
};
7270

7371
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
@@ -88,13 +86,7 @@ class IssueFact : public Fact {
8886
LoanID getLoanID() const { return LID; }
8987
OriginID getOriginID() const { return OID; }
9088
void dump(llvm::raw_ostream &OS, const LoanManager &LM,
91-
const OriginManager &OM) const override {
92-
OS << "Issue (";
93-
LM.getLoan(getLoanID()).dump(OS);
94-
OS << ", ToOrigin: ";
95-
OM.dump(getOriginID(), OS);
96-
OS << ")\n";
97-
}
89+
const OriginManager &OM) const override;
9890
};
9991

10092
class ExpireFact : public Fact {
@@ -111,11 +103,7 @@ class ExpireFact : public Fact {
111103
SourceLocation getExpiryLoc() const { return ExpiryLoc; }
112104

113105
void dump(llvm::raw_ostream &OS, const LoanManager &LM,
114-
const OriginManager &) const override {
115-
OS << "Expire (";
116-
LM.getLoan(getLoanID()).dump(OS);
117-
OS << ")\n";
118-
}
106+
const OriginManager &) const override;
119107
};
120108

121109
class OriginFlowFact : public Fact {
@@ -139,14 +127,7 @@ class OriginFlowFact : public Fact {
139127
bool getKillDest() const { return KillDest; }
140128

141129
void dump(llvm::raw_ostream &OS, const LoanManager &,
142-
const OriginManager &OM) const override {
143-
OS << "OriginFlow (Dest: ";
144-
OM.dump(getDestOriginID(), OS);
145-
OS << ", Src: ";
146-
OM.dump(getSrcOriginID(), OS);
147-
OS << (getKillDest() ? "" : ", Merge");
148-
OS << ")\n";
149-
}
130+
const OriginManager &OM) const override;
150131
};
151132

152133
class ReturnOfOriginFact : public Fact {
@@ -160,11 +141,7 @@ class ReturnOfOriginFact : public Fact {
160141
ReturnOfOriginFact(OriginID OID) : Fact(Kind::ReturnOfOrigin), OID(OID) {}
161142
OriginID getReturnedOriginID() const { return OID; }
162143
void dump(llvm::raw_ostream &OS, const LoanManager &,
163-
const OriginManager &OM) const override {
164-
OS << "ReturnOfOrigin (";
165-
OM.dump(getReturnedOriginID(), OS);
166-
OS << ")\n";
167-
}
144+
const OriginManager &OM) const override;
168145
};
169146

170147
class UseFact : public Fact {
@@ -187,11 +164,7 @@ class UseFact : public Fact {
187164
bool isWritten() const { return IsWritten; }
188165

189166
void dump(llvm::raw_ostream &OS, const LoanManager &,
190-
const OriginManager &OM) const override {
191-
OS << "Use (";
192-
OM.dump(getUsedOrigin(OM), OS);
193-
OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
194-
}
167+
const OriginManager &OM) const override;
195168
};
196169

197170
/// A dummy-fact used to mark a specific point in the code for testing.
@@ -208,9 +181,7 @@ class TestPointFact : public Fact {
208181
StringRef getAnnotation() const { return Annotation; }
209182

210183
void dump(llvm::raw_ostream &OS, const LoanManager &,
211-
const OriginManager &) const override {
212-
OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
213-
}
184+
const OriginManager &) const override;
214185
};
215186

216187
class FactManager {
@@ -233,26 +204,7 @@ class FactManager {
233204
return new (Mem) FactType(std::forward<Args>(args)...);
234205
}
235206

236-
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const {
237-
llvm::dbgs() << "==========================================\n";
238-
llvm::dbgs() << " Lifetime Analysis Facts:\n";
239-
llvm::dbgs() << "==========================================\n";
240-
if (const Decl *D = AC.getDecl())
241-
if (const auto *ND = dyn_cast<NamedDecl>(D))
242-
llvm::dbgs() << "Function: " << ND->getQualifiedNameAsString() << "\n";
243-
// Print blocks in the order as they appear in code for a stable ordering.
244-
for (const CFGBlock *B : *AC.getAnalysis<PostOrderCFGView>()) {
245-
llvm::dbgs() << " Block B" << B->getBlockID() << ":\n";
246-
auto It = BlockToFactsMap.find(B);
247-
if (It != BlockToFactsMap.end()) {
248-
for (const Fact *F : It->second) {
249-
llvm::dbgs() << " ";
250-
F->dump(llvm::dbgs(), LoanMgr, OriginMgr);
251-
}
252-
}
253-
llvm::dbgs() << " End of Block\n";
254-
}
255-
}
207+
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const;
256208

257209
LoanManager &getLoanMgr() { return LoanMgr; }
258210
const LoanManager &getLoanMgr() const { return LoanMgr; }

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,19 @@ class LifetimeSafetyAnalysis {
5252

5353
void run();
5454

55-
/// Returns the set of loans an origin holds at a specific program point.
56-
LoanSet getLoansAtPoint(OriginID OID, ProgramPoint PP) const;
55+
/// Returns the loan propagation analysis object.
56+
/// \note This is intended for testing only.
57+
LoanPropagationAnalysis &getLoanPropagationAnalysis() const {
58+
assert(LoanPropagation && "Analysis has not been run.");
59+
return *LoanPropagation;
60+
}
61+
62+
/// Returns the live origin analysis object.
63+
/// \note This is intended for testing only.
64+
LiveOriginAnalysis &getLiveOriginAnalysis() const {
65+
assert(LiveOrigins && "Analysis has not been run.");
66+
return *LiveOrigins;
67+
}
5768

5869
/// Returns the set of origins that are live at a specific program point,
5970
/// along with the confidence level of their liveness.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
add_clang_library(clangAnalysisLifetimeSafety
22
Checker.cpp
3+
Facts.cpp
34
FactsGenerator.cpp
4-
LiveOrigins.cpp
55
LifetimeAnnotations.cpp
66
LifetimeSafety.cpp
7+
LiveOrigins.cpp
78
LoanPropagation.cpp
89
)
910

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===- Facts.cpp - Lifetime Analysis Facts Implementation -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
10+
#include "clang/AST/Decl.h"
11+
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
12+
13+
namespace clang::lifetimes {
14+
namespace internal {
15+
16+
void Fact::dump(llvm::raw_ostream &OS, const LoanManager &,
17+
const OriginManager &) const {
18+
OS << "Fact (Kind: " << static_cast<int>(K) << ")\n";
19+
}
20+
21+
void IssueFact::dump(llvm::raw_ostream &OS, const LoanManager &LM,
22+
const OriginManager &OM) const {
23+
OS << "Issue (";
24+
LM.getLoan(getLoanID()).dump(OS);
25+
OS << ", ToOrigin: ";
26+
OM.dump(getOriginID(), OS);
27+
OS << ")\n";
28+
}
29+
30+
void ExpireFact::dump(llvm::raw_ostream &OS, const LoanManager &LM,
31+
const OriginManager &) const {
32+
OS << "Expire (";
33+
LM.getLoan(getLoanID()).dump(OS);
34+
OS << ")\n";
35+
}
36+
37+
void OriginFlowFact::dump(llvm::raw_ostream &OS, const LoanManager &,
38+
const OriginManager &OM) const {
39+
OS << "OriginFlow (Dest: ";
40+
OM.dump(getDestOriginID(), OS);
41+
OS << ", Src: ";
42+
OM.dump(getSrcOriginID(), OS);
43+
OS << (getKillDest() ? "" : ", Merge");
44+
OS << ")\n";
45+
}
46+
47+
void ReturnOfOriginFact::dump(llvm::raw_ostream &OS, const LoanManager &,
48+
const OriginManager &OM) const {
49+
OS << "ReturnOfOrigin (";
50+
OM.dump(getReturnedOriginID(), OS);
51+
OS << ")\n";
52+
}
53+
54+
void UseFact::dump(llvm::raw_ostream &OS, const LoanManager &,
55+
const OriginManager &OM) const {
56+
OS << "Use (";
57+
OM.dump(getUsedOrigin(OM), OS);
58+
OS << ", " << (isWritten() ? "Write" : "Read") << ")\n";
59+
}
60+
61+
void TestPointFact::dump(llvm::raw_ostream &OS, const LoanManager &,
62+
const OriginManager &) const {
63+
OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
64+
}
65+
66+
void FactManager::dump(const CFG &Cfg, AnalysisDeclContext &AC) const {
67+
llvm::dbgs() << "==========================================\n";
68+
llvm::dbgs() << " Lifetime Analysis Facts:\n";
69+
llvm::dbgs() << "==========================================\n";
70+
if (const Decl *D = AC.getDecl())
71+
if (const auto *ND = dyn_cast<NamedDecl>(D))
72+
llvm::dbgs() << "Function: " << ND->getQualifiedNameAsString() << "\n";
73+
// Print blocks in the order as they appear in code for a stable ordering.
74+
for (const CFGBlock *B : *AC.getAnalysis<PostOrderCFGView>()) {
75+
llvm::dbgs() << " Block B" << B->getBlockID() << ":\n";
76+
auto It = BlockToFactsMap.find(B);
77+
if (It != BlockToFactsMap.end()) {
78+
for (const Fact *F : It->second) {
79+
llvm::dbgs() << " ";
80+
F->dump(llvm::dbgs(), LoanMgr, OriginMgr);
81+
}
82+
}
83+
llvm::dbgs() << " End of Block\n";
84+
}
85+
}
86+
87+
} // namespace internal
88+
} // namespace clang::lifetimes

clang/lib/Analysis/LifetimeSafety/LifetimeSafety.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ void LifetimeSafetyAnalysis::run() {
7777
runLifetimeChecker(*LoanPropagation, *LiveOrigins, FactMgr, AC, Reporter);
7878
}
7979

80-
LoanSet LifetimeSafetyAnalysis::getLoansAtPoint(OriginID OID,
81-
ProgramPoint PP) const {
82-
assert(LoanPropagation && "Analysis has not been run.");
83-
return LoanPropagation->getLoans(OID, PP);
84-
}
85-
8680
std::optional<OriginID>
8781
LifetimeSafetyAnalysis::getOriginIDForDecl(const ValueDecl *D) const {
8882
// This assumes the OriginManager's `get` can find an existing origin.

clang/unittests/Analysis/LifetimeSafetyTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class LifetimeTestHelper {
123123
ProgramPoint PP = Runner.getProgramPoint(Annotation);
124124
if (!PP)
125125
return std::nullopt;
126-
return Analysis.getLoansAtPoint(OID, PP);
126+
return Analysis.getLoanPropagationAnalysis().getLoans(OID, PP);
127127
}
128128

129129
std::optional<std::vector<std::pair<OriginID, LivenessKind>>>

0 commit comments

Comments
 (0)