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 <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::formatters;
19
21
23public:
25
26 ~MsvcStlSpanSyntheticFrontEnd() override = default;
27
28 llvm::Expected<uint32_t> CalculateNumChildren() override {
29 return m_num_elements;
30 }
31
32 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
33
35
36 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
37
38private:
39 ValueObject *m_start = nullptr; ///< First element of span. Held, not owned.
40 CompilerType m_element_type{}; ///< Type of span elements.
41 size_t m_num_elements = 0; ///< Number of elements in span.
42 uint32_t m_element_size = 0; ///< Size in bytes of each span element.
43};
44
51
54 uint32_t idx) {
55 if (!m_start)
56 return {};
57
58 uint64_t offset = idx * m_element_size;
59 offset = offset + m_start->GetValueAsUnsigned(0);
60 StreamString name;
61 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
62 return CreateValueObjectFromAddress(name.GetString(), offset,
63 m_backend.GetExecutionContextRef(),
65}
66
69 m_start = nullptr;
73
74 ValueObjectSP data_sp = m_backend.GetChildMemberWithName("_Mydata");
75 if (!data_sp)
77
78 m_element_type = data_sp->GetCompilerType().GetPointeeType();
79
80 // Get element size.
81 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
82 if (!size_or_err) {
83 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
84 "{0}");
86 }
87
88 m_element_size = *size_or_err;
89
90 // Get data.
91 if (m_element_size > 0)
92 m_start = data_sp.get();
93
94 // Get number of elements.
95 if (auto size_sp = m_backend.GetChildMemberWithName("_Mysize"))
96 m_num_elements = size_sp->GetValueAsUnsigned(0);
97 else if (auto field =
98 m_backend.GetCompilerType()
99 .GetDirectBaseClassAtIndex(0, nullptr) // _Span_extent_type
100 .GetStaticFieldWithName("_Mysize"))
101 m_num_elements = field.GetConstantValue().ULongLong(0);
102
104}
105
106llvm::Expected<size_t>
108 ConstString name) {
109 if (!m_start)
110 return llvm::createStringError("Type has no child named '%s'",
111 name.AsCString());
112
113 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
114 if (!optional_idx)
115 return llvm::createStringError("Type has no child named '%s'",
116 name.AsCString());
117 return *optional_idx;
118}
119
121 if (auto valobj_sp = valobj.GetNonSyntheticValue())
122 return valobj_sp->GetChildMemberWithName("_Mydata") != nullptr;
123 return false;
124}
125
128 lldb::ValueObjectSP valobj_sp) {
129 if (!valobj_sp)
130 return nullptr;
131 return new MsvcStlSpanSyntheticFrontEnd(valobj_sp);
132}
133
134} // namespace lldb_private::formatters
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:408
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
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:134
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
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:332
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