xref: /llvm-project/lldb/include/lldb/Core/SourceManager.h (revision a81a4b2a7ac2d0b8195bb008b2c0f464cfbda223)
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 
12 #include "lldb/Utility/Checksum.h"
13 #include "lldb/Utility/FileSpec.h"
14 #include "lldb/lldb-defines.h"
15 #include "lldb/lldb-forward.h"
16 
17 #include "llvm/Support/Chrono.h"
18 #include "llvm/Support/RWMutex.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <map>
23 #include <memory>
24 #include <optional>
25 #include <string>
26 #include <vector>
27 
28 namespace lldb_private {
29 class RegularExpression;
30 class Stream;
31 class SymbolContextList;
32 class Target;
33 
34 class SourceManager {
35 public:
36   class File {
37     friend bool operator==(const SourceManager::File &lhs,
38                            const SourceManager::File &rhs);
39 
40   public:
41     File(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp);
42     File(lldb::SupportFileSP support_file_sp, lldb::DebuggerSP debugger_sp);
43 
44     bool ModificationTimeIsStale() const;
45     bool PathRemappingIsStale() const;
46 
47     size_t DisplaySourceLines(uint32_t line, std::optional<size_t> column,
48                               uint32_t context_before, uint32_t context_after,
49                               Stream *s);
50     void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
51                                 uint32_t end_line,
52                                 std::vector<uint32_t> &match_lines);
53 
54     bool GetLine(uint32_t line_no, std::string &buffer);
55 
56     uint32_t GetLineOffset(uint32_t line);
57 
58     bool LineIsValid(uint32_t line);
59 
60     lldb::SupportFileSP GetSupportFile() const {
61       assert(m_support_file_sp && "SupportFileSP must always be valid");
62       return m_support_file_sp;
63     }
64 
65     uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
66 
67     const char *PeekLineData(uint32_t line);
68 
69     uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
70 
71     uint32_t GetNumLines();
72 
73     llvm::sys::TimePoint<> GetTimestamp() const { return m_mod_time; }
74 
75     const Checksum &GetChecksum() const { return m_checksum; }
76 
77     std::once_flag &GetChecksumWarningOnceFlag() {
78       return m_checksum_warning_once_flag;
79     }
80 
81   protected:
82     /// Set file and update modification time.
83     void SetSupportFile(lldb::SupportFileSP support_file_sp);
84 
85     bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
86 
87     /// The support file. If the target has source mappings, this might be
88     /// different from the original support file passed to the constructor.
89     lldb::SupportFileSP m_support_file_sp;
90 
91     /// Keep track of the on-disk checksum.
92     Checksum m_checksum;
93 
94     /// Once flag for emitting a checksum mismatch warning.
95     std::once_flag m_checksum_warning_once_flag;
96 
97     // Keep the modification time that this file data is valid for
98     llvm::sys::TimePoint<> m_mod_time;
99 
100     // If the target uses path remappings, be sure to clear our notion of a
101     // source file if the path modification ID changes
102     uint32_t m_source_map_mod_id = 0;
103     lldb::DataBufferSP m_data_sp;
104     typedef std::vector<uint32_t> LineOffsets;
105     LineOffsets m_offsets;
106     lldb::DebuggerWP m_debugger_wp;
107     lldb::TargetWP m_target_wp;
108 
109   private:
110     void CommonInitializer(lldb::SupportFileSP support_file_sp,
111                            lldb::TargetSP target_sp);
112   };
113 
114   typedef std::shared_ptr<File> FileSP;
115 
116   /// The SourceFileCache class separates the source manager from the cache of
117   /// source files. There is one source manager per Target but both the Debugger
118   /// and the Process have their own source caches.
119   ///
120   /// The SourceFileCache just handles adding, storing, removing and looking up
121   /// source files. The caching policies are implemented in
122   /// SourceManager::GetFile.
123   class SourceFileCache {
124   public:
125     SourceFileCache() = default;
126     ~SourceFileCache() = default;
127 
128     void AddSourceFile(const FileSpec &file_spec, FileSP file_sp);
129     void RemoveSourceFile(const FileSP &file_sp);
130 
131     FileSP FindSourceFile(const FileSpec &file_spec) const;
132 
133     // Removes all elements from the cache.
134     void Clear() { m_file_cache.clear(); }
135 
136     void Dump(Stream &stream) const;
137 
138   private:
139     void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp);
140 
141     typedef std::map<FileSpec, FileSP> FileCache;
142     FileCache m_file_cache;
143 
144     mutable llvm::sys::RWMutex m_mutex;
145   };
146 
147   /// A source manager can be made with a valid Target, in which case it can use
148   /// the path remappings to find source files that are not in their build
149   /// locations.  Without a target it won't be able to do this.
150   /// @{
151   SourceManager(const lldb::DebuggerSP &debugger_sp);
152   SourceManager(const lldb::TargetSP &target_sp);
153   /// @}
154 
155   ~SourceManager();
156 
157   FileSP GetLastFile() { return GetFile(m_last_support_file_sp); }
158 
159   size_t DisplaySourceLinesWithLineNumbers(
160       lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
161       uint32_t context_before, uint32_t context_after,
162       const char *current_line_cstr, Stream *s,
163       const SymbolContextList *bp_locs = nullptr);
164 
165   // This variant uses the last file we visited.
166   size_t DisplaySourceLinesWithLineNumbersUsingLastFile(
167       uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
168       const char *current_line_cstr, Stream *s,
169       const SymbolContextList *bp_locs = nullptr);
170 
171   size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
172                                     const SymbolContextList *bp_locs = nullptr);
173 
174   bool SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp,
175                              uint32_t line);
176 
177   struct SupportFileAndLine {
178     lldb::SupportFileSP support_file_sp;
179     uint32_t line;
180     SupportFileAndLine(lldb::SupportFileSP support_file_sp, uint32_t line)
181         : support_file_sp(support_file_sp), line(line) {}
182   };
183 
184   std::optional<SupportFileAndLine> GetDefaultFileAndLine();
185 
186   bool DefaultFileAndLineSet() {
187     return (GetFile(m_last_support_file_sp).get() != nullptr);
188   }
189 
190   void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp,
191                               RegularExpression &regex, uint32_t start_line,
192                               uint32_t end_line,
193                               std::vector<uint32_t> &match_lines);
194 
195   FileSP GetFile(lldb::SupportFileSP support_file_sp);
196 
197 protected:
198   lldb::SupportFileSP m_last_support_file_sp;
199   uint32_t m_last_line;
200   uint32_t m_last_count;
201   bool m_default_set;
202   lldb::TargetWP m_target_wp;
203   lldb::DebuggerWP m_debugger_wp;
204 
205 private:
206   SourceManager(const SourceManager &) = delete;
207   const SourceManager &operator=(const SourceManager &) = delete;
208 };
209 
210 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
211 
212 } // namespace lldb_private
213 
214 #endif // LLDB_CORE_SOURCEMANAGER_H
215