Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 1 | //===-- SourceFile.cpp - SourceFile implementation for the debugger -------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 10 | // This file implements the SourceFile class for the LLVM debugger. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Debugger/SourceFile.h" |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | c24e94e | 2004-12-21 00:22:51 +0000 | [diff] [blame] | 16 | #include <cassert> |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 19 | static const char EmptyFile = 0; |
| 20 | |
| 21 | SourceFile::SourceFile(const std::string &fn, const GlobalVariable *Desc) |
| 22 | : Filename(fn), Descriptor(Desc) { |
| 23 | File.reset(MemoryBuffer::getFileOrSTDIN(fn)); |
| 24 | |
| 25 | // On error, return an empty buffer. |
| 26 | if (File == 0) |
| 27 | File.reset(MemoryBuffer::getMemBuffer(&EmptyFile, &EmptyFile)); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 30 | SourceFile::~SourceFile() { |
| 31 | } |
| 32 | |
| 33 | |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 34 | /// calculateLineOffsets - Compute the LineOffset vector for the current file. |
| 35 | /// |
| 36 | void SourceFile::calculateLineOffsets() const { |
| 37 | assert(LineOffset.empty() && "Line offsets already computed!"); |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 38 | const char *BufPtr = File->getBufferStart(); |
Reid Spencer | 663601c | 2004-12-13 02:59:15 +0000 | [diff] [blame] | 39 | const char *FileStart = BufPtr; |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 40 | const char *FileEnd = File->getBufferEnd(); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 41 | do { |
| 42 | LineOffset.push_back(BufPtr-FileStart); |
| 43 | |
| 44 | // Scan until we get to a newline. |
| 45 | while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r') |
| 46 | ++BufPtr; |
| 47 | |
| 48 | if (BufPtr != FileEnd) { |
| 49 | ++BufPtr; // Skip over the \n or \r |
| 50 | if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n') |
| 51 | ++BufPtr; // Skip over dos/windows style \r\n's |
| 52 | } |
| 53 | } while (BufPtr != FileEnd); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /// getSourceLine - Given a line number, return the start and end of the line |
| 58 | /// in the file. If the line number is invalid, or if the file could not be |
| 59 | /// loaded, null pointers are returned for the start and end of the file. Note |
| 60 | /// that line numbers start with 0, not 1. |
| 61 | void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart, |
| 62 | const char *&LineEnd) const { |
| 63 | LineStart = LineEnd = 0; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 64 | if (LineOffset.empty()) calculateLineOffsets(); |
| 65 | |
| 66 | // Asking for an out-of-range line number? |
| 67 | if (LineNo >= LineOffset.size()) return; |
| 68 | |
| 69 | // Otherwise, they are asking for a valid line, which we can fulfill. |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 70 | LineStart = File->getBufferStart()+LineOffset[LineNo]; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 71 | |
| 72 | if (LineNo+1 < LineOffset.size()) |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 73 | LineEnd = File->getBufferStart()+LineOffset[LineNo+1]; |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 74 | else |
Chris Lattner | 2fcebc5 | 2008-04-01 03:59:34 +0000 | [diff] [blame] | 75 | LineEnd = File->getBufferEnd(); |
Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 76 | |
| 77 | // If the line ended with a newline, strip it off. |
| 78 | while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r')) |
| 79 | --LineEnd; |
| 80 | |
| 81 | assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!"); |
| 82 | } |