2004-01-05 05:25:10 +00:00
|
|
|
//===-- SourceFile.cpp - SourceFile implementation for the debugger -------===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2004-01-05 05:25:10 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2004-01-05 05:25:10 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2004-01-05 05:25:10 +00:00
|
|
|
// This file implements the SourceFile class for the LLVM debugger.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Debugger/SourceFile.h"
|
2004-12-21 00:22:51 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2004-01-05 05:25:10 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2005-04-21 22:36:52 +00:00
|
|
|
/// readFile - Load Filename
|
2004-01-05 05:25:10 +00:00
|
|
|
///
|
|
|
|
void SourceFile::readFile() {
|
2006-08-22 16:06:27 +00:00
|
|
|
std::string ErrMsg;
|
2006-08-22 18:03:02 +00:00
|
|
|
if (!File.map(&ErrMsg))
|
2006-08-22 16:06:27 +00:00
|
|
|
throw ErrMsg;
|
2004-01-05 05:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// calculateLineOffsets - Compute the LineOffset vector for the current file.
|
|
|
|
///
|
|
|
|
void SourceFile::calculateLineOffsets() const {
|
|
|
|
assert(LineOffset.empty() && "Line offsets already computed!");
|
2004-12-13 02:59:15 +00:00
|
|
|
const char *BufPtr = File.charBase();
|
|
|
|
const char *FileStart = BufPtr;
|
|
|
|
const char *FileEnd = FileStart + File.size();
|
2004-01-05 05:25:10 +00:00
|
|
|
do {
|
|
|
|
LineOffset.push_back(BufPtr-FileStart);
|
|
|
|
|
|
|
|
// Scan until we get to a newline.
|
|
|
|
while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
|
|
|
|
++BufPtr;
|
|
|
|
|
|
|
|
if (BufPtr != FileEnd) {
|
|
|
|
++BufPtr; // Skip over the \n or \r
|
|
|
|
if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
|
|
|
|
++BufPtr; // Skip over dos/windows style \r\n's
|
|
|
|
}
|
|
|
|
} while (BufPtr != FileEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getSourceLine - Given a line number, return the start and end of the line
|
|
|
|
/// in the file. If the line number is invalid, or if the file could not be
|
|
|
|
/// loaded, null pointers are returned for the start and end of the file. Note
|
|
|
|
/// that line numbers start with 0, not 1.
|
|
|
|
void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
|
|
|
|
const char *&LineEnd) const {
|
|
|
|
LineStart = LineEnd = 0;
|
2004-12-13 02:59:15 +00:00
|
|
|
if (!File.isMapped()) return; // Couldn't load file, return null pointers
|
2004-01-05 05:25:10 +00:00
|
|
|
if (LineOffset.empty()) calculateLineOffsets();
|
|
|
|
|
|
|
|
// Asking for an out-of-range line number?
|
|
|
|
if (LineNo >= LineOffset.size()) return;
|
|
|
|
|
|
|
|
// Otherwise, they are asking for a valid line, which we can fulfill.
|
2004-12-13 02:59:15 +00:00
|
|
|
LineStart = File.charBase()+LineOffset[LineNo];
|
2004-01-05 05:25:10 +00:00
|
|
|
|
|
|
|
if (LineNo+1 < LineOffset.size())
|
2004-12-13 02:59:15 +00:00
|
|
|
LineEnd = File.charBase()+LineOffset[LineNo+1];
|
2004-01-05 05:25:10 +00:00
|
|
|
else
|
2004-12-13 02:59:15 +00:00
|
|
|
LineEnd = File.charBase() + File.size();
|
2004-01-05 05:25:10 +00:00
|
|
|
|
|
|
|
// If the line ended with a newline, strip it off.
|
|
|
|
while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
|
|
|
|
--LineEnd;
|
|
|
|
|
|
|
|
assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
|
|
|
|
}
|