1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
//===-- LibCxxSpan.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "LibCxx.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Utility/ConstString.h"
#include "llvm/ADT/APSInt.h"
#include <optional>
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;
namespace lldb_private {
namespace formatters {
class LibcxxStdSpanSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
LibcxxStdSpanSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
~LibcxxStdSpanSyntheticFrontEnd() override = default;
size_t CalculateNumChildren() override;
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
/// Determines properties of the std::span<> associated with this object
//
// std::span can either be instantiated with a compile-time known
// extent or a std::dynamic_extent (this is the default if only the
// type template argument is provided). The layout of std::span
// depends on whether the extent is dynamic or not. For static
// extents (e.g., std::span<int, 9>):
//
// (std::__1::span<const int, 9>) s = {
// __data = 0x000000016fdff494
// }
//
// For dynamic extents, e.g., std::span<int>, the layout is:
//
// (std::__1::span<const int, 18446744073709551615>) s = {
// __data = 0x000000016fdff494
// __size = 6
// }
//
// This function checks for a '__size' member to determine the number
// of elements in the span. If no such member exists, we get the size
// from the only other place it can be: the template argument.
bool Update() override;
bool MightHaveChildren() override;
size_t GetIndexOfChildWithName(ConstString name) override;
private:
ValueObject *m_start = nullptr; ///< First element of span. Held, not owned.
CompilerType m_element_type{}; ///< Type of span elements.
size_t m_num_elements = 0; ///< Number of elements in span.
uint32_t m_element_size = 0; ///< Size in bytes of each span element.
};
lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::
LibcxxStdSpanSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
: SyntheticChildrenFrontEnd(*valobj_sp) {
if (valobj_sp)
Update();
}
size_t lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::
CalculateNumChildren() {
return m_num_elements;
}
lldb::ValueObjectSP
lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::GetChildAtIndex(
size_t idx) {
if (!m_start)
return {};
uint64_t offset = idx * m_element_size;
offset = offset + m_start->GetValueAsUnsigned(0);
StreamString name;
name.Printf("[%" PRIu64 "]", (uint64_t)idx);
return CreateValueObjectFromAddress(name.GetString(), offset,
m_backend.GetExecutionContextRef(),
m_element_type);
}
bool lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::Update() {
// Get element type.
ValueObjectSP data_type_finder_sp = GetChildMemberWithName(
m_backend, {ConstString("__data_"), ConstString("__data")});
if (!data_type_finder_sp)
return false;
m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType();
// Get element size.
if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
m_element_size = *size;
// Get data.
if (m_element_size > 0) {
m_start = data_type_finder_sp.get();
}
// Get number of elements.
if (auto size_sp = GetChildMemberWithName(
m_backend, {ConstString("__size_"), ConstString("__size")})) {
m_num_elements = size_sp->GetValueAsUnsigned(0);
} else if (auto arg =
m_backend.GetCompilerType().GetIntegralTemplateArgument(1)) {
m_num_elements = arg->value.getLimitedValue();
}
}
return true;
}
bool lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::
MightHaveChildren() {
return true;
}
size_t lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::
GetIndexOfChildWithName(ConstString name) {
if (!m_start)
return UINT32_MAX;
return ExtractIndexFromString(name.GetCString());
}
lldb_private::SyntheticChildrenFrontEnd *
LibcxxStdSpanSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP valobj_sp) {
if (!valobj_sp)
return nullptr;
CompilerType type = valobj_sp->GetCompilerType();
if (!type.IsValid() || type.GetNumTemplateArguments() != 2)
return nullptr;
return new LibcxxStdSpanSyntheticFrontEnd(valobj_sp);
}
} // namespace formatters
} // namespace lldb_private
|