[llvm-commits] [llvm] r49027 - in /llvm/trunk: include/llvm/Debugger/SourceFile.h lib/Debugger/SourceFile.cpp

Chris Lattner sabre at nondot.org
Mon Mar 31 20:59:34 PDT 2008


Author: lattner
Date: Mon Mar 31 22:59:34 2008
New Revision: 49027

URL: http://llvm.org/viewvc/llvm-project?rev=49027&view=rev
Log:
rewrite SourceFile to be in terms of MemoryBuffer, not MappedFile.

Modified:
    llvm/trunk/include/llvm/Debugger/SourceFile.h
    llvm/trunk/lib/Debugger/SourceFile.cpp

Modified: llvm/trunk/include/llvm/Debugger/SourceFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Debugger/SourceFile.h?rev=49027&r1=49026&r2=49027&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Debugger/SourceFile.h (original)
+++ llvm/trunk/include/llvm/Debugger/SourceFile.h Mon Mar 31 22:59:34 2008
@@ -17,11 +17,12 @@
 #define LLVM_DEBUGGER_SOURCEFILE_H
 
 #include "llvm/System/Path.h"
-#include "llvm/System/MappedFile.h"
+#include "llvm/ADT/OwningPtr.h"
 #include <vector>
 
 namespace llvm {
   class GlobalVariable;
+  class MemoryBuffer;
 
   class SourceFile {
     /// Filename - This is the full path of the file that is loaded.
@@ -35,7 +36,7 @@
     const GlobalVariable *Descriptor;
 
     /// This is the memory mapping for the file so we can gain access to it.
-    sys::MappedFile File;
+    OwningPtr<MemoryBuffer> File;
 
     /// LineOffset - This vector contains a mapping from source line numbers to
     /// their offsets in the file.  This data is computed lazily, the first time
@@ -49,16 +50,9 @@
     /// NOT throw an exception if the file is not found, if there is an error
     /// reading it, or if the user cancels the operation.  Instead, it will just
     /// be an empty source file.
-    SourceFile(const std::string &fn, const GlobalVariable *Desc)
-      : Filename(fn), Descriptor(Desc), File() {
-        std::string ErrMsg;
-      if (File.open(Filename, &ErrMsg))
-        throw ErrMsg;
-      readFile();
-    }
-    ~SourceFile() {
-      File.unmap();
-    }
+    SourceFile(const std::string &fn, const GlobalVariable *Desc);
+    
+    ~SourceFile();
 
     /// getDescriptor - Return the debugging decriptor for this source file.
     ///
@@ -84,10 +78,6 @@
     }
 
   private:
-    /// readFile - Load Filename into memory
-    ///
-    void readFile();
-
     /// calculateLineOffsets - Compute the LineOffset vector for the current
     /// file.
     void calculateLineOffsets() const;

Modified: llvm/trunk/lib/Debugger/SourceFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Debugger/SourceFile.cpp?rev=49027&r1=49026&r2=49027&view=diff

==============================================================================
--- llvm/trunk/lib/Debugger/SourceFile.cpp (original)
+++ llvm/trunk/lib/Debugger/SourceFile.cpp Mon Mar 31 22:59:34 2008
@@ -12,25 +12,32 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Debugger/SourceFile.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include <cassert>
-
 using namespace llvm;
 
-/// readFile - Load Filename
-///
-void SourceFile::readFile() {
-  std::string ErrMsg;
-  if (!File.map(&ErrMsg))
-    throw ErrMsg;
+static const char EmptyFile = 0;
+
+SourceFile::SourceFile(const std::string &fn, const GlobalVariable *Desc)
+  : Filename(fn), Descriptor(Desc) {
+  File.reset(MemoryBuffer::getFileOrSTDIN(fn));
+    
+  // On error, return an empty buffer.
+  if (File == 0)
+    File.reset(MemoryBuffer::getMemBuffer(&EmptyFile, &EmptyFile));
 }
 
+SourceFile::~SourceFile() {
+}
+
+
 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
 ///
 void SourceFile::calculateLineOffsets() const {
   assert(LineOffset.empty() && "Line offsets already computed!");
-  const char *BufPtr = (const char *)File.getBase();
+  const char *BufPtr = File->getBufferStart();
   const char *FileStart = BufPtr;
-  const char *FileEnd = FileStart + File.size();
+  const char *FileEnd = File->getBufferEnd();
   do {
     LineOffset.push_back(BufPtr-FileStart);
 
@@ -54,19 +61,18 @@
 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
                                const char *&LineEnd) const {
   LineStart = LineEnd = 0;
-  if (!File.isMapped()) return;  // Couldn't load file, return null pointers
   if (LineOffset.empty()) calculateLineOffsets();
 
   // Asking for an out-of-range line number?
   if (LineNo >= LineOffset.size()) return;
 
   // Otherwise, they are asking for a valid line, which we can fulfill.
-  LineStart = (const char *)File.getBase()+LineOffset[LineNo];
+  LineStart = File->getBufferStart()+LineOffset[LineNo];
 
   if (LineNo+1 < LineOffset.size())
-    LineEnd = (const char *)File.getBase()+LineOffset[LineNo+1];
+    LineEnd = File->getBufferStart()+LineOffset[LineNo+1];
   else
-    LineEnd = (const char *)File.getBase() + File.size();
+    LineEnd = File->getBufferEnd();
 
   // If the line ended with a newline, strip it off.
   while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))





More information about the llvm-commits mailing list