blob: 743d5651bbbf7c1bf9decb0e08c8d9ffec9d172b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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
Jim Inghamcc637462011-09-13 00:29:56 +000010#include "lldb/API/SBDebugger.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000011#include "lldb/API/SBSourceManager.h"
Jim Inghamcc637462011-09-13 00:29:56 +000012#include "lldb/API/SBTarget.h"
Johnny Chene5791dd2010-12-11 01:20:39 +000013#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000014
15#include "lldb/API/SBFileSpec.h"
Jim Inghamcc637462011-09-13 00:29:56 +000016#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamFile.h"
19#include "lldb/Core/SourceManager.h"
20
Jim Inghamcc637462011-09-13 00:29:56 +000021#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
Greg Clayton15afa9f2011-10-01 02:59:24 +000023namespace 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,
Johnny Chendccbc4b2011-12-20 00:04:58 +000051 const char *current_line_cstr,
Greg Clayton15afa9f2011-10-01 02:59:24 +000052 lldb_private::Stream *s)
53 {
Johnny Chen59566042011-10-03 20:56:39 +000054 if (!file)
Greg Clayton15afa9f2011-10-01 02:59:24 +000055 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
Chris Lattner24943d22010-06-08 16:52:24 +000082using namespace lldb;
83using namespace lldb_private;
84
Jim Inghamcc637462011-09-13 00:29:56 +000085SBSourceManager::SBSourceManager (const SBDebugger &debugger)
86{
Greg Clayton15afa9f2011-10-01 02:59:24 +000087 m_opaque_ap.reset(new SourceManagerImpl (debugger.get_sp()));
Jim Inghamcc637462011-09-13 00:29:56 +000088}
89
90SBSourceManager::SBSourceManager (const SBTarget &target)
91{
Greg Clayton334d33a2012-01-30 07:41:31 +000092 m_opaque_ap.reset(new SourceManagerImpl (target.GetSP()));
Jim Inghamcc637462011-09-13 00:29:56 +000093}
94
95SBSourceManager::SBSourceManager (const SBSourceManager &rhs)
96{
97 if (&rhs == this)
98 return;
99
Greg Clayton15afa9f2011-10-01 02:59:24 +0000100 m_opaque_ap.reset(new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
Jim Inghamcc637462011-09-13 00:29:56 +0000101}
102
103const lldb::SBSourceManager &
104SBSourceManager::operator = (const lldb::SBSourceManager &rhs)
105{
Greg Clayton15afa9f2011-10-01 02:59:24 +0000106 m_opaque_ap.reset (new SourceManagerImpl (*(rhs.m_opaque_ap.get())));
Jim Inghamcc637462011-09-13 00:29:56 +0000107 return *this;
Chris Lattner24943d22010-06-08 16:52:24 +0000108}
109
110SBSourceManager::~SBSourceManager()
111{
112}
113
114size_t
115SBSourceManager::DisplaySourceLinesWithLineNumbers
116(
117 const SBFileSpec &file,
118 uint32_t line,
119 uint32_t context_before,
120 uint32_t context_after,
Johnny Chendccbc4b2011-12-20 00:04:58 +0000121 const char *current_line_cstr,
Johnny Chene5791dd2010-12-11 01:20:39 +0000122 SBStream &s
Chris Lattner24943d22010-06-08 16:52:24 +0000123)
124{
Jim Inghamcc637462011-09-13 00:29:56 +0000125 if (m_opaque_ap.get() == NULL)
Greg Clayton538eb822010-11-05 23:17:00 +0000126 return 0;
127
Greg Clayton15afa9f2011-10-01 02:59:24 +0000128 return m_opaque_ap->DisplaySourceLinesWithLineNumbers (file.ref(),
Jim Inghamcc637462011-09-13 00:29:56 +0000129 line,
130 context_before,
131 context_after,
132 current_line_cstr,
Greg Clayton15afa9f2011-10-01 02:59:24 +0000133 s.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000134}