blob: 5804c22bacb8b3b44a1229c34cb296a2bcf8d7a4 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman4c5de692010-06-09 07:44:37 +000010#include "lldb/API/SBSourceManager.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "lldb/API/SBDebugger.h"
Johnny Chenf6eaba82010-12-11 01:20:39 +000012#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014
15#include "lldb/API/SBFileSpec.h"
Jim Inghame37d6052011-09-13 00:29:56 +000016#include "lldb/Core/Debugger.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Core/SourceManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/StreamFile.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
Jim Inghame37d6052011-09-13 00:29:56 +000021#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023namespace lldb_private {
24class SourceManagerImpl {
25public:
26 SourceManagerImpl(const lldb::DebuggerSP &debugger_sp)
27 : m_debugger_wp(debugger_sp), m_target_wp() {}
28
29 SourceManagerImpl(const lldb::TargetSP &target_sp)
30 : m_debugger_wp(), m_target_wp(target_sp) {}
31
32 SourceManagerImpl(const SourceManagerImpl &rhs) {
33 if (&rhs == this)
34 return;
35 m_debugger_wp = rhs.m_debugger_wp;
36 m_target_wp = rhs.m_target_wp;
37 }
38
39 size_t DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec &file,
Todd Fiala9666ba72016-09-21 20:13:14 +000040 uint32_t line, uint32_t column,
Greg Clayton9a377662011-10-01 02:59:24 +000041 uint32_t context_before,
42 uint32_t context_after,
Johnny Chen1cbbdac2011-12-20 00:04:58 +000043 const char *current_line_cstr,
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 lldb_private::Stream *s) {
45 if (!file)
46 return 0;
47
48 lldb::TargetSP target_sp(m_target_wp.lock());
49 if (target_sp) {
50 return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +000051 file, line, column, context_before, context_after, current_line_cstr,
52 s);
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 } else {
54 lldb::DebuggerSP debugger_sp(m_debugger_wp.lock());
55 if (debugger_sp) {
56 return debugger_sp->GetSourceManager()
Todd Fiala9666ba72016-09-21 20:13:14 +000057 .DisplaySourceLinesWithLineNumbers(file, line, column,
58 context_before, context_after,
59 current_line_cstr, s);
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 }
61 }
62 return 0;
63 }
64
65private:
66 lldb::DebuggerWP m_debugger_wp;
67 lldb::TargetWP m_target_wp;
68};
Greg Clayton9a377662011-10-01 02:59:24 +000069}
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071using namespace lldb;
72using namespace lldb_private;
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074SBSourceManager::SBSourceManager(const SBDebugger &debugger) {
75 m_opaque_ap.reset(new SourceManagerImpl(debugger.get_sp()));
Jim Inghame37d6052011-09-13 00:29:56 +000076}
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078SBSourceManager::SBSourceManager(const SBTarget &target) {
79 m_opaque_ap.reset(new SourceManagerImpl(target.GetSP()));
Jim Inghame37d6052011-09-13 00:29:56 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082SBSourceManager::SBSourceManager(const SBSourceManager &rhs) {
83 if (&rhs == this)
84 return;
85
86 m_opaque_ap.reset(new SourceManagerImpl(*(rhs.m_opaque_ap.get())));
Jim Inghame37d6052011-09-13 00:29:56 +000087}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089const lldb::SBSourceManager &SBSourceManager::
90operator=(const lldb::SBSourceManager &rhs) {
91 m_opaque_ap.reset(new SourceManagerImpl(*(rhs.m_opaque_ap.get())));
92 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095SBSourceManager::~SBSourceManager() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097size_t SBSourceManager::DisplaySourceLinesWithLineNumbers(
98 const SBFileSpec &file, uint32_t line, uint32_t context_before,
99 uint32_t context_after, const char *current_line_cstr, SBStream &s) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000100 const uint32_t column = 0;
101 return DisplaySourceLinesWithLineNumbersAndColumn(
102 file.ref(), line, column, context_before, context_after,
103 current_line_cstr, s);
104}
105
106size_t SBSourceManager::DisplaySourceLinesWithLineNumbersAndColumn(
107 const SBFileSpec &file, uint32_t line, uint32_t column,
108 uint32_t context_before, uint32_t context_after,
109 const char *current_line_cstr, SBStream &s) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 if (m_opaque_ap.get() == NULL)
111 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 return m_opaque_ap->DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +0000114 file.ref(), line, column, context_before, context_after,
115 current_line_cstr, s.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}