diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-02-27 02:42:25 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-02-27 02:42:25 +0000 |
commit | d1eabfb15c87837c57d1eb658d75a1f48d6fd5ed (patch) | |
tree | 1674794461bdb4baf33f78c7357f7d149707ac35 /lib/Basic/SourceManager.cpp | |
parent | de431c2dc7ec3592327ab23ae315fc915c549f05 (diff) |
Robustify SourceManager::getLocation(), so that it returns an
end-of-line source location when given a column number beyond the
length of the line, or an end-of-file source location when given a
line number beyond the length of the file. Previously, we would return
an invalid location.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97299 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Basic/SourceManager.cpp')
-rw-r--r-- | lib/Basic/SourceManager.cpp | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index b91671ad17..0c22de7bdd 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -980,20 +980,6 @@ SourceLocation SourceManager::getLocation(const FileEntry *SourceFile, if (Content->SourceLineCache == 0) ComputeLineNumbers(Content, ContentCacheAlloc); - if (Line > Content->NumLines) - return SourceLocation(); - - unsigned FilePos = Content->SourceLineCache[Line - 1]; - const char *Buf = Content->getBuffer()->getBufferStart() + FilePos; - unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf; - 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 SourceLocation(); - // Find the first file ID that corresponds to the given file. FileID FirstFID; @@ -1020,6 +1006,24 @@ SourceLocation SourceManager::getLocation(const FileEntry *SourceFile, if (FirstFID.isInvalid()) return SourceLocation(); + if (Line > Content->NumLines) { + unsigned Size = Content->getBuffer()->getBufferSize(); + if (Size > 0) + --Size; + return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size); + } + + unsigned FilePos = Content->SourceLineCache[Line - 1]; + const char *Buf = Content->getBuffer()->getBufferStart() + FilePos; + unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf; + 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(FirstFID).getFileLocWithOffset(FilePos + i); + return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1); } |