1 //===-- SourceManager.h -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_SourceManager_h_
10 #define liblldb_SourceManager_h_
12 #include "lldb/Utility/FileSpec.h"
13 #include "lldb/lldb-defines.h"
14 #include "lldb/lldb-forward.h"
16 #include "llvm/Support/Chrono.h"
25 namespace lldb_private {
26 class RegularExpression;
28 class SymbolContextList;
34 friend bool operator==(const SourceManager::File &lhs,
35 const SourceManager::File &rhs);
38 File(const FileSpec &file_spec, Target *target);
39 File(const FileSpec &file_spec, lldb::DebuggerSP debugger_sp);
42 void UpdateIfNeeded();
44 size_t DisplaySourceLines(uint32_t line, llvm::Optional<size_t> column,
45 uint32_t context_before, uint32_t context_after,
47 void FindLinesMatchingRegex(RegularExpression ®ex, uint32_t start_line,
49 std::vector<uint32_t> &match_lines);
51 bool GetLine(uint32_t line_no, std::string &buffer);
53 uint32_t GetLineOffset(uint32_t line);
55 bool LineIsValid(uint32_t line);
57 bool FileSpecMatches(const FileSpec &file_spec);
59 const FileSpec &GetFileSpec() { return m_file_spec; }
61 uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
63 const char *PeekLineData(uint32_t line);
65 uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
67 uint32_t GetNumLines();
70 bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
72 FileSpec m_file_spec_orig; // The original file spec that was used (can be
73 // different from m_file_spec)
74 FileSpec m_file_spec; // The actually file spec being used (if the target
75 // has source mappings, this might be different from
78 // Keep the modification time that this file data is valid for
79 llvm::sys::TimePoint<> m_mod_time;
81 // If the target uses path remappings, be sure to clear our notion of a
82 // source file if the path modification ID changes
83 uint32_t m_source_map_mod_id = 0;
84 lldb::DataBufferSP m_data_sp;
85 typedef std::vector<uint32_t> LineOffsets;
86 LineOffsets m_offsets;
87 lldb::DebuggerWP m_debugger_wp;
90 void CommonInitializer(const FileSpec &file_spec, Target *target);
93 typedef std::shared_ptr<File> FileSP;
95 // The SourceFileCache class separates the source manager from the cache of
96 // source files, so the cache can be stored in the Debugger, but the source
97 // managers can be per target.
98 class SourceFileCache {
100 SourceFileCache() = default;
101 ~SourceFileCache() = default;
103 void AddSourceFile(const FileSP &file_sp);
104 FileSP FindSourceFile(const FileSpec &file_spec) const;
107 typedef std::map<FileSpec, FileSP> FileCache;
108 FileCache m_file_cache;
111 // Constructors and Destructors
112 // A source manager can be made with a non-null target, in which case it can
113 // use the path remappings to find
114 // source files that are not in their build locations. With no target it
115 // won't be able to do this.
116 SourceManager(const lldb::DebuggerSP &debugger_sp);
117 SourceManager(const lldb::TargetSP &target_sp);
121 FileSP GetLastFile() { return m_last_file_sp; }
124 DisplaySourceLinesWithLineNumbers(const FileSpec &file, uint32_t line,
125 uint32_t column, uint32_t context_before,
126 uint32_t context_after,
127 const char *current_line_cstr, Stream *s,
128 const SymbolContextList *bp_locs = nullptr);
130 // This variant uses the last file we visited.
131 size_t DisplaySourceLinesWithLineNumbersUsingLastFile(
132 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
133 const char *current_line_cstr, Stream *s,
134 const SymbolContextList *bp_locs = nullptr);
136 size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
137 const SymbolContextList *bp_locs = nullptr);
139 bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line);
141 bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
143 bool DefaultFileAndLineSet() { return (m_last_file_sp.get() != nullptr); }
145 void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression ®ex,
146 uint32_t start_line, uint32_t end_line,
147 std::vector<uint32_t> &match_lines);
149 FileSP GetFile(const FileSpec &file_spec);
152 FileSP m_last_file_sp;
153 uint32_t m_last_line;
154 uint32_t m_last_count;
156 lldb::TargetWP m_target_wp;
157 lldb::DebuggerWP m_debugger_wp;
160 DISALLOW_COPY_AND_ASSIGN(SourceManager);
163 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
165 } // namespace lldb_private
167 #endif // liblldb_SourceManager_h_