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 "lldb/Utility/ReproducerInstrumentation.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 *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   return sb_file_spec;
74 }
75 
76 uint32_t SBDeclaration::GetLine() const {
77   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDeclaration, GetLine);
78 
79 
80   uint32_t line = 0;
81   if (m_opaque_up)
82     line = m_opaque_up->GetLine();
83 
84 
85   return line;
86 }
87 
88 uint32_t SBDeclaration::GetColumn() const {
89   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBDeclaration, GetColumn);
90 
91   if (m_opaque_up)
92     return m_opaque_up->GetColumn();
93   return 0;
94 }
95 
96 void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) {
97   LLDB_RECORD_METHOD(void, SBDeclaration, SetFileSpec, (lldb::SBFileSpec),
98                      filespec);
99 
100   if (filespec.IsValid())
101     ref().SetFile(filespec.ref());
102   else
103     ref().SetFile(FileSpec());
104 }
105 void SBDeclaration::SetLine(uint32_t line) {
106   LLDB_RECORD_METHOD(void, SBDeclaration, SetLine, (uint32_t), line);
107 
108   ref().SetLine(line);
109 }
110 
111 void SBDeclaration::SetColumn(uint32_t column) {
112   LLDB_RECORD_METHOD(void, SBDeclaration, SetColumn, (uint32_t), column);
113 
114   ref().SetColumn(column);
115 }
116 
117 bool SBDeclaration::operator==(const SBDeclaration &rhs) const {
118   LLDB_RECORD_METHOD_CONST(
119       bool, SBDeclaration, operator==,(const lldb::SBDeclaration &), rhs);
120 
121   lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
122   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
123 
124   if (lhs_ptr && rhs_ptr)
125     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0;
126 
127   return lhs_ptr == rhs_ptr;
128 }
129 
130 bool SBDeclaration::operator!=(const SBDeclaration &rhs) const {
131   LLDB_RECORD_METHOD_CONST(
132       bool, SBDeclaration, operator!=,(const lldb::SBDeclaration &), rhs);
133 
134   lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
135   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
136 
137   if (lhs_ptr && rhs_ptr)
138     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0;
139 
140   return lhs_ptr != rhs_ptr;
141 }
142 
143 const lldb_private::Declaration *SBDeclaration::operator->() const {
144   return m_opaque_up.get();
145 }
146 
147 lldb_private::Declaration &SBDeclaration::ref() {
148   if (m_opaque_up == nullptr)
149     m_opaque_up = std::make_unique<lldb_private::Declaration>();
150   return *m_opaque_up;
151 }
152 
153 const lldb_private::Declaration &SBDeclaration::ref() const {
154   return *m_opaque_up;
155 }
156 
157 bool SBDeclaration::GetDescription(SBStream &description) {
158   LLDB_RECORD_METHOD(bool, SBDeclaration, GetDescription, (lldb::SBStream &),
159                      description);
160 
161   Stream &strm = description.ref();
162 
163   if (m_opaque_up) {
164     char file_path[PATH_MAX * 2];
165     m_opaque_up->GetFile().GetPath(file_path, sizeof(file_path));
166     strm.Printf("%s:%u", file_path, GetLine());
167     if (GetColumn() > 0)
168       strm.Printf(":%u", GetColumn());
169   } else
170     strm.PutCString("No value");
171 
172   return true;
173 }
174 
175 lldb_private::Declaration *SBDeclaration::get() { return m_opaque_up.get(); }
176