Made warning red.
[llvm-complete.git] / lib / Debugger / SourceFile.cpp
blob222cdfa26aaa3e2df6098df71076a7e847e9dfde
1 //===-- 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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Debugger/SourceFile.h"
15 #include <cassert>
17 using namespace llvm;
19 /// readFile - Load Filename
20 ///
21 void SourceFile::readFile() {
22 File.map();
25 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
26 ///
27 void SourceFile::calculateLineOffsets() const {
28 assert(LineOffset.empty() && "Line offsets already computed!");
29 const char *BufPtr = File.charBase();
30 const char *FileStart = BufPtr;
31 const char *FileEnd = FileStart + File.size();
32 do {
33 LineOffset.push_back(BufPtr-FileStart);
35 // Scan until we get to a newline.
36 while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
37 ++BufPtr;
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
44 } while (BufPtr != FileEnd);
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.
52 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
53 const char *&LineEnd) const {
54 LineStart = LineEnd = 0;
55 if (!File.isMapped()) return; // Couldn't load file, return null pointers
56 if (LineOffset.empty()) calculateLineOffsets();
58 // Asking for an out-of-range line number?
59 if (LineNo >= LineOffset.size()) return;
61 // Otherwise, they are asking for a valid line, which we can fulfill.
62 LineStart = File.charBase()+LineOffset[LineNo];
64 if (LineNo+1 < LineOffset.size())
65 LineEnd = File.charBase()+LineOffset[LineNo+1];
66 else
67 LineEnd = File.charBase() + File.size();
69 // If the line ended with a newline, strip it off.
70 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
71 --LineEnd;
73 assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");