1 //===-- 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
10 #include "lldb/API/SBDebugger.h"
11 #include "lldb/API/SBSourceManager.h"
12 #include "lldb/API/SBTarget.h"
13 #include "lldb/API/SBStream.h"
14
15 #include "lldb/API/SBFileSpec.h"
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Core/SourceManager.h"
20
21 #include "lldb/Target/Target.h"
22
23 namespace lldb_private
24 {
25 class SourceManagerImpl
26 {
27 public:
SourceManagerImpl(const lldb::DebuggerSP & debugger_sp)28 SourceManagerImpl (const lldb::DebuggerSP &debugger_sp) :
29 m_debugger_wp (debugger_sp),
30 m_target_wp ()
31 {
32 }
33
SourceManagerImpl(const lldb::TargetSP & target_sp)34 SourceManagerImpl (const lldb::TargetSP &target_sp) :
35 m_debugger_wp (),
36 m_target_wp (target_sp)
37 {
38 }
39
SourceManagerImpl(const SourceManagerImpl & rhs)40 SourceManagerImpl (const SourceManagerImpl &rhs)
41 {
42 if (&rhs == this)
43 return;
44 m_debugger_wp = rhs.m_debugger_wp;
45 m_target_wp = rhs.m_target_wp;
46 }
47
48 size_t
DisplaySourceLinesWithLineNumbers(const lldb_private::FileSpec & file,uint32_t line,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,lldb_private::Stream * s)49 DisplaySourceLinesWithLineNumbers (const lldb_private::FileSpec &file,
50 uint32_t line,
51 uint32_t context_before,
52 uint32_t context_after,
53 const char *current_line_cstr,
54 lldb_private::Stream *s)
55 {
56 if (!file)
57 return 0;
58
59 lldb::TargetSP target_sp (m_target_wp.lock());
60 if (target_sp)
61 {
62 return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
63 line,
64 context_before,
65 context_after,
66 current_line_cstr,
67 s);
68 }
69 else
70 {
71 lldb::DebuggerSP debugger_sp (m_debugger_wp.lock());
72 if (debugger_sp)
73 {
74 return debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
75 line,
76 context_before,
77 context_after,
78 current_line_cstr,
79 s);
80 }
81 }
82 return 0;
83 }
84
85 private:
86 lldb::DebuggerWP m_debugger_wp;
87 lldb::TargetWP m_target_wp;
88
89 };
90 }
91
92 using namespace lldb;
93 using namespace lldb_private;
94
SBSourceManager(const SBDebugger & debugger)95 SBSourceManager::SBSourceManager (const SBDebugger &debugger)
96 {
97 m_opaque_ap.reset(new SourceManagerImpl (debugger.get_sp()));
98 }
99
SBSourceManager(const SBTarget & target)100 SBSourceManager::SBSourceManager (const SBTarget &target)
101 {
102 m_opaque_ap.reset(new SourceManagerImpl (target.GetSP()));
103 }
104
SBSourceManager(const SBSourceManager & rhs)105 SBSourceManager::SBSourceManager (const SBSourceManager &rhs)
106 {
107 if (&rhs == this)
108 return;
109
110 m_opaque_ap.reset(new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
111 }
112
113 const lldb::SBSourceManager &
operator =(const lldb::SBSourceManager & rhs)114 SBSourceManager::operator = (const lldb::SBSourceManager &rhs)
115 {
116 m_opaque_ap.reset (new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
117 return *this;
118 }
119
~SBSourceManager()120 SBSourceManager::~SBSourceManager()
121 {
122 }
123
124 size_t
DisplaySourceLinesWithLineNumbers(const SBFileSpec & file,uint32_t line,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,SBStream & s)125 SBSourceManager::DisplaySourceLinesWithLineNumbers
126 (
127 const SBFileSpec &file,
128 uint32_t line,
129 uint32_t context_before,
130 uint32_t context_after,
131 const char *current_line_cstr,
132 SBStream &s
133 )
134 {
135 if (m_opaque_ap.get() == NULL)
136 return 0;
137
138 return m_opaque_ap->DisplaySourceLinesWithLineNumbers (file.ref(),
139 line,
140 context_before,
141 context_after,
142 current_line_cstr,
143 s.get());
144 }
145