Skip to content

Commit 362954b

Browse files
authored
Merge pull request #36287 from rintaro/resolvefromlinecol
[SourceManager] Use llvm::SourceMgr::FindLocForLineAndColumn()
2 parents 375d246 + de71c61 commit 362954b

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

lib/Basic/SourceLoc.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -346,39 +346,26 @@ llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
346346
if (Line == 0) {
347347
return None;
348348
}
349-
const bool LineEnd = Col == ~0u;
350-
auto InputBuf = getLLVMSourceMgr().getMemoryBuffer(BufferId);
351-
const char *Ptr = InputBuf->getBufferStart();
352-
const char *End = InputBuf->getBufferEnd();
353-
const char *LineStart = Ptr;
354-
--Line;
355-
for (; Line && (Ptr < End); ++Ptr) {
356-
if (*Ptr == '\n') {
357-
--Line;
358-
LineStart = Ptr+1;
359-
}
360-
}
361-
if (Line != 0) {
349+
const bool LineEnd = (Col == ~0u);
350+
if (LineEnd)
351+
Col = 0;
352+
353+
auto loc = const_cast<SourceManager *>(this)
354+
->getLLVMSourceMgr()
355+
.FindLocForLineAndColumn(BufferId, Line, Col);
356+
if (!loc.isValid())
362357
return None;
363-
}
364-
Ptr = LineStart;
365-
if (Col == 0) {
366-
return Ptr - InputBuf->getBufferStart();
367-
}
368-
// The <= here is to allow for non-inclusive range end positions at EOF
369-
for (; ; ++Ptr) {
370-
--Col;
371-
if (Col == 0)
372-
return Ptr - InputBuf->getBufferStart();
373-
if (*Ptr == '\n' || Ptr == End) {
374-
if (LineEnd) {
375-
return Ptr - InputBuf->getBufferStart();
376-
} else {
358+
359+
auto InputBuf = getLLVMSourceMgr().getMemoryBuffer(BufferId);
360+
const char *Ptr = loc.getPointer();
361+
if (LineEnd) {
362+
const char *End = InputBuf->getBufferEnd();
363+
for (;; ++Ptr) {
364+
if (Ptr == End || *Ptr == '\n')
377365
break;
378-
}
379366
}
380367
}
381-
return None;
368+
return Ptr - InputBuf->getBufferStart();
382369
}
383370

384371
unsigned SourceManager::getExternalSourceBufferId(StringRef Path) {

0 commit comments

Comments
 (0)