Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame] | 1 | //===-- SBDeclaration.cpp ----------------------------------------*- C++-*-===// |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBDeclaration.h" |
| 10 | #include "lldb/API/SBStream.h" |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame] | 11 | #include "lldb/Host/PosixApi.h" |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 12 | #include "lldb/Symbol/Declaration.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 13 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/Stream.h" |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 15 | |
Greg Clayton | df3df25 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 16 | #include <limits.h> |
| 17 | |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | SBDeclaration::SBDeclaration() : m_opaque_ap() {} |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | SBDeclaration::SBDeclaration(const SBDeclaration &rhs) : m_opaque_ap() { |
| 24 | if (rhs.IsValid()) |
| 25 | ref() = rhs.ref(); |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr) |
| 29 | : m_opaque_ap() { |
| 30 | if (lldb_object_ptr) |
| 31 | ref() = *lldb_object_ptr; |
| 32 | } |
| 33 | |
| 34 | const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) { |
| 35 | if (this != &rhs) { |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 36 | if (rhs.IsValid()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | ref() = rhs.ref(); |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 38 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | m_opaque_ap.reset(); |
| 40 | } |
| 41 | return *this; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | void SBDeclaration::SetDeclaration( |
| 45 | const lldb_private::Declaration &lldb_object_ref) { |
| 46 | ref() = lldb_object_ref; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | SBDeclaration::~SBDeclaration() {} |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | bool SBDeclaration::IsValid() const { |
| 52 | return m_opaque_ap.get() && m_opaque_ap->IsValid(); |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | SBFileSpec SBDeclaration::GetFileSpec() const { |
| 56 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 57 | |
| 58 | SBFileSpec sb_file_spec; |
| 59 | if (m_opaque_ap.get() && m_opaque_ap->GetFile()) |
| 60 | sb_file_spec.SetFileSpec(m_opaque_ap->GetFile()); |
| 61 | |
| 62 | if (log) { |
| 63 | SBStream sstr; |
| 64 | sb_file_spec.GetDescription(sstr); |
| 65 | log->Printf("SBLineEntry(%p)::GetFileSpec () => SBFileSpec(%p): %s", |
| 66 | static_cast<void *>(m_opaque_ap.get()), |
| 67 | static_cast<const void *>(sb_file_spec.get()), sstr.GetData()); |
| 68 | } |
| 69 | |
| 70 | return sb_file_spec; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | uint32_t SBDeclaration::GetLine() const { |
| 74 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 75 | |
| 76 | uint32_t line = 0; |
Jonas Devlieghere | 3447077 | 2018-12-20 21:02:55 +0000 | [diff] [blame] | 77 | if (m_opaque_ap) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | line = m_opaque_ap->GetLine(); |
| 79 | |
| 80 | if (log) |
| 81 | log->Printf("SBLineEntry(%p)::GetLine () => %u", |
| 82 | static_cast<void *>(m_opaque_ap.get()), line); |
| 83 | |
| 84 | return line; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | uint32_t SBDeclaration::GetColumn() const { |
Jonas Devlieghere | 3447077 | 2018-12-20 21:02:55 +0000 | [diff] [blame] | 88 | if (m_opaque_ap) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | return m_opaque_ap->GetColumn(); |
| 90 | return 0; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) { |
| 94 | if (filespec.IsValid()) |
| 95 | ref().SetFile(filespec.ref()); |
| 96 | else |
| 97 | ref().SetFile(FileSpec()); |
| 98 | } |
| 99 | void SBDeclaration::SetLine(uint32_t line) { ref().SetLine(line); } |
| 100 | |
| 101 | void SBDeclaration::SetColumn(uint32_t column) { ref().SetColumn(column); } |
| 102 | |
| 103 | bool SBDeclaration::operator==(const SBDeclaration &rhs) const { |
| 104 | lldb_private::Declaration *lhs_ptr = m_opaque_ap.get(); |
| 105 | lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get(); |
| 106 | |
| 107 | if (lhs_ptr && rhs_ptr) |
| 108 | return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0; |
| 109 | |
| 110 | return lhs_ptr == rhs_ptr; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | bool SBDeclaration::operator!=(const SBDeclaration &rhs) const { |
| 114 | lldb_private::Declaration *lhs_ptr = m_opaque_ap.get(); |
| 115 | lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get(); |
| 116 | |
| 117 | if (lhs_ptr && rhs_ptr) |
| 118 | return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0; |
| 119 | |
| 120 | return lhs_ptr != rhs_ptr; |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | const lldb_private::Declaration *SBDeclaration::operator->() const { |
| 124 | return m_opaque_ap.get(); |
Enrico Granata | 10de090 | 2012-10-10 22:54:17 +0000 | [diff] [blame] | 125 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | |
| 127 | lldb_private::Declaration &SBDeclaration::ref() { |
Jonas Devlieghere | 3447077 | 2018-12-20 21:02:55 +0000 | [diff] [blame] | 128 | if (m_opaque_ap == NULL) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | m_opaque_ap.reset(new lldb_private::Declaration()); |
| 130 | return *m_opaque_ap; |
| 131 | } |
| 132 | |
| 133 | const lldb_private::Declaration &SBDeclaration::ref() const { |
| 134 | return *m_opaque_ap; |
| 135 | } |
| 136 | |
| 137 | bool SBDeclaration::GetDescription(SBStream &description) { |
| 138 | Stream &strm = description.ref(); |
| 139 | |
Jonas Devlieghere | 3447077 | 2018-12-20 21:02:55 +0000 | [diff] [blame] | 140 | if (m_opaque_ap) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | char file_path[PATH_MAX * 2]; |
| 142 | m_opaque_ap->GetFile().GetPath(file_path, sizeof(file_path)); |
| 143 | strm.Printf("%s:%u", file_path, GetLine()); |
| 144 | if (GetColumn() > 0) |
| 145 | strm.Printf(":%u", GetColumn()); |
| 146 | } else |
| 147 | strm.PutCString("No value"); |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | lldb_private::Declaration *SBDeclaration::get() { return m_opaque_ap.get(); } |