Changes For Bug 352
[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 "llvm/Support/FileUtilities.h"
17 #include <iostream>
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <unistd.h>
21 using namespace llvm;
22
23 /// readFile - Load Filename into FileStart and FileEnd.
24 ///
25 void SourceFile::readFile() {
26   ssize_t FileSize = getFileSize(Filename);
27   if (FileSize != -1) {
28     FDHandle FD(open(Filename.c_str(), O_RDONLY));
29     if (FD != -1) {
30       char *FilePos = new char[FileSize];
31       FileStart = FilePos;
32
33       // If this takes a long time, inform the user what we are doing.
34       SlowOperationInformer SOI("loading source file '" + Filename + "'");
35
36       try {
37         // Read in the whole buffer.
38         unsigned Amount = FileSize;
39         while (Amount) {
40           unsigned AmountToRead = 512*1024;
41           if (Amount < AmountToRead) AmountToRead = Amount;
42           ssize_t ReadAmount = read(FD, FilePos, AmountToRead);
43           if (ReadAmount < 0 && errno == EINTR)
44             continue;
45           else if (ReadAmount <= 0) {
46             // Couldn't read whole file just free memory and continue.
47             throw "Error reading file '" + Filename + "'!";
48           }
49           Amount -= ReadAmount;
50           FilePos += ReadAmount;
51           
52           SOI.progress(FileSize-Amount, FileSize);
53         }
54
55       } catch (const std::string &Msg) {
56         std::cout << Msg << "\n";
57         // If the user cancels the operation, clean up after ourselves.
58         delete [] FileStart;
59         FileStart = 0;
60         return;
61       }
62       
63       FileEnd = FileStart+FileSize;
64     }
65   }
66 }
67
68 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
69 ///
70 void SourceFile::calculateLineOffsets() const {
71   assert(LineOffset.empty() && "Line offsets already computed!");
72   const char *BufPtr = FileStart;
73   do {
74     LineOffset.push_back(BufPtr-FileStart);
75
76     // Scan until we get to a newline.
77     while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
78       ++BufPtr;
79
80     if (BufPtr != FileEnd) {
81       ++BufPtr;               // Skip over the \n or \r
82       if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
83         ++BufPtr;   // Skip over dos/windows style \r\n's
84     }
85   } while (BufPtr != FileEnd);
86 }
87
88
89 /// getSourceLine - Given a line number, return the start and end of the line
90 /// in the file.  If the line number is invalid, or if the file could not be
91 /// loaded, null pointers are returned for the start and end of the file. Note
92 /// that line numbers start with 0, not 1.
93 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
94                                const char *&LineEnd) const {
95   LineStart = LineEnd = 0;
96   if (FileStart == 0) return;  // Couldn't load file, return null pointers
97   if (LineOffset.empty()) calculateLineOffsets();
98
99   // Asking for an out-of-range line number?
100   if (LineNo >= LineOffset.size()) return;
101
102   // Otherwise, they are asking for a valid line, which we can fulfill.
103   LineStart = FileStart+LineOffset[LineNo];
104
105   if (LineNo+1 < LineOffset.size())
106     LineEnd = FileStart+LineOffset[LineNo+1];
107   else
108     LineEnd = FileEnd;
109
110   // If the line ended with a newline, strip it off.
111   while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
112     --LineEnd;
113
114   assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
115 }
116