From: Argyrios Kyrtzidis Date: Tue, 20 Sep 2011 22:14:54 +0000 (+0000) Subject: In SourceManager::translateLineCol, handle the case where we are pointing X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e5e95dec56d8f2392068c43c84eaaad674c2702;p=clang In SourceManager::translateLineCol, handle the case where we are pointing directly at the end of the source file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140192 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 68c98fe02c..f15c5dc334 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -1453,8 +1453,10 @@ SourceLocation SourceManager::translateLineCol(FileID FID, if (!Entry.isFile()) return SourceLocation(); + SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset()); + if (Line == 1 && Col == 1) - return getLocForStartOfFile(FID); + return FileLoc; ContentCache *Content = const_cast(Entry.getFile().getContentCache()); @@ -1474,21 +1476,24 @@ SourceLocation SourceManager::translateLineCol(FileID FID, unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize(); if (Size > 0) --Size; - return getLocForStartOfFile(FID).getLocWithOffset(Size); + return FileLoc.getLocWithOffset(Size); } unsigned FilePos = Content->SourceLineCache[Line - 1]; const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos; unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf; + if (BufLength == 0) + return FileLoc.getLocWithOffset(FilePos); + unsigned i = 0; // Check that the given column is valid. while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') ++i; if (i < Col-1) - return getLocForStartOfFile(FID).getLocWithOffset(FilePos + i); + return FileLoc.getLocWithOffset(FilePos + i); - return getLocForStartOfFile(FID).getLocWithOffset(FilePos + Col - 1); + return FileLoc.getLocWithOffset(FilePos + Col - 1); } /// \brief Compute a map of macro argument chunks to their expanded source