blob: 23144d9ea5ddd0a7a120a220173f216be61ddfc8 [file] [log] [blame]
Chris Lattner2eacf262004-01-05 05:25:10 +00001//===-- 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"
Reid Spencerc24e94e2004-12-21 00:22:51 +000015#include <cassert>
16
Chris Lattner2eacf262004-01-05 05:25:10 +000017using namespace llvm;
18
Reid Spencer663601c2004-12-13 02:59:15 +000019/// readFile - Load Filename
Chris Lattner2eacf262004-01-05 05:25:10 +000020///
21void SourceFile::readFile() {
Reid Spencer663601c2004-12-13 02:59:15 +000022 File.map();
Chris Lattner2eacf262004-01-05 05:25:10 +000023}
24
25/// calculateLineOffsets - Compute the LineOffset vector for the current file.
26///
27void SourceFile::calculateLineOffsets() const {
28 assert(LineOffset.empty() && "Line offsets already computed!");
Reid Spencer663601c2004-12-13 02:59:15 +000029 const char *BufPtr = File.charBase();
30 const char *FileStart = BufPtr;
31 const char *FileEnd = FileStart + File.size();
Chris Lattner2eacf262004-01-05 05:25:10 +000032 do {
33 LineOffset.push_back(BufPtr-FileStart);
34
35 // Scan until we get to a newline.
36 while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
37 ++BufPtr;
38
39 if (BufPtr != FileEnd) {
40 ++BufPtr; // Skip over the \n or \r
41 if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
42 ++BufPtr; // Skip over dos/windows style \r\n's
43 }
44 } while (BufPtr != FileEnd);
45}
46
47
48/// getSourceLine - Given a line number, return the start and end of the line
49/// in the file. If the line number is invalid, or if the file could not be
50/// loaded, null pointers are returned for the start and end of the file. Note
51/// that line numbers start with 0, not 1.
52void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
53 const char *&LineEnd) const {
54 LineStart = LineEnd = 0;
Reid Spencer663601c2004-12-13 02:59:15 +000055 if (!File.isMapped()) return; // Couldn't load file, return null pointers
Chris Lattner2eacf262004-01-05 05:25:10 +000056 if (LineOffset.empty()) calculateLineOffsets();
57
58 // Asking for an out-of-range line number?
59 if (LineNo >= LineOffset.size()) return;
60
61 // Otherwise, they are asking for a valid line, which we can fulfill.
Reid Spencer663601c2004-12-13 02:59:15 +000062 LineStart = File.charBase()+LineOffset[LineNo];
Chris Lattner2eacf262004-01-05 05:25:10 +000063
64 if (LineNo+1 < LineOffset.size())
Reid Spencer663601c2004-12-13 02:59:15 +000065 LineEnd = File.charBase()+LineOffset[LineNo+1];
Chris Lattner2eacf262004-01-05 05:25:10 +000066 else
Reid Spencer663601c2004-12-13 02:59:15 +000067 LineEnd = File.charBase() + File.size();
Chris Lattner2eacf262004-01-05 05:25:10 +000068
69 // If the line ended with a newline, strip it off.
70 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
71 --LineEnd;
72
73 assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
74}