xref: /src/contrib/llvm-project/lldb/source/API/SBDeclaration.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
1cfca06d7SDimitry Andric //===-- SBDeclaration.cpp -------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/API/SBDeclaration.h"
105f29bb8aSDimitry Andric #include "Utils.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
12344a3780SDimitry Andric #include "lldb/Core/Declaration.h"
1374a628f7SDimitry Andric #include "lldb/Host/PosixApi.h"
146f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
1574a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
16f034231aSEd Maste 
17344a3780SDimitry Andric #include <climits>
18f034231aSEd Maste 
19f034231aSEd Maste using namespace lldb;
20f034231aSEd Maste using namespace lldb_private;
21f034231aSEd Maste 
SBDeclaration()226f8fc217SDimitry Andric SBDeclaration::SBDeclaration() { LLDB_INSTRUMENT_VA(this); }
23f034231aSEd Maste 
SBDeclaration(const SBDeclaration & rhs)246f8fc217SDimitry Andric SBDeclaration::SBDeclaration(const SBDeclaration &rhs) {
256f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
265f29bb8aSDimitry Andric 
275f29bb8aSDimitry Andric   m_opaque_up = clone(rhs.m_opaque_up);
28f034231aSEd Maste }
29f034231aSEd Maste 
SBDeclaration(const lldb_private::Declaration * lldb_object_ptr)306f8fc217SDimitry Andric SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr) {
31f034231aSEd Maste   if (lldb_object_ptr)
32ead24645SDimitry Andric     m_opaque_up = std::make_unique<Declaration>(*lldb_object_ptr);
33f034231aSEd Maste }
34f034231aSEd Maste 
operator =(const SBDeclaration & rhs)3514f1b3e8SDimitry Andric const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) {
366f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
375f29bb8aSDimitry Andric 
385f29bb8aSDimitry Andric   if (this != &rhs)
395f29bb8aSDimitry Andric     m_opaque_up = clone(rhs.m_opaque_up);
406f8fc217SDimitry Andric   return *this;
41f034231aSEd Maste }
42f034231aSEd Maste 
SetDeclaration(const lldb_private::Declaration & lldb_object_ref)4314f1b3e8SDimitry Andric void SBDeclaration::SetDeclaration(
4414f1b3e8SDimitry Andric     const lldb_private::Declaration &lldb_object_ref) {
45f034231aSEd Maste   ref() = lldb_object_ref;
46f034231aSEd Maste }
47f034231aSEd Maste 
48cfca06d7SDimitry Andric SBDeclaration::~SBDeclaration() = default;
49f034231aSEd Maste 
IsValid() const5014f1b3e8SDimitry Andric bool SBDeclaration::IsValid() const {
516f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
525f29bb8aSDimitry Andric   return this->operator bool();
535f29bb8aSDimitry Andric }
operator bool() const545f29bb8aSDimitry Andric SBDeclaration::operator bool() const {
556f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
565f29bb8aSDimitry Andric 
575f29bb8aSDimitry Andric   return m_opaque_up.get() && m_opaque_up->IsValid();
58f034231aSEd Maste }
59f034231aSEd Maste 
GetFileSpec() const6014f1b3e8SDimitry Andric SBFileSpec SBDeclaration::GetFileSpec() const {
616f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
62f034231aSEd Maste 
63f034231aSEd Maste   SBFileSpec sb_file_spec;
645f29bb8aSDimitry Andric   if (m_opaque_up.get() && m_opaque_up->GetFile())
655f29bb8aSDimitry Andric     sb_file_spec.SetFileSpec(m_opaque_up->GetFile());
66f034231aSEd Maste 
676f8fc217SDimitry Andric   return sb_file_spec;
68f034231aSEd Maste }
69f034231aSEd Maste 
GetLine() const7014f1b3e8SDimitry Andric uint32_t SBDeclaration::GetLine() const {
716f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
72f034231aSEd Maste 
73f034231aSEd Maste   uint32_t line = 0;
745f29bb8aSDimitry Andric   if (m_opaque_up)
755f29bb8aSDimitry Andric     line = m_opaque_up->GetLine();
76f034231aSEd Maste 
77f034231aSEd Maste 
78f034231aSEd Maste   return line;
79f034231aSEd Maste }
80f034231aSEd Maste 
GetColumn() const8114f1b3e8SDimitry Andric uint32_t SBDeclaration::GetColumn() const {
826f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
835f29bb8aSDimitry Andric 
845f29bb8aSDimitry Andric   if (m_opaque_up)
855f29bb8aSDimitry Andric     return m_opaque_up->GetColumn();
86f034231aSEd Maste   return 0;
87f034231aSEd Maste }
88f034231aSEd Maste 
SetFileSpec(lldb::SBFileSpec filespec)8914f1b3e8SDimitry Andric void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) {
906f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, filespec);
915f29bb8aSDimitry Andric 
92f034231aSEd Maste   if (filespec.IsValid())
93f034231aSEd Maste     ref().SetFile(filespec.ref());
94f034231aSEd Maste   else
95f034231aSEd Maste     ref().SetFile(FileSpec());
96f034231aSEd Maste }
SetLine(uint32_t line)975f29bb8aSDimitry Andric void SBDeclaration::SetLine(uint32_t line) {
986f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, line);
99f034231aSEd Maste 
1005f29bb8aSDimitry Andric   ref().SetLine(line);
1015f29bb8aSDimitry Andric }
1025f29bb8aSDimitry Andric 
SetColumn(uint32_t column)1035f29bb8aSDimitry Andric void SBDeclaration::SetColumn(uint32_t column) {
1046f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, column);
1055f29bb8aSDimitry Andric 
1065f29bb8aSDimitry Andric   ref().SetColumn(column);
1075f29bb8aSDimitry Andric }
108f034231aSEd Maste 
operator ==(const SBDeclaration & rhs) const10914f1b3e8SDimitry Andric bool SBDeclaration::operator==(const SBDeclaration &rhs) const {
1106f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
1115f29bb8aSDimitry Andric 
1125f29bb8aSDimitry Andric   lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
1135f29bb8aSDimitry Andric   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
114f034231aSEd Maste 
115f034231aSEd Maste   if (lhs_ptr && rhs_ptr)
116f034231aSEd Maste     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0;
117f034231aSEd Maste 
118f034231aSEd Maste   return lhs_ptr == rhs_ptr;
119f034231aSEd Maste }
120f034231aSEd Maste 
operator !=(const SBDeclaration & rhs) const12114f1b3e8SDimitry Andric bool SBDeclaration::operator!=(const SBDeclaration &rhs) const {
1226f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
1235f29bb8aSDimitry Andric 
1245f29bb8aSDimitry Andric   lldb_private::Declaration *lhs_ptr = m_opaque_up.get();
1255f29bb8aSDimitry Andric   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get();
126f034231aSEd Maste 
127f034231aSEd Maste   if (lhs_ptr && rhs_ptr)
128f034231aSEd Maste     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0;
129f034231aSEd Maste 
130f034231aSEd Maste   return lhs_ptr != rhs_ptr;
131f034231aSEd Maste }
132f034231aSEd Maste 
operator ->() const13314f1b3e8SDimitry Andric const lldb_private::Declaration *SBDeclaration::operator->() const {
1345f29bb8aSDimitry Andric   return m_opaque_up.get();
135f034231aSEd Maste }
136f034231aSEd Maste 
ref()13714f1b3e8SDimitry Andric lldb_private::Declaration &SBDeclaration::ref() {
1385f29bb8aSDimitry Andric   if (m_opaque_up == nullptr)
139cfca06d7SDimitry Andric     m_opaque_up = std::make_unique<lldb_private::Declaration>();
1405f29bb8aSDimitry Andric   return *m_opaque_up;
141f034231aSEd Maste }
142f034231aSEd Maste 
ref() const14314f1b3e8SDimitry Andric const lldb_private::Declaration &SBDeclaration::ref() const {
1445f29bb8aSDimitry Andric   return *m_opaque_up;
145f034231aSEd Maste }
146f034231aSEd Maste 
GetDescription(SBStream & description)14714f1b3e8SDimitry Andric bool SBDeclaration::GetDescription(SBStream &description) {
1486f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, description);
1495f29bb8aSDimitry Andric 
150f034231aSEd Maste   Stream &strm = description.ref();
151f034231aSEd Maste 
1525f29bb8aSDimitry Andric   if (m_opaque_up) {
153f034231aSEd Maste     char file_path[PATH_MAX * 2];
1545f29bb8aSDimitry Andric     m_opaque_up->GetFile().GetPath(file_path, sizeof(file_path));
155f034231aSEd Maste     strm.Printf("%s:%u", file_path, GetLine());
156f034231aSEd Maste     if (GetColumn() > 0)
157f034231aSEd Maste       strm.Printf(":%u", GetColumn());
15814f1b3e8SDimitry Andric   } else
159f034231aSEd Maste     strm.PutCString("No value");
160f034231aSEd Maste 
161f034231aSEd Maste   return true;
162f034231aSEd Maste }
163f034231aSEd Maste 
get()1645f29bb8aSDimitry Andric lldb_private::Declaration *SBDeclaration::get() { return m_opaque_up.get(); }
165