Don't attribute in file headers anymore. See llvmdev for the
[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 is distributed under the University of Illinois Open Source
6 // 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 "llvm/System/Path.h"
20 #include "llvm/System/MappedFile.h"
21 #include <vector>
22
23 namespace llvm {
24   class GlobalVariable;
25
26   class SourceFile {
27     /// Filename - This is the full path of the file that is loaded.
28     ///
29     sys::Path Filename;
30
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
33     /// encountered.
34     ///
35     const GlobalVariable *Descriptor;
36
37     /// This is the memory mapping for the file so we can gain access to it.
38     sys::MappedFile File;
39
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;
45
46   public:
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() {
54         std::string ErrMsg;
55       if (File.open(Filename, sys::MappedFile::READ_ACCESS, &ErrMsg))
56         throw ErrMsg;
57       readFile();
58     }
59     ~SourceFile() {
60       File.unmap();
61     }
62
63     /// getDescriptor - Return the debugging decriptor for this source file.
64     ///
65     const GlobalVariable *getDescriptor() const { return Descriptor; }
66
67     /// getFilename - Return the fully resolved path that this file was loaded
68     /// from.
69     const std::string &getFilename() const { return Filename.toString(); }
70
71     /// getSourceLine - Given a line number, return the start and end of the
72     /// line in the file.  If the line number is invalid, or if the file could
73     /// not be loaded, null pointers are returned for the start and end of the
74     /// file.  Note that line numbers start with 0, not 1.  This also strips off
75     /// any newlines from the end of the line, to ease formatting of the text.
76     void getSourceLine(unsigned LineNo, const char *&LineStart,
77                        const char *&LineEnd) const;
78
79     /// getNumLines - Return the number of lines the source file contains.
80     ///
81     unsigned getNumLines() const {
82       if (LineOffset.empty()) calculateLineOffsets();
83       return LineOffset.size();
84     }
85
86   private:
87     /// readFile - Load Filename into memory
88     ///
89     void readFile();
90
91     /// calculateLineOffsets - Compute the LineOffset vector for the current
92     /// file.
93     void calculateLineOffsets() const;
94   };
95 } // end namespace llvm
96
97 #endif