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 
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 <cstddef>
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace lldb_private {
26 class RegularExpression;
27 class Stream;
28 class SymbolContextList;
29 class Target;
30 
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 
54 
55  bool LineIsValid(uint32_t line);
56 
57  const FileSpec &GetFileSpec() { return m_file_spec; }
58 
60 
61  const char *PeekLineData(uint32_t line);
62 
63  uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
64 
66 
67  protected:
69 
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
74  // m_file_spec_orig)
75 
76  // Keep the modification time that this file data is valid for
77  llvm::sys::TimePoint<> m_mod_time;
78 
79  // If the target uses path remappings, be sure to clear our notion of a
80  // source file if the path modification ID changes
82  lldb::DataBufferSP m_data_sp;
83  typedef std::vector<uint32_t> LineOffsets;
85  lldb::DebuggerWP m_debugger_wp;
86 
87  private:
88  void CommonInitializer(const FileSpec &file_spec, Target *target);
89  };
90 
91  typedef std::shared_ptr<File> FileSP;
92 
93  // The SourceFileCache class separates the source manager from the cache of
94  // source files, so the cache can be stored in the Debugger, but the source
95  // managers can be per target.
97  public:
98  SourceFileCache() = default;
99  ~SourceFileCache() = default;
100 
101  void AddSourceFile(const FileSP &file_sp);
102  FileSP FindSourceFile(const FileSpec &file_spec) const;
103 
104  // Removes all elements from the cache.
105  void Clear() { m_file_cache.clear(); }
106 
107  protected:
108  typedef std::map<FileSpec, FileSP> FileCache;
110  };
111 
112  // Constructors and Destructors
113  // A source manager can be made with a non-null target, in which case it can
114  // use the path remappings to find
115  // source files that are not in their build locations. With no target it
116  // won't be able to do this.
117  SourceManager(const lldb::DebuggerSP &debugger_sp);
118  SourceManager(const lldb::TargetSP &target_sp);
119 
120  ~SourceManager();
121 
123 
124  size_t
126  uint32_t column, uint32_t context_before,
127  uint32_t context_after,
128  const char *current_line_cstr, Stream *s,
129  const SymbolContextList *bp_locs = nullptr);
130 
131  // This variant uses the last file we visited.
133  uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
134  const char *current_line_cstr, Stream *s,
135  const SymbolContextList *bp_locs = nullptr);
136 
137  size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
138  const SymbolContextList *bp_locs = nullptr);
139 
140  bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line);
141 
142  bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
143 
145  return (GetFile(m_last_file_spec).get() != nullptr);
146  }
147 
148  void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression &regex,
149  uint32_t start_line, uint32_t end_line,
150  std::vector<uint32_t> &match_lines);
151 
152  FileSP GetFile(const FileSpec &file_spec);
153 
154 protected:
159  lldb::TargetWP m_target_wp;
160  lldb::DebuggerWP m_debugger_wp;
161 
162 private:
163  SourceManager(const SourceManager &) = delete;
164  const SourceManager &operator=(const SourceManager &) = delete;
165 };
166 
167 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
168 
169 } // namespace lldb_private
170 
171 #endif // LLDB_CORE_SOURCEMANAGER_H
lldb_private::SourceManager::File::PeekLineData
const char * PeekLineData(uint32_t line)
Definition: SourceManager.cpp:487
lldb_private::SourceManager::File::FindLinesMatchingRegex
void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
Definition: SourceManager.cpp:608
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb_private::SourceManager::DefaultFileAndLineSet
bool DefaultFileAndLineSet()
Definition: SourceManager.h:144
lldb_private::SourceManager::SourceFileCache
Definition: SourceManager.h:96
lldb-defines.h
lldb_private::SourceManager::File
Definition: SourceManager.h:33
lldb_private::SourceManager::SourceManager
SourceManager(const lldb::DebuggerSP &debugger_sp)
lldb_private::SourceManager::File::m_file_spec
FileSpec m_file_spec
Definition: SourceManager.h:72
lldb_private::SourceManager::File::GetFileSpec
const FileSpec & GetFileSpec()
Definition: SourceManager.h:57
lldb_private::SymbolContextList
Definition: SymbolContext.h:379
lldb_private::SourceManager::SourceFileCache::m_file_cache
FileCache m_file_cache
Definition: SourceManager.h:109
lldb_private::SourceManager::DisplaySourceLinesWithLineNumbers
size_t DisplaySourceLinesWithLineNumbers(const FileSpec &file, 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)
Definition: SourceManager.cpp:242
lldb_private::SourceManager::File::UpdateIfNeeded
void UpdateIfNeeded()
Definition: SourceManager.cpp:534
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Target
Definition: Target.h:454
lldb_private::SourceManager::File::m_mod_time
llvm::sys::TimePoint m_mod_time
Definition: SourceManager.h:77
lldb_private::SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile
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)
Definition: SourceManager.cpp:153
lldb_private::SourceManager::File::GetLineOffset
uint32_t GetLineOffset(uint32_t line)
Definition: SourceManager.cpp:468
lldb_private::SourceManager::SourceFileCache::SourceFileCache
SourceFileCache()=default
lldb_private::operator==
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:999
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::SourceManager::SourceFileCache::AddSourceFile
void AddSourceFile(const FileSP &file_sp)
Definition: SourceManager.cpp:704
lldb_private::SourceManager::GetDefaultFileAndLine
bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line)
Definition: SourceManager.cpp:325
lldb_private::SourceManager::m_last_count
uint32_t m_last_count
Definition: SourceManager.h:157
lldb_private::SourceManager::SourceFileCache::~SourceFileCache
~SourceFileCache()=default
lldb_private::SourceManager::File::File
File(const FileSpec &file_spec, Target *target)
Definition: SourceManager.cpp:393
lldb_private::SourceManager::File::GetLineLength
uint32_t GetLineLength(uint32_t line, bool include_newline_chars)
Definition: SourceManager.cpp:497
lldb_private::SourceManager::SourceFileCache::FileCache
std::map< FileSpec, FileSP > FileCache
Definition: SourceManager.h:108
lldb_private::NameMatch::RegularExpression
@ RegularExpression
lldb_private::SourceManager::GetFile
FileSP GetFile(const FileSpec &file_spec)
Definition: SourceManager.cpp:66
lldb_private::SourceManager::m_last_line
uint32_t m_last_line
Definition: SourceManager.h:156
lldb_private::SourceManager::m_debugger_wp
lldb::DebuggerWP m_debugger_wp
Definition: SourceManager.h:160
lldb_private::SourceManager::SourceFileCache::Clear
void Clear()
Definition: SourceManager.h:105
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::SourceManager::SetDefaultFileAndLine
bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line)
Definition: SourceManager.cpp:311
lldb_private::SourceManager::File::LineOffsets
std::vector< uint32_t > LineOffsets
Definition: SourceManager.h:83
lldb_private::SourceManager::File::~File
~File()=default
lldb_private::SourceManager::operator=
const SourceManager & operator=(const SourceManager &)=delete
lldb_private::SourceManager::m_last_file_spec
FileSpec m_last_file_spec
Definition: SourceManager.h:155
lldb_private::SourceManager::GetLastFile
FileSP GetLastFile()
Definition: SourceManager.h:122
lldb_private::SourceManager::m_target_wp
lldb::TargetWP m_target_wp
Definition: SourceManager.h:159
uint32_t
lldb_private::SourceManager::File::m_source_map_mod_id
uint32_t m_source_map_mod_id
Definition: SourceManager.h:81
lldb_private::SourceManager::m_default_set
bool m_default_set
Definition: SourceManager.h:158
lldb_private::SourceManager::File::operator==
friend bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs)
lldb_private::SourceManager::File::m_offsets
LineOffsets m_offsets
Definition: SourceManager.h:84
lldb_private::SourceManager::File::GetNumLines
uint32_t GetNumLines()
Definition: SourceManager.cpp:482
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::SourceManager::File::m_file_spec_orig
FileSpec m_file_spec_orig
Definition: SourceManager.h:70
lldb_private::SourceManager::File::m_data_sp
lldb::DataBufferSP m_data_sp
Definition: SourceManager.h:82
lldb-forward.h
lldb_private::SourceManager::SourceFileCache::FindSourceFile
FileSP FindSourceFile(const FileSpec &file_spec) const
Definition: SourceManager.cpp:715
lldb_private::SourceManager::FileSP
std::shared_ptr< File > FileSP
Definition: SourceManager.h:91
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::SourceManager::FindLinesMatchingRegex
void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
Definition: SourceManager.cpp:372
lldb_private::SourceManager::File::GetLine
bool GetLine(uint32_t line_no, std::string &buffer)
Definition: SourceManager.cpp:689
FileSpec.h
lldb_private::SourceManager::File::CalculateLineOffsets
bool CalculateLineOffsets(uint32_t line=UINT32_MAX)
Definition: SourceManager.cpp:636
lldb_private::SourceManager::DisplayMoreWithLineNumbers
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs=nullptr)
Definition: SourceManager.cpp:266
lldb_private::SourceManager::File::GetSourceMapModificationID
uint32_t GetSourceMapModificationID() const
Definition: SourceManager.h:59
lldb_private::SourceManager::File::DisplaySourceLines
size_t DisplaySourceLines(uint32_t line, llvm::Optional< size_t > column, uint32_t context_before, uint32_t context_after, Stream *s)
Definition: SourceManager.cpp:548
lldb_private::SourceManager::File::CommonInitializer
void CommonInitializer(const FileSpec &file_spec, Target *target)
Definition: SourceManager.cpp:401
lldb_private::SourceManager::File::LineIsValid
bool LineIsValid(uint32_t line)
Definition: SourceManager.cpp:525
lldb_private::SourceManager::File::m_debugger_wp
lldb::DebuggerWP m_debugger_wp
Definition: SourceManager.h:85
lldb_private::SourceManager
Definition: SourceManager.h:31
lldb_private::SourceManager::~SourceManager
~SourceManager()