1 //===- SourceFile.h - Class to represent a source code file -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ADT/OwningPtr.h"
28 /// Filename - This is the full path of the file that is loaded.
32 /// Descriptor - The debugging descriptor for this source file. If there
33 /// are multiple descriptors for the same file, this is just the first one
36 const GlobalVariable *Descriptor;
38 /// This is the memory mapping for the file so we can gain access to it.
39 OwningPtr<MemoryBuffer> File;
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);
57 /// getDescriptor - Return the debugging decriptor for this source file.
59 const GlobalVariable *getDescriptor() const { return Descriptor; }
61 /// getFilename - Return the fully resolved path that this file was loaded
63 const std::string &getFilename() const { return Filename.toString(); }
65 /// getSourceLine - Given a line number, return the start and end of the
66 /// line in the file. If the line number is invalid, or if the file could
67 /// not be loaded, null pointers are returned for the start and end of the
68 /// file. Note that line numbers start with 0, not 1. This also strips off
69 /// any newlines from the end of the line, to ease formatting of the text.
70 void getSourceLine(unsigned LineNo, const char *&LineStart,
71 const char *&LineEnd) const;
73 /// getNumLines - Return the number of lines the source file contains.
75 unsigned getNumLines() const {
76 if (LineOffset.empty()) calculateLineOffsets();
77 return static_cast<unsigned>(LineOffset.size());
81 /// calculateLineOffsets - Compute the LineOffset vector for the current
83 void calculateLineOffsets() const;
85 } // end namespace llvm