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 "Support/SlowOperationInformer.h"
16 #include "Support/FileUtilities.h"
23 /// readFile - Load Filename into FileStart and FileEnd.
25 void SourceFile::readFile() {
26 ssize_t FileSize = getFileSize(Filename);
28 FDHandle FD(open(Filename.c_str(), O_RDONLY));
30 char *FilePos = new char[FileSize];
33 // If this takes a long time, inform the user what we are doing.
34 SlowOperationInformer SOI("loading source file '" + Filename + "'");
37 // Read in the whole buffer.
38 unsigned Amount = FileSize;
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)
45 else if (ReadAmount <= 0) {
46 // Couldn't read whole file just free memory and continue.
47 throw "Error reading file '" + Filename + "'!";
50 FilePos += ReadAmount;
52 SOI.progress(FileSize-Amount, FileSize);
55 } catch (const std::string &Msg) {
56 std::cout << Msg << "\n";
57 // If the user cancels the operation, clean up after ourselves.
63 FileEnd = FileStart+FileSize;
68 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
70 void SourceFile::calculateLineOffsets() const {
71 assert(LineOffset.empty() && "Line offsets already computed!");
72 const char *BufPtr = FileStart;
74 LineOffset.push_back(BufPtr-FileStart);
76 // Scan until we get to a newline.
77 while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
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
85 } while (BufPtr != FileEnd);
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();
99 // Asking for an out-of-range line number?
100 if (LineNo >= LineOffset.size()) return;
102 // Otherwise, they are asking for a valid line, which we can fulfill.
103 LineStart = FileStart+LineOffset[LineNo];
105 if (LineNo+1 < LineOffset.size())
106 LineEnd = FileStart+LineOffset[LineNo+1];
110 // If the line ended with a newline, strip it off.
111 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
114 assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");