1 //===-- SBSourceManager.cpp -----------------------------------------------===//
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 #include "lldb/API/SBSourceManager.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBDebugger.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBTarget.h"
14
15 #include "lldb/API/SBFileSpec.h"
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/SourceManager.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Utility/Stream.h"
20
21 #include "lldb/Target/Target.h"
22
23 namespace lldb_private {
24 class SourceManagerImpl {
25 public:
SourceManagerImpl(const lldb::DebuggerSP & debugger_sp)26 SourceManagerImpl(const lldb::DebuggerSP &debugger_sp)
27 : m_debugger_wp(debugger_sp), m_target_wp() {}
28
SourceManagerImpl(const lldb::TargetSP & target_sp)29 SourceManagerImpl(const lldb::TargetSP &target_sp)
30 : m_debugger_wp(), m_target_wp(target_sp) {}
31
SourceManagerImpl(const SourceManagerImpl & rhs)32 SourceManagerImpl(const SourceManagerImpl &rhs) {
33 if (&rhs == this)
34 return;
35 m_debugger_wp = rhs.m_debugger_wp;
36 m_target_wp = rhs.m_target_wp;
37 }
38
DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec & file,uint32_t line,uint32_t column,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,lldb_private::Stream * s)39 size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file,
40 uint32_t line, uint32_t column,
41 uint32_t context_before,
42 uint32_t context_after,
43 const char *current_line_cstr,
44 lldb_private::Stream *s) {
45 if (!file)
46 return 0;
47
48 lldb::TargetSP target_sp(m_target_wp.lock());
49 if (target_sp) {
50 return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers(
51 file, line, column, context_before, context_after, current_line_cstr,
52 s);
53 } else {
54 lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
55 if (debugger_sp) {
56 return debugger_sp->GetSourceManager()
57 .DisplaySourceLinesWithLineNumbers(file, line, column,
58 context_before, context_after,
59 current_line_cstr, s);
60 }
61 }
62 return 0;
63 }
64
65 private:
66 lldb::DebuggerWP m_debugger_wp;
67 lldb::TargetWP m_target_wp;
68 };
69 }
70
71 using namespace lldb;
72 using namespace lldb_private;
73
SBSourceManager(const SBDebugger & debugger)74 SBSourceManager::SBSourceManager(const SBDebugger &debugger) {
75 LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBDebugger &),
76 debugger);
77
78 m_opaque_up = std::make_unique<SourceManagerImpl>(debugger.get_sp());
79 }
80
SBSourceManager(const SBTarget & target)81 SBSourceManager::SBSourceManager(const SBTarget &target) {
82 LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBTarget &), target);
83
84 m_opaque_up = std::make_unique<SourceManagerImpl>(target.GetSP());
85 }
86
SBSourceManager(const SBSourceManager & rhs)87 SBSourceManager::SBSourceManager(const SBSourceManager &rhs) {
88 LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBSourceManager &),
89 rhs);
90
91 if (&rhs == this)
92 return;
93
94 m_opaque_up = std::make_unique<SourceManagerImpl>(*(rhs.m_opaque_up.get()));
95 }
96
97 const lldb::SBSourceManager &SBSourceManager::
operator =(const lldb::SBSourceManager & rhs)98 operator=(const lldb::SBSourceManager &rhs) {
99 LLDB_RECORD_METHOD(const lldb::SBSourceManager &,
100 SBSourceManager, operator=,(const lldb::SBSourceManager &),
101 rhs);
102
103 m_opaque_up = std::make_unique<SourceManagerImpl>(*(rhs.m_opaque_up.get()));
104 return LLDB_RECORD_RESULT(*this);
105 }
106
107 SBSourceManager::~SBSourceManager() = default;
108
DisplaySourceLinesWithLineNumbers(const SBFileSpec & file,uint32_t line,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,SBStream & s)109 size_t SBSourceManager::DisplaySourceLinesWithLineNumbers(
110 const SBFileSpec &file, uint32_t line, uint32_t context_before,
111 uint32_t context_after, const char *current_line_cstr, SBStream &s) {
112 LLDB_RECORD_METHOD(size_t, SBSourceManager, DisplaySourceLinesWithLineNumbers,
113 (const lldb::SBFileSpec &, uint32_t, uint32_t, uint32_t,
114 const char *, lldb::SBStream &),
115 file, line, context_before, context_after,
116 current_line_cstr, s);
117
118 const uint32_t column = 0;
119 return DisplaySourceLinesWithLineNumbersAndColumn(
120 file.ref(), line, column, context_before, context_after,
121 current_line_cstr, s);
122 }
123
DisplaySourceLinesWithLineNumbersAndColumn(const SBFileSpec & file,uint32_t line,uint32_t column,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,SBStream & s)124 size_t SBSourceManager::DisplaySourceLinesWithLineNumbersAndColumn(
125 const SBFileSpec &file, uint32_t line, uint32_t column,
126 uint32_t context_before, uint32_t context_after,
127 const char *current_line_cstr, SBStream &s) {
128 LLDB_RECORD_METHOD(
129 size_t, SBSourceManager, DisplaySourceLinesWithLineNumbersAndColumn,
130 (const lldb::SBFileSpec &, uint32_t, uint32_t, uint32_t, uint32_t,
131 const char *, lldb::SBStream &),
132 file, line, column, context_before, context_after, current_line_cstr, s);
133
134 if (m_opaque_up == nullptr)
135 return 0;
136
137 return m_opaque_up->DisplaySourceLinesWithLineNumbers(
138 file.ref(), line, column, context_before, context_after,
139 current_line_cstr, s.get());
140 }
141
142 namespace lldb_private {
143 namespace repro {
144
145 template <>
RegisterMethods(Registry & R)146 void RegisterMethods<SBSourceManager>(Registry &R) {
147 LLDB_REGISTER_CONSTRUCTOR(SBSourceManager, (const lldb::SBDebugger &));
148 LLDB_REGISTER_CONSTRUCTOR(SBSourceManager, (const lldb::SBTarget &));
149 LLDB_REGISTER_CONSTRUCTOR(SBSourceManager, (const lldb::SBSourceManager &));
150 LLDB_REGISTER_METHOD(
151 const lldb::SBSourceManager &,
152 SBSourceManager, operator=,(const lldb::SBSourceManager &));
153 LLDB_REGISTER_METHOD(size_t, SBSourceManager,
154 DisplaySourceLinesWithLineNumbers,
155 (const lldb::SBFileSpec &, uint32_t, uint32_t,
156 uint32_t, const char *, lldb::SBStream &));
157 LLDB_REGISTER_METHOD(size_t, SBSourceManager,
158 DisplaySourceLinesWithLineNumbersAndColumn,
159 (const lldb::SBFileSpec &, uint32_t, uint32_t,
160 uint32_t, uint32_t, const char *, lldb::SBStream &));
161 }
162
163 }
164 }
165