1 //===-- SBLineEntry.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBLineEntry.h"
10 #include "Utils.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Host/PosixApi.h"
13 #include "lldb/Symbol/LineEntry.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/StreamString.h"
16 
17 #include <limits.h>
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 SBLineEntry::SBLineEntry() : m_opaque_up() {}
23 
24 SBLineEntry::SBLineEntry(const SBLineEntry &rhs) : m_opaque_up() {
25   m_opaque_up = clone(rhs.m_opaque_up);
26 }
27 
28 SBLineEntry::SBLineEntry(const lldb_private::LineEntry *lldb_object_ptr)
29     : m_opaque_up() {
30   if (lldb_object_ptr)
31     m_opaque_up = llvm::make_unique<LineEntry>(*lldb_object_ptr);
32 }
33 
34 const SBLineEntry &SBLineEntry::operator=(const SBLineEntry &rhs) {
35   if (this != &rhs)
36     m_opaque_up = clone(rhs.m_opaque_up);
37   return *this;
38 }
39 
40 void SBLineEntry::SetLineEntry(const lldb_private::LineEntry &lldb_object_ref) {
41   m_opaque_up = llvm::make_unique<LineEntry>(lldb_object_ref);
42 }
43 
44 SBLineEntry::~SBLineEntry() {}
45 
46 SBAddress SBLineEntry::GetStartAddress() const {
47   SBAddress sb_address;
48   if (m_opaque_up)
49     sb_address.SetAddress(&m_opaque_up->range.GetBaseAddress());
50 
51   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
52   if (log) {
53     StreamString sstr;
54     const Address *addr = sb_address.get();
55     if (addr)
56       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
57                  Address::DumpStyleInvalid, 4);
58     log->Printf("SBLineEntry(%p)::GetStartAddress () => SBAddress (%p): %s",
59                 static_cast<void *>(m_opaque_up.get()),
60                 static_cast<void *>(sb_address.get()), sstr.GetData());
61   }
62 
63   return sb_address;
64 }
65 
66 SBAddress SBLineEntry::GetEndAddress() const {
67   SBAddress sb_address;
68   if (m_opaque_up) {
69     sb_address.SetAddress(&m_opaque_up->range.GetBaseAddress());
70     sb_address.OffsetAddress(m_opaque_up->range.GetByteSize());
71   }
72   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
73   if (log) {
74     StreamString sstr;
75     const Address *addr = sb_address.get();
76     if (addr)
77       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
78                  Address::DumpStyleInvalid, 4);
79     log->Printf("SBLineEntry(%p)::GetEndAddress () => SBAddress (%p): %s",
80                 static_cast<void *>(m_opaque_up.get()),
81                 static_cast<void *>(sb_address.get()), sstr.GetData());
82   }
83   return sb_address;
84 }
85 
86 bool SBLineEntry::IsValid() const {
87   return m_opaque_up.get() && m_opaque_up->IsValid();
88 }
89 
90 SBFileSpec SBLineEntry::GetFileSpec() const {
91   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
92 
93   SBFileSpec sb_file_spec;
94   if (m_opaque_up.get() && m_opaque_up->file)
95     sb_file_spec.SetFileSpec(m_opaque_up->file);
96 
97   if (log) {
98     SBStream sstr;
99     sb_file_spec.GetDescription(sstr);
100     log->Printf("SBLineEntry(%p)::GetFileSpec () => SBFileSpec(%p): %s",
101                 static_cast<void *>(m_opaque_up.get()),
102                 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
103   }
104 
105   return sb_file_spec;
106 }
107 
108 uint32_t SBLineEntry::GetLine() const {
109   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
110 
111   uint32_t line = 0;
112   if (m_opaque_up)
113     line = m_opaque_up->line;
114 
115   if (log)
116     log->Printf("SBLineEntry(%p)::GetLine () => %u",
117                 static_cast<void *>(m_opaque_up.get()), line);
118 
119   return line;
120 }
121 
122 uint32_t SBLineEntry::GetColumn() const {
123   if (m_opaque_up)
124     return m_opaque_up->column;
125   return 0;
126 }
127 
128 void SBLineEntry::SetFileSpec(lldb::SBFileSpec filespec) {
129   if (filespec.IsValid())
130     ref().file = filespec.ref();
131   else
132     ref().file.Clear();
133 }
134 void SBLineEntry::SetLine(uint32_t line) { ref().line = line; }
135 
136 void SBLineEntry::SetColumn(uint32_t column) { ref().line = column; }
137 
138 bool SBLineEntry::operator==(const SBLineEntry &rhs) const {
139   lldb_private::LineEntry *lhs_ptr = m_opaque_up.get();
140   lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get();
141 
142   if (lhs_ptr && rhs_ptr)
143     return lldb_private::LineEntry::Compare(*lhs_ptr, *rhs_ptr) == 0;
144 
145   return lhs_ptr == rhs_ptr;
146 }
147 
148 bool SBLineEntry::operator!=(const SBLineEntry &rhs) const {
149   lldb_private::LineEntry *lhs_ptr = m_opaque_up.get();
150   lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get();
151 
152   if (lhs_ptr && rhs_ptr)
153     return lldb_private::LineEntry::Compare(*lhs_ptr, *rhs_ptr) != 0;
154 
155   return lhs_ptr != rhs_ptr;
156 }
157 
158 const lldb_private::LineEntry *SBLineEntry::operator->() const {
159   return m_opaque_up.get();
160 }
161 
162 lldb_private::LineEntry &SBLineEntry::ref() {
163   if (m_opaque_up == NULL)
164     m_opaque_up.reset(new lldb_private::LineEntry());
165   return *m_opaque_up;
166 }
167 
168 const lldb_private::LineEntry &SBLineEntry::ref() const { return *m_opaque_up; }
169 
170 bool SBLineEntry::GetDescription(SBStream &description) {
171   Stream &strm = description.ref();
172 
173   if (m_opaque_up) {
174     char file_path[PATH_MAX * 2];
175     m_opaque_up->file.GetPath(file_path, sizeof(file_path));
176     strm.Printf("%s:%u", file_path, GetLine());
177     if (GetColumn() > 0)
178       strm.Printf(":%u", GetColumn());
179   } else
180     strm.PutCString("No value");
181 
182   return true;
183 }
184 
185 lldb_private::LineEntry *SBLineEntry::get() { return m_opaque_up.get(); }
186