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 using namespace lldb;
24 using namespace lldb_private;
25 
26 class lldb::SBSourceManager_impl
27 {
28 public:
29     SBSourceManager_impl (const SBDebugger &debugger)
30     {
31         m_debugger_sp = debugger.m_opaque_sp;
32     }
33 
34     SBSourceManager_impl (const SBTarget &target)
35     {
36         m_target_sp = target.m_opaque_sp;
37     }
38 
39     SBSourceManager_impl (const SBSourceManager_impl &rhs)
40     {
41         if (&rhs == this)
42             return;
43         m_debugger_sp = rhs.m_debugger_sp;
44         m_target_sp   = rhs.m_target_sp;
45     }
46 
47     size_t
48     DisplaySourceLinesWithLineNumbers
49     (
50         const SBFileSpec &file,
51         uint32_t line,
52         uint32_t context_before,
53         uint32_t context_after,
54         const char* current_line_cstr,
55         SBStream &s
56     )
57     {
58         if (!file.IsValid())
59             return 0;
60 
61         if (m_debugger_sp)
62             return m_debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (*file,
63                                                                                         line,
64                                                                                         context_before,
65                                                                                         context_after,
66                                                                                         current_line_cstr,
67                                                                                         s.m_opaque_ap.get());
68         else if (m_target_sp)
69             return m_target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (*file,
70                                                                                       line,
71                                                                                       context_before,
72                                                                                       context_after,
73                                                                                       current_line_cstr,
74                                                                                       s.m_opaque_ap.get());
75         else
76             return 0;
77     }
78 
79 private:
80     lldb::DebuggerSP m_debugger_sp;
81     lldb::TargetSP   m_target_sp;
82 
83 };
84 
85 SBSourceManager::SBSourceManager (const SBDebugger &debugger)
86 {
87     m_opaque_ap.reset(new SBSourceManager_impl (debugger));
88 }
89 
90 SBSourceManager::SBSourceManager (const SBTarget &target)
91 {
92     m_opaque_ap.reset(new SBSourceManager_impl (target));
93 }
94 
95 SBSourceManager::SBSourceManager (const SBSourceManager &rhs)
96 {
97     if (&rhs == this)
98         return;
99 
100     m_opaque_ap.reset(new SBSourceManager_impl (*(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 SBSourceManager_impl (*(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,
129                                                            line,
130                                                            context_before,
131                                                            context_after,
132                                                            current_line_cstr,
133                                                            s);
134 }
135