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 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 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/System/MappedFile.h"
00021 #include <vector>
00022 
00023 namespace llvm {
00024   class GlobalVariable;
00025 
00026   class SourceFile {
00027     /// Filename - This is the full path of the file that is loaded.
00028     ///
00029     sys::Path Filename;
00030 
00031     /// Descriptor - The debugging descriptor for this source file.  If there
00032     /// are multiple descriptors for the same file, this is just the first one
00033     /// encountered.
00034     ///
00035     const GlobalVariable *Descriptor;
00036 
00037     /// This is the memory mapping for the file so we can gain access to it.
00038     sys::MappedFile File;
00039 
00040     /// LineOffset - This vector contains a mapping from source line numbers to
00041     /// their offsets in the file.  This data is computed lazily, the first time
00042     /// it is asked for.  If there are zero elements allocated in this vector,
00043     /// then it has not yet been computed.
00044     mutable std::vector<unsigned> LineOffset;
00045 
00046   public:
00047     /// SourceFile constructor - Read in the specified source file if it exists,
00048     /// but do not build the LineOffsets table until it is requested.  This will
00049     /// NOT throw an exception if the file is not found, if there is an error
00050     /// reading it, or if the user cancels the operation.  Instead, it will just
00051     /// be an empty source file.
00052     SourceFile(const std::string &fn, const GlobalVariable *Desc)
00053       : Filename(fn), Descriptor(Desc), File(Filename) {
00054       readFile();
00055     }
00056     ~SourceFile() {
00057       File.unmap();
00058     }
00059 
00060     /// getDescriptor - Return the debugging decriptor for this source file.
00061     ///
00062     const GlobalVariable *getDescriptor() const { return Descriptor; }
00063 
00064     /// getFilename - Return the fully resolved path that this file was loaded
00065     /// from.
00066     const std::string &getFilename() const { return Filename.toString(); }
00067 
00068     /// getSourceLine - Given a line number, return the start and end of the
00069     /// line in the file.  If the line number is invalid, or if the file could
00070     /// not be loaded, null pointers are returned for the start and end of the
00071     /// file.  Note that line numbers start with 0, not 1.  This also strips off
00072     /// any newlines from the end of the line, to ease formatting of the text.
00073     void getSourceLine(unsigned LineNo, const char *&LineStart,
00074                        const char *&LineEnd) const;
00075 
00076     /// getNumLines - Return the number of lines the source file contains.
00077     ///
00078     unsigned getNumLines() const {
00079       if (LineOffset.empty()) calculateLineOffsets();
00080       return LineOffset.size();
00081     }
00082 
00083   private:
00084     /// readFile - Load Filename into memory
00085     ///
00086     void readFile();
00087 
00088     /// calculateLineOffsets - Compute the LineOffset vector for the current
00089     /// file.
00090     void calculateLineOffsets() const;
00091   };
00092 } // end namespace llvm
00093 
00094 #endif