LLVM API Documentation

SourceFile.cpp

Go to the documentation of this file.
00001 //===-- SourceFile.cpp - SourceFile implementation for the debugger -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SourceFile class for the LLVM debugger.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Debugger/SourceFile.h"
00015 #include <cassert>
00016 
00017 using namespace llvm;
00018 
00019 /// readFile - Load Filename
00020 ///
00021 void SourceFile::readFile() {
00022   File.map();
00023 }
00024 
00025 /// calculateLineOffsets - Compute the LineOffset vector for the current file.
00026 ///
00027 void SourceFile::calculateLineOffsets() const {
00028   assert(LineOffset.empty() && "Line offsets already computed!");
00029   const char *BufPtr = File.charBase();
00030   const char *FileStart = BufPtr;
00031   const char *FileEnd = FileStart + File.size();
00032   do {
00033     LineOffset.push_back(BufPtr-FileStart);
00034 
00035     // Scan until we get to a newline.
00036     while (BufPtr != FileEnd && *BufPtr != '\n' && *BufPtr != '\r')
00037       ++BufPtr;
00038 
00039     if (BufPtr != FileEnd) {
00040       ++BufPtr;               // Skip over the \n or \r
00041       if (BufPtr[-1] == '\r' && BufPtr != FileEnd && BufPtr[0] == '\n')
00042         ++BufPtr;   // Skip over dos/windows style \r\n's
00043     }
00044   } while (BufPtr != FileEnd);
00045 }
00046 
00047 
00048 /// getSourceLine - Given a line number, return the start and end of the line
00049 /// in the file.  If the line number is invalid, or if the file could not be
00050 /// loaded, null pointers are returned for the start and end of the file. Note
00051 /// that line numbers start with 0, not 1.
00052 void SourceFile::getSourceLine(unsigned LineNo, const char *&LineStart,
00053                                const char *&LineEnd) const {
00054   LineStart = LineEnd = 0;
00055   if (!File.isMapped()) return;  // Couldn't load file, return null pointers
00056   if (LineOffset.empty()) calculateLineOffsets();
00057 
00058   // Asking for an out-of-range line number?
00059   if (LineNo >= LineOffset.size()) return;
00060 
00061   // Otherwise, they are asking for a valid line, which we can fulfill.
00062   LineStart = File.charBase()+LineOffset[LineNo];
00063 
00064   if (LineNo+1 < LineOffset.size())
00065     LineEnd = File.charBase()+LineOffset[LineNo+1];
00066   else
00067     LineEnd = File.charBase() + File.size();
00068 
00069   // If the line ended with a newline, strip it off.
00070   while (LineEnd != LineStart && (LineEnd[-1] == '\n' || LineEnd[-1] == '\r'))
00071     --LineEnd;
00072 
00073   assert(LineEnd >= LineStart && "We somehow got our pointers swizzled!");
00074 }