LLDB mainline
GenericInitializerList.cpp
Go to the documentation of this file.
1//===-- GenericInitializerList.cpp ----------------------------------------===//
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
12#include <cstddef>
13#include <optional>
14#include <type_traits>
15
16using namespace lldb;
17using namespace lldb_private;
18
19namespace generic_check {
20template <class T>
21using size_func = decltype(T::GetSizeMember(std::declval<ValueObject &>()));
22template <class T>
23using start_func = decltype(T::GetStartMember(std::declval<ValueObject &>()));
24namespace {
25template <typename...> struct check_func : std::true_type {};
26} // namespace
27
28template <typename T>
29using has_functions = check_func<size_func<T>, start_func<T>>;
30} // namespace generic_check
31
32struct LibCxx {
34 return backend.GetChildMemberWithName("__begin_");
35 }
36
38 return backend.GetChildMemberWithName("__size_");
39 }
40};
41
42struct LibStdcpp {
44 return backend.GetChildMemberWithName("_M_array");
45 }
46
48 return backend.GetChildMemberWithName("_M_len");
49 }
50};
51
53
54template <class StandardImpl>
57public:
59 "Missing Required Functions.");
60
66
68 // this needs to stay around because it's a child object who will follow its
69 // parent's life cycle
70 // delete m_start;
71 }
72
73 llvm::Expected<uint32_t> CalculateNumChildren() override {
75
76 const ValueObjectSP size_sp(StandardImpl::GetSizeMember(m_backend));
77 if (size_sp)
78 m_num_elements = size_sp->GetValueAsUnsigned(0);
79 return m_num_elements;
80 }
81
82 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
83 if (!m_start)
84 return {};
85
86 uint64_t offset = static_cast<uint64_t>(idx) * m_element_size;
87 offset = offset + m_start->GetValueAsUnsigned(0);
88 StreamString name;
89 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
90 return CreateValueObjectFromAddress(name.GetString(), offset,
91 m_backend.GetExecutionContextRef(),
93 }
94
96 m_start = nullptr;
98 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
99 if (!m_element_type.IsValid())
101
102 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
103 if (!size_or_err)
104 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
105 "{0}");
106 else {
107 m_element_size = *size_or_err;
108 // Store raw pointers or end up with a circular dependency.
109 m_start = StandardImpl::GetStartMember(m_backend).get();
110 }
111
113 }
114
115 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
116 if (!m_start) {
117 return llvm::createStringError("Type has no child named '%s'",
118 name.AsCString());
119 }
120 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
121 if (!optional_idx) {
122 return llvm::createStringError("Type has no child named '%s'",
123 name.AsCString());
124 }
125 return *optional_idx;
126 }
127
128private:
131 uint32_t m_element_size = 0;
132 size_t m_num_elements = 0;
133};
134
136 CXXSyntheticChildren * /*unused*/, lldb::ValueObjectSP valobj_sp) {
137 if (!valobj_sp)
138 return nullptr;
139
140 if (LibCxx::GetStartMember(*valobj_sp) != nullptr)
142
144}
145} // 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 GetChildMemberWithName(llvm::StringRef name, bool can_create=true)
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
decltype(T::GetSizeMember(std::declval< ValueObject & >())) size_func
decltype(T::GetStartMember(std::declval< ValueObject & >())) start_func
check_func< size_func< T >, start_func< T > > has_functions
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * GenericInitializerListSyntheticFrontEndCreator(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
static ValueObjectSP GetSizeMember(ValueObject &backend)
static ValueObjectSP GetStartMember(ValueObject &backend)
static ValueObjectSP GetSizeMember(ValueObject &backend)
static ValueObjectSP GetStartMember(ValueObject &backend)