LLVM API Documentation

SourceFile.h

Go to the documentation of this file.
00001 //===- SourceFile.h - Class to represent a source code file -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the SourceFile class which is used to represent a single
00011 // file of source code in the program, caching data from the file to make access
00012 // efficient.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_DEBUGGER_SOURCEFILE_H
00017 #define LLVM_DEBUGGER_SOURCEFILE_H
00018 
00019 #include "llvm/System/Path.h"
00020 #include "llvm/ADT/OwningPtr.h"
00021 #include <vector>
00022 
00023 namespace llvm {
00024   class GlobalVariable;
00025   class MemoryBuffer;
00026 
00027   class SourceFile {
00028     /// Filename - This is the full path of the file that is loaded.
00029     ///
00030     sys::Path Filename;
00031 
00032     /// Descriptor - The debugging descriptor for this source file.  If there
00033     /// are multiple descriptors for the same file, this is just the first one
00034     /// encountered.
00035     ///
00036     const GlobalVariable *Descriptor;
00037 
00038     /// This is the memory mapping for the file so we can gain access to it.
00039     OwningPtr<MemoryBuffer> File;
00040 
00041     /// LineOffset - This vector contains a mapping from source line numbers to
00042     /// their offsets in the file.  This data is computed lazily, the first time
00043     /// it is asked for.  If there are zero elements allocated in this vector,
00044     /// then it has not yet been computed.
00045     mutable std::vector<unsigned> LineOffset;
00046 
00047   public:
00048     /// SourceFile constructor - Read in the specified source file if it exists,
00049     /// but do not build the LineOffsets table until it is requested.  This will
00050     /// NOT throw an exception if the file is not found, if there is an error
00051     /// reading it, or if the user cancels the operation.  Instead, it will just
00052     /// be an empty source file.
00053     SourceFile(const std::string &fn, const GlobalVariable *Desc);
00054     
00055     ~SourceFile();
00056 
00057     /// getDescriptor - Return the debugging decriptor for this source file.
00058     ///
00059     const GlobalVariable *getDescriptor() const { return Descriptor; }
00060 
00061     /// getFilename - Return the fully resolved path that this file was loaded
00062     /// from.
00063     const std::string &getFilename() const { return Filename.toString(); }
00064 
00065     /// getSourceLine - Given a line number, return the start and end of the
00066     /// line in the file.  If the line number is invalid, or if the file could
00067     /// not be loaded, null pointers are returned for the start and end of the
00068     /// file.  Note that line numbers start with 0, not 1.  This also strips off
00069     /// any newlines from the end of the line, to ease formatting of the text.
00070     void getSourceLine(unsigned LineNo, const char *&LineStart,
00071                        const char *&LineEnd) const;
00072 
00073     /// getNumLines - Return the number of lines the source file contains.
00074     ///
00075     unsigned getNumLines() const {
00076       if (LineOffset.empty()) calculateLineOffsets();
00077       return static_cast<unsigned>(LineOffset.size());
00078     }
00079 
00080   private:
00081     /// calculateLineOffsets - Compute the LineOffset vector for the current
00082     /// file.
00083     void calculateLineOffsets() const;
00084   };
00085 } // end namespace llvm
00086 
00087 #endif