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