Skip to content
Closed
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
93 changes: 51 additions & 42 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ class SourceManager {
/// buffer identifier, or None if there is no such buffer.
Optional<unsigned> getIDForBufferIdentifier(StringRef BufIdentifier);

/// Returns the identifier for the buffer with the given ID.
///
/// \p BufferID must be a valid buffer ID.
StringRef getIdentifierForBuffer(unsigned BufferID) const;

/// \brief Returns a SourceRange covering the entire specified buffer.
///
/// Note that the start location might not point at the first token: it
Expand Down Expand Up @@ -163,40 +158,71 @@ class SourceManager {
return getLocForBufferStart(BufferID).getAdvancedLoc(Offset);
}

/// Returns the identifier string for the buffer containing the given source
/// Returns the identifier string for the buffer with the given ID.
///
/// \p BufferID must be a valid buffer ID.
StringRef getIdentifierForBuffer(unsigned BufferID) const;

/// Returns the identifier string for the buffer containing the given valid
/// source location.
///
/// Note that this doesn't respect #sourceLocation directives.
StringRef getIdentifierForBuffer(SourceLoc Loc) const {
return getIdentifierForBuffer(findBufferContainingLoc(Loc));
}

/// Returns the physical line and column represented by the given valid
/// source location.
///
/// Note that this doesn't respect #sourceLocation directives.
/// If \p BufferID is provided, \p Loc must come from that source buffer.
std::pair<unsigned, unsigned>
getLineAndColumnInBuffer(SourceLoc Loc, unsigned BufferID = 0) const {
return LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID);
}

/// Translate line and column pair to the offset.
llvm::Optional<unsigned>
getOffsetForLineAndColumnInBuffer(unsigned BufferId, unsigned Line,
unsigned Col) const;


/// Returns the source location from the given physical line and col in the
/// specified buffer.
SourceLoc getLocForLineAndColumnInBuffer(unsigned BufferId, unsigned Line,
unsigned Col) const {
auto Offset = getOffsetForLineAndColumnInBuffer(BufferId, Line, Col);
return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) :
SourceLoc();
}

