1dda28197Spatrick //===-- SBDeclaration.cpp -------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/API/SBDeclaration.h"
10061da546Spatrick #include "Utils.h"
11061da546Spatrick #include "lldb/API/SBStream.h"
12be691f3bSpatrick #include "lldb/Core/Declaration.h"
13061da546Spatrick #include "lldb/Host/PosixApi.h"
14*f6aab3d8Srobert #include "lldb/Utility/Instrumentation.h"
15061da546Spatrick #include "lldb/Utility/Stream.h"
16061da546Spatrick
17be691f3bSpatrick #include <climits>
18061da546Spatrick
19061da546Spatrick using namespace lldb;
20061da546Spatrick using namespace lldb_private;
21061da546Spatrick
SBDeclaration()22*f6aab3d8Srobert SBDeclaration::SBDeclaration() { LLDB_INSTRUMENT_VA(this); }
23061da546Spatrick
SBDeclaration(const SBDeclaration & rhs)24*f6aab3d8Srobert SBDeclaration::SBDeclaration(const SBDeclaration &rhs) {
25*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, rhs);
26061da546Spatrick
27061da546Spatrick m_opaque_up = clone(rhs.m_opaque_up);
28061da546Spatrick }
29061da546Spatrick
SBDeclaration(const lldb_private::Declaration * lldb_object_ptr)30*f6aab3d8Srobert SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr) {
31061da546Spatrick if (lldb_object_ptr)
32061da546Spatrick m_opaque_up = std::make_unique<Declaration>(*lldb_object_ptr);
33061da546Spatrick }
34061da546Spatrick
operator =(const SBDeclaration & rhs)35061da546Spatrick const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) {
36*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, rhs);
37061da546Spatrick
38061da546Spatrick if (this != &rhs)
39061da546Spatrick m_opaque_up = clone(rhs.m_opaque_up);
40*f6aab3d8Srobert return *this;
41061da546Spatrick }
42061da546Spatrick
SetDeclaration(const lldb_private::Declaration & lldb_object_ref)43061da546Spatrick void SBDeclaration::SetDeclaration(
44061da546Spatrick const lldb_private::Declaration &lldb_object_ref) {
45061da546Spatrick ref() = lldb_object_ref;
46061da546Spatrick }
47061da546Spatrick
48dda28197Spatrick SBDeclaration::~SBDeclaration() = default;
49061da546Spatrick
IsValid() const50061da546Spatrick bool SBDeclaration::IsValid() const {
51*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this);
52061da546Spatrick return this->operator bool();
53061da546Spatrick }
operator bool() const54061da546Spatrick SBDeclaration::operator bool() const {
55*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this);
56061da546Spatrick
57061da546Spatrick return m_opaque_up.get() && m_opaque_up->IsValid();
58061da546Spatrick }
59061da546Spatrick
GetFileSpec() const60061da546Spatrick SBFileSpec SBDeclaration::GetFileSpec() const {
61*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this);
62061da546Spatrick
63061da546Spatrick SBFileSpec sb_file_spec;
64061da546Spatrick if (m_opaque_up.get() && m_opaque_up->GetFile())
65061da546Spatrick sb_file_spec.SetFileSpec(m_opaque_up->GetFile());
66061da546Spatrick
67*f6aab3d8Srobert return sb_file_spec;
68061da546Spatrick }
69061da546Spatrick
GetLine() const70061da546Spatrick uint32_t SBDeclaration::GetLine() const {
71*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this);
72061da546Spatrick
73061da546Spatrick uint32_t line = 0;
74061da546Spatrick if (m_opaque_up)
75061da546Spatrick line = m_opaque_up->GetLine();
76061da546Spatrick
77061da546Spatrick
78061da546Spatrick return line;
79061da546Spatrick }
80061da546Spatrick
GetColumn() const81061da546Spatrick uint32_t SBDeclaration::GetColumn() const {
82*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this);
83061da546Spatrick
84061da546Spatrick if (m_opaque_up)
85061da546Spatrick return m_opaque_up->GetColumn();
86061da546Spatrick return 0;
87061da546Spatrick }
88061da546Spatrick
SetFileSpec(lldb::SBFileSpec filespec)89061da546Spatrick void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) {
90*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, filespec);
91061da546Spatrick
92061da546Spatrick if (filespec.IsValid())
93061da546Spatrick ref().SetFile(filespec.ref());
94061da546Spatrick else
95061da546Spatrick ref().SetFile(FileSpec());
96061da546Spatrick }
SetLine(uint32_t line)97061da546Spatrick void SBDeclaration::SetLine(uint32_t line) {
98*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, line);
99061da546Spatrick
100061da546Spatrick ref().SetLine(line);
101061da546Spatrick }
102061da546Spatrick
SetColumn(uint32_t column)103061da546Spatrick void SBDeclaration::SetColumn(uint32_t column) {
104*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, column);
105061da546Spatrick
106061da546Spatrick ref().SetColumn(column);
107061da546Spatrick }
108061da546Spatrick
operator ==(const SBDeclaration & rhs) const109061da546Spatrick bool SBDeclaration::operator==(const SBDeclaration &rhs) const {
110*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, rhs);
111061da546Spatrick
112061da546Spatrick lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
113061da546Spatrick lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
114061da546Spatrick
115061da546Spatrick if (lhs_ptr && rhs_ptr)
116061da546Spatrick return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0;
117061da546Spatrick
118061da546Spatrick return lhs_ptr == rhs_ptr;
119061da546Spatrick }
120061da546Spatrick
operator !=(const SBDeclaration & rhs) const121061da546Spatrick bool SBDeclaration::operator!=(const SBDeclaration &rhs) const {
122*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, rhs);
123061da546Spatrick
124061da546Spatrick lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
125061da546Spatrick lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
126061da546Spatrick
127061da546Spatrick if (lhs_ptr && rhs_ptr)
128061da546Spatrick return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0;
129061da546Spatrick
130061da546Spatrick return lhs_ptr != rhs_ptr;
131061da546Spatrick }
132061da546Spatrick
operator ->() const133061da546Spatrick const lldb_private::Declaration *SBDeclaration::operator->() const {
134061da546Spatrick return m_opaque_up.get();
135061da546Spatrick }
136061da546Spatrick
ref()137061da546Spatrick lldb_private::Declaration &SBDeclaration::ref() {
138061da546Spatrick if (m_opaque_up == nullptr)
139dda28197Spatrick m_opaque_up = std::make_unique<lldb_private::Declaration>();
140061da546Spatrick return *m_opaque_up;
141061da546Spatrick }
142061da546Spatrick
ref() const143061da546Spatrick const lldb_private::Declaration &SBDeclaration::ref() const {
144061da546Spatrick return *m_opaque_up;
145061da546Spatrick }
146061da546Spatrick
GetDescription(SBStream & description)147061da546Spatrick bool SBDeclaration::GetDescription(SBStream &description) {
148*f6aab3d8Srobert LLDB_INSTRUMENT_VA(this, description);
149061da546Spatrick
150061da546Spatrick Stream &strm = description.ref();
151061da546Spatrick
152061da546Spatrick if (m_opaque_up) {
153061da546Spatrick char file_path[PATH_MAX * 2];
154061da546Spatrick m_opaque_up->GetFile().GetPath(file_path, sizeof(file_path));
155061da546Spatrick strm.Printf("%s:%u", file_path, GetLine());
156061da546Spatrick if (GetColumn() > 0)
157061da546Spatrick strm.Printf(":%u", GetColumn());
158061da546Spatrick } else
159061da546Spatrick strm.PutCString("No value");
160061da546Spatrick
161061da546Spatrick return true;
162061da546Spatrick }
163061da546Spatrick
get()164061da546Spatrick lldb_private::Declaration *SBDeclaration::get() { return m_opaque_up.get(); }
165