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:
28         SourceManagerImpl (const lldb::DebuggerSP &debugger_sp)
29         {
30             m_debugger_sp = debugger_sp;
31         }
32 
33         SourceManagerImpl (const lldb::TargetSP &target_sp)
34         {
35             m_target_sp = target_sp;
36         }
37 
38         SourceManagerImpl (const SourceManagerImpl &rhs)
39         {
40             if (&rhs == this)
41                 return;
42             m_debugger_sp = rhs.m_debugger_sp;
43             m_target_sp   = rhs.m_target_sp;
44         }
45 
46         size_t
47         DisplaySourceLinesWithLineNumbers (const lldb_private::FileSpec &file,
48                                            uint32_t line,
49                                            uint32_t context_before,
50                                            uint32_t context_after,
51                                            const char* current_line_cstr,
52                                            lldb_private::Stream *s)
53         {
54             if (!file)
55                 return 0;
56 
57             if (m_debugger_sp)
58                 return m_debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
59                                                                                             line,
60                                                                                             context_before,
61                                                                                             context_after,
62                                                                                             current_line_cstr,
63                                                                                             s);
64             else if (m_target_sp)
65                 return m_target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
66                                                                                           line,
67                                                                                           context_before,
68                                                                                           context_after,
69                                                                                           current_line_cstr,
70                                                                                           s);
71             else
72                 return 0;
73         }
74 
75     private:
76         lldb::DebuggerSP m_debugger_sp;
77         lldb::TargetSP   m_target_sp;
78 
79     };
80 }
81 
82 using namespace lldb;
83 using namespace lldb_private;
84 
85 SBSourceManager::SBSourceManager (const SBDebugger &debugger)
86 {
87     m_opaque_ap.reset(new SourceManagerImpl (debugger.get_sp()));
88 }
89 
90 SBSourceManager::SBSourceManager (const SBTarget &target)
91 {
92     m_opaque_ap.reset(new SourceManagerImpl (target.get_sp()));
93 }
94 
95 SBSourceManager::SBSourceManager (const SBSourceManager &rhs)
96 {
97     if (&rhs == this)
98         return;
99 
100     m_opaque_ap.reset(new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
101 }
102 
103 const lldb::SBSourceManager &
104 SBSourceManager::operator = (const lldb::SBSourceManager &rhs)
105 {
106     m_opaque_ap.reset (new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
107     return *this;
108 }
109 
110 SBSourceManager::~SBSourceManager()
111 {
112 }
113 
114 size_t
115 SBSourceManager::DisplaySourceLinesWithLineNumbers
116 (
117     const SBFileSpec &file,
118     uint32_t line,
119     uint32_t context_before,
120     uint32_t context_after,
121     const char* current_line_cstr,
122     SBStream &s
123 )
124 {
125     if (m_opaque_ap.get() == NULL)
126         return 0;
127 
128     return m_opaque_ap->DisplaySourceLinesWithLineNumbers (file.ref(),
129                                                            line,
130                                                            context_before,
131                                                            context_after,
132                                                            current_line_cstr,
133                                                            s.get());
134 }
135