1 //===-- SBValueList.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/SBValueList.h" 10 #include "lldb/API/SBError.h" 11 #include "lldb/API/SBStream.h" 12 #include "lldb/API/SBValue.h" 13 #include "lldb/Utility/Instrumentation.h" 14 #include "lldb/Utility/Status.h" 15 #include "lldb/ValueObject/ValueObjectList.h" 16 #include <vector> 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 class ValueListImpl { 22 public: 23 ValueListImpl() = default; 24 25 ValueListImpl(const ValueListImpl &rhs) 26 : m_values(rhs.m_values), m_error(rhs.m_error.Clone()) {} 27 28 ValueListImpl &operator=(const ValueListImpl &rhs) { 29 if (this == &rhs) 30 return *this; 31 m_values = rhs.m_values; 32 m_error = rhs.m_error.Clone(); 33 return *this; 34 } 35 36 uint32_t GetSize() { return m_values.size(); } 37 38 void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); } 39 40 void Append(const ValueListImpl &list) { 41 for (auto val : list.m_values) 42 Append(val); 43 } 44 45 lldb::SBValue GetValueAtIndex(uint32_t index) { 46 if (index >= GetSize()) 47 return lldb::SBValue(); 48 return m_values[index]; 49 } 50 51 lldb::SBValue FindValueByUID(lldb::user_id_t uid) { 52 for (auto val : m_values) { 53 if (val.IsValid() && val.GetID() == uid) 54 return val; 55 } 56 return lldb::SBValue(); 57 } 58 59 lldb::SBValue GetFirstValueByName(const char *name) const { 60 if (name) { 61 for (auto val : m_values) { 62 if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0) 63 return val; 64 } 65 } 66 return lldb::SBValue(); 67 } 68 69 const Status &GetError() const { return m_error; } 70 71 void SetError(Status &&error) { m_error = std::move(error); } 72 73 private: 74 std::vector<lldb::SBValue> m_values; 75 Status m_error; 76 }; 77 78 SBValueList::SBValueList() { LLDB_INSTRUMENT_VA(this); } 79 80 SBValueList::SBValueList(const SBValueList &rhs) { 81 LLDB_INSTRUMENT_VA(this, rhs); 82 83 if (rhs.IsValid()) 84 m_opaque_up = std::make_unique<ValueListImpl>(*rhs); 85 } 86 87 SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) { 88 if (lldb_object_ptr) 89 m_opaque_up = std::make_unique<ValueListImpl>(*lldb_object_ptr); 90 } 91 92 SBValueList::~SBValueList() = default; 93 94 bool SBValueList::IsValid() const { 95 LLDB_INSTRUMENT_VA(this); 96 return this->operator bool(); 97 } 98 SBValueList::operator bool() const { 99 LLDB_INSTRUMENT_VA(this); 100 101 return (m_opaque_up != nullptr); 102 } 103 104 void SBValueList::Clear() { 105 LLDB_INSTRUMENT_VA(this); 106 107 m_opaque_up.reset(); 108 } 109 110 const SBValueList &SBValueList::operator=(const SBValueList &rhs) { 111 LLDB_INSTRUMENT_VA(this, rhs); 112 113 if (this != &rhs) { 114 if (rhs.IsValid()) 115 m_opaque_up = std::make_unique<ValueListImpl>(*rhs); 116 else 117 m_opaque_up.reset(); 118 } 119 return *this; 120 } 121 122 ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); } 123 124 ValueListImpl &SBValueList::operator*() { return *m_opaque_up; } 125 126 const ValueListImpl *SBValueList::operator->() const { 127 return m_opaque_up.get(); 128 } 129 130 const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; } 131 132 void SBValueList::Append(const SBValue &val_obj) { 133 LLDB_INSTRUMENT_VA(this, val_obj); 134 135 CreateIfNeeded(); 136 m_opaque_up->Append(val_obj); 137 } 138 139 void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) { 140 if (val_obj_sp) { 141 CreateIfNeeded(); 142 m_opaque_up->Append(SBValue(val_obj_sp)); 143 } 144 } 145 146 void SBValueList::Append(const lldb::SBValueList &value_list) { 147 LLDB_INSTRUMENT_VA(this, value_list); 148 149 if (value_list.IsValid()) { 150 CreateIfNeeded(); 151 m_opaque_up->Append(*value_list); 152 } 153 } 154 155 SBValue SBValueList::GetValueAtIndex(uint32_t idx) const { 156 LLDB_INSTRUMENT_VA(this, idx); 157 158 SBValue sb_value; 159 if (m_opaque_up) 160 sb_value = m_opaque_up->GetValueAtIndex(idx); 161 162 return sb_value; 163 } 164 165 uint32_t SBValueList::GetSize() const { 166 LLDB_INSTRUMENT_VA(this); 167 168 uint32_t size = 0; 169 if (m_opaque_up) 170 size = m_opaque_up->GetSize(); 171 172 return size; 173 } 174 175 void SBValueList::CreateIfNeeded() { 176 if (m_opaque_up == nullptr) 177 m_opaque_up = std::make_unique<ValueListImpl>(); 178 } 179 180 SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) { 181 LLDB_INSTRUMENT_VA(this, uid); 182 183 SBValue sb_value; 184 if (m_opaque_up) 185 sb_value = m_opaque_up->FindValueByUID(uid); 186 return sb_value; 187 } 188 189 SBValue SBValueList::GetFirstValueByName(const char *name) const { 190 LLDB_INSTRUMENT_VA(this, name); 191 192 SBValue sb_value; 193 if (m_opaque_up) 194 sb_value = m_opaque_up->GetFirstValueByName(name); 195 return sb_value; 196 } 197 198 void *SBValueList::opaque_ptr() { return m_opaque_up.get(); } 199 200 ValueListImpl &SBValueList::ref() { 201 CreateIfNeeded(); 202 return *m_opaque_up; 203 } 204 205 lldb::SBError SBValueList::GetError() { 206 LLDB_INSTRUMENT_VA(this); 207 SBError sb_error; 208 if (m_opaque_up) 209 sb_error.SetError(m_opaque_up->GetError().Clone()); 210 return sb_error; 211 } 212 213 void SBValueList::SetError(lldb_private::Status &&status) { 214 ref().SetError(std::move(status)); 215 } 216