LLDB mainline
SourceManager.h
Go to the documentation of this file.
1//===-- SourceManager.h -----------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_CORE_SOURCEMANAGER_H
10#define LLDB_CORE_SOURCEMANAGER_H
11
15#include "lldb/lldb-defines.h"
16#include "lldb/lldb-forward.h"
17
18#include "llvm/Support/Chrono.h"
19#include "llvm/Support/RWMutex.h"
20
21#include <cstddef>
22#include <cstdint>
23#include <map>
24#include <memory>
25#include <optional>
26#include <string>
27#include <vector>
28
29namespace lldb_private {
31class Stream;
33class Target;
34
36public:
37 class File {
38 friend bool operator==(const SourceManager::File &lhs,
39 const SourceManager::File &rhs);
40
41 public:
42 File(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp);
43 File(SupportFileNSP support_file_nsp, lldb::DebuggerSP debugger_sp);
44
45 bool ModificationTimeIsStale() const;
46 bool PathRemappingIsStale() const;
47
48 size_t DisplaySourceLines(
49 uint32_t line, std::optional<size_t> column, uint32_t context_before,
50 uint32_t context_after, Stream *s,
52 void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
53 uint32_t end_line,
54 std::vector<uint32_t> &match_lines);
55
56 bool GetLine(uint32_t line_no, std::string &buffer);
57
58 uint32_t GetLineOffset(uint32_t line);
59
60 bool LineIsValid(uint32_t line);
61
63 assert(m_support_file_nsp && "SupportFileNSP must always be valid");
64 return m_support_file_nsp;
65 }
66
68
69 const char *PeekLineData(uint32_t line);
70
71 uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
72
73 uint32_t GetNumLines();
74
75 llvm::sys::TimePoint<> GetTimestamp() const { return m_mod_time; }
76
77 const Checksum &GetChecksum() const { return m_checksum; }
78
79 std::once_flag &GetChecksumWarningOnceFlag() {
81 }
82
83 protected:
84 /// Set file and update modification time.
85 void SetSupportFile(SupportFileNSP support_file_nsp);
86
87 bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
88
89 /// The support file. If the target has source mappings, this might be
90 /// different from the original support file passed to the constructor.
92
93 /// Keep track of the on-disk checksum.
95
96 /// Once flag for emitting a checksum mismatch warning.
98
99 // Keep the modification time that this file data is valid for
100 llvm::sys::TimePoint<> m_mod_time;
101
102 // If the target uses path remappings, be sure to clear our notion of a
103 // source file if the path modification ID changes
106 typedef std::vector<uint32_t> LineOffsets;
110
111 private:
112 void CommonInitializer(SupportFileNSP support_file_nsp,
113 lldb::TargetSP target_sp);
114 void CommonInitializerImpl(SupportFileNSP support_file_nsp,
115 lldb::TargetSP target_sp);
116 };
117
118 typedef std::shared_ptr<File> FileSP;
119
120 /// The SourceFileCache class separates the source manager from the cache of
121 /// source files. There is one source manager per Target but both the Debugger
122 /// and the Process have their own source caches.
123 ///
124 /// The SourceFileCache just handles adding, storing, removing and looking up
125 /// source files. The caching policies are implemented in
126 /// SourceManager::GetFile.
128 public:
129 SourceFileCache() = default;
130 ~SourceFileCache() = default;
131
132 void AddSourceFile(const FileSpec &file_spec, FileSP file_sp);
133 void RemoveSourceFile(const FileSP &file_sp);
134
135 FileSP FindSourceFile(const FileSpec &file_spec) const;
136
137 // Removes all elements from the cache.
138 void Clear() { m_file_cache.clear(); }
139
140 void Dump(Stream &stream) const;
141
142 private:
143 void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp);
144
145 typedef std::map<FileSpec, FileSP> FileCache;
147
148 mutable llvm::sys::RWMutex m_mutex;
149 };
150
151 /// A source manager can be made with a valid Target, in which case it can use
152 /// the path remappings to find source files that are not in their build
153 /// locations. Without a target it won't be able to do this.
154 /// @{
155 SourceManager(const lldb::DebuggerSP &debugger_sp);
156 SourceManager(const lldb::TargetSP &target_sp);
157 /// @}
158
160
162 bool AtLastLine(bool reverse) {
163 return m_last_line == UINT32_MAX || (reverse && m_last_line == 1);
164 }
165
167 SupportFileNSP support_file_nsp, uint32_t line, uint32_t column,
168 uint32_t context_before, uint32_t context_after,
169 const char *current_line_cstr, Stream *s,
170 const SymbolContextList *bp_locs = nullptr,
172
173 // This variant uses the last file we visited.
175 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
176 const char *current_line_cstr, Stream *s,
177 const SymbolContextList *bp_locs = nullptr,
179
181 Stream *s, uint32_t count, bool reverse,
182 const SymbolContextList *bp_locs = nullptr,
184
185 bool SetDefaultFileAndLine(SupportFileNSP support_file_nsp, uint32_t line);
186
193
194 std::optional<SupportFileAndLine> GetDefaultFileAndLine();
195
197 return (GetFile(m_last_support_file_nsp).get() != nullptr);
198 }
199
200 void FindLinesMatchingRegex(SupportFileNSP support_file_nsp,
201 RegularExpression &regex, uint32_t start_line,
202 uint32_t end_line,
203 std::vector<uint32_t> &match_lines);
204
205 FileSP GetFile(SupportFileNSP support_file_nsp);
206
207protected:
209 uint32_t m_last_line;
210 uint32_t m_last_count;
214
215private:
216 SourceManager(const SourceManager &) = delete;
217 const SourceManager &operator=(const SourceManager &) = delete;
218};
219
220bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
221
222} // namespace lldb_private
223
224#endif // LLDB_CORE_SOURCEMANAGER_H
A file utility class.
Definition FileSpec.h:57
std::once_flag & GetChecksumWarningOnceFlag()
void CommonInitializerImpl(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp)
File(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp)
File(SupportFileNSP support_file_nsp, lldb::DebuggerSP debugger_sp)
uint32_t GetSourceMapModificationID() const
void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
std::vector< uint32_t > LineOffsets
void CommonInitializer(SupportFileNSP support_file_nsp, lldb::TargetSP target_sp)
const char * PeekLineData(uint32_t line)
size_t DisplaySourceLines(uint32_t line, std::optional< size_t > column, uint32_t context_before, uint32_t context_after, Stream *s, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
SupportFileNSP GetSupportFile() const
Checksum m_checksum
Keep track of the on-disk checksum.
uint32_t GetLineLength(uint32_t line, bool include_newline_chars)
bool CalculateLineOffsets(uint32_t line=UINT32_MAX)
SupportFileNSP m_support_file_nsp
The support file.
friend bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs)
bool GetLine(uint32_t line_no, std::string &buffer)
uint32_t GetLineOffset(uint32_t line)
const Checksum & GetChecksum() const
void SetSupportFile(SupportFileNSP support_file_nsp)
Set file and update modification time.
llvm::sys::TimePoint GetTimestamp() const
std::once_flag m_checksum_warning_once_flag
Once flag for emitting a checksum mismatch warning.
void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp)
FileSP FindSourceFile(const FileSpec &file_spec) const
void AddSourceFile(const FileSpec &file_spec, FileSP file_sp)
SourceManager(const SourceManager &)=delete
void FindLinesMatchingRegex(SupportFileNSP support_file_nsp, RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
std::shared_ptr< File > FileSP
const SourceManager & operator=(const SourceManager &)=delete
std::optional< SupportFileAndLine > GetDefaultFileAndLine()
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs=nullptr, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
bool AtLastLine(bool reverse)
SourceManager(const lldb::DebuggerSP &debugger_sp)
A source manager can be made with a valid Target, in which case it can use the path remappings to fin...
SourceManager(const lldb::TargetSP &target_sp)
lldb::DebuggerWP m_debugger_wp
FileSP GetFile(SupportFileNSP support_file_nsp)
size_t DisplaySourceLinesWithLineNumbers(SupportFileNSP support_file_nsp, uint32_t line, uint32_t column, uint32_t context_before, uint32_t context_after, const char *current_line_cstr, Stream *s, const SymbolContextList *bp_locs=nullptr, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
bool SetDefaultFileAndLine(SupportFileNSP support_file_nsp, uint32_t line)
SupportFileNSP m_last_support_file_nsp
size_t DisplaySourceLinesWithLineNumbersUsingLastFile(uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column, const char *current_line_cstr, Stream *s, const SymbolContextList *bp_locs=nullptr, lldb::LanguageType language_type=lldb::eLanguageTypeUnknown)
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Defines a list of symbol context objects.
#define UINT32_MAX
A class that represents a running process on the host machine.
NonNullSharedPtr< lldb_private::SupportFile > SupportFileNSP
Definition SupportFile.h:80
bool operator==(const Address &lhs, const Address &rhs)
Definition Address.cpp:1011
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::weak_ptr< lldb_private::Debugger > DebuggerWP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::weak_ptr< lldb_private::Target > TargetWP
std::shared_ptr< lldb_private::Target > TargetSP
SupportFileAndLine(SupportFileNSP support_file_nsp, uint32_t line)