/// Returns the presumed filename string containing the given valid source
/// location.
///
/// This respects #line directives.
StringRef getBufferIdentifierForLoc(SourceLoc Loc) const {
/// This respects \c #sourceLocation direvtive.
StringRef getPresumedFilenameForLoc(SourceLoc Loc) const {
if (auto VFile = getVirtualFile(Loc))
return VFile->Name;
else
return getIdentifierForBuffer(findBufferContainingLoc(Loc));
return getIdentifierForBuffer(Loc);
}

/// Returns the line and column represented by the given source location.
/// Returns the line and column represented by the given valid source
/// location.
///
/// If \p BufferID is provided, \p Loc must come from that source buffer.
///
/// This respects #line directives.
/// This respects \c #sourceLocation direvtive.
std::pair<unsigned, unsigned>
getLineAndColumn(SourceLoc Loc, unsigned BufferID = 0) const {
getPresumedLineAndColumnForLoc(SourceLoc Loc, unsigned BufferID = 0) const {
assert(Loc.isValid());
int LineOffset = getLineOffset(Loc);
int l, c;
std::tie(l, c) = LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID);
assert(LineOffset+l > 0 && "bogus line offset");
return { LineOffset + l, c };
}

/// Returns the real line number for a source location.
///
/// If \p BufferID is provided, \p Loc must come from that source buffer.
///
/// This does not respect #line directives.
unsigned getLineNumber(SourceLoc Loc, unsigned BufferID = 0) const {
assert(Loc.isValid());
return LLVMSourceMgr.FindLineNumber(Loc.Value, BufferID);
if (auto VFile = getVirtualFile(Loc)) {
assert(l + VFile->LineOffset > 0 && "bogus line offset");
l += VFile->LineOffset;
}
return { l, c };
}

StringRef extractText(CharSourceRange Range,
Expand All @@ -210,25 +236,8 @@ class SourceManager {
/// Verifies that all buffers are still valid.
void verifyAllBuffers() const;

/// Translate line and column pair to the offset.
llvm::Optional<unsigned> resolveFromLineCol(unsigned BufferId, unsigned Line,
unsigned Col) const;

SourceLoc getLocForLineCol(unsigned BufferId, unsigned Line, unsigned Col) const {
auto Offset = resolveFromLineCol(BufferId, Line, Col);
return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) :
SourceLoc();
}

private:
const VirtualFile *getVirtualFile(SourceLoc Loc) const;

int getLineOffset(SourceLoc Loc) const {
if (auto VFile = getVirtualFile(Loc))
return VFile->LineOffset;
else
return 0;
}
};

} // end namespace swift
Expand Down
2 changes: 0 additions & 2 deletions include/swift/Parse/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,6 @@ class Lexer {
static SourceLoc getLocForStartOfToken(SourceManager &SM, unsigned BufferID,
unsigned Offset);

static SourceLoc getLocForStartOfToken(SourceManager &SM, SourceLoc Loc);

/// Retrieve the start location of the line containing the given location.
/// the given location.
static SourceLoc getLocForStartOfLine(SourceManager &SM, SourceLoc Loc);
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,8 +1973,8 @@ void ASTScope::print(llvm::raw_ostream &out, unsigned level,
return;
}

auto startLineAndCol = sourceMgr.getLineAndColumn(range.Start);
auto endLineAndCol = sourceMgr.getLineAndColumn(range.End);
auto startLineAndCol = sourceMgr.getPresumedLineAndColumnForLoc(range.Start);
auto endLineAndCol = sourceMgr.getPresumedLineAndColumnForLoc(range.End);

out << " [" << startLineAndCol.first << ":" << startLineAndCol.second
<< " - " << endLineAndCol.first << ":" << endLineAndCol.second << "]";
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ static unsigned getLineNumber(DCType *DC) {
return 0;

const ASTContext &ctx = static_cast<const DeclContext *>(DC)->getASTContext();
return ctx.SourceMgr.getLineAndColumn(loc).first;
return ctx.SourceMgr.getPresumedLineAndColumnForLoc(loc).first;
}

bool DeclContext::classof(const Decl *D) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ void RequirementSource::print(llvm::raw_ostream &out,

unsigned bufferID = srcMgr->findBufferContainingLoc(loc);

auto lineAndCol = srcMgr->getLineAndColumn(loc, bufferID);
auto lineAndCol = srcMgr->getPresumedLineAndColumnForLoc(loc, bufferID);
out << " @ " << lineAndCol.first << ':' << lineAndCol.second;
};

Expand Down
8 changes: 4 additions & 4 deletions lib/AST/RawComment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ SingleRawComment::SingleRawComment(CharSourceRange Range,
const SourceManager &SourceMgr)
: Range(Range), RawText(SourceMgr.extractText(Range)),
Kind(static_cast<unsigned>(getCommentKind(RawText))),
EndLine(SourceMgr.getLineNumber(Range.getEnd())) {
auto StartLineAndColumn = SourceMgr.getLineAndColumn(Range.getStart());
StartLine = StartLineAndColumn.first;
StartColumn = StartLineAndColumn.second;
EndLine(SourceMgr.getLineAndColumnInBuffer(Range.getEnd()).first) {
auto Start = SourceMgr.getLineAndColumnInBuffer(Range.getStart());
StartLine = Start.first;
StartColumn = Start.second;
}

SingleRawComment::SingleRawComment(StringRef RawText, unsigned StartColumn)
Expand Down
10 changes: 5 additions & 5 deletions lib/Basic/SourceLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void SourceLoc::printLineAndColumn(raw_ostream &OS,
return;
}

auto LineAndCol = SM.getLineAndColumn(*this);
auto LineAndCol = SM.getPresumedLineAndColumnForLoc(*this);
OS << "line:" << LineAndCol.first << ':' << LineAndCol.second;
}

Expand All @@ -227,7 +227,7 @@ void SourceLoc::print(raw_ostream &OS, const SourceManager &SM,
OS << "line";
}

auto LineAndCol = SM.getLineAndColumn(*this, BufferID);
auto LineAndCol = SM.getPresumedLineAndColumnForLoc(*this, BufferID);
OS << ':' << LineAndCol.first << ':' << LineAndCol.second;
}

