LLDB mainline
LibStdcppSpan.cpp
Go to the documentation of this file.
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
14#include "llvm/ADT/APSInt.h"
15#include "llvm/Support/Error.h"
16#include <cstddef>
17#include <optional>
18
19using namespace lldb;
20
22
24public:
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);
45 name, offset, m_backend.GetExecutionContextRef(), m_element_type);
46 }
47
49 const ValueObjectSP data_ptr = m_backend.GetChildMemberWithName("_M_ptr");
50 if (!data_ptr)
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}");
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
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
94private:
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
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
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:408
Generic representation of a type in a programming language.
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
SyntheticChildrenFrontEnd(ValueObject &backend)
lldb::ValueObjectSP CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
ValueObject * m_start
First element of span. Held, not owned.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
LibStdcppSpanSyntheticFrontEnd(const lldb::ValueObjectSP &valobj_sp)
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
llvm::Expected< uint32_t > CalculateNumChildren() override
uint32_t m_element_size
Size in bytes of each span element.
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibStdcppSpanSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
@ eReuse
Children did not change and don't need to be recomputed; re-use what we computed the last time we cal...
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP