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
19 #include "llvm/System/Path.h"
20 #include "llvm/System/MappedFile.h"
27 /// Filename - This is the full path of the file that is loaded.
31 /// Descriptor - The debugging descriptor for this source file. If there
32 /// are multiple descriptors for the same file, this is just the first one
35 const GlobalVariable *Descriptor;
37 /// This is the memory mapping for the file so we can gain access to it.
40 /// LineOffset - This vector contains a mapping from source line numbers to
41 /// their offsets in the file. This data is computed lazily, the first time
42 /// it is asked for. If there are zero elements allocated in this vector,
43 /// then it has not yet been computed.
44 mutable std::vector<unsigned> LineOffset;
47 /// SourceFile constructor - Read in the specified source file if it exists,
48 /// but do not build the LineOffsets table until it is requested. This will
49 /// NOT throw an exception if the file is not found, if there is an error
50 /// reading it, or if the user cancels the operation. Instead, it will just
51 /// be an empty source file.
52 SourceFile(const std::string &fn, const GlobalVariable *Desc)
53 : Filename(fn), Descriptor(Desc), File(Filename) {
60 /// getDescriptor - Return the debugging decriptor for this source file.
62 const GlobalVariable *getDescriptor() const { return Descriptor; }
64 /// getFilename - Return the fully resolved path that this file was loaded
66 const std::string &getFilename() const { return Filename.toString(); }
68 /// getSourceLine - Given a line number, return the start and end of the
69 /// line in the file. If the line number is invalid, or if the file could
70 /// not be loaded, null pointers are returned for the start and end of the
71 /// file. Note that line numbers start with 0, not 1. This also strips off
72 /// any newlines from the end of the line, to ease formatting of the text.
73 void getSourceLine(unsigned LineNo, const char *&LineStart,
74 const char *&LineEnd) const;
76 /// getNumLines - Return the number of lines the source file contains.
78 unsigned getNumLines() const {
79 if (LineOffset.empty()) calculateLineOffsets();
80 return LineOffset.size();
84 /// readFile - Load Filename into memory
88 /// calculateLineOffsets - Compute the LineOffset vector for the current
90 void calculateLineOffsets() const;
92 } // end namespace llvm