blob: 0b8cbfceda0fe4a1b7e7baf6d3f1055fe28867c0 [file] [log] [blame]
Chris Lattner24943d22010-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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Jim Inghamcc637462011-09-13 00:29:56 +000012#include "lldb/API/SBDebugger.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000013#include "lldb/API/SBSourceManager.h"
Jim Inghamcc637462011-09-13 00:29:56 +000014#include "lldb/API/SBTarget.h"
Johnny Chene5791dd2010-12-11 01:20:39 +000015#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016
17#include "lldb/API/SBFileSpec.h"
Jim Inghamcc637462011-09-13 00:29:56 +000018#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamFile.h"
21#include "lldb/Core/SourceManager.h"
22
Jim Inghamcc637462011-09-13 00:29:56 +000023#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024
Greg Clayton15afa9f2011-10-01 02:59:24 +000025namespace lldb_private
26{
27 class SourceManagerImpl
28 {
29 public:
Greg Clayton535f53c2013-03-19 00:20:55 +000030 SourceManagerImpl (const lldb::DebuggerSP &debugger_sp) :
31 m_debugger_wp (debugger_sp),
32 m_target_wp ()
Greg Clayton15afa9f2011-10-01 02:59:24 +000033 {
Greg Clayton15afa9f2011-10-01 02:59:24 +000034 }
35
Greg Clayton535f53c2013-03-19 00:20:55 +000036 SourceManagerImpl (const lldb::TargetSP &target_sp) :
37 m_debugger_wp (),
38 m_target_wp (target_sp)
Greg Clayton15afa9f2011-10-01 02:59:24 +000039 {
Greg Clayton15afa9f2011-10-01 02:59:24 +000040 }
41
42 SourceManagerImpl (const SourceManagerImpl &rhs)
43 {
44 if (&rhs == this)
45 return;
Greg Clayton535f53c2013-03-19 00:20:55 +000046 m_debugger_wp = rhs.m_debugger_wp;
47 m_target_wp = rhs.m_target_wp;
Greg Clayton15afa9f2011-10-01 02:59:24 +000048 }
49
50 size_t
51 DisplaySourceLinesWithLineNumbers (const lldb_private::FileSpec &file,
52 uint32_t line,
53 uint32_t context_before,
54 uint32_t context_after,
Johnny Chendccbc4b2011-12-20 00:04:58 +000055 const char *current_line_cstr,
Greg Clayton15afa9f2011-10-01 02:59:24 +000056 lldb_private::Stream *s)
57 {
Johnny Chen59566042011-10-03 20:56:39 +000058 if (!file)
Greg Clayton15afa9f2011-10-01 02:59:24 +000059 return 0;
60
Greg Clayton535f53c2013-03-19 00:20:55 +000061 lldb::TargetSP target_sp (m_target_wp.lock());
62 if (target_sp)
63 {
64 return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
65 line,
66 context_before,
67 context_after,
68 current_line_cstr,
69 s);
70 }
Greg Clayton15afa9f2011-10-01 02:59:24 +000071 else
Greg Clayton535f53c2013-03-19 00:20:55 +000072 {
73 lldb::DebuggerSP debugger_sp (m_debugger_wp.lock());
74 if (debugger_sp)
75 {
76 return debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
77 line,
78 context_before,
79 context_after,
80 current_line_cstr,
81 s);
82 }
83 }
84 return 0;
Greg Clayton15afa9f2011-10-01 02:59:24 +000085 }
86
87 private:
Greg Clayton535f53c2013-03-19 00:20:55 +000088 lldb::DebuggerWP m_debugger_wp;
89 lldb::TargetWP m_target_wp;
Greg Clayton15afa9f2011-10-01 02:59:24 +000090
91 };
92}
93
Chris Lattner24943d22010-06-08 16:52:24 +000094using namespace lldb;
95using namespace lldb_private;
96
Jim Inghamcc637462011-09-13 00:29:56 +000097SBSourceManager::SBSourceManager (const SBDebugger &debugger)
98{
Greg Clayton15afa9f2011-10-01 02:59:24 +000099 m_opaque_ap.reset(new SourceManagerImpl (debugger.get_sp()));
Jim Inghamcc637462011-09-13 00:29:56 +0000100}
101
102SBSourceManager::SBSourceManager (const SBTarget &target)
103{
Greg Clayton334d33a2012-01-30 07:41:31 +0000104 m_opaque_ap.reset(new SourceManagerImpl (target.GetSP()));
Jim Inghamcc637462011-09-13 00:29:56 +0000105}
106
107SBSourceManager::SBSourceManager (const SBSourceManager &rhs)
108{
109 if (&rhs == this)
110 return;
111
Greg Clayton15afa9f2011-10-01 02:59:24 +0000112 m_opaque_ap.reset(new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
Jim Inghamcc637462011-09-13 00:29:56 +0000113}
114
115const lldb::SBSourceManager &
116SBSourceManager::operator = (const lldb::SBSourceManager &rhs)
117{
Greg Clayton15afa9f2011-10-01 02:59:24 +0000118 m_opaque_ap.reset (new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
Jim Inghamcc637462011-09-13 00:29:56 +0000119 return *this;
Chris Lattner24943d22010-06-08 16:52:24 +0000120}
121
122SBSourceManager::~SBSourceManager()
123{
124}
125
126size_t
127SBSourceManager::DisplaySourceLinesWithLineNumbers
128(
129 const SBFileSpec &file,
130 uint32_t line,
131 uint32_t context_before,
132 uint32_t context_after,
Johnny Chendccbc4b2011-12-20 00:04:58 +0000133 const char *current_line_cstr,
Johnny Chene5791dd2010-12-11 01:20:39 +0000134 SBStream &s
Chris Lattner24943d22010-06-08 16:52:24 +0000135)
136{
Jim Inghamcc637462011-09-13 00:29:56 +0000137 if (m_opaque_ap.get() == NULL)
Greg Clayton538eb822010-11-05 23:17:00 +0000138 return 0;
139
Greg Clayton15afa9f2011-10-01 02:59:24 +0000140 return m_opaque_ap->DisplaySourceLinesWithLineNumbers (file.ref(),
Jim Inghamcc637462011-09-13 00:29:56 +0000141 line,
142 context_before,
143 context_after,
144 current_line_cstr,
Greg Clayton15afa9f2011-10-01 02:59:24 +0000145 s.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000146}