Fix braces.
[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/ADT/OwningPtr.h"
21 #include <vector>
22
23 namespace llvm {
24   class GlobalVariable;
25   class MemoryBuffer;
26
27   class SourceFile {
28     /// Filename - This is the full path of the file that is loaded.
29     ///
30     sys::Path Filename;
31
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
34     /// encountered.
35     ///
36     const GlobalVariable *Descriptor;
37
38     /// This is the memory mapping for the file so we can gain access to it.
39     OwningPtr<MemoryBuffer> File;
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     
55     ~SourceFile();
56
57     /// getDescriptor - Return the debugging decriptor for this source file.
58     ///
59     const GlobalVariable *getDescriptor() const { return Descriptor; }
60
61     /// getFilename - Return the fully resolved path that this file was loaded
62     /// from.
63     const std::string &getFilename() const { return Filename.toString(); }
64
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;
72
73     /// getNumLines - Return the number of lines the source file contains.
74     ///
75     unsigned getNumLines() const {
76       if (LineOffset.empty()) calculateLineOffsets();
77       return static_cast<unsigned>(LineOffset.size());
78     }
79
80   private:
81     /// calculateLineOffsets - Compute the LineOffset vector for the current
82     /// file.
83     void calculateLineOffsets() const;
84   };
85 } // end namespace llvm
86
87 #endif