Skip to content

[SourceManager] Use llvm::SourceMgr::FindLocForLineAndColumn() #36287

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 1 commit into from
Mar 4, 2021
Merged
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
45 changes: 16 additions & 29 deletions lib/Basic/SourceLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,39 +346,26 @@ llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
if (Line == 0) {
return None;
}
const bool LineEnd = Col == ~0u;
auto InputBuf = getLLVMSourceMgr().getMemoryBuffer(BufferId);
const char *Ptr = InputBuf->getBufferStart();
const char *End = InputBuf->getBufferEnd();
const char *LineStart = Ptr;
--Line;
for (; Line && (Ptr < End); ++Ptr) {
if (*Ptr == '\n') {
--Line;
LineStart = Ptr+1;
}
}
if (Line != 0) {
const bool LineEnd = (Col == ~0u);
if (LineEnd)
Col = 0;

auto loc = const_cast<SourceManager *>(this)
->getLLVMSourceMgr()
.FindLocForLineAndColumn(BufferId, Line, Col);
if (!loc.isValid())
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;
if (Col == 0)
return Ptr - InputBuf->getBufferStart();
if (*Ptr == '\n' || Ptr == End) {
if (LineEnd) {
return Ptr - InputBuf->getBufferStart();
} else {

auto InputBuf = getLLVMSourceMgr().getMemoryBuffer(BufferId);
const char *Ptr = loc.getPointer();
if (LineEnd) {
const char *End = InputBuf->getBufferEnd();
for (;; ++Ptr) {
if (Ptr == End || *Ptr == '\n')
break;
}
}
}
return None;
return Ptr - InputBuf->getBufferStart();
}

unsigned SourceManager::getExternalSourceBufferId(StringRef Path) {
Expand Down