Skip to content

Commit b896013

Browse files
authored
Add source annotation to SILPrinter under flag -sil-print-sourceinfo (#29444)
* Add source annotation to SILPrinter under flag -sil-print-sourceinfo rdar://58365252
1 parent f7b0cbc commit b896013

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ class SourceManager {
243243
llvm::Optional<unsigned> resolveOffsetForEndOfLine(unsigned BufferId,
244244
unsigned Line) const;
245245

246+
/// Get the length of the line
247+
llvm::Optional<unsigned> getLineLength(unsigned BufferId, unsigned Line) const;
248+
246249
SourceLoc getLocForLineCol(unsigned BufferId, unsigned Line, unsigned Col) const {
247250
auto Offset = resolveFromLineCol(BufferId, Line, Col);
248251
return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) :

lib/Basic/SourceLoc.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,20 @@ SourceManager::resolveOffsetForEndOfLine(unsigned BufferId,
331331
return resolveFromLineCol(BufferId, Line, ~0u);
332332
}
333333

334+
llvm::Optional<unsigned>
335+
SourceManager::getLineLength(unsigned BufferId, unsigned Line) const {
336+
auto BegOffset = resolveFromLineCol(BufferId, Line, 0);
337+
auto EndOffset = resolveFromLineCol(BufferId, Line, ~0u);
338+
if (BegOffset && EndOffset) {
339+
return EndOffset.getValue() - BegOffset.getValue();
340+
}
341+
return None;
342+
}
343+
334344
llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
335345
unsigned Line,
336346
unsigned Col) const {
337-
if (Line == 0 || Col == 0) {
347+
if (Line == 0) {
338348
return None;
339349
}
340350
const bool LineEnd = Col == ~0u;
@@ -353,6 +363,9 @@ llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
353363
return None;
354364
}
355365
Ptr = LineStart;
366+
if (Col == 0) {
367+
return Ptr - InputBuf->getBufferStart();
368+
}
356369
// The <= here is to allow for non-inclusive range end positions at EOF
357370
for (; ; ++Ptr) {
358371
--Col;

lib/SIL/SILPrinter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ llvm::cl::opt<bool>
6464
SILPrintDebugInfo("sil-print-debuginfo", llvm::cl::init(false),
6565
llvm::cl::desc("Include debug info in SIL output"));
6666

67+
llvm::cl::opt<bool>
68+
SILPrintSourceInfo("sil-print-sourceinfo", llvm::cl::init(false),
69+
llvm::cl::desc("Include source annotation in SIL output"));
70+
6771
llvm::cl::opt<bool> SILPrintGenericSpecializationInfo(
6872
"sil-print-generic-specialization-info", llvm::cl::init(false),
6973
llvm::cl::desc("Include generic specialization"
@@ -618,7 +622,22 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
618622
}
619623
*this << '\n';
620624

625+
const auto &SM = BB->getModule().getASTContext().SourceMgr;
626+
Optional<SILLocation> PrevLoc;
621627
for (const SILInstruction &I : *BB) {
628+
if (SILPrintSourceInfo) {
629+
auto CurSourceLoc = I.getLoc().getSourceLoc();
630+
if (CurSourceLoc.isValid()) {
631+
if (!PrevLoc || SM.getLineNumber(CurSourceLoc) > SM.getLineNumber(PrevLoc->getSourceLoc())) {
632+
auto Buffer = SM.findBufferContainingLoc(CurSourceLoc);
633+
auto Line = SM.getLineNumber(CurSourceLoc);
634+
auto LineLength = SM.getLineLength(Buffer, Line);
635+
PrintState.OS << " // " << SM.extractText({SM.getLocForLineCol(Buffer, Line, 0), LineLength.getValueOr(0)}) <<
636+
"\tSourceLoc: " << SM.getDisplayNameForLoc(CurSourceLoc) << ":" << Line << "\n";
637+
PrevLoc = I.getLoc();
638+
}
639+
}
640+
}
622641
Ctx.printInstructionCallBack(&I);
623642
if (SILPrintGenericSpecializationInfo) {
624643
if (auto AI = ApplySite::isa(const_cast<SILInstruction *>(&I)))

0 commit comments

Comments
 (0)