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

Argiris Kirtzidis akyrtzi at gmail.com
Sat Jun 20 01:09:58 PDT 2009


Author: akirtzidis
Date: Sat Jun 20 03:09:57 2009
New Revision: 73823

URL: http://llvm.org/viewvc/llvm-project?rev=73823&view=rev
Log:
Introduce SourceManager::getLocation() to get a source location out of a "file:line:column" triplet.

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=73823&r1=73822&r2=73823&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Sat Jun 20 03:09:57 2009
@@ -68,7 +68,12 @@
     /// NumLines - The number of lines in this ContentCache.  This is only valid
     /// if SourceLineCache is non-null.
     unsigned NumLines;
-    
+
+    /// FirstFID - First FileID that was created for this ContentCache.
+    /// Represents the first source inclusion of the file associated with this
+    /// ContentCache.
+    mutable FileID FirstFID;
+
     /// getBuffer - Returns the memory buffer for the associated content.
     const llvm::MemoryBuffer *getBuffer() const;
     
@@ -624,6 +629,13 @@
   //===--------------------------------------------------------------------===//
   // Other miscellaneous methods.
   //===--------------------------------------------------------------------===//
+
+  /// \brief Get the source location for the given file:line:col triplet.
+  ///
+  /// If the source file is included multiple times, the source location will
+  /// be based upon the first inclusion.
+  SourceLocation getLocation(const FileEntry *SourceFile,
+                             unsigned Line, unsigned Col) const;
   
   // Iterators over FileInfos.
   typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=73823&r1=73822&r2=73823&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Sat Jun 20 03:09:57 2009
@@ -358,7 +358,10 @@
     SLocEntryTable[PreallocatedID] 
       = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
     SLocEntryLoaded[PreallocatedID] = true;
-    return LastFileIDLookup = FileID::get(PreallocatedID);
+    FileID FID = FileID::get(PreallocatedID);
+    if (File->FirstFID.isInvalid())
+      File->FirstFID = FID;
+    return LastFileIDLookup = FID;
   }
 
   SLocEntryTable.push_back(SLocEntry::get(NextOffset, 
@@ -914,6 +917,43 @@
 // Other miscellaneous methods.
 //===----------------------------------------------------------------------===//
 
+/// \brief Get the source location for the given file:line:col triplet.
+///
+/// If the source file is included multiple times, the source location will
+/// be based upon the first inclusion.
+SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
+                                          unsigned Line, unsigned Col) const {
+  assert(SourceFile && "Null source file!");
+  assert(Line && Col && "Line and column should start from 1!");
+
+  fileinfo_iterator FI = FileInfos.find(SourceFile);
+  if (FI == FileInfos.end())
+    return SourceLocation();
+  ContentCache *Content = FI->second;
+  
+  // If this is the first use of line information for this buffer, compute the
+  /// SourceLineCache for it on demand.
+  if (Content->SourceLineCache == 0)
+    ComputeLineNumbers(Content, ContentCacheAlloc);
+
+  if (Line > Content->NumLines)
+    return SourceLocation();
+  
+  unsigned FilePos = Content->SourceLineCache[Line - 1];
+  const char *BufStart = Content->getBuffer()->getBufferStart();
+  const char *BufEnd = Content->getBuffer()->getBufferEnd();
+  const char *p = BufStart;
+
+  // Check that the given column is valid.
+  while (p < BufEnd && *p != '\n' && *p != '\r')
+    ++p;
+  if (Col > p-BufStart)
+    return SourceLocation();
+  
+  return getLocForStartOfFile(Content->FirstFID).
+            getFileLocWithOffset(FilePos + Col - 1);
+}
+
 
 /// PrintStats - Print statistics to stderr.
 ///





More information about the cfe-commits mailing list