1cfca06d7SDimitry Andric //===-- SBSourceManager.cpp -----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBSourceManager.h"
1014f1b3e8SDimitry Andric #include "lldb/API/SBDebugger.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
1214f1b3e8SDimitry Andric #include "lldb/API/SBTarget.h"
136f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
14f034231aSEd Maste
15f034231aSEd Maste #include "lldb/API/SBFileSpec.h"
16f034231aSEd Maste #include "lldb/Core/Debugger.h"
1714f1b3e8SDimitry Andric #include "lldb/Core/SourceManager.h"
1874a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
19f034231aSEd Maste
20f034231aSEd Maste #include "lldb/Target/Target.h"
21f034231aSEd Maste
2214f1b3e8SDimitry Andric namespace lldb_private {
2314f1b3e8SDimitry Andric class SourceManagerImpl {
24f034231aSEd Maste public:
SourceManagerImpl(const lldb::DebuggerSP & debugger_sp)2514f1b3e8SDimitry Andric SourceManagerImpl(const lldb::DebuggerSP &debugger_sp)
266f8fc217SDimitry Andric : m_debugger_wp(debugger_sp) {}
27f034231aSEd Maste
SourceManagerImpl(const lldb::TargetSP & target_sp)286f8fc217SDimitry Andric SourceManagerImpl(const lldb::TargetSP &target_sp) : m_target_wp(target_sp) {}
29f034231aSEd Maste
SourceManagerImpl(const SourceManagerImpl & rhs)3014f1b3e8SDimitry Andric SourceManagerImpl(const SourceManagerImpl &rhs) {
31f034231aSEd Maste if (&rhs == this)
32f034231aSEd Maste return;
33f034231aSEd Maste m_debugger_wp = rhs.m_debugger_wp;
34f034231aSEd Maste m_target_wp = rhs.m_target_wp;
35f034231aSEd Maste }
36f034231aSEd Maste
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)3714f1b3e8SDimitry Andric size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file,
3814f1b3e8SDimitry Andric uint32_t line, uint32_t column,
39f034231aSEd Maste uint32_t context_before,
40f034231aSEd Maste uint32_t context_after,
41f034231aSEd Maste const char *current_line_cstr,
4214f1b3e8SDimitry Andric lldb_private::Stream *s) {
43f034231aSEd Maste if (!file)
44f034231aSEd Maste return 0;
45f034231aSEd Maste
46f034231aSEd Maste lldb::TargetSP target_sp(m_target_wp.lock());
4714f1b3e8SDimitry Andric if (target_sp) {
4814f1b3e8SDimitry Andric return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers(
4914f1b3e8SDimitry Andric file, line, column, context_before, context_after, current_line_cstr,
50f034231aSEd Maste s);
5114f1b3e8SDimitry Andric } else {
52f034231aSEd Maste lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
5314f1b3e8SDimitry Andric if (debugger_sp) {
5414f1b3e8SDimitry Andric return debugger_sp->GetSourceManager()
5514f1b3e8SDimitry Andric .DisplaySourceLinesWithLineNumbers(file, line, column,
5614f1b3e8SDimitry Andric context_before, context_after,
5714f1b3e8SDimitry Andric current_line_cstr, s);
58f034231aSEd Maste }
59f034231aSEd Maste }
60f034231aSEd Maste return 0;
61f034231aSEd Maste }
62f034231aSEd Maste
63f034231aSEd Maste private:
64f034231aSEd Maste lldb::DebuggerWP m_debugger_wp;
65f034231aSEd Maste lldb::TargetWP m_target_wp;
66f034231aSEd Maste };
67f034231aSEd Maste }
68f034231aSEd Maste
69f034231aSEd Maste using namespace lldb;
70f034231aSEd Maste using namespace lldb_private;
71f034231aSEd Maste
SBSourceManager(const SBDebugger & debugger)7214f1b3e8SDimitry Andric SBSourceManager::SBSourceManager(const SBDebugger &debugger) {
736f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, debugger);
745f29bb8aSDimitry Andric
75cfca06d7SDimitry Andric m_opaque_up = std::make_unique<SourceManagerImpl>(debugger.get_sp());
76f034231aSEd Maste }
77f034231aSEd Maste
SBSourceManager(const SBTarget & target)7814f1b3e8SDimitry Andric SBSourceManager::SBSourceManager(const SBTarget &target) {
796f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, target);
805f29bb8aSDimitry Andric
81cfca06d7SDimitry Andric m_opaque_up = std::make_unique<SourceManagerImpl>(target.GetSP());
82f034231aSEd Maste }
83f034231aSEd Maste
SBSourceManager(const SBSourceManager & rhs)8414f1b3e8SDimitry Andric SBSourceManager::SBSourceManager(const SBSourceManager &rhs) {
856f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
865f29bb8aSDimitry Andric
87f034231aSEd Maste if (&rhs == this)
88f034231aSEd Maste return;
89f034231aSEd Maste
90e3b55780SDimitry Andric m_opaque_up = std::make_unique<SourceManagerImpl>(*rhs.m_opaque_up);
91f034231aSEd Maste }
92f034231aSEd Maste
9314f1b3e8SDimitry Andric const lldb::SBSourceManager &SBSourceManager::
operator =(const lldb::SBSourceManager & rhs)9414f1b3e8SDimitry Andric operator=(const lldb::SBSourceManager &rhs) {
956f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
965f29bb8aSDimitry Andric
97e3b55780SDimitry Andric m_opaque_up = std::make_unique<SourceManagerImpl>(*rhs.m_opaque_up);
986f8fc217SDimitry Andric return *this;
99f034231aSEd Maste }
100f034231aSEd Maste
101cfca06d7SDimitry Andric SBSourceManager::~SBSourceManager() = default;
10214f1b3e8SDimitry Andric
DisplaySourceLinesWithLineNumbers(const SBFileSpec & file,uint32_t line,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,SBStream & s)10314f1b3e8SDimitry Andric size_t SBSourceManager::DisplaySourceLinesWithLineNumbers(
10414f1b3e8SDimitry Andric const SBFileSpec &file, uint32_t line, uint32_t context_before,
10514f1b3e8SDimitry Andric uint32_t context_after, const char *current_line_cstr, SBStream &s) {
1066f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, file, line, context_before, context_after,
1075f29bb8aSDimitry Andric current_line_cstr, s);
1085f29bb8aSDimitry Andric
10914f1b3e8SDimitry Andric const uint32_t column = 0;
11014f1b3e8SDimitry Andric return DisplaySourceLinesWithLineNumbersAndColumn(
11114f1b3e8SDimitry Andric file.ref(), line, column, context_before, context_after,
11214f1b3e8SDimitry Andric current_line_cstr, s);
113f034231aSEd Maste }
114f034231aSEd Maste
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)11514f1b3e8SDimitry Andric size_t SBSourceManager::DisplaySourceLinesWithLineNumbersAndColumn(
11614f1b3e8SDimitry Andric const SBFileSpec &file, uint32_t line, uint32_t column,
11714f1b3e8SDimitry Andric uint32_t context_before, uint32_t context_after,
11814f1b3e8SDimitry Andric const char *current_line_cstr, SBStream &s) {
1196f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, file, line, column, context_before, context_after,
1206f8fc217SDimitry Andric current_line_cstr, s);
1215f29bb8aSDimitry Andric
1225f29bb8aSDimitry Andric if (m_opaque_up == nullptr)
123f034231aSEd Maste return 0;
124f034231aSEd Maste
1255f29bb8aSDimitry Andric return m_opaque_up->DisplaySourceLinesWithLineNumbers(
12614f1b3e8SDimitry Andric file.ref(), line, column, context_before, context_after,
12714f1b3e8SDimitry Andric current_line_cstr, s.get());
128f034231aSEd Maste }
129