SourceManager.h revision 13d24fb1817faa7ccc4cfd799113ba1a2b8968eb
1//===-- SourceManager.h -----------------------------------------*- 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#ifndef liblldb_SourceManager_h_
11#define liblldb_SourceManager_h_
12
13// C Includes
14// C++ Includes
15#include <map>
16#include <vector>
17
18// Other libraries and framework includes
19// Project includes
20#include "lldb/lldb-private.h"
21#include "lldb/Host/FileSpec.h"
22
23namespace lldb_private {
24
25class SourceManager
26{
27public:
28#ifndef SWIG
29
30    class File
31    {
32    friend bool operator== (const SourceManager::File &lhs, const SourceManager::File &rhs);
33    public:
34
35        File (const FileSpec &file_spec, Target *target);
36        ~File();
37
38        size_t
39        DisplaySourceLines (uint32_t line,
40                            uint32_t context_before,
41                            uint32_t context_after,
42                            Stream *s);
43        void
44        FindLinesMatchingRegex (RegularExpression& regex,
45                                uint32_t start_line,
46                                uint32_t end_line,
47                                std::vector<uint32_t> &match_lines);
48
49        bool
50        GetLine (uint32_t line_no, std::string &buffer);
51
52        uint32_t
53        GetLineOffset (uint32_t line);
54
55        bool
56        LineIsValid (uint32_t line);
57
58        bool
59        FileSpecMatches (const FileSpec &file_spec);
60
61        const FileSpec &
62        GetFileSpec ()
63        {
64            return m_file_spec;
65        }
66
67    protected:
68
69        bool
70        CalculateLineOffsets (uint32_t line = UINT32_MAX);
71
72        FileSpec m_file_spec_orig;  // The original file spec that was used (can be different from m_file_spec)
73        FileSpec m_file_spec;       // The actualy file spec being used (if the target has source mappings, this might be different from m_file_spec_orig)
74        TimeValue m_mod_time;       // Keep the modification time that this file data is valid for
75        lldb::DataBufferSP m_data_sp;
76        typedef std::vector<uint32_t> LineOffsets;
77        LineOffsets m_offsets;
78    };
79
80#endif // SWIG
81
82    typedef std::tr1::shared_ptr<File> FileSP;
83
84#ifndef SWIG
85
86   // The SourceFileCache class separates the source manager from the cache of source files, so the
87   // cache can be stored in the Debugger, but the source managers can be per target.
88    class SourceFileCache
89    {
90    public:
91        SourceFileCache () {};
92        ~SourceFileCache() {};
93
94        void AddSourceFile (const FileSP &file_sp);
95        FileSP FindSourceFile (const FileSpec &file_spec) const;
96
97    protected:
98        typedef std::map <FileSpec, FileSP> FileCache;
99        FileCache m_file_cache;
100    };
101#endif
102
103
104    //------------------------------------------------------------------
105    // Constructors and Destructors
106    //------------------------------------------------------------------
107    // A source manager can be made with a non-null target, in which case it can use the path remappings to find
108    // source files that are not in their build locations.  With no target it won't be able to do this.
109    SourceManager (Debugger &debugger);
110    SourceManager (Target &target);
111
112    ~SourceManager();
113
114
115    FileSP
116    GetLastFile ()
117    {
118        return m_last_file_sp;
119    }
120
121    size_t
122    DisplaySourceLines (const FileSpec &file,
123                        uint32_t line,
124                        uint32_t context_before,
125                        uint32_t context_after,
126                        Stream *s);
127
128    size_t
129    DisplaySourceLinesWithLineNumbers (const FileSpec &file,
130                                       uint32_t line,
131                                       uint32_t context_before,
132                                       uint32_t context_after,
133                                       const char* current_line_cstr,
134                                       Stream *s,
135                                       const SymbolContextList *bp_locs = NULL);
136
137    // This variant uses the last file we visited.
138    size_t
139    DisplaySourceLinesWithLineNumbersUsingLastFile (uint32_t line,
140                                                    uint32_t context_before,
141                                                    uint32_t context_after,
142                                                    const char* current_line_cstr,
143                                                    Stream *s,
144                                                    const SymbolContextList *bp_locs = NULL);
145
146    size_t
147    DisplayMoreWithLineNumbers (Stream *s,
148                                const SymbolContextList *bp_locs = NULL);
149
150    bool
151    SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line);
152
153    bool
154    GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line);
155
156    bool
157    DefaultFileAndLineSet ()
158    {
159        return (m_last_file_sp.get() != NULL);
160    }
161
162    void
163    FindLinesMatchingRegex (FileSpec &file_spec,
164                            RegularExpression& regex,
165                            uint32_t start_line,
166                            uint32_t end_line,
167                            std::vector<uint32_t> &match_lines);
168
169protected:
170
171    FileSP
172    GetFile (const FileSpec &file_spec);
173
174    //------------------------------------------------------------------
175    // Classes that inherit from SourceManager can see and modify these
176    //------------------------------------------------------------------
177    FileSP m_last_file_sp;
178    uint32_t m_last_file_line;
179    uint32_t m_last_file_context_before;
180    uint32_t m_last_file_context_after;
181    bool     m_default_set;
182    Target *m_target;
183    Debugger *m_debugger;
184
185private:
186    //------------------------------------------------------------------
187    // For SourceManager only
188    //------------------------------------------------------------------
189    DISALLOW_COPY_AND_ASSIGN (SourceManager);
190};
191
192bool operator== (const SourceManager::File &lhs, const SourceManager::File &rhs);
193} // namespace lldb_private
194
195#endif  // liblldb_SourceManager_h_
196