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