blob: 85eba4f888705cc5daa201dfc35f509802111d43 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eli Friedman4c5de692010-06-09 07:44:37 +00009#include "lldb/API/SBSourceManager.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "lldb/API/SBDebugger.h"
Johnny Chenf6eaba82010-12-11 01:20:39 +000011#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013
14#include "lldb/API/SBFileSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000015#include "lldb/Core/Debugger.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/Core/SourceManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/StreamFile.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
Jim Inghame37d6052011-09-13 00:29:56 +000020#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022namespace lldb_private {
23class SourceManagerImpl {
24public:
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 Fiala9666ba72016-09-21 20:13:14 +000039 uint32_t line, uint32_t column,
Greg Clayton9a377662011-10-01 02:59:24 +000040 uint32_t context_before,
41 uint32_t context_after,
Johnny Chen1cbbdac2011-12-20 00:04:58 +000042 const char *current_line_cstr,
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 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 Fiala9666ba72016-09-21 20:13:14 +000050 file, line, column, context_before, context_after, current_line_cstr,
51 s);
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 } else {
53 lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
54 if (debugger_sp) {
55 return debugger_sp->GetSourceManager()
Todd Fiala9666ba72016-09-21 20:13:14 +000056 .DisplaySourceLinesWithLineNumbers(file, line, column,
57 context_before, context_after,
58 current_line_cstr, s);
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 }
60 }
61 return 0;
62 }
63
64private:
65 lldb::DebuggerWP m_debugger_wp;
66 lldb::TargetWP m_target_wp;
67};
Greg Clayton9a377662011-10-01 02:59:24 +000068}
69
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070using namespace lldb;
71using namespace lldb_private;
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073SBSourceManager::SBSourceManager(const SBDebugger &debugger) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000074 m_opaque_up.reset(new SourceManagerImpl(debugger.get_sp()));
Jim Inghame37d6052011-09-13 00:29:56 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077SBSourceManager::SBSourceManager(const SBTarget &target) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000078 m_opaque_up.reset(new SourceManagerImpl(target.GetSP()));
Jim Inghame37d6052011-09-13 00:29:56 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081SBSourceManager::SBSourceManager(const SBSourceManager &rhs) {
82 if (&rhs == this)
83 return;
84
Jonas Devlieghered5b44032019-02-13 06:25:41 +000085 m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get())));
Jim Inghame37d6052011-09-13 00:29:56 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088const lldb::SBSourceManager &SBSourceManager::
89operator=(const lldb::SBSourceManager &rhs) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000090 m_opaque_up.reset(new SourceManagerImpl(*(rhs.m_opaque_up.get())));
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094SBSourceManager::~SBSourceManager() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096size_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 Fiala9666ba72016-09-21 20:13:14 +000099 const uint32_t column = 0;
100 return DisplaySourceLinesWithLineNumbersAndColumn(
101 file.ref(), line, column, context_before, context_after,
102 current_line_cstr, s);
103}
104
105size_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 Devlieghered5b44032019-02-13 06:25:41 +0000109 if (m_opaque_up == NULL)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +0000111
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000112 return m_opaque_up->DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +0000113 file.ref(), line, column, context_before, context_after,
114 current_line_cstr, s.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115}