| 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 "lldb/API/SBDebugger.h" |
| 11 | #include "lldb/API/SBStream.h" |
| 12 | #include "lldb/API/SBTarget.h" |
| 13 | #include "lldb/Utility/Instrumentation.h" |
| 14 | |
| 15 | #include "lldb/API/SBFileSpec.h" |
| 16 | #include "lldb/Core/Debugger.h" |
| 17 | #include "lldb/Core/SourceManager.h" |
| 18 | #include "lldb/Utility/Stream.h" |
| 19 | |
| 20 | #include "lldb/Target/Target.h" |
| 21 | |
| 22 | namespace lldb_private { |
| 23 | class SourceManagerImpl { |
| 24 | public: |
| 25 | SourceManagerImpl(const lldb::DebuggerSP &debugger_sp) |
| 26 | : m_debugger_wp(debugger_sp) {} |
| 27 | |
| 28 | SourceManagerImpl(const lldb::TargetSP &target_sp) : m_target_wp(target_sp) {} |
| 29 | |
| 30 | SourceManagerImpl(const SourceManagerImpl &rhs) { |
| 31 | if (&rhs == this) |
| 32 | return; |
| 33 | m_debugger_wp = rhs.m_debugger_wp; |
| 34 | m_target_wp = rhs.m_target_wp; |
| 35 | } |
| 36 | |
| 37 | size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file, |
| 38 | uint32_t line, uint32_t column, |
| 39 | uint32_t context_before, |
| 40 | uint32_t context_after, |
| 41 | const char *current_line_cstr, |
| 42 | lldb_private::Stream *s) { |
| 43 | if (!file) |
| 44 | return 0; |
| 45 | |
| 46 | lldb::TargetSP target_sp(m_target_wp.lock()); |
| 47 | if (target_sp) { |
| 48 | return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers( |
| 49 | support_file_sp: std::make_shared<SupportFile>(args: file), line, column, context_before, |
| 50 | context_after, current_line_cstr, s); |
| 51 | } else { |
| 52 | lldb::DebuggerSP debugger_sp(m_debugger_wp.lock()); |
| 53 | if (debugger_sp) { |
| 54 | return debugger_sp->GetSourceManager() |
| 55 | .DisplaySourceLinesWithLineNumbers( |
| 56 | support_file_sp: std::make_shared<SupportFile>(args: file), line, column, |
| 57 | context_before, context_after, current_line_cstr, s); |
| 58 | } |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | lldb::DebuggerWP m_debugger_wp; |
| 65 | lldb::TargetWP m_target_wp; |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | using namespace lldb; |
| 70 | using namespace lldb_private; |
| 71 | |
| 72 | SBSourceManager::SBSourceManager(const SBDebugger &debugger) { |
| 73 | LLDB_INSTRUMENT_VA(this, debugger); |
| 74 | |
| 75 | m_opaque_up = std::make_unique<SourceManagerImpl>(args: debugger.get_sp()); |
| 76 | } |
| 77 | |
| 78 | SBSourceManager::SBSourceManager(const SBTarget &target) { |
| 79 | LLDB_INSTRUMENT_VA(this, target); |
| 80 | |
| 81 | m_opaque_up = std::make_unique<SourceManagerImpl>(args: target.GetSP()); |
| 82 | } |
| 83 | |
| 84 | SBSourceManager::SBSourceManager(const SBSourceManager &rhs) { |
| 85 | LLDB_INSTRUMENT_VA(this, rhs); |
| 86 | |
| 87 | if (&rhs == this) |
| 88 | return; |
| 89 | |
| 90 | m_opaque_up = std::make_unique<SourceManagerImpl>(args&: *rhs.m_opaque_up); |
| 91 | } |
| 92 | |
| 93 | const lldb::SBSourceManager &SBSourceManager:: |
| 94 | operator=(const lldb::SBSourceManager &rhs) { |
| 95 | LLDB_INSTRUMENT_VA(this, rhs); |
| 96 | |
| 97 | m_opaque_up = std::make_unique<SourceManagerImpl>(args&: *rhs.m_opaque_up); |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | SBSourceManager::~SBSourceManager() = default; |
| 102 | |
| 103 | size_t SBSourceManager::DisplaySourceLinesWithLineNumbers( |
| 104 | const SBFileSpec &file, uint32_t line, uint32_t context_before, |
| 105 | uint32_t context_after, const char *current_line_cstr, SBStream &s) { |
| 106 | LLDB_INSTRUMENT_VA(this, file, line, context_before, context_after, |
| 107 | current_line_cstr, s); |
| 108 | |
| 109 | const uint32_t column = 0; |
| 110 | return DisplaySourceLinesWithLineNumbersAndColumn( |
| 111 | file: file.ref(), line, column, context_before, context_after, |
| 112 | current_line_cstr, s); |
| 113 | } |
| 114 | |
| 115 | size_t SBSourceManager::DisplaySourceLinesWithLineNumbersAndColumn( |
| 116 | const SBFileSpec &file, uint32_t line, uint32_t column, |
| 117 | uint32_t context_before, uint32_t context_after, |
| 118 | const char *current_line_cstr, SBStream &s) { |
| 119 | LLDB_INSTRUMENT_VA(this, file, line, column, context_before, context_after, |
| 120 | current_line_cstr, s); |
| 121 | |
| 122 | if (m_opaque_up == nullptr) |
| 123 | return 0; |
| 124 | |
| 125 | return m_opaque_up->DisplaySourceLinesWithLineNumbers( |
| 126 | file: file.ref(), line, column, context_before, context_after, |
| 127 | current_line_cstr, s: s.get()); |
| 128 | } |
| 129 | |