blob: 8e40c8815d26b17ad2dfc7ca6634205906999203 [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"
Chris Lattner2eacf262004-01-05 05:25:10 +000015using namespace llvm;
16
Reid Spencer663601c2004-12-13 02:59:15 +000017/// readFile - Load Filename
Chris Lattner2eacf262004-01-05 05:25:10 +000018///
19void SourceFile::readFile() {
Reid Spencer663601c2004-12-13 02:59:15 +000020 File.map();
Chris Lattner2eacf262004-01-05 05:25:10 +000021}
22
23/// calculateLineOffsets - Compute the LineOffset vector for the current file.
24///
25void SourceFile::calculateLineOffsets() const {
26 assert(LineOffset.empty() && "Line offsets already computed!");
Reid Spencer663601c2004-12-13 02:59:15 +000027 const char *BufPtr = File.charBase();
28 const char *FileStart = BufPtr;
29 const char *FileEnd = FileStart + File.size();
Chris Lattner2eacf262004-01-05 05:25:10 +000030 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.
50void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
51 const char *&LineEnd) const {
52 LineStart = LineEnd = 0;
Reid Spencer663601c2004-12-13 02:59:15 +000053 if (!File.isMapped()) return; // Couldn't load file, return null pointers
Chris Lattner2eacf262004-01-05 05:25:10 +000054 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 Spencer663601c2004-12-13 02:59:15 +000060 LineStart = File.charBase()+LineOffset[LineNo];
Chris Lattner2eacf262004-01-05 05:25:10 +000061
62 if (LineNo+1 < LineOffset.size())
Reid Spencer663601c2004-12-13 02:59:15 +000063 LineEnd = File.charBase()+LineOffset[LineNo+1];
Chris Lattner2eacf262004-01-05 05:25:10 +000064 else
Reid Spencer663601c2004-12-13 02:59:15 +000065 LineEnd = File.charBase() + File.size();
Chris Lattner2eacf262004-01-05 05:25:10 +000066
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}