• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SBDeclaration()22 SBDeclaration::SBDeclaration() : m_opaque_up() {
23   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBDeclaration);
24 }
25 
SBDeclaration(const SBDeclaration & rhs)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 
SBDeclaration(const lldb_private::Declaration * lldb_object_ptr)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 
operator =(const SBDeclaration & rhs)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 
SetDeclaration(const lldb_private::Declaration & lldb_object_ref)48 void SBDeclaration::SetDeclaration(
49     const lldb_private::Declaration &lldb_object_ref) {
50   ref() = lldb_object_ref;
51 }
52 
53 SBDeclaration::~SBDeclaration() = default;
54 
IsValid() const55 bool SBDeclaration::IsValid() const {
56   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBDeclaration, IsValid);
57   return this->operator bool();
58 }
operator bool() const59 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 
GetFileSpec() const65 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 
GetLine() const78 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 
GetColumn() const90 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 
SetFileSpec(lldb::SBFileSpec filespec)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 }
SetLine(uint32_t line)107 void SBDeclaration::SetLine(uint32_t line) {
108   LLDB_RECORD_METHOD(void, SBDeclaration, SetLine, (uint32_t), line);
109 
110   ref().SetLine(line);
111 }
112 
SetColumn(uint32_t column)113 void SBDeclaration::SetColumn(uint32_t column) {
114   LLDB_RECORD_METHOD(void, SBDeclaration, SetColumn, (uint32_t), column);
115 
116   ref().SetColumn(column);
117 }
118 
operator ==(const SBDeclaration & rhs) const119 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 
operator !=(const SBDeclaration & rhs) const132 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 
operator ->() const145 const lldb_private::Declaration *SBDeclaration::operator->() const {
146   return m_opaque_up.get();
147 }
148 
ref()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 
ref() const155 const lldb_private::Declaration &SBDeclaration::ref() const {
156   return *m_opaque_up;
157 }
158 
GetDescription(SBStream & description)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 
get()177 lldb_private::Declaration *SBDeclaration::get() { return m_opaque_up.get(); }
178 
179 namespace lldb_private {
180 namespace repro {
181 
182 template <>
RegisterMethods(Registry & R)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