da2d3475df5c3e8410dd7ba2a8f60b18d5e80f12
[oota-llvm.git] / lib / Debugger / SourceFile.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Debugger/SourceFile.h"
15 #include "llvm/Support/SlowOperationInformer.h"
16 #include <iostream>
17 #include <cerrno>
18 #include <fcntl.h>
19 #include <unistd.h>
20 using namespace llvm;
21
22 /// readFile - Load Filename 
23 ///
24 void SourceFile::readFile() {
25   File.map();
26 }
27
28 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
29 ///
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();
35   do {
36     LineOffset.push_back(BufPtr-FileStart);
37
38     // Scan until we get to a newline.
39     while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
40       ++BufPtr;
41
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
46     }
47   } while (BufPtr != FileEnd);
48 }
49
50
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();
60
61   // Asking for an out-of-range line number?
62   if (LineNo >= LineOffset.size()) return;
63
64   // Otherwise, they are asking for a valid line, which we can fulfill.
65   LineStart = File.charBase()+LineOffset[LineNo];
66
67   if (LineNo+1 < LineOffset.size())
68     LineEnd = File.charBase()+LineOffset[LineNo+1];
69   else
70     LineEnd = File.charBase() + File.size();
71
72   // If the line ended with a newline, strip it off.
73   while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
74     --LineEnd;
75
76   assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
77 }
78