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