Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp
39644 views
//===-- LibCxxSpan.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "LibCxx.h"910#include "lldb/Core/ValueObject.h"11#include "lldb/DataFormatters/FormattersHelpers.h"12#include "lldb/Utility/ConstString.h"13#include "llvm/ADT/APSInt.h"14#include <optional>1516using namespace lldb;17using namespace lldb_private;18using namespace lldb_private::formatters;1920namespace lldb_private {21namespace formatters {2223class LibcxxStdSpanSyntheticFrontEnd : public SyntheticChildrenFrontEnd {24public:25LibcxxStdSpanSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);2627~LibcxxStdSpanSyntheticFrontEnd() override = default;2829llvm::Expected<uint32_t> CalculateNumChildren() override;3031lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;3233/// Determines properties of the std::span<> associated with this object34//35// std::span can either be instantiated with a compile-time known36// extent or a std::dynamic_extent (this is the default if only the37// type template argument is provided). The layout of std::span38// depends on whether the extent is dynamic or not. For static39// extents (e.g., std::span<int, 9>):40//41// (std::__1::span<const int, 9>) s = {42// __data = 0x000000016fdff49443// }44//45// For dynamic extents, e.g., std::span<int>, the layout is:46//47// (std::__1::span<const int, 18446744073709551615>) s = {48// __data = 0x000000016fdff49449// __size = 650// }51//52// This function checks for a '__size' member to determine the number53// of elements in the span. If no such member exists, we get the size54// from the only other place it can be: the template argument.55lldb::ChildCacheState Update() override;5657bool MightHaveChildren() override;5859size_t GetIndexOfChildWithName(ConstString name) override;6061private:62ValueObject *m_start = nullptr; ///< First element of span. Held, not owned.63CompilerType m_element_type{}; ///< Type of span elements.64size_t m_num_elements = 0; ///< Number of elements in span.65uint32_t m_element_size = 0; ///< Size in bytes of each span element.66};6768lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::69LibcxxStdSpanSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)70: SyntheticChildrenFrontEnd(*valobj_sp) {71if (valobj_sp)72Update();73}7475llvm::Expected<uint32_t> lldb_private::formatters::76LibcxxStdSpanSyntheticFrontEnd::CalculateNumChildren() {77return m_num_elements;78}7980lldb::ValueObjectSP81lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::GetChildAtIndex(82uint32_t idx) {83if (!m_start)84return {};8586uint64_t offset = idx * m_element_size;87offset = offset + m_start->GetValueAsUnsigned(0);88StreamString name;89name.Printf("[%" PRIu64 "]", (uint64_t)idx);90return CreateValueObjectFromAddress(name.GetString(), offset,91m_backend.GetExecutionContextRef(),92m_element_type);93}9495lldb::ChildCacheState96lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::Update() {97// Get element type.98ValueObjectSP data_type_finder_sp = GetChildMemberWithName(99m_backend, {ConstString("__data_"), ConstString("__data")});100if (!data_type_finder_sp)101return lldb::ChildCacheState::eRefetch;102103m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType();104105// Get element size.106if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {107m_element_size = *size;108109// Get data.110if (m_element_size > 0) {111m_start = data_type_finder_sp.get();112}113114// Get number of elements.115if (auto size_sp = GetChildMemberWithName(116m_backend, {ConstString("__size_"), ConstString("__size")})) {117m_num_elements = size_sp->GetValueAsUnsigned(0);118} else if (auto arg =119m_backend.GetCompilerType().GetIntegralTemplateArgument(1)) {120121m_num_elements = arg->value.getLimitedValue();122}123}124125return lldb::ChildCacheState::eReuse;126}127128bool lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::129MightHaveChildren() {130return true;131}132133size_t lldb_private::formatters::LibcxxStdSpanSyntheticFrontEnd::134GetIndexOfChildWithName(ConstString name) {135if (!m_start)136return UINT32_MAX;137return ExtractIndexFromString(name.GetCString());138}139140lldb_private::SyntheticChildrenFrontEnd *141LibcxxStdSpanSyntheticFrontEndCreator(CXXSyntheticChildren *,142lldb::ValueObjectSP valobj_sp) {143if (!valobj_sp)144return nullptr;145CompilerType type = valobj_sp->GetCompilerType();146if (!type.IsValid() || type.GetNumTemplateArguments() != 2)147return nullptr;148return new LibcxxStdSpanSyntheticFrontEnd(valobj_sp);149}150151} // namespace formatters152} // namespace lldb_private153154155