Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===// |
| 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" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | #include "lldb/API/SBDebugger.h" |
Johnny Chen | f6eaba8 | 2010-12-11 01:20:39 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBStream.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBTarget.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | |
| 14 | #include "lldb/API/SBFileSpec.h" |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Debugger.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | #include "lldb/Core/SourceManager.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamFile.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Target.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | namespace lldb_private { |
| 23 | class SourceManagerImpl { |
| 24 | public: |
| 25 | SourceManagerImpl(const lldb::DebuggerSP &debugger_sp) |
| 26 | : m_debugger_wp(debugger_sp), m_target_wp() {} |
| 27 | |
| 28 | SourceManagerImpl(const lldb::TargetSP &target_sp) |
| 29 | : m_debugger_wp(), m_target_wp(target_sp) {} |
| 30 | |
| 31 | SourceManagerImpl(const SourceManagerImpl &rhs) { |
| 32 | if (&rhs == this) |
| 33 | return; |
| 34 | m_debugger_wp = rhs.m_debugger_wp; |
| 35 | m_target_wp = rhs.m_target_wp; |
| 36 | } |
| 37 | |
| 38 | size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file, |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 39 | uint32_t line, uint32_t column, |
Greg Clayton | 9a37766 | 2011-10-01 02:59:24 +0000 | [diff] [blame] | 40 | uint32_t context_before, |
| 41 | uint32_t context_after, |
Johnny Chen | 1cbbdac | 2011-12-20 00:04:58 +0000 | [diff] [blame] | 42 | const char *current_line_cstr, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | lldb_private::Stream *s) { |
| 44 | if (!file) |
| 45 | return 0; |
| 46 | |
| 47 | lldb::TargetSP target_sp(m_target_wp.lock()); |
| 48 | if (target_sp) { |
| 49 | return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers( |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 50 | file, line, column, context_before, context_after, current_line_cstr, |
| 51 | s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | } else { |
| 53 | lldb::DebuggerSP debugger_sp(m_debugger_wp.lock()); |
| 54 | if (debugger_sp) { |
| 55 | return debugger_sp->GetSourceManager() |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 56 | .DisplaySourceLinesWithLineNumbers(file, line, column, |
| 57 | context_before, context_after, |
| 58 | current_line_cstr, s); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | lldb::DebuggerWP m_debugger_wp; |
| 66 | lldb::TargetWP m_target_wp; |
| 67 | }; |
Greg Clayton | 9a37766 | 2011-10-01 02:59:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | using namespace lldb; |
| 71 | using namespace lldb_private; |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | SBSourceManager::SBSourceManager(const SBDebugger &debugger) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 74 | m_opaque_up.reset(new SourceManagerImpl(debugger.get_sp())); |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | SBSourceManager::SBSourceManager(const SBTarget &target) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 78 | m_opaque_up.reset(new SourceManagerImpl(target.GetSP())); |
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 SBSourceManager &rhs) { |
| 82 | if (&rhs == this) |
| 83 | return; |
| 84 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 85 | m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get()))); |
Jim Ingham | e37d605 | 2011-09-13 00:29:56 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | const lldb::SBSourceManager &SBSourceManager:: |
| 89 | operator=(const lldb::SBSourceManager &rhs) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 90 | m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get()))); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | SBSourceManager::~SBSourceManager() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | size_t SBSourceManager::DisplaySourceLinesWithLineNumbers( |
| 97 | const SBFileSpec &file, uint32_t line, uint32_t context_before, |
| 98 | uint32_t context_after, const char *current_line_cstr, SBStream &s) { |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 99 | const uint32_t column = 0; |
| 100 | return DisplaySourceLinesWithLineNumbersAndColumn( |
| 101 | file.ref(), line, column, context_before, context_after, |
| 102 | current_line_cstr, s); |
| 103 | } |
| 104 | |
| 105 | size_t SBSourceManager::DisplaySourceLinesWithLineNumbersAndColumn( |
| 106 | const SBFileSpec &file, uint32_t line, uint32_t column, |
| 107 | uint32_t context_before, uint32_t context_after, |
| 108 | const char *current_line_cstr, SBStream &s) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 109 | if (m_opaque_up == NULL) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | return 0; |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 111 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 112 | return m_opaque_up->DisplaySourceLinesWithLineNumbers( |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 +0000 | [diff] [blame] | 113 | file.ref(), line, column, context_before, context_after, |
| 114 | current_line_cstr, s.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | } |