]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/include/lldb/Core/SourceManager.h
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / include / lldb / Core / SourceManager.h
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 liblldb_SourceManager_h_
10 #define liblldb_SourceManager_h_
11
12 #include "lldb/Utility/FileSpec.h"
13 #include "lldb/lldb-defines.h"
14 #include "lldb/lldb-forward.h"
15
16 #include "llvm/Support/Chrono.h"
17
18 #include <cstdint>
19 #include <map>
20 #include <memory>
21 #include <stddef.h>
22 #include <string>
23 #include <vector>
24
25 namespace lldb_private {
26 class RegularExpression;
27 class Stream;
28 class SymbolContextList;
29 class Target;
30
31 class SourceManager {
32 public:
33   class File {
34     friend bool operator==(const SourceManager::File &lhs,
35                            const SourceManager::File &rhs);
36
37   public:
38     File(const FileSpec &file_spec, Target *target);
39     File(const FileSpec &file_spec, lldb::DebuggerSP debugger_sp);
40     ~File() = default;
41
42     void UpdateIfNeeded();
43
44     size_t DisplaySourceLines(uint32_t line, llvm::Optional<size_t> column,
45                               uint32_t context_before, uint32_t context_after,
46                               Stream *s);
47     void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
48                                 uint32_t end_line,
49                                 std::vector<uint32_t> &match_lines);
50
51     bool GetLine(uint32_t line_no, std::string &buffer);
52
53     uint32_t GetLineOffset(uint32_t line);
54
55     bool LineIsValid(uint32_t line);
56
57     bool FileSpecMatches(const FileSpec &file_spec);
58
59     const FileSpec &GetFileSpec() { return m_file_spec; }
60
61     uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
62
63     const char *PeekLineData(uint32_t line);
64
65     uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
66
67     uint32_t GetNumLines();
68
69   protected:
70     bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
71
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
76                           // m_file_spec_orig)
77
78     // Keep the modification time that this file data is valid for
79     llvm::sys::TimePoint<> m_mod_time;
80
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;
88
89   private:
90     void CommonInitializer(const FileSpec &file_spec, Target *target);
91   };
92
93   typedef std::shared_ptr<File> FileSP;
94
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 {
99   public:
100     SourceFileCache() = default;
101     ~SourceFileCache() = default;
102
103     void AddSourceFile(const FileSP &file_sp);
104     FileSP FindSourceFile(const FileSpec &file_spec) const;
105
106   protected:
107     typedef std::map<FileSpec, FileSP> FileCache;
108     FileCache m_file_cache;
109   };
110
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);
118
119   ~SourceManager();
120
121   FileSP GetLastFile() { return m_last_file_sp; }
122
123   size_t
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);
129
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);
135
136   size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
137                                     const SymbolContextList *bp_locs = nullptr);
138
139   bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line);
140
141   bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
142
143   bool DefaultFileAndLineSet() { return (m_last_file_sp.get() != nullptr); }
144
145   void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression &regex,
146                               uint32_t start_line, uint32_t end_line,
147                               std::vector<uint32_t> &match_lines);
148
149   FileSP GetFile(const FileSpec &file_spec);
150
151 protected:
152   FileSP m_last_file_sp;
153   uint32_t m_last_line;
154   uint32_t m_last_count;
155   bool m_default_set;
156   lldb::TargetWP m_target_wp;
157   lldb::DebuggerWP m_debugger_wp;
158
159 private:
160   DISALLOW_COPY_AND_ASSIGN(SourceManager);
161 };
162
163 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
164
165 } // namespace lldb_private
166
167 #endif // liblldb_SourceManager_h_