Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- SBSourceManager.cpp -----------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eli Friedman | 4c5de69 | 2010-06-09 07:44:37 +0000 | [diff] [blame] | 9 | #include "lldb/API/SBSourceManager.h" |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 10 | #include "SBReproducerPrivate.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBDebugger.h" |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBStream.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include "lldb/API/SBTarget.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | |
| 15 | #include "lldb/API/SBFileSpec.h" |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Debugger.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include "lldb/Core/SourceManager.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/StreamFile.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | namespace lldb_private { |
| 24 | class SourceManagerImpl { |
| 25 | public: |
| 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 Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 40 | uint32_t line, uint32_t column, |
Greg Clayton | 9a37766 | 2011-10-01 02:59:24 +0000 | [diff] [blame] | 41 | uint32_t context_before, |
| 42 | uint32_t context_after, |
Johnny Chen | 1cbbdac | 2011-12-20 00:04:58 +0000 | [diff] [blame] | 43 | const char *current_line_cstr, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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( |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 51 | file, line, column, context_before, context_after, current_line_cstr, |
| 52 | s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | } else { |
| 54 | lldb::DebuggerSP debugger_sp(m_debugger_wp.lock()); |
| 55 | if (debugger_sp) { |
| 56 | return debugger_sp->GetSourceManager() |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 57 | .DisplaySourceLinesWithLineNumbers(file, line, column, |
| 58 | context_before, context_after, |
| 59 | current_line_cstr, s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | lldb::DebuggerWP m_debugger_wp; |
| 67 | lldb::TargetWP m_target_wp; |
| 68 | }; |
Greg Clayton | 9a37766 | 2011-10-01 02:59:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | using namespace lldb; |
| 72 | using namespace lldb_private; |
| 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | SBSourceManager::SBSourceManager(const SBDebugger &debugger) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 75 | LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBDebugger &), |
| 76 | debugger); |
| 77 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 78 | m_opaque_up.reset(new SourceManagerImpl(debugger.get_sp())); |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | SBSourceManager::SBSourceManager(const SBTarget &target) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 82 | LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBTarget &), target); |
| 83 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 84 | m_opaque_up.reset(new SourceManagerImpl(target.GetSP())); |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | SBSourceManager::SBSourceManager(const SBSourceManager &rhs) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 88 | LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBSourceManager &), |
| 89 | rhs); |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | if (&rhs == this) |
| 92 | return; |
| 93 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 94 | m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get()))); |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | const lldb::SBSourceManager &SBSourceManager:: |
| 98 | operator=(const lldb::SBSourceManager &rhs) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 99 | LLDB_RECORD_METHOD(const lldb::SBSourceManager &, |
| 100 | SBSourceManager, operator=,(const lldb::SBSourceManager &), |
| 101 | rhs); |
| 102 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 103 | m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get()))); |
Jonas Devlieghere | 306809f | 2019-04-03 21:31:22 +0000 | [diff] [blame] | 104 | return LLDB_RECORD_RESULT(*this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jonas Devlieghere | 866b7a6 | 2020-02-17 22:57:06 -0800 | [diff] [blame^] | 107 | SBSourceManager::~SBSourceManager() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 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 | |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 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 | |
| 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) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 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 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 134 | if (m_opaque_up == nullptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | return 0; |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 136 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 137 | return m_opaque_up->DisplaySourceLinesWithLineNumbers( |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 138 | file.ref(), line, column, context_before, context_after, |
| 139 | current_line_cstr, s.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | } |
Michal Gorny | ae211ec | 2019-03-19 17:13:13 +0000 | [diff] [blame] | 141 | |
| 142 | namespace lldb_private { |
| 143 | namespace repro { |
| 144 | |
| 145 | template <> |
| 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 | } |