130fdc8d8SChris Lattner //===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
94c5de699SEli Friedman #include "lldb/API/SBSourceManager.h"
10baf5664fSJonas Devlieghere #include "SBReproducerPrivate.h"
11b9c1b51eSKate Stone #include "lldb/API/SBDebugger.h"
12f6eaba85SJohnny Chen #include "lldb/API/SBStream.h"
13b9c1b51eSKate Stone #include "lldb/API/SBTarget.h"
1430fdc8d8SChris Lattner 
1530fdc8d8SChris Lattner #include "lldb/API/SBFileSpec.h"
16e37d605eSJim Ingham #include "lldb/Core/Debugger.h"
17b9c1b51eSKate Stone #include "lldb/Core/SourceManager.h"
1830fdc8d8SChris Lattner #include "lldb/Core/StreamFile.h"
19bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2030fdc8d8SChris Lattner 
21e37d605eSJim Ingham #include "lldb/Target/Target.h"
2230fdc8d8SChris Lattner 
23b9c1b51eSKate Stone namespace lldb_private {
24b9c1b51eSKate Stone class SourceManagerImpl {
25e37d605eSJim Ingham public:
26b9c1b51eSKate Stone   SourceManagerImpl(const lldb::DebuggerSP &debugger_sp)
27b9c1b51eSKate Stone       : m_debugger_wp(debugger_sp), m_target_wp() {}
28e37d605eSJim Ingham 
29b9c1b51eSKate Stone   SourceManagerImpl(const lldb::TargetSP &target_sp)
30b9c1b51eSKate Stone       : m_debugger_wp(), m_target_wp(target_sp) {}
31e37d605eSJim Ingham 
32b9c1b51eSKate Stone   SourceManagerImpl(const SourceManagerImpl &rhs) {
33e37d605eSJim Ingham     if (&rhs == this)
34e37d605eSJim Ingham       return;
359585fbfcSGreg Clayton     m_debugger_wp = rhs.m_debugger_wp;
369585fbfcSGreg Clayton     m_target_wp = rhs.m_target_wp;
37e37d605eSJim Ingham   }
38e37d605eSJim Ingham 
39b9c1b51eSKate Stone   size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file,
409666ba75STodd Fiala                                            uint32_t line, uint32_t column,
41e37d605eSJim Ingham                                            uint32_t context_before,
42e37d605eSJim Ingham                                            uint32_t context_after,
43e37d605eSJim Ingham                                            const char *current_line_cstr,
44b9c1b51eSKate Stone                                            lldb_private::Stream *s) {
4544d6d2c5SJohnny Chen     if (!file)
46e37d605eSJim Ingham       return 0;
47e37d605eSJim Ingham 
489585fbfcSGreg Clayton     lldb::TargetSP target_sp(m_target_wp.lock());
49b9c1b51eSKate Stone     if (target_sp) {
50b9c1b51eSKate Stone       return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers(
519666ba75STodd Fiala           file, line, column, context_before, context_after, current_line_cstr,
529666ba75STodd Fiala           s);
53b9c1b51eSKate Stone     } else {
549585fbfcSGreg Clayton       lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
55b9c1b51eSKate Stone       if (debugger_sp) {
56b9c1b51eSKate Stone         return debugger_sp->GetSourceManager()
579666ba75STodd Fiala             .DisplaySourceLinesWithLineNumbers(file, line, column,
589666ba75STodd Fiala                                                context_before, context_after,
599666ba75STodd Fiala                                                current_line_cstr, s);
609585fbfcSGreg Clayton       }
619585fbfcSGreg Clayton     }
62e37d605eSJim Ingham     return 0;
63e37d605eSJim Ingham   }
64e37d605eSJim Ingham 
65e37d605eSJim Ingham private:
669585fbfcSGreg Clayton   lldb::DebuggerWP m_debugger_wp;
679585fbfcSGreg Clayton   lldb::TargetWP m_target_wp;
68e37d605eSJim Ingham };
699a37766eSGreg Clayton }
709a37766eSGreg Clayton 
719a37766eSGreg Clayton using namespace lldb;
729a37766eSGreg Clayton using namespace lldb_private;
73e37d605eSJim Ingham 
74b9c1b51eSKate Stone SBSourceManager::SBSourceManager(const SBDebugger &debugger) {
75baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBDebugger &),
76baf5664fSJonas Devlieghere                           debugger);
77baf5664fSJonas Devlieghere 
78d5b44036SJonas Devlieghere   m_opaque_up.reset(new SourceManagerImpl(debugger.get_sp()));
79e37d605eSJim Ingham }
80e37d605eSJim Ingham 
81b9c1b51eSKate Stone SBSourceManager::SBSourceManager(const SBTarget &target) {
82baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBTarget &), target);
83baf5664fSJonas Devlieghere 
84d5b44036SJonas Devlieghere   m_opaque_up.reset(new SourceManagerImpl(target.GetSP()));
85e37d605eSJim Ingham }
86e37d605eSJim Ingham 
87b9c1b51eSKate Stone SBSourceManager::SBSourceManager(const SBSourceManager &rhs) {
88baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBSourceManager, (const lldb::SBSourceManager &),
89baf5664fSJonas Devlieghere                           rhs);
90baf5664fSJonas Devlieghere 
91e37d605eSJim Ingham   if (&rhs == this)
92e37d605eSJim Ingham     return;
93e37d605eSJim Ingham 
94d5b44036SJonas Devlieghere   m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get())));
95e37d605eSJim Ingham }
96e37d605eSJim Ingham 
97b9c1b51eSKate Stone const lldb::SBSourceManager &SBSourceManager::
98b9c1b51eSKate Stone operator=(const lldb::SBSourceManager &rhs) {
99baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(const lldb::SBSourceManager &,
100baf5664fSJonas Devlieghere                      SBSourceManager, operator=,(const lldb::SBSourceManager &),
101baf5664fSJonas Devlieghere                      rhs);
102baf5664fSJonas Devlieghere 
103d5b44036SJonas Devlieghere   m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get())));
104e37d605eSJim Ingham   return *this;
10530fdc8d8SChris Lattner }
10630fdc8d8SChris Lattner 
107b9c1b51eSKate Stone SBSourceManager::~SBSourceManager() {}
10830fdc8d8SChris Lattner 
109b9c1b51eSKate Stone size_t SBSourceManager::DisplaySourceLinesWithLineNumbers(
110b9c1b51eSKate Stone     const SBFileSpec &file, uint32_t line, uint32_t context_before,
111b9c1b51eSKate Stone     uint32_t context_after, const char *current_line_cstr, SBStream &s) {
112baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(size_t, SBSourceManager, DisplaySourceLinesWithLineNumbers,
113baf5664fSJonas Devlieghere                      (const lldb::SBFileSpec &, uint32_t, uint32_t, uint32_t,
114baf5664fSJonas Devlieghere                       const char *, lldb::SBStream &),
115baf5664fSJonas Devlieghere                      file, line, context_before, context_after,
116baf5664fSJonas Devlieghere                      current_line_cstr, s);
117baf5664fSJonas Devlieghere 
1189666ba75STodd Fiala   const uint32_t column = 0;
1199666ba75STodd Fiala   return DisplaySourceLinesWithLineNumbersAndColumn(
1209666ba75STodd Fiala       file.ref(), line, column, context_before, context_after,
1219666ba75STodd Fiala       current_line_cstr, s);
1229666ba75STodd Fiala }
1239666ba75STodd Fiala 
1249666ba75STodd Fiala size_t SBSourceManager::DisplaySourceLinesWithLineNumbersAndColumn(
1259666ba75STodd Fiala     const SBFileSpec &file, uint32_t line, uint32_t column,
1269666ba75STodd Fiala     uint32_t context_before, uint32_t context_after,
1279666ba75STodd Fiala     const char *current_line_cstr, SBStream &s) {
128baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(
129baf5664fSJonas Devlieghere       size_t, SBSourceManager, DisplaySourceLinesWithLineNumbersAndColumn,
130baf5664fSJonas Devlieghere       (const lldb::SBFileSpec &, uint32_t, uint32_t, uint32_t, uint32_t,
131baf5664fSJonas Devlieghere        const char *, lldb::SBStream &),
132baf5664fSJonas Devlieghere       file, line, column, context_before, context_after, current_line_cstr, s);
133baf5664fSJonas Devlieghere 
134d5b44036SJonas Devlieghere   if (m_opaque_up == NULL)
135efabb123SGreg Clayton     return 0;
136efabb123SGreg Clayton 
137d5b44036SJonas Devlieghere   return m_opaque_up->DisplaySourceLinesWithLineNumbers(
1389666ba75STodd Fiala       file.ref(), line, column, context_before, context_after,
1399666ba75STodd Fiala       current_line_cstr, s.get());
14030fdc8d8SChris Lattner }
141*ae211eceSMichal Gorny 
142*ae211eceSMichal Gorny namespace lldb_private {
143*ae211eceSMichal Gorny namespace repro {
144*ae211eceSMichal Gorny 
145*ae211eceSMichal Gorny template <>
146*ae211eceSMichal Gorny void RegisterMethods<SBSourceManager>(Registry &R) {
147*ae211eceSMichal Gorny   LLDB_REGISTER_CONSTRUCTOR(SBSourceManager, (const lldb::SBDebugger &));
148*ae211eceSMichal Gorny   LLDB_REGISTER_CONSTRUCTOR(SBSourceManager, (const lldb::SBTarget &));
149*ae211eceSMichal Gorny   LLDB_REGISTER_CONSTRUCTOR(SBSourceManager, (const lldb::SBSourceManager &));
150*ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(
151*ae211eceSMichal Gorny       const lldb::SBSourceManager &,
152*ae211eceSMichal Gorny       SBSourceManager, operator=,(const lldb::SBSourceManager &));
153*ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(size_t, SBSourceManager,
154*ae211eceSMichal Gorny                        DisplaySourceLinesWithLineNumbers,
155*ae211eceSMichal Gorny                        (const lldb::SBFileSpec &, uint32_t, uint32_t,
156*ae211eceSMichal Gorny                         uint32_t, const char *, lldb::SBStream &));
157*ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(size_t, SBSourceManager,
158*ae211eceSMichal Gorny                        DisplaySourceLinesWithLineNumbersAndColumn,
159*ae211eceSMichal Gorny                        (const lldb::SBFileSpec &, uint32_t, uint32_t,
160*ae211eceSMichal Gorny                         uint32_t, uint32_t, const char *, lldb::SBStream &));
161*ae211eceSMichal Gorny }
162*ae211eceSMichal Gorny 
163*ae211eceSMichal Gorny }
164*ae211eceSMichal Gorny }
165