|
| 1 | +//===---------------------------------------------------------------------===// |
| 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 "LibStdcpp.h" |
| 10 | + |
| 11 | +#include "lldb/DataFormatters/FormattersHelpers.h" |
| 12 | +#include "lldb/Utility/ConstString.h" |
| 13 | +#include "lldb/ValueObject/ValueObject.h" |
| 14 | +#include "llvm/ADT/APSInt.h" |
| 15 | +#include "llvm/Support/Error.h" |
| 16 | +#include <cstddef> |
| 17 | +#include <optional> |
| 18 | + |
| 19 | +using namespace lldb; |
| 20 | + |
| 21 | +namespace lldb_private::formatters { |
| 22 | + |
| 23 | +class LibStdcppSpanSyntheticFrontEnd : public SyntheticChildrenFrontEnd { |
| 24 | +public: |
| 25 | + LibStdcppSpanSyntheticFrontEnd(const lldb::ValueObjectSP &valobj_sp) |
| 26 | + : SyntheticChildrenFrontEnd(*valobj_sp) { |
| 27 | + if (valobj_sp) |
| 28 | + Update(); |
| 29 | + } |
| 30 | + |
| 31 | + ~LibStdcppSpanSyntheticFrontEnd() override = default; |
| 32 | + |
| 33 | + llvm::Expected<uint32_t> CalculateNumChildren() override { |
| 34 | + return m_num_elements; |
| 35 | + } |
| 36 | + |
| 37 | + lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override { |
| 38 | + if (!m_start) |
| 39 | + return {}; |
| 40 | + |
| 41 | + uint64_t offset = (static_cast<uint64_t>(idx) * m_element_size); |
| 42 | + offset += m_start->GetValueAsUnsigned(0); |
| 43 | + const std::string name = llvm::formatv("[{0}]", idx); |
| 44 | + return CreateValueObjectFromAddress( |
| 45 | + name, offset, m_backend.GetExecutionContextRef(), m_element_type); |
| 46 | + } |
| 47 | + |
| 48 | + lldb::ChildCacheState Update() override { |
| 49 | + const ValueObjectSP data_ptr = m_backend.GetChildMemberWithName("_M_ptr"); |
| 50 | + if (!data_ptr) |
| 51 | + return lldb::ChildCacheState::eRefetch; |
| 52 | + |
| 53 | + m_element_type = data_ptr->GetCompilerType().GetPointeeType(); |
| 54 | + |
| 55 | + // Get element size. |
| 56 | + llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr); |
| 57 | + if (!size_or_err) { |
| 58 | + LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(), |
| 59 | + "{0}"); |
| 60 | + return lldb::ChildCacheState::eReuse; |
| 61 | + } |
| 62 | + |
| 63 | + m_element_size = *size_or_err; |
| 64 | + if (m_element_size > 0) { |
| 65 | + m_start = data_ptr.get(); |
| 66 | + } |
| 67 | + |
| 68 | + // Get number of elements. |
| 69 | + if (const ValueObjectSP size_sp = |
| 70 | + m_backend.GetChildAtNamePath({"_M_extent", "_M_extent_value"})) { |
| 71 | + m_num_elements = size_sp->GetValueAsUnsigned(0); |
| 72 | + } else if (const auto arg = |
| 73 | + m_backend.GetCompilerType().GetIntegralTemplateArgument(1)) { |
| 74 | + |
| 75 | + m_num_elements = arg->value.GetAPSInt().getLimitedValue(); |
| 76 | + } |
| 77 | + |
| 78 | + return lldb::ChildCacheState::eReuse; |
| 79 | + } |
| 80 | + |
| 81 | + llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override { |
| 82 | + if (!m_start) |
| 83 | + return llvm::createStringError( |
| 84 | + llvm::formatv("Type has no child named {0}", name.GetStringRef())); |
| 85 | + |
| 86 | + auto optional_idx = formatters::ExtractIndexFromString(name.GetCString()); |
| 87 | + if (!optional_idx) { |
| 88 | + return llvm::createStringError( |
| 89 | + llvm::formatv("Type has no child named {0}", name.GetStringRef())); |
| 90 | + } |
| 91 | + return *optional_idx; |
| 92 | + } |
| 93 | + |
| 94 | +private: |
| 95 | + ValueObject *m_start = nullptr; ///< First element of span. Held, not owned. |
| 96 | + CompilerType m_element_type; ///< Type of span elements. |
| 97 | + size_t m_num_elements = 0; ///< Number of elements in span. |
| 98 | + uint32_t m_element_size = 0; ///< Size in bytes of each span element. |
| 99 | +}; |
| 100 | + |
| 101 | +SyntheticChildrenFrontEnd * |
| 102 | +LibStdcppSpanSyntheticFrontEndCreator(CXXSyntheticChildren * /*unused*/, |
| 103 | + lldb::ValueObjectSP valobj_sp) { |
| 104 | + if (!valobj_sp) |
| 105 | + return nullptr; |
| 106 | + const CompilerType type = valobj_sp->GetCompilerType(); |
| 107 | + if (!type || type.GetNumTemplateArguments() != 2) |
| 108 | + return nullptr; |
| 109 | + return new LibStdcppSpanSyntheticFrontEnd(valobj_sp); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace lldb_private::formatters |
0 commit comments