SourceManager.h revision 269024
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        uint32_t
68        GetSourceMapModificationID() const
69        {
70            return m_source_map_mod_id;
71        }
72
73        const char *
74        PeekLineData (uint32_t line);
75
76        uint32_t
77        GetLineLength (uint32_t line, bool include_newline_chars);
78
79        uint32_t
80        GetNumLines ();
81
82    protected:
83
84        bool
85        CalculateLineOffsets (uint32_t line = UINT32_MAX);
86
87        FileSpec m_file_spec_orig;  // The original file spec that was used (can be different from m_file_spec)
88        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)
89        TimeValue m_mod_time;       // Keep the modification time that this file data is valid for
90        uint32_t m_source_map_mod_id; // If the target uses path remappings, be sure to clear our notion of a source file if the path modification ID changes
91        lldb::DataBufferSP m_data_sp;
92        typedef std::vector<uint32_t> LineOffsets;
93        LineOffsets m_offsets;
94    };
95
96#endif // SWIG
97
98    typedef std::shared_ptr<File> FileSP;
99
100#ifndef SWIG
101
102   // The SourceFileCache class separates the source manager from the cache of source files, so the
103   // cache can be stored in the Debugger, but the source managers can be per target.
104    class SourceFileCache
105    {
106    public:
107        SourceFileCache () {}
108        ~SourceFileCache() {}
109
110        void AddSourceFile (const FileSP &file_sp);
111        FileSP FindSourceFile (const FileSpec &file_spec) const;
112
113    protected:
114        typedef std::map <FileSpec, FileSP> FileCache;
115        FileCache m_file_cache;
116    };
117#endif
118
119
120    //------------------------------------------------------------------
121    // Constructors and Destructors
122    //------------------------------------------------------------------
123    // A source manager can be made with a non-null target, in which case it can use the path remappings to find
124    // source files that are not in their build locations.  With no target it won't be able to do this.
125    SourceManager (const lldb::DebuggerSP &debugger_sp);
126    SourceManager (const lldb::TargetSP &target_sp);
127
128    ~SourceManager();
129
130
131    FileSP
132    GetLastFile ()
133    {
134        return m_last_file_sp;
135    }
136
137    size_t
138    DisplaySourceLinesWithLineNumbers (const FileSpec &file,
139                                       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    // This variant uses the last file we visited.
147    size_t
148    DisplaySourceLinesWithLineNumbersUsingLastFile (uint32_t start_line,
149                                                    uint32_t count,
150                                                    uint32_t curr_line,
151                                                    const char* current_line_cstr,
152                                                    Stream *s,
153                                                    const SymbolContextList *bp_locs = NULL);
154
155    size_t
156    DisplayMoreWithLineNumbers (Stream *s,
157                                uint32_t count,
158                                bool reverse,
159                                const SymbolContextList *bp_locs = NULL);
160
161    bool
162    SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line);
163
164    bool
165    GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line);
166
167    bool
168    DefaultFileAndLineSet ()
169    {
170        return (m_last_file_sp.get() != NULL);
171    }
172
173    void
174    FindLinesMatchingRegex (FileSpec &file_spec,
175                            RegularExpression& regex,
176                            uint32_t start_line,
177                            uint32_t end_line,
178                            std::vector<uint32_t> &match_lines);
179
180    FileSP
181    GetFile (const FileSpec &file_spec);
182
183protected:
184
185    //------------------------------------------------------------------
186    // Classes that inherit from SourceManager can see and modify these
187    //------------------------------------------------------------------
188    FileSP m_last_file_sp;
189    uint32_t m_last_line;
190    uint32_t m_last_count;
191    bool     m_default_set;
192    lldb::TargetWP m_target_wp;
193    lldb::DebuggerWP m_debugger_wp;
194
195private:
196    //------------------------------------------------------------------
197    // For SourceManager only
198    //------------------------------------------------------------------
199    DISALLOW_COPY_AND_ASSIGN (SourceManager);
200};
201
202bool operator== (const SourceManager::File &lhs, const SourceManager::File &rhs);
203} // namespace lldb_private
204
205#endif  // liblldb_SourceManager_h_
206