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