1 //===- SourceFile.h - Class to represent a source code file -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SourceFile class which is used to represent a single
11 // file of source code in the program, caching data from the file to make access
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_DEBUGGER_SOURCEFILE_H
17 #define LLVM_DEBUGGER_SOURCEFILE_H
26 /// Filename - This is the full path of the file that is loaded.
30 /// Descriptor - The debugging descriptor for this source file. If there
31 /// are multiple descriptors for the same file, this is just the first one
34 const GlobalVariable *Descriptor;
36 /// FileStart, FileEnd - These pointers point to the start and end of the
37 /// file data for this file. If there was an error loading the file, these
38 /// pointers will both be null.
39 const char *FileStart, *FileEnd;
41 /// LineOffset - This vector contains a mapping from source line numbers to
42 /// their offsets in the file. This data is computed lazily, the first time
43 /// it is asked for. If there are zero elements allocated in this vector,
44 /// then it has not yet been computed.
45 mutable std::vector<unsigned> LineOffset;
48 /// SourceFile constructor - Read in the specified source file if it exists,
49 /// but do not build the LineOffsets table until it is requested. This will
50 /// NOT throw an exception if the file is not found, if there is an error
51 /// reading it, or if the user cancels the operation. Instead, it will just
52 /// be an empty source file.
53 SourceFile(const std::string &fn, const GlobalVariable *Desc)
54 : Filename(fn), Descriptor(Desc), FileStart(0), FileEnd(0) {
61 /// getDescriptor - Return the debugging decriptor for this source file.
63 const GlobalVariable *getDescriptor() const { return Descriptor; }
65 /// getFilename - Return the fully resolved path that this file was loaded
67 const std::string &getFilename() const { return Filename; }
69 /// getSourceLine - Given a line number, return the start and end of the
70 /// line in the file. If the line number is invalid, or if the file could
71 /// not be loaded, null pointers are returned for the start and end of the
72 /// file. Note that line numbers start with 0, not 1. This also strips off
73 /// any newlines from the end of the line, to ease formatting of the text.
74 void getSourceLine(unsigned LineNo, const char *&LineStart,
75 const char *&LineEnd) const;
77 /// getNumLines - Return the number of lines the source file contains.
79 unsigned getNumLines() const {
80 if (LineOffset.empty()) calculateLineOffsets();
81 return LineOffset.size();
85 /// readFile - Load Filename into FileStart and FileEnd.
89 /// calculateLineOffsets - Compute the LineOffset vector for the current
91 void calculateLineOffsets() const;
93 } // end namespace llvm