Initial implementation of some source-level debugging stuff
[oota-llvm.git] / include / llvm / Debugger / SourceFile.h
1 //===- SourceFile.h - Class to represent a source code file -----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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
12 // efficient.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_DEBUGGER_SOURCEFILE_H
17 #define LLVM_DEBUGGER_SOURCEFILE_H
18
19 #include <string>
20 #include <vector>
21
22 namespace llvm {
23   class GlobalVariable;
24
25   class SourceFile {
26     /// Filename - This is the full path of the file that is loaded.
27     ///
28     std::string Filename;
29
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
32     /// encountered.
33     ///
34     const GlobalVariable *Descriptor;
35
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;
40
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;
46
47   public:
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) {
55       readFile();
56     }
57     ~SourceFile() {
58       delete[] FileStart;
59     }
60
61     /// getDescriptor - Return the debugging decriptor for this source file.
62     ///
63     const GlobalVariable *getDescriptor() const { return Descriptor; }
64     
65     /// getFilename - Return the fully resolved path that this file was loaded
66     /// from.
67     const std::string &getFilename() const { return Filename; }
68     
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;
76   
77     /// getNumLines - Return the number of lines the source file contains.
78     ///
79     unsigned getNumLines() const {
80       if (LineOffset.empty()) calculateLineOffsets();
81       return LineOffset.size();
82     }
83
84   private:
85     /// readFile - Load Filename into FileStart and FileEnd.
86     ///
87     void readFile();
88     
89     /// calculateLineOffsets - Compute the LineOffset vector for the current
90     /// file.
91     void calculateLineOffsets() const;
92   };
93 } // end namespace llvm
94
95 #endif