| 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" | 
| Reid Spencer | c24e94e | 2004-12-21 00:22:51 +0000 | [diff] [blame] | 15 | #include <cassert> | 
|  | 16 |  | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 17 | using namespace llvm; | 
|  | 18 |  | 
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 19 | /// readFile - Load Filename | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 20 | /// | 
|  | 21 | void SourceFile::readFile() { | 
| Reid Spencer | 8627969 | 2006-08-22 16:06:27 +0000 | [diff] [blame] | 22 | std::string ErrMsg; | 
| Reid Spencer | 1cb6057 | 2006-08-22 18:03:02 +0000 | [diff] [blame] | 23 | if (!File.map(&ErrMsg)) | 
| Reid Spencer | 8627969 | 2006-08-22 16:06:27 +0000 | [diff] [blame] | 24 | throw ErrMsg; | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 25 | } | 
|  | 26 |  | 
|  | 27 | /// calculateLineOffsets - Compute the LineOffset vector for the current file. | 
|  | 28 | /// | 
|  | 29 | void SourceFile::calculateLineOffsets() const { | 
|  | 30 | assert(LineOffset.empty() && "Line offsets already computed!"); | 
| Reid Spencer | 663601c | 2004-12-13 02:59:15 +0000 | [diff] [blame] | 31 | const char *BufPtr = File.charBase(); | 
|  | 32 | const char *FileStart = BufPtr; | 
|  | 33 | const char *FileEnd = FileStart + File.size(); | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 34 | do { | 
|  | 35 | LineOffset.push_back(BufPtr-FileStart); | 
|  | 36 |  | 
|  | 37 | // Scan until we get to a newline. | 
|  | 38 | while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r') | 
|  | 39 | ++BufPtr; | 
|  | 40 |  | 
|  | 41 | if (BufPtr != FileEnd) { | 
|  | 42 | ++BufPtr;               // Skip over the \n or \r | 
|  | 43 | if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n') | 
|  | 44 | ++BufPtr;   // Skip over dos/windows style \r\n's | 
|  | 45 | } | 
|  | 46 | } while (BufPtr != FileEnd); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 |  | 
|  | 50 | /// getSourceLine - Given a line number, return the start and end of the line | 
|  | 51 | /// in the file.  If the line number is invalid, or if the file could not be | 
|  | 52 | /// loaded, null pointers are returned for the start and end of the file. Note | 
|  | 53 | /// that line numbers start with 0, not 1. | 
|  | 54 | void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart, | 
|  | 55 | const char *&LineEnd) const { | 
|  | 56 | LineStart = LineEnd = 0; | 
| Reid Spencer | 663601c | 2004-12-13 02:59:15 +0000 | [diff] [blame] | 57 | if (!File.isMapped()) return;  // Couldn't load file, return null pointers | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 58 | if (LineOffset.empty()) calculateLineOffsets(); | 
|  | 59 |  | 
|  | 60 | // Asking for an out-of-range line number? | 
|  | 61 | if (LineNo >= LineOffset.size()) return; | 
|  | 62 |  | 
|  | 63 | // Otherwise, they are asking for a valid line, which we can fulfill. | 
| Reid Spencer | 663601c | 2004-12-13 02:59:15 +0000 | [diff] [blame] | 64 | LineStart = File.charBase()+LineOffset[LineNo]; | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | if (LineNo+1 < LineOffset.size()) | 
| Reid Spencer | 663601c | 2004-12-13 02:59:15 +0000 | [diff] [blame] | 67 | LineEnd = File.charBase()+LineOffset[LineNo+1]; | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 68 | else | 
| Reid Spencer | 663601c | 2004-12-13 02:59:15 +0000 | [diff] [blame] | 69 | LineEnd = File.charBase() + File.size(); | 
| Chris Lattner | 2eacf26 | 2004-01-05 05:25:10 +0000 | [diff] [blame] | 70 |  | 
|  | 71 | // If the line ended with a newline, strip it off. | 
|  | 72 | while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r')) | 
|  | 73 | --LineEnd; | 
|  | 74 |  | 
|  | 75 | assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!"); | 
|  | 76 | } |