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