Expand Down Expand Up @@ -281,9 +281,9 @@ void CharSourceRange::dump(const SourceManager &SM) const {
print(llvm::errs(), SM);
}

llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
unsigned Line,
unsigned Col) const {
llvm::Optional<unsigned>
SourceManager::getOffsetForLineAndColumnInBuffer(unsigned BufferId, unsigned Line,
unsigned Col) const {
if (Line == 0 || Col == 0) {
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/DiagnosticVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ bool DiagnosticVerifier::verifyFile(unsigned BufferID,
if (PrevExpectedContinuationLine)
Expected.LineNo = PrevExpectedContinuationLine;
else
Expected.LineNo = SM.getLineAndColumn(
Expected.LineNo = SM.getPresumedLineAndColumnForLoc(
BufferStartLoc.getAdvancedLoc(MatchStart.data() - InputFile.data()),
BufferID).first;
Expected.LineNo += LineOffset;
Expand Down
4 changes: 2 additions & 2 deletions lib/Frontend/PrintingDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind,
std::string LineStr;

if (Loc.isValid()) {
BufferID = getBufferIdentifierForLoc(Loc);
BufferID = getPresumedFilenameForLoc(Loc);
auto CurMB = LLVMSourceMgr.getMemoryBuffer(findBufferContainingLoc(Loc));

// Scan backward to find the start of the line.
Expand Down Expand Up @@ -160,7 +160,7 @@ SourceManager::GetMessage(SourceLoc Loc, llvm::SourceMgr::DiagKind Kind,
R.End.getPointer()-LineStart));
}

LineAndCol = getLineAndColumn(Loc);
LineAndCol = getPresumedLineAndColumnForLoc(Loc);
}

return llvm::SMDiagnostic(LLVMSourceMgr, Loc.Value, BufferID,
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/SerializedDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void SerializedDiagnosticConsumer::addLocToRecord(SourceLoc Loc,
}

unsigned line, col;
std::tie(line, col) = SM.getLineAndColumn(Loc);
std::tie(line, col) = SM.getPresumedLineAndColumnForLoc(Loc);

Record.push_back(getEmitFile(Filename));
Record.push_back(line);
Expand Down
5 changes: 2 additions & 3 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,8 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
SourceManager &sourceMgr = Instance->getSourceMgr();
// Probe each of the locations, and dump what we find.
for (auto lineColumn : opts.DumpScopeMapLocations) {
SourceLoc loc = sourceMgr.getLocForLineCol(*bufferID,
lineColumn.first,
lineColumn.second);
SourceLoc loc = sourceMgr.getLocForLineAndColumnInBuffer(
*bufferID, lineColumn.first, lineColumn.second);
if (loc.isInvalid()) continue;

llvm::errs() << "***Scope at " << lineColumn.first << ":"
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/CommentConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void CommentToXMLConverter::visitDocComment(const DocComment *DC) {
const auto &SM = D->getASTContext().SourceMgr;
unsigned BufferID = SM.findBufferContainingLoc(Loc);
StringRef FileName = SM.getIdentifierForBuffer(BufferID);
auto LineAndColumn = SM.getLineAndColumn(Loc);
auto LineAndColumn = SM.getPresumedLineAndColumnForLoc(Loc);
OS << " file=\"";
appendWithXMLEscaping(OS, FileName);
OS << "\"";
Expand Down
Loading