Skip to content

Commit 92a91f7

Browse files
authored
[LifetimeSafety] Improve Origin information in debug output (#153951)
The previous debug output only showed numeric IDs for origins, making it difficult to understand what each origin represented. This change makes the debug output more informative by showing what kind of entity each origin refers to (declaration or expression) and additional details like declaration names or expression class names. This improved output makes it easier to debug and understand the lifetime safety analysis.
1 parent dc82b2c commit 92a91f7

File tree

2 files changed

+111
-90
lines changed

2 files changed

+111
-90
lines changed

clang/lib/Analysis/LifetimeSafety.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ class OriginManager {
175175
return NewID;
176176
}
177177

178+
void dump(OriginID OID, llvm::raw_ostream &OS) const {
179+
OS << OID << " (";
180+
Origin O = getOrigin(OID);
181+
if (const ValueDecl *VD = O.getDecl())
182+
OS << "Decl: " << VD->getNameAsString();
183+
else if (const Expr *E = O.getExpr())
184+
OS << "Expr: " << E->getStmtClassName();
185+
else
186+
OS << "Unknown";
187+
OS << ")";
188+
}
189+
178190
private:
179191
OriginID getNextOriginID() { return NextOriginID++; }
180192

@@ -222,7 +234,7 @@ class Fact {
222234
return nullptr;
223235
}
224236

225-
virtual void dump(llvm::raw_ostream &OS) const {
237+
virtual void dump(llvm::raw_ostream &OS, const OriginManager &) const {
226238
OS << "Fact (Kind: " << static_cast<int>(K) << ")\n";
227239
}
228240
};
@@ -237,9 +249,10 @@ class IssueFact : public Fact {
237249
IssueFact(LoanID LID, OriginID OID) : Fact(Kind::Issue), LID(LID), OID(OID) {}
238250
LoanID getLoanID() const { return LID; }
239251
OriginID getOriginID() const { return OID; }
240-
void dump(llvm::raw_ostream &OS) const override {
241-
OS << "Issue (LoanID: " << getLoanID() << ", OriginID: " << getOriginID()
242-
<< ")\n";
252+
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const override {
253+
OS << "Issue (LoanID: " << getLoanID() << ", ToOrigin: ";
254+
OM.dump(getOriginID(), OS);
255+
OS << ")\n";
243256
}
244257
};
245258

@@ -256,7 +269,7 @@ class ExpireFact : public Fact {
256269
LoanID getLoanID() const { return LID; }
257270
SourceLocation getExpiryLoc() const { return ExpiryLoc; }
258271

259-
void dump(llvm::raw_ostream &OS) const override {
272+
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const override {
260273
OS << "Expire (LoanID: " << getLoanID() << ")\n";
261274
}
262275
};
@@ -274,9 +287,12 @@ class AssignOriginFact : public Fact {
274287
: Fact(Kind::AssignOrigin), OIDDest(OIDDest), OIDSrc(OIDSrc) {}
275288
OriginID getDestOriginID() const { return OIDDest; }
276289
OriginID getSrcOriginID() const { return OIDSrc; }
277-
void dump(llvm::raw_ostream &OS) const override {
278-
OS << "AssignOrigin (DestID: " << getDestOriginID()
279-
<< ", SrcID: " << getSrcOriginID() << ")\n";
290+
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const override {
291+
OS << "AssignOrigin (Dest: ";
292+
OM.dump(getDestOriginID(), OS);
293+
OS << ", Src: ";
294+
OM.dump(getSrcOriginID(), OS);
295+
OS << ")\n";
280296
}
281297
};
282298

@@ -290,8 +306,10 @@ class ReturnOfOriginFact : public Fact {
290306

291307
ReturnOfOriginFact(OriginID OID) : Fact(Kind::ReturnOfOrigin), OID(OID) {}
292308
OriginID getReturnedOriginID() const { return OID; }
293-
void dump(llvm::raw_ostream &OS) const override {
294-
OS << "ReturnOfOrigin (OriginID: " << getReturnedOriginID() << ")\n";
309+
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const override {
310+
OS << "ReturnOfOrigin (";
311+
OM.dump(getReturnedOriginID(), OS);
312+
OS << ")\n";
295313
}
296314
};
297315

@@ -308,8 +326,10 @@ class UseFact : public Fact {
308326
OriginID getUsedOrigin() const { return UsedOrigin; }
309327
const Expr *getUseExpr() const { return UseExpr; }
310328

311-
void dump(llvm::raw_ostream &OS) const override {
312-
OS << "Use (OriginID: " << UsedOrigin << ")\n";
329+
void dump(llvm::raw_ostream &OS, const OriginManager &OM) const override {
330+
OS << "Use (";
331+
OM.dump(getUsedOrigin(), OS);
332+
OS << ")\n";
313333
}
314334
};
315335

@@ -326,7 +346,7 @@ class TestPointFact : public Fact {
326346

327347
StringRef getAnnotation() const { return Annotation; }
328348

329-
void dump(llvm::raw_ostream &OS) const override {
349+
void dump(llvm::raw_ostream &OS, const OriginManager &) const override {
330350
OS << "TestPoint (Annotation: \"" << getAnnotation() << "\")\n";
331351
}
332352
};
@@ -365,7 +385,7 @@ class FactManager {
365385
if (It != BlockToFactsMap.end()) {
366386
for (const Fact *F : It->second) {
367387
llvm::dbgs() << " ";
368-
F->dump(llvm::dbgs());
388+
F->dump(llvm::dbgs(), OriginMgr);
369389
}
370390
}
371391
llvm::dbgs() << " End of Block\n";

0 commit comments

Comments
 (0)