[cfe-commits] r140059 - in /cfe/trunk: include/clang/Basic/SourceManager.h lib/Basic/SourceManager.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Sep 19 13:40:29 PDT 2011


Author: akirtzidis
Date: Mon Sep 19 15:40:29 2011
New Revision: 140059

URL: http://llvm.org/viewvc/llvm-project?rev=140059&view=rev
Log:
Break SourceManager::translateFileLineCol into translateLineCol that returns the
source location of line:col of a specific FileID.

Modified:
    cfe/trunk/include/clang/Basic/SourceManager.h
    cfe/trunk/lib/Basic/SourceManager.cpp

Modified: cfe/trunk/include/clang/Basic/SourceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceManager.h?rev=140059&r1=140058&r2=140059&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Mon Sep 19 15:40:29 2011
@@ -1119,6 +1119,10 @@
   SourceLocation translateFileLineCol(const FileEntry *SourceFile,
                                       unsigned Line, unsigned Col);
 
+  /// \brief Get the source location in \arg FID for the given line:col.
+  /// Returns null location if \arg FID is not a file SLocEntry.
+  SourceLocation translateLineCol(FileID FID, unsigned Line, unsigned Col);
+
   /// \brief If \arg Loc points inside a function macro argument, the returned
   /// location will be the macro location in which the argument was expanded.
   /// If a macro argument is used multiple times, the expanded location will

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=140059&r1=140058&r2=140059&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Mon Sep 19 15:40:29 2011
@@ -1432,15 +1432,30 @@
       }
     }      
   }
-    
-  if (FirstFID.isInvalid())
+
+  return translateLineCol(FirstFID, Line, Col);
+}
+
+/// \brief Get the source location in \arg FID for the given line:col.
+/// Returns null location if \arg FID is not a file SLocEntry.
+SourceLocation SourceManager::translateLineCol(FileID FID,
+                                               unsigned Line, unsigned Col) {
+  if (FID.isInvalid())
+    return SourceLocation();
+
+  bool Invalid = false;
+  const SLocEntry &Entry = getSLocEntry(FID, &Invalid);
+  if (Invalid)
+    return SourceLocation();
+  
+  if (!Entry.isFile())
     return SourceLocation();
 
   if (Line == 1 && Col == 1)
-    return getLocForStartOfFile(FirstFID);
+    return getLocForStartOfFile(FID);
 
   ContentCache *Content
-    = const_cast<ContentCache *>(getOrCreateContentCache(SourceFile));
+    = const_cast<ContentCache *>(Entry.getFile().getContentCache());
   if (!Content)
     return SourceLocation();
     
@@ -1457,7 +1472,7 @@
     unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
     if (Size > 0)
       --Size;
-    return getLocForStartOfFile(FirstFID).getLocWithOffset(Size);
+    return getLocForStartOfFile(FID).getLocWithOffset(Size);
   }
 
   unsigned FilePos = Content->SourceLineCache[Line - 1];
@@ -1469,9 +1484,9 @@
   while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
     ++i;
   if (i < Col-1)
-    return getLocForStartOfFile(FirstFID).getLocWithOffset(FilePos + i);
+    return getLocForStartOfFile(FID).getLocWithOffset(FilePos + i);
 
-  return getLocForStartOfFile(FirstFID).getLocWithOffset(FilePos + Col - 1);
+  return getLocForStartOfFile(FID).getLocWithOffset(FilePos + Col - 1);
 }
 
 /// \brief Compute a map of macro argument chunks to their expanded source





More information about the cfe-commits mailing list