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 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 }
28 namespace lldb_private {
29 class Stream;
30 }
31 namespace lldb_private {
32 class SymbolContextList;
33 }
34 namespace lldb_private {
35 class Target;
36 }
37 
38 namespace lldb_private {
39 
41 public:
42  class File {
43  friend bool operator==(const SourceManager::File &lhs,
44  const SourceManager::File &rhs);
45 
46  public:
47  File(const FileSpec &file_spec, Target *target);
48  File(const FileSpec &file_spec, lldb::DebuggerSP debugger_sp);
49  ~File() = default;
50 
51  void UpdateIfNeeded();
52 
53  size_t DisplaySourceLines(uint32_t line, llvm::Optional<size_t> column,
54  uint32_t context_before, uint32_t context_after,
55  Stream *s);
56  void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
57  uint32_t end_line,
58  std::vector<uint32_t> &match_lines);
59 
60  bool GetLine(uint32_t line_no, std::string &buffer);
61 
63 
64  bool LineIsValid(uint32_t line);
65 
66  bool FileSpecMatches(const FileSpec &file_spec);
67 
68  const FileSpec &GetFileSpec() { return m_file_spec; }
69 
71 
72  const char *PeekLineData(uint32_t line);
73 
74  uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
75 
77 
78  protected:
80 
81  FileSpec m_file_spec_orig; // The original file spec that was used (can be
82  // different from m_file_spec)
83  FileSpec m_file_spec; // The actually file spec being used (if the target
84  // has source mappings, this might be different from
85  // m_file_spec_orig)
86 
87  // Keep the modification time that this file data is valid for
88  llvm::sys::TimePoint<> m_mod_time;
89 
90  // If the target uses path remappings, be sure to clear our notion of a
91  // source file if the path modification ID changes
93  lldb::DataBufferSP m_data_sp;
94  typedef std::vector<uint32_t> LineOffsets;
95  LineOffsets m_offsets;
96  lldb::DebuggerWP m_debugger_wp;
97 
98  private:
99  void CommonInitializer(const FileSpec &file_spec, Target *target);
100  };
101 
102  typedef std::shared_ptr<File> FileSP;
103 
104  // The SourceFileCache class separates the source manager from the cache of
105  // source files, so the cache can be stored in the Debugger, but the source
106  // managers can be per target.
108  public:
109  SourceFileCache() = default;
110  ~SourceFileCache() = default;
111 
112  void AddSourceFile(const FileSP &file_sp);
113  FileSP FindSourceFile(const FileSpec &file_spec) const;
114 
115  protected:
116  typedef std::map<FileSpec, FileSP> FileCache;
117  FileCache m_file_cache;
118  };
119 
120  // Constructors and Destructors
121  // A source manager can be made with a non-null target, in which case it can
122  // use the path remappings to find
123  // source files that are not in their build locations. With no target it
124  // won't be able to do this.
125  SourceManager(const lldb::DebuggerSP &debugger_sp);
126  SourceManager(const lldb::TargetSP &target_sp);
127 
128  ~SourceManager();
129 
130  FileSP GetLastFile() { return m_last_file_sp; }
131 
132  size_t
134  uint32_t column, uint32_t context_before,
135  uint32_t context_after,
136  const char *current_line_cstr, Stream *s,
137  const SymbolContextList *bp_locs = nullptr);
138 
139  // This variant uses the last file we visited.
141  uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
142  const char *current_line_cstr, Stream *s,
143  const SymbolContextList *bp_locs = nullptr);
144 
145  size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
146  const SymbolContextList *bp_locs = nullptr);
147 
148  bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line);
149 
150  bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line);
151 
152  bool DefaultFileAndLineSet() { return (m_last_file_sp.get() != nullptr); }
153 
154  void FindLinesMatchingRegex(FileSpec &file_spec, RegularExpression &regex,
155  uint32_t start_line, uint32_t end_line,
156  std::vector<uint32_t> &match_lines);
157 
158  FileSP GetFile(const FileSpec &file_spec);
159 
160 protected:
165  lldb::TargetWP m_target_wp;
166  lldb::DebuggerWP m_debugger_wp;
167 
168 private:
169  DISALLOW_COPY_AND_ASSIGN(SourceManager);
170 };
171 
172 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
173 
174 } // namespace lldb_private
175 
176 #endif // liblldb_SourceManager_h_
Defines a list of symbol context objects.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
bool CalculateLineOffsets(uint32_t line=UINT32_MAX)
bool FileSpecMatches(const FileSpec &file_spec)
A file utility class.
Definition: FileSpec.h:55
"lldb/Utility/RegularExpression.h" A C++ wrapper class for regex.
llvm::sys::TimePoint m_mod_time
Definition: SourceManager.h:88
size_t DisplaySourceLines(uint32_t line, llvm::Optional< size_t > column, uint32_t context_before, uint32_t context_after, Stream *s)
std::map< FileSpec, FileSP > FileCache
lldb::DebuggerWP m_debugger_wp
#define UINT32_MAX
Definition: lldb-defines.h:31
void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line, uint32_t end_line, std::vector< uint32_t > &match_lines)
File(const FileSpec &file_spec, Target *target)
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)
uint32_t GetSourceMapModificationID() const
Definition: SourceManager.h:70
size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs=nullptr)
bool SetDefaultFileAndLine(const FileSpec &file_spec, uint32_t line)
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)
const char * PeekLineData(uint32_t line)
SourceManager(const lldb::DebuggerSP &debugger_sp)
uint32_t GetLineOffset(uint32_t line)
FileSP GetFile(const FileSpec &file_spec)
uint32_t GetLineLength(uint32_t line, bool include_newline_chars)
friend bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs)
std::shared_ptr< File > FileSP
bool GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line)
bool GetLine(uint32_t line_no, std::string &buffer)
std::vector< uint32_t > LineOffsets
Definition: SourceManager.h:94