rewrite SourceFile to be in terms of MemoryBuffer, not MappedFile.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49027 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2008-04-01 03:59:34 +00:00
parent 23ffec8258
commit 2fcebc5e63
2 changed files with 25 additions and 29 deletions

View File

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

View File

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