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