LLDB mainline
MsvcStlSpan.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 "MsvcStl.h"
10
14#include "llvm/Support/ErrorExtras.h"
15#include <optional>
16
17using namespace lldb;
18using namespace lldb_private;
19using namespace lldb_private::formatters;
20
22
24public:
26
27 ~MsvcStlSpanSyntheticFrontEnd() override = default;
28
29 llvm::Expected<uint32_t> CalculateNumChildren() override {
30 return m_num_elements;
31 }
32
33 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
34
36
37 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
38
39private:
40 ValueObject *m_start = nullptr; ///< First element of span. Held, not owned.
41 CompilerType m_element_type{}; ///< Type of span elements.
42 size_t m_num_elements = 0; ///< Number of elements in span.
43 uint32_t m_element_size = 0; ///< Size in bytes of each span element.
44};
45
52
55 uint32_t idx) {
56 if (!m_start)
57 return {};
58
59 uint64_t offset = idx * m_element_size;
60 offset = offset + m_start->GetValueAsUnsigned(0);
61 StreamString name;
62 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
63 return CreateValueObjectFromAddress(name.GetString(), offset,
64 m_backend.GetExecutionContextRef(),
66}
67
70 m_start = nullptr;
74
75 ValueObjectSP data_sp = m_backend.GetChildMemberWithName("_Mydata");
76 if (!data_sp)
78
79 m_element_type = data_sp->GetCompilerType().GetPointeeType();
80
81 // Get element size.
82 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
83 if (!size_or_err) {
84 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
85 "{0}");
87 }
88
89 m_element_size = *size_or_err;
90
91 // Get data.
92 if (m_element_size > 0)
93 m_start = data_sp.get();
94
95 // Get number of elements.
96 if (auto size_sp = m_backend.GetChildMemberWithName("_Mysize"))
97 m_num_elements = size_sp->GetValueAsUnsigned(0);
98 else if (auto field =
99 m_backend.GetCompilerType()
100 .GetDirectBaseClassAtIndex(0, nullptr) // _Span_extent_type
101 .GetStaticFieldWithName("_Mysize"))
102 m_num_elements = field.GetConstantValue().ULongLong(0);
103
105}
106
107llvm::Expected<size_t>
109 ConstString name) {
110 if (!m_start)
111 return llvm::createStringErrorV("type has no child named '{0}'", name);
112
113 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
114 if (!optional_idx)
115 return llvm::createStringErrorV("type has no child named '{0}'", name);
116 return *optional_idx;
117}
118
120 if (auto valobj_sp = valobj.GetNonSyntheticValue())
121 return valobj_sp->GetChildMemberWithName("_Mydata") != nullptr;
122 return false;
123}
124
127 lldb::ValueObjectSP valobj_sp) {
128 if (!valobj_sp)
129 return nullptr;
130 return new MsvcStlSpanSyntheticFrontEnd(valobj_sp);
131}
132
133} // namespace lldb_private::formatters
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:410
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
SyntheticChildrenFrontEnd(ValueObject &backend)
lldb::ValueObjectSP CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
virtual lldb::ValueObjectSP GetNonSyntheticValue()
llvm::Expected< uint32_t > CalculateNumChildren() override
size_t m_num_elements
Number of elements in span.
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
uint32_t m_element_size
Size in bytes of each span element.
CompilerType m_element_type
Type of span elements.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
MsvcStlSpanSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
ValueObject * m_start
First element of span. Held, not owned.
std::optional< size_t > ExtractIndexFromString(const char *item_name)
bool IsMsvcStlSpan(ValueObject &valobj)
SyntheticChildrenFrontEnd * MsvcStlSpanSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
A class that represents a running process on the host machine.
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.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP