From f2d72f7133d33b9912d612b31c34843787d1822d Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Fri, 24 Jan 2020 10:49:52 -0800 Subject: [PATCH 1/3] Add source annotation to SILPrinter under flag -sil-print-sourceinfo rdar://58365252 --- include/swift/Basic/SourceManager.h | 3 +++ lib/Basic/SourceLoc.cpp | 15 ++++++++++++++- lib/SIL/SILPrinter.cpp | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/swift/Basic/SourceManager.h b/include/swift/Basic/SourceManager.h index ed4f5dc34126e..7cf754731c1ab 100644 --- a/include/swift/Basic/SourceManager.h +++ b/include/swift/Basic/SourceManager.h @@ -243,6 +243,9 @@ class SourceManager { llvm::Optional resolveOffsetForEndOfLine(unsigned BufferId, unsigned Line) const; + /// Get the length of the line + llvm::Optional getLineLength(unsigned BufferId, unsigned Line) const; + SourceLoc getLocForLineCol(unsigned BufferId, unsigned Line, unsigned Col) const { auto Offset = resolveFromLineCol(BufferId, Line, Col); return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) : diff --git a/lib/Basic/SourceLoc.cpp b/lib/Basic/SourceLoc.cpp index 7c66731e41fe1..76547ff2e91f6 100644 --- a/lib/Basic/SourceLoc.cpp +++ b/lib/Basic/SourceLoc.cpp @@ -331,10 +331,20 @@ SourceManager::resolveOffsetForEndOfLine(unsigned BufferId, return resolveFromLineCol(BufferId, Line, ~0u); } +llvm::Optional +SourceManager::getLineLength(unsigned BufferId, unsigned Line) const { + auto BegOffset = resolveFromLineCol(BufferId, Line, 0); + auto EndOffset = resolveFromLineCol(BufferId, Line, ~0u); + if (BegOffset && EndOffset) { + return EndOffset.getValue() - BegOffset.getValue(); + } + return None; +} + llvm::Optional SourceManager::resolveFromLineCol(unsigned BufferId, unsigned Line, unsigned Col) const { - if (Line == 0 || Col == 0) { + if (Line == 0) { return None; } const bool LineEnd = Col == ~0u; @@ -353,6 +363,9 @@ llvm::Optional SourceManager::resolveFromLineCol(unsigned BufferId, return None; } Ptr = LineStart; + if (Col == 0) { + return Ptr - InputBuf->getBufferStart(); + } // The <= here is to allow for non-inclusive range end positions at EOF for (; ; ++Ptr) { --Col; diff --git a/lib/SIL/SILPrinter.cpp b/lib/SIL/SILPrinter.cpp index 4900797283f88..c74f0261d75ca 100644 --- a/lib/SIL/SILPrinter.cpp +++ b/lib/SIL/SILPrinter.cpp @@ -64,6 +64,10 @@ llvm::cl::opt SILPrintDebugInfo("sil-print-debuginfo", llvm::cl::init(false), llvm::cl::desc("Include debug info in SIL output")); +llvm::cl::opt +SILPrintSourceInfo("sil-print-sourceinfo", llvm::cl::init(false), + llvm::cl::desc("Include source annotation in SIL output")); + llvm::cl::opt SILPrintGenericSpecializationInfo( "sil-print-generic-specialization-info", llvm::cl::init(false), llvm::cl::desc("Include generic specialization" @@ -618,7 +622,21 @@ class SILPrinter : public SILInstructionVisitor { } *this << '\n'; + const auto &SM = BB->getModule().getASTContext().SourceMgr; + Optional PrevLoc; for (const SILInstruction &I : *BB) { + if (SILPrintSourceInfo) { + auto CurSourceLoc = I.getLoc().getSourceLoc(); + if (CurSourceLoc.isValid()) { + if (!PrevLoc || SM.getLineNumber(CurSourceLoc) != SM.getLineNumber(PrevLoc->getSourceLoc())) { + auto Buffer = SM.findBufferContainingLoc(CurSourceLoc); + auto Line = SM.getLineNumber(CurSourceLoc); + auto LineLength = SM.getLineLength(Buffer, Line); + PrintState.OS << " // " << SM.extractText({SM.getLocForLineCol(Buffer, Line, 0), LineLength.getValueOr(0)}) << "\n"; + PrevLoc = I.getLoc(); + } + } + } Ctx.printInstructionCallBack(&I); if (SILPrintGenericSpecializationInfo) { if (auto AI = ApplySite::isa(const_cast(&I))) From 1b9ca41d1fa2f186d180aa9889ff66bb6963599e Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Mon, 27 Jan 2020 10:34:51 -0800 Subject: [PATCH 2/3] Add > check to compare previous and current source locations --- lib/SIL/SILPrinter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/SIL/SILPrinter.cpp b/lib/SIL/SILPrinter.cpp index c74f0261d75ca..f7a92d17fed5d 100644 --- a/lib/SIL/SILPrinter.cpp +++ b/lib/SIL/SILPrinter.cpp @@ -628,7 +628,7 @@ class SILPrinter : public SILInstructionVisitor { if (SILPrintSourceInfo) { auto CurSourceLoc = I.getLoc().getSourceLoc(); if (CurSourceLoc.isValid()) { - if (!PrevLoc || SM.getLineNumber(CurSourceLoc) != SM.getLineNumber(PrevLoc->getSourceLoc())) { + if (!PrevLoc || SM.getLineNumber(CurSourceLoc) > SM.getLineNumber(PrevLoc->getSourceLoc())) { auto Buffer = SM.findBufferContainingLoc(CurSourceLoc); auto Line = SM.getLineNumber(CurSourceLoc); auto LineLength = SM.getLineLength(Buffer, Line); From d49195c2eee325240f9e65d0bf54c8656c33ab5f Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Tue, 28 Jan 2020 11:29:26 -0800 Subject: [PATCH 3/3] Add != back. Also print filename:linenumber --- lib/SIL/SILPrinter.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/SIL/SILPrinter.cpp b/lib/SIL/SILPrinter.cpp index f7a92d17fed5d..976b9c790185c 100644 --- a/lib/SIL/SILPrinter.cpp +++ b/lib/SIL/SILPrinter.cpp @@ -632,7 +632,8 @@ class SILPrinter : public SILInstructionVisitor { auto Buffer = SM.findBufferContainingLoc(CurSourceLoc); auto Line = SM.getLineNumber(CurSourceLoc); auto LineLength = SM.getLineLength(Buffer, Line); - PrintState.OS << " // " << SM.extractText({SM.getLocForLineCol(Buffer, Line, 0), LineLength.getValueOr(0)}) << "\n"; + PrintState.OS << " // " << SM.extractText({SM.getLocForLineCol(Buffer, Line, 0), LineLength.getValueOr(0)}) << + "\tSourceLoc: " << SM.getDisplayNameForLoc(CurSourceLoc) << ":" << Line << "\n"; PrevLoc = I.getLoc(); } }