1 //===-- SourceFile.cpp - SourceFile implementation for the debugger -------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SourceFile class for the LLVM debugger.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Debugger/SourceFile.h"
15 #include "llvm/Support/SlowOperationInformer.h"
22 /// readFile - Load Filename
24 void SourceFile::readFile() {
28 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
30 void SourceFile::calculateLineOffsets() const {
31 assert(LineOffset.empty() && "Line offsets already computed!");
32 const char *BufPtr = File.charBase();
33 const char *FileStart = BufPtr;
34 const char *FileEnd = FileStart + File.size();
36 LineOffset.push_back(BufPtr-FileStart);
38 // Scan until we get to a newline.
39 while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
42 if (BufPtr != FileEnd) {
43 ++BufPtr; // Skip over the \n or \r
44 if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
45 ++BufPtr; // Skip over dos/windows style \r\n's
47 } while (BufPtr != FileEnd);
51 /// getSourceLine - Given a line number, return the start and end of the line
52 /// in the file. If the line number is invalid, or if the file could not be
53 /// loaded, null pointers are returned for the start and end of the file. Note
54 /// that line numbers start with 0, not 1.
55 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
56 const char *&LineEnd) const {
57 LineStart = LineEnd = 0;
58 if (!File.isMapped()) return; // Couldn't load file, return null pointers
59 if (LineOffset.empty()) calculateLineOffsets();
61 // Asking for an out-of-range line number?
62 if (LineNo >= LineOffset.size()) return;
64 // Otherwise, they are asking for a valid line, which we can fulfill.
65 LineStart = File.charBase()+LineOffset[LineNo];
67 if (LineNo+1 < LineOffset.size())
68 LineEnd = File.charBase()+LineOffset[LineNo+1];
70 LineEnd = File.charBase() + File.size();
72 // If the line ended with a newline, strip it off.
73 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
76 assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");