LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 <string>
00020 #include <vector>
00021 
00022 namespace llvm {
00023   class GlobalVariable;
00024 
00025   class SourceFile {
00026     /// Filename - This is the full path of the file that is loaded.
00027     ///
00028     std::string Filename;
00029 
00030     /// Descriptor - The debugging descriptor for this source file.  If there
00031     /// are multiple descriptors for the same file, this is just the first one
00032     /// encountered.
00033     ///
00034     const GlobalVariable *Descriptor;
00035 
00036     /// FileStart, FileEnd - These pointers point to the start and end of the
00037     /// file data for this file.  If there was an error loading the file, these
00038     /// pointers will both be null.
00039     const char *FileStart, *FileEnd;
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       : Filename(fn), Descriptor(Desc), FileStart(0), FileEnd(0) {
00055       readFile();
00056     }
00057     ~SourceFile() {
00058       delete[] FileStart;
00059     }
00060 
00061     /// getDescriptor - Return the debugging decriptor for this source file.
00062     ///
00063     const GlobalVariable *getDescriptor() const { return Descriptor; }
00064     
00065     /// getFilename - Return the fully resolved path that this file was loaded
00066     /// from.
00067     const std::string &getFilename() const { return Filename; }
00068     
00069     /// getSourceLine - Given a line number, return the start and end of the
00070     /// line in the file.  If the line number is invalid, or if the file could
00071     /// not be loaded, null pointers are returned for the start and end of the
00072     /// file.  Note that line numbers start with 0, not 1.  This also strips off
00073     /// any newlines from the end of the line, to ease formatting of the text.
00074     void getSourceLine(unsigned LineNo, const char *&LineStart,
00075                        const char *&LineEnd) const;
00076   
00077     /// getNumLines - Return the number of lines the source file contains.
00078     ///
00079     unsigned getNumLines() const {
00080       if (LineOffset.empty()) calculateLineOffsets();
00081       return LineOffset.size();
00082     }
00083 
00084   private:
00085     /// readFile - Load Filename into FileStart and FileEnd.
00086     ///
00087     void readFile();
00088     
00089     /// calculateLineOffsets - Compute the LineOffset vector for the current
00090     /// file.
00091     void calculateLineOffsets() const;
00092   };
00093 } // end namespace llvm
00094 
00095 #endif