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 "Utils.h" 11 #include "lldb/API/SBStream.h" 12 #include "lldb/Core/Declaration.h" 13 #include "lldb/Host/PosixApi.h" 14 #include "lldb/Utility/Instrumentation.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() { LLDB_INSTRUMENT_VA(this); } 23 24 SBDeclaration::SBDeclaration(const SBDeclaration &rhs) { 25 LLDB_INSTRUMENT_VA(this, rhs); 26 27 m_opaque_up = clone(rhs.m_opaque_up); 28 } 29 30 SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr) { 31 if (lldb_object_ptr) 32 m_opaque_up = std::make_unique<Declaration>(*lldb_object_ptr); 33 } 34 35 const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) { 36 LLDB_INSTRUMENT_VA(this, rhs); 37 38 if (this != &rhs) 39 m_opaque_up = clone(rhs.m_opaque_up); 40 return *this; 41 } 42 43 void SBDeclaration::SetDeclaration( 44 const lldb_private::Declaration &lldb_object_ref) { 45 ref() = lldb_object_ref; 46 } 47 48 SBDeclaration::~SBDeclaration() = default; 49 50 bool SBDeclaration::IsValid() const { 51 LLDB_INSTRUMENT_VA(this); 52 return this->operator bool(); 53 } 54 SBDeclaration::operator bool() const { 55 LLDB_INSTRUMENT_VA(this); 56 57 return m_opaque_up.get() && m_opaque_up->IsValid(); 58 } 59 60 SBFileSpec SBDeclaration::GetFileSpec() const { 61 LLDB_INSTRUMENT_VA(this); 62 63 SBFileSpec sb_file_spec; 64 if (m_opaque_up.get() && m_opaque_up->GetFile()) 65 sb_file_spec.SetFileSpec(m_opaque_up->GetFile()); 66 67 return sb_file_spec; 68 } 69 70 uint32_t SBDeclaration::GetLine() const { 71 LLDB_INSTRUMENT_VA(this); 72 73 uint32_t line = 0; 74 if (m_opaque_up) 75 line = m_opaque_up->GetLine(); 76 77 78 return line; 79 } 80 81 uint32_t SBDeclaration::GetColumn() const { 82 LLDB_INSTRUMENT_VA(this); 83 84 if (m_opaque_up) 85 return m_opaque_up->GetColumn(); 86 return 0; 87 } 88 89 void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) { 90 LLDB_INSTRUMENT_VA(this, filespec); 91 92 if (filespec.IsValid()) 93 ref().SetFile(filespec.ref()); 94 else 95 ref().SetFile(FileSpec()); 96 } 97 void SBDeclaration::SetLine(uint32_t line) { 98 LLDB_INSTRUMENT_VA(this, line); 99 100 ref().SetLine(line); 101 } 102 103 void SBDeclaration::SetColumn(uint32_t column) { 104 LLDB_INSTRUMENT_VA(this, column); 105 106 ref().SetColumn(column); 107 } 108 109 bool SBDeclaration::operator==(const SBDeclaration &rhs) const { 110 LLDB_INSTRUMENT_VA(this, rhs); 111 112 lldb_private::Declaration *lhs_ptr = m_opaque_up.get(); 113 lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get(); 114 115 if (lhs_ptr && rhs_ptr) 116 return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0; 117 118 return lhs_ptr == rhs_ptr; 119 } 120 121 bool SBDeclaration::operator!=(const SBDeclaration &rhs) const { 122 LLDB_INSTRUMENT_VA(this, rhs); 123 124 lldb_private::Declaration *lhs_ptr = m_opaque_up.get(); 125 lldb_private::Declaration *rhs_ptr = rhs.m_opaque_up.get(); 126 127 if (lhs_ptr && rhs_ptr) 128 return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0; 129 130 return lhs_ptr != rhs_ptr; 131 } 132 133 const lldb_private::Declaration *SBDeclaration::operator->() const { 134 return m_opaque_up.get(); 135 } 136 137 lldb_private::Declaration &SBDeclaration::ref() { 138 if (m_opaque_up == nullptr) 139 m_opaque_up = std::make_unique<lldb_private::Declaration>(); 140 return *m_opaque_up; 141 } 142 143 const lldb_private::Declaration &SBDeclaration::ref() const { 144 return *m_opaque_up; 145 } 146 147 bool SBDeclaration::GetDescription(SBStream &description) { 148 LLDB_INSTRUMENT_VA(this, description); 149 150 Stream &strm = description.ref(); 151 152 if (m_opaque_up) { 153 char file_path[PATH_MAX * 2]; 154 m_opaque_up->GetFile().GetPath(file_path, sizeof(file_path)); 155 strm.Printf("%s:%u", file_path, GetLine()); 156 if (GetColumn() > 0) 157 strm.Printf(":%u", GetColumn()); 158 } else 159 strm.PutCString("No value"); 160 161 return true; 162 } 163 164 lldb_private::Declaration *SBDeclaration::get() { return m_opaque_up.get(); } 165