Skip to content

Add source annotation to SILPrinter under flag -sil-print-sourceinfo #29444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ class SourceManager {
llvm::Optional<unsigned> resolveOffsetForEndOfLine(unsigned BufferId,
unsigned Line) const;

/// Get the length of the line
llvm::Optional<unsigned> 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()) :
Expand Down
15 changes: 14 additions & 1 deletion lib/Basic/SourceLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,20 @@ SourceManager::resolveOffsetForEndOfLine(unsigned BufferId,
return resolveFromLineCol(BufferId, Line, ~0u);
}

llvm::Optional<unsigned>
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<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
unsigned Line,
unsigned Col) const {
if (Line == 0 || Col == 0) {
if (Line == 0) {
return None;
}
const bool LineEnd = Col == ~0u;
Expand All @@ -353,6 +363,9 @@ llvm::Optional<unsigned> 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;
Expand Down
19 changes: 19 additions & 0 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ llvm::cl::opt<bool>
SILPrintDebugInfo("sil-print-debuginfo", llvm::cl::init(false),
llvm::cl::desc("Include debug info in SIL output"));

llvm::cl::opt<bool>
SILPrintSourceInfo("sil-print-sourceinfo", llvm::cl::init(false),
llvm::cl::desc("Include source annotation in SIL output"));

llvm::cl::opt<bool> SILPrintGenericSpecializationInfo(
"sil-print-generic-specialization-info", llvm::cl::init(false),
llvm::cl::desc("Include generic specialization"
Expand Down Expand Up @@ -618,7 +622,22 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
}
*this << '\n';

const auto &SM = BB->getModule().getASTContext().SourceMgr;
Optional<SILLocation> 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)}) <<
"\tSourceLoc: " << SM.getDisplayNameForLoc(CurSourceLoc) << ":" << Line << "\n";
PrevLoc = I.getLoc();
}
}
}
Ctx.printInstructionCallBack(&I);
if (SILPrintGenericSpecializationInfo) {
if (auto AI = ApplySite::isa(const_cast<SILInstruction *>(&I)))
Expand Down