blob: 9dc0a9616f5f565d9c353ea708e483b4ab8915bc [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 Spencer551ccae2004-09-01 22:55:40 +000015#include "llvm/Support/SlowOperationInformer.h"
16#include "llvm/Support/FileUtilities.h"
Chris Lattner2eacf262004-01-05 05:25:10 +000017#include <iostream>
18#include <cerrno>
19#include <fcntl.h>
20#include <unistd.h>
21using namespace llvm;
22
Reid Spencer663601c2004-12-13 02:59:15 +000023/// readFile - Load Filename
Chris Lattner2eacf262004-01-05 05:25:10 +000024///
25void SourceFile::readFile() {
Reid Spencer663601c2004-12-13 02:59:15 +000026 File.map();
Chris Lattner2eacf262004-01-05 05:25:10 +000027}
28
29/// calculateLineOffsets - Compute the LineOffset vector for the current file.
30///
31void SourceFile::calculateLineOffsets() const {
32 assert(LineOffset.empty() && "Line offsets already computed!");
Reid Spencer663601c2004-12-13 02:59:15 +000033 const char *BufPtr = File.charBase();
34 const char *FileStart = BufPtr;
35 const char *FileEnd = FileStart + File.size();
Chris Lattner2eacf262004-01-05 05:25:10 +000036 do {
37 LineOffset.push_back(BufPtr-FileStart);
38
39 // Scan until we get to a newline.
40 while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
41 ++BufPtr;
42
43 if (BufPtr != FileEnd) {
44 ++BufPtr; // Skip over the \n or \r
45 if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
46 ++BufPtr; // Skip over dos/windows style \r\n's
47 }
48 } while (BufPtr != FileEnd);
49}
50
51
52/// getSourceLine - Given a line number, return the start and end of the line
53/// in the file. If the line number is invalid, or if the file could not be
54/// loaded, null pointers are returned for the start and end of the file. Note
55/// that line numbers start with 0, not 1.
56void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
57 const char *&LineEnd) const {
58 LineStart = LineEnd = 0;
Reid Spencer663601c2004-12-13 02:59:15 +000059 if (!File.isMapped()) return; // Couldn't load file, return null pointers
Chris Lattner2eacf262004-01-05 05:25:10 +000060 if (LineOffset.empty()) calculateLineOffsets();
61
62 // Asking for an out-of-range line number?
63 if (LineNo >= LineOffset.size()) return;
64
65 // Otherwise, they are asking for a valid line, which we can fulfill.
Reid Spencer663601c2004-12-13 02:59:15 +000066 LineStart = File.charBase()+LineOffset[LineNo];
Chris Lattner2eacf262004-01-05 05:25:10 +000067
68 if (LineNo+1 < LineOffset.size())
Reid Spencer663601c2004-12-13 02:59:15 +000069 LineEnd = File.charBase()+LineOffset[LineNo+1];
Chris Lattner2eacf262004-01-05 05:25:10 +000070 else
Reid Spencer663601c2004-12-13 02:59:15 +000071 LineEnd = File.charBase() + File.size();
Chris Lattner2eacf262004-01-05 05:25:10 +000072
73 // If the line ended with a newline, strip it off.
74 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
75 --LineEnd;
76
77 assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
78}
79