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