blob: 6fe3dac7767dcd69a6563221b22bca9898887b49 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- SBSourceManager.cpp -----------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman4c5de692010-06-09 07:44:37 +00009#include "lldb/API/SBSourceManager.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "lldb/API/SBDebugger.h"
Johnny Chenf6eaba82010-12-11 01:20:39 +000012#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include "lldb/API/SBFileSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000016#include "lldb/Core/Debugger.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Core/SourceManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/StreamFile.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Jim Inghame37d6052011-09-13 00:29:56 +000021#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023namespace lldb_private {
24class SourceManagerImpl {
25public:
26 SourceManagerImpl(const lldb::DebuggerSP &debugger_sp)
27 : m_debugger_wp(debugger_sp), m_target_wp() {}
28
29 SourceManagerImpl(const lldb::TargetSP &target_sp)
30 : m_debugger_wp(), m_target_wp(target_sp) {}
31
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
39 size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file,
Todd Fiala9666ba72016-09-21 20:13:14 +000040 uint32_t line, uint32_t column,
Greg Clayton9a377662011-10-01 02:59:24 +000041 uint32_t context_before,
42 uint32_t context_after,
Johnny Chen1cbbdac2011-12-20 00:04:58 +000043 const char *current_line_cstr,
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 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(
Todd Fiala9666ba72016-09-21 20:13:14 +000051 file, line, column, context_before, context_after, current_line_cstr,
52 s);
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 } else {
54 lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
55 if (debugger_sp) {
56 return debugger_sp->GetSourceManager()
Todd Fiala9666ba72016-09-21 20:13:14 +000057 .DisplaySourceLinesWithLineNumbers(file, line, column,
58 context_before, context_after,
59 current_line_cstr, s);
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 }
61 }
62 return 0;
63 }
64
65private:
66 lldb::DebuggerWP m_debugger_wp;
67 lldb::TargetWP m_target_wp;
68};
Greg Clayton9a377662011-10-01 02:59:24 +000069}
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071using namespace lldb;
72using namespace lldb_private;
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074SBSourceManager::SBSourceManager(const SBDebugger &debugger) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000075 LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBDebugger &),
76 debugger);
77
Jonas Devlieghered5b44032019-02-13 06:25:41 +000078 m_opaque_up.reset(new SourceManagerImpl(debugger.get_sp()));
Jim Inghame37d6052011-09-13 00:29:56 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081SBSourceManager::SBSourceManager(const SBTarget &target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000082 LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBTarget &), target);
83
Jonas Devlieghered5b44032019-02-13 06:25:41 +000084 m_opaque_up.reset(new SourceManagerImpl(target.GetSP()));
Jim Inghame37d6052011-09-13 00:29:56 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087SBSourceManager::SBSourceManager(const SBSourceManager &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000088 LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBSourceManager &),
89 rhs);
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (&rhs == this)
92 return;
93
Jonas Devlieghered5b44032019-02-13 06:25:41 +000094 m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get())));
Jim Inghame37d6052011-09-13 00:29:56 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097const lldb::SBSourceManager &SBSourceManager::
98operator=(const lldb::SBSourceManager &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000099 LLDB_RECORD_METHOD(const lldb::SBSourceManager &,
100 SBSourceManager, operator=,(const lldb::SBSourceManager &),
101 rhs);
102
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000103 m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get())));
Jonas Devlieghere306809f2019-04-03 21:31:22 +0000104 return LLDB_RECORD_RESULT(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
Jonas Devlieghere866b7a62020-02-17 22:57:06 -0800107SBSourceManager::~SBSourceManager() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109size_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) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000112 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
Todd Fiala9666ba72016-09-21 20:13:14 +0000118 const uint32_t column = 0;
119 return DisplaySourceLinesWithLineNumbersAndColumn(
120 file.ref(), line, column, context_before, context_after,
121 current_line_cstr, s);
122}
123
124size_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) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000128 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
Konrad Kleine248a1302019-05-23 11:14:47 +0000134 if (m_opaque_up == nullptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +0000136
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000137 return m_opaque_up->DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +0000138 file.ref(), line, column, context_before, context_after,
139 current_line_cstr, s.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140}
Michal Gornyae211ec2019-03-19 17:13:13 +0000141
142namespace lldb_private {
143namespace repro {
144
145template <>
146void 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}