1301169Slidl//===-- SBSourceManager.cpp -------------------------------------*- C++ -*-===//
2301169Slidl//
3301169Slidl//                     The LLVM Compiler Infrastructure
4301169Slidl//
5301169Slidl// This file is distributed under the University of Illinois Open Source
6301169Slidl// License. See LICENSE.TXT for details.
7301169Slidl//
8301169Slidl//===----------------------------------------------------------------------===//
9301169Slidl
10301169Slidl#include "lldb/lldb-python.h"
11301169Slidl
12301169Slidl#include "lldb/API/SBDebugger.h"
13301169Slidl#include "lldb/API/SBSourceManager.h"
14301169Slidl#include "lldb/API/SBTarget.h"
15301169Slidl#include "lldb/API/SBStream.h"
16301169Slidl
17301169Slidl#include "lldb/API/SBFileSpec.h"
18301169Slidl#include "lldb/Core/Debugger.h"
19301169Slidl#include "lldb/Core/Stream.h"
20301169Slidl#include "lldb/Core/StreamFile.h"
21301169Slidl#include "lldb/Core/SourceManager.h"
22301169Slidl
23301169Slidl#include "lldb/Target/Target.h"
24301169Slidl
25301169Slidlnamespace lldb_private
26301169Slidl{
27301169Slidl    class SourceManagerImpl
28301169Slidl    {
29301169Slidl    public:
30301169Slidl        SourceManagerImpl (const lldb::DebuggerSP &debugger_sp) :
31301169Slidl            m_debugger_wp (debugger_sp),
32301169Slidl            m_target_wp ()
33301169Slidl        {
34301169Slidl        }
35301169Slidl
36301169Slidl        SourceManagerImpl (const lldb::TargetSP &target_sp) :
37301169Slidl            m_debugger_wp (),
38301169Slidl            m_target_wp (target_sp)
39301169Slidl        {
40301169Slidl        }
41301169Slidl
42301169Slidl        SourceManagerImpl (const SourceManagerImpl &rhs)
43318950Slidl        {
44318950Slidl            if (&rhs == this)
45318950Slidl                return;
46301169Slidl            m_debugger_wp = rhs.m_debugger_wp;
47301169Slidl            m_target_wp   = rhs.m_target_wp;
48301169Slidl        }
49301169Slidl
50301169Slidl        size_t
51301169Slidl        DisplaySourceLinesWithLineNumbers (const lldb_private::FileSpec &file,
52301169Slidl                                           uint32_t line,
53301169Slidl                                           uint32_t context_before,
54301169Slidl                                           uint32_t context_after,
55301169Slidl                                           const char *current_line_cstr,
56301169Slidl                                           lldb_private::Stream *s)
57301169Slidl        {
58301169Slidl            if (!file)
59301169Slidl                return 0;
60301169Slidl
61301169Slidl            lldb::TargetSP target_sp (m_target_wp.lock());
62301169Slidl            if (target_sp)
63301169Slidl            {
64301169Slidl                return target_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
65301169Slidl                                                                                        line,
66301169Slidl                                                                                        context_before,
67301169Slidl                                                                                        context_after,
68301169Slidl                                                                                        current_line_cstr,
69301169Slidl                                                                                        s);
70301169Slidl            }
71301169Slidl            else
72301169Slidl            {
73301169Slidl                lldb::DebuggerSP debugger_sp (m_debugger_wp.lock());
74301169Slidl                if (debugger_sp)
75301169Slidl                {
76301169Slidl                    return debugger_sp->GetSourceManager().DisplaySourceLinesWithLineNumbers (file,
77301169Slidl                                                                                              line,
78301169Slidl                                                                                              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
94using namespace lldb;
95using namespace lldb_private;
96
97SBSourceManager::SBSourceManager (const SBDebugger &debugger)
98{
99    m_opaque_ap.reset(new SourceManagerImpl (debugger.get_sp()));
100}
101
102SBSourceManager::SBSourceManager (const SBTarget &target)
103{
104    m_opaque_ap.reset(new SourceManagerImpl (target.GetSP()));
105}
106
107SBSourceManager::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
115const lldb::SBSourceManager &
116SBSourceManager::operator = (const lldb::SBSourceManager &rhs)
117{
118    m_opaque_ap.reset (new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
119    return *this;
120}
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,
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