llvm.org GIT mirror llvm / 2fcebc5
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 Chris Lattner 11 years ago
2 changed file(s) with 25 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
1616 #define LLVM_DEBUGGER_SOURCEFILE_H
1717
1818 #include "llvm/System/Path.h"
19 #include "llvm/System/MappedFile.h"
19 #include "llvm/ADT/OwningPtr.h"
2020 #include
2121
2222 namespace llvm {
2323 class GlobalVariable;
24 class MemoryBuffer;
2425
2526 class SourceFile {
2627 /// Filename - This is the full path of the file that is loaded.
3435 const GlobalVariable *Descriptor;
3536
3637 /// This is the memory mapping for the file so we can gain access to it.
37 sys::MappedFile File;
38 OwningPtr File;
3839
3940 /// LineOffset - This vector contains a mapping from source line numbers to
4041 /// their offsets in the file. This data is computed lazily, the first time
4849 /// NOT throw an exception if the file is not found, if there is an error
4950 /// reading it, or if the user cancels the operation. Instead, it will just
5051 /// be an empty source file.
51 SourceFile(const std::string &fn, const GlobalVariable *Desc)
52 : Filename(fn), Descriptor(Desc), File() {
53 std::string ErrMsg;
54 if (File.open(Filename, &ErrMsg))
55 throw ErrMsg;
56 readFile();
57 }
58 ~SourceFile() {
59 File.unmap();
60 }
52 SourceFile(const std::string &fn, const GlobalVariable *Desc);
53
54 ~SourceFile();
6155
6256 /// getDescriptor - Return the debugging decriptor for this source file.
6357 ///
8377 }
8478
8579 private:
86 /// readFile - Load Filename into memory
87 ///
88 void readFile();
89
9080 /// calculateLineOffsets - Compute the LineOffset vector for the current
9181 /// file.
9282 void calculateLineOffsets() const;
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Debugger/SourceFile.h"
14 #include "llvm/Support/MemoryBuffer.h"
1415 #include
15
1616 using namespace llvm;
1717
18 /// readFile - Load Filename
19 ///
20 void SourceFile::readFile() {
21 std::string ErrMsg;
22 if (!File.map(&ErrMsg))
23 throw ErrMsg;
18 static const char EmptyFile = 0;
19
20 SourceFile::SourceFile(const std::string &fn, const GlobalVariable *Desc)
21 : Filename(fn), Descriptor(Desc) {
22 File.reset(MemoryBuffer::getFileOrSTDIN(fn));
23
24 // On error, return an empty buffer.
25 if (File == 0)
26 File.reset(MemoryBuffer::getMemBuffer(&EmptyFile, &EmptyFile));
2427 }
28
29 SourceFile::~SourceFile() {
30 }
31
2532
2633 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
2734 ///
2835 void SourceFile::calculateLineOffsets() const {
2936 assert(LineOffset.empty() && "Line offsets already computed!");
30 const char *BufPtr = (const char *)File.getBase();
37 const char *BufPtr = File->getBufferStart();
3138 const char *FileStart = BufPtr;
32 const char *FileEnd = FileStart + File.size();
39 const char *FileEnd = File->getBufferEnd();
3340 do {
3441 LineOffset.push_back(BufPtr-FileStart);
3542
5360 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
5461 const char *&LineEnd) const {
5562 LineStart = LineEnd = 0;
56 if (!File.isMapped()) return; // Couldn't load file, return null pointers
5763 if (LineOffset.empty()) calculateLineOffsets();
5864
5965 // Asking for an out-of-range line number?
6066 if (LineNo >= LineOffset.size()) return;
6167
6268 // Otherwise, they are asking for a valid line, which we can fulfill.
63 LineStart = (const char *)File.getBase()+LineOffset[LineNo];
69 LineStart = File->getBufferStart()+LineOffset[LineNo];
6470
6571 if (LineNo+1 < LineOffset.size())
66 LineEnd = (const char *)File.getBase()+LineOffset[LineNo+1];
72 LineEnd = File->getBufferStart()+LineOffset[LineNo+1];
6773 else
68 LineEnd = (const char *)File.getBase() + File.size();
74 LineEnd = File->getBufferEnd();
6975
7076 // If the line ended with a newline, strip it off.
7177 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))