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