blob: 03c60f87f7859705b549d6143f29d0666a67924e [file] [log] [blame]
Chris Lattner2eacf262004-01-05 05:25:10 +00001//===-- SourceFile.cpp - SourceFile implementation for the debugger -------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner2eacf262004-01-05 05:25:10 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner2eacf262004-01-05 05:25:10 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattner2eacf262004-01-05 05:25:10 +000010// This file implements the SourceFile class for the LLVM debugger.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Debugger/SourceFile.h"
Chris Lattner2fcebc52008-04-01 03:59:34 +000015#include "llvm/Support/MemoryBuffer.h"
Reid Spencerc24e94e2004-12-21 00:22:51 +000016#include <cassert>
Chris Lattner2eacf262004-01-05 05:25:10 +000017using namespace llvm;
18
Chris Lattner2fcebc52008-04-01 03:59:34 +000019static const char EmptyFile = 0;
20
21SourceFile::SourceFile(const std::string &fn, const GlobalVariable *Desc)
22 : Filename(fn), Descriptor(Desc) {
23 File.reset(MemoryBuffer::getFileOrSTDIN(fn));
24
25 // On error, return an empty buffer.
26 if (File == 0)
27 File.reset(MemoryBuffer::getMemBuffer(&EmptyFile, &EmptyFile));
Chris Lattner2eacf262004-01-05 05:25:10 +000028}
29
Chris Lattner2fcebc52008-04-01 03:59:34 +000030SourceFile::~SourceFile() {
31}
32
33
Chris Lattner2eacf262004-01-05 05:25:10 +000034/// calculateLineOffsets - Compute the LineOffset vector for the current file.
35///
36void SourceFile::calculateLineOffsets() const {
37 assert(LineOffset.empty() && "Line offsets already computed!");
Chris Lattner2fcebc52008-04-01 03:59:34 +000038 const char *BufPtr = File->getBufferStart();
Reid Spencer663601c2004-12-13 02:59:15 +000039 const char *FileStart = BufPtr;
Chris Lattner2fcebc52008-04-01 03:59:34 +000040 const char *FileEnd = File->getBufferEnd();
Chris Lattner2eacf262004-01-05 05:25:10 +000041 do {
42 LineOffset.push_back(BufPtr-FileStart);
43
44 // Scan until we get to a newline.
45 while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
46 ++BufPtr;
47
48 if (BufPtr != FileEnd) {
49 ++BufPtr; // Skip over the \n or \r
50 if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
51 ++BufPtr; // Skip over dos/windows style \r\n's
52 }
53 } while (BufPtr != FileEnd);
54}
55
56
57/// getSourceLine - Given a line number, return the start and end of the line
58/// in the file. If the line number is invalid, or if the file could not be
59/// loaded, null pointers are returned for the start and end of the file. Note
60/// that line numbers start with 0, not 1.
61void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
62 const char *&LineEnd) const {
63 LineStart = LineEnd = 0;
Chris Lattner2eacf262004-01-05 05:25:10 +000064 if (LineOffset.empty()) calculateLineOffsets();
65
66 // Asking for an out-of-range line number?
67 if (LineNo >= LineOffset.size()) return;
68
69 // Otherwise, they are asking for a valid line, which we can fulfill.
Chris Lattner2fcebc52008-04-01 03:59:34 +000070 LineStart = File->getBufferStart()+LineOffset[LineNo];
Chris Lattner2eacf262004-01-05 05:25:10 +000071
72 if (LineNo+1 < LineOffset.size())
Chris Lattner2fcebc52008-04-01 03:59:34 +000073 LineEnd = File->getBufferStart()+LineOffset[LineNo+1];
Chris Lattner2eacf262004-01-05 05:25:10 +000074 else
Chris Lattner2fcebc52008-04-01 03:59:34 +000075 LineEnd = File->getBufferEnd();
Chris Lattner2eacf262004-01-05 05:25:10 +000076
77 // If the line ended with a newline, strip it off.
78 while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
79 --LineEnd;
80
81 assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
82}