[cfe-commits] r97299 - in /cfe/trunk: lib/Basic/SourceManager.cpp test/Index/annotate-tokens.c

Douglas Gregor dgregor at apple.com
Fri Feb 26 18:42:25 PST 2010


Author: dgregor
Date: Fri Feb 26 20:42:25 2010
New Revision: 97299

URL: http://llvm.org/viewvc/llvm-project?rev=97299&view=rev
Log:
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.

Modified:
    cfe/trunk/lib/Basic/SourceManager.cpp
    cfe/trunk/test/Index/annotate-tokens.c

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=97299&r1=97298&r2=97299&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Fri Feb 26 20:42:25 2010
@@ -980,20 +980,6 @@
   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 @@
   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);
 }
 

Modified: cfe/trunk/test/Index/annotate-tokens.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/annotate-tokens.c?rev=97299&r1=97298&r2=97299&view=diff
==============================================================================
--- cfe/trunk/test/Index/annotate-tokens.c (original)
+++ cfe/trunk/test/Index/annotate-tokens.c Fri Feb 26 20:42:25 2010
@@ -61,4 +61,5 @@
 // CHECK: Literal: ""Hello"" [9:24 - 9:31]
 // CHECK: Punctuation: ";" [9:31 - 9:32]
 // CHECK: Punctuation: "}" [10:1 - 10:2]
-
+// RUN: c-index-test -test-annotate-tokens=%s:4:1:165:32 %s | FileCheck %s
+// RUN: c-index-test -test-annotate-tokens=%s:4:1:165:38 %s | FileCheck %s





More information about the cfe-commits mailing list