1 //===-- SourceManager.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_SourceManager_h_
11 #define liblldb_SourceManager_h_
14 #include "lldb/Host/FileSpec.h"
15 #include "lldb/lldb-private.h"
17 // Other libraries and framework includes
18 #include "llvm/Support/Chrono.h"
26 namespace lldb_private {
32 friend bool operator==(const SourceManager::File &lhs,
33 const SourceManager::File &rhs);
36 File(const FileSpec &file_spec, Target *target);
37 File(const FileSpec &file_spec, lldb::DebuggerSP debugger_sp);
40 void UpdateIfNeeded();
42 size_t DisplaySourceLines(uint32_t line, uint32_t column,
43 uint32_t context_before, uint32_t context_after,
45 void FindLinesMatchingRegex(RegularExpression ®ex, uint32_t start_line,
47 std::vector<uint32_t> &match_lines);
49 bool GetLine(uint32_t line_no, std::string &buffer);
51 uint32_t GetLineOffset(uint32_t line);
53 bool LineIsValid(uint32_t line);
55 bool FileSpecMatches(const FileSpec &file_spec);
57 const FileSpec &GetFileSpec() { return m_file_spec; }
59 uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
61 const char *PeekLineData(uint32_t line);
63 uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
65 uint32_t GetNumLines();
68 bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
70 FileSpec m_file_spec_orig; // The original file spec that was used (can be
71 // different from m_file_spec)
72 FileSpec m_file_spec; // The actually file spec being used (if the target
73 // has source mappings, this might be different from
76 // Keep the modification time that this file data is valid for
77 llvm::sys::TimePoint<> m_mod_time;
79 // If the target uses path remappings, be sure to clear our notion of a
80 // source file if the path modification ID changes
81 uint32_t m_source_map_mod_id = 0;
82 lldb::DataBufferSP m_data_sp;
83 typedef std::vector<uint32_t> LineOffsets;
84 LineOffsets m_offsets;
85 lldb::DebuggerWP m_debugger_wp;
88 void CommonInitializer(const FileSpec &file_spec, Target *target);
92 typedef std::shared_ptr<File> FileSP;
95 // The SourceFileCache class separates the source manager from the cache of
96 // source files, so the
97 // cache can be stored in the Debugger, but the source managers can be per
99 class SourceFileCache {
101 SourceFileCache() = default;
102 ~SourceFileCache() = default;
104 void AddSourceFile(const FileSP &file_sp);
105 FileSP FindSourceFile(const FileSpec &file_spec) const;
108 typedef std::map<FileSpec, FileSP> FileCache;
109 FileCache m_file_cache;
113 //------------------------------------------------------------------
114 // Constructors and Destructors
115 //------------------------------------------------------------------
116 // A source manager can be made with a non-null target, in which case it can
117 // use the path remappings to find
118 // source files that are not in their build locations. With no target it
119 // won't be able to do this.
120 SourceManager(const lldb::DebuggerSP &debugger_sp);
121 SourceManager(const lldb::TargetSP &target_sp);
125 FileSP GetLastFile() { return m_last_file_sp; }
128 DisplaySourceLinesWithLineNumbers(const FileSpec &file, uint32_t line,
129 uint32_t column, uint32_t context_before,
130 uint32_t context_after,
131 const char *current_line_cstr, Stream *s,
132 const SymbolContextList *bp_locs = nullptr);
134 // This variant uses the last file we visited.
135 size_t DisplaySourceLinesWithLineNumbersUsingLastFile(
136 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
137 const char *current_line_cstr, Stream *s,
138 const SymbolContextList *bp_locs = nullptr);
140 size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
141 const SymbolContextList *bp_locs = nullptr);
143 bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line);
145 bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
147 bool DefaultFileAndLineSet() { return (m_last_file_sp.get() != nullptr); }
149 void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression ®ex,
150 uint32_t start_line, uint32_t end_line,
151 std::vector<uint32_t> &match_lines);
153 FileSP GetFile(const FileSpec &file_spec);
156 FileSP m_last_file_sp;
157 uint32_t m_last_line;
158 uint32_t m_last_count;
160 lldb::TargetWP m_target_wp;
161 lldb::DebuggerWP m_debugger_wp;
164 DISALLOW_COPY_AND_ASSIGN(SourceManager);
167 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
169 } // namespace lldb_private
171 #endif // liblldb_SourceManager_h_