LLDB mainline
LibCxxProxyArray.cpp
Go to the documentation of this file.
1//===-- LibCxxProxyArray.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
9#include "LibCxx.h"
10
13#include "llvm/Support/ErrorExtras.h"
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::formatters;
19
20namespace lldb_private {
21namespace formatters {
22
23/// Data formatter for libc++'s std::"proxy_array".
24///
25/// A proxy_array's are created by using:
26/// std::gslice_array operator[](const std::gslice& gslicearr);
27/// std::mask_array operator[](const std::valarray<bool>& boolarr);
28/// std::indirect_array operator[](const std::valarray<std::size_t>& indarr);
29///
30/// These arrays have the following members:
31/// - __vp_ points to std::valarray::__begin_
32/// - __1d_ an array of offsets of the elements from @a __vp_
34public:
36
38
39 llvm::Expected<uint32_t> CalculateNumChildren() override;
40
41 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
42
44
45 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
46
47private:
48 /// A non-owning pointer to the array's __vp_.
49 ValueObject *m_base = nullptr;
50 /// The type of the array's template argument T.
52 /// The sizeof the array's template argument T.
53 uint32_t m_element_size = 0;
54
55 /// A non-owning pointer to the array's __1d_.__begin_.
56 ValueObject *m_start = nullptr;
57 /// A non-owning pointer to the array's __1d_.__end_.
59 /// The type of the __1d_ array's template argument T (size_t).
61 /// The sizeof the __1d_ array's template argument T (size_t)
63};
64
65} // namespace formatters
66} // namespace lldb_private
67
74
77 // these need to stay around because they are child objects who will follow
78 // their parent's life cycle
79 // delete m_base;
80}
81
82llvm::Expected<uint32_t> lldb_private::formatters::
84
85 if (!m_start || !m_finish)
86 return 0;
87 uint64_t start_val = m_start->GetValueAsUnsigned(0);
88 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
89
90 if (start_val == 0 || finish_val == 0)
91 return 0;
92
93 if (start_val >= finish_val)
94 return 0;
95
96 size_t num_children = (finish_val - start_val);
97 if (num_children % m_element_size_size_t)
98 return 0;
99 return num_children / m_element_size_size_t;
100}
101
104 uint32_t idx) {
105 if (!m_base)
106 return lldb::ValueObjectSP();
107
108 uint64_t offset = idx * m_element_size_size_t;
109 offset = offset + m_start->GetValueAsUnsigned(0);
110
112 "", offset, m_backend.GetExecutionContextRef(), m_element_type_size_t);
113 if (!indirect)
114 return lldb::ValueObjectSP();
115
116 const size_t value = indirect->GetValueAsUnsigned(0);
117 if (!value)
118 return lldb::ValueObjectSP();
119
120 offset = value * m_element_size;
121 offset = offset + m_base->GetValueAsUnsigned(0);
122
123 StreamString name;
124 name.Printf("[%" PRIu64 "] -> [%zu]", (uint64_t)idx, value);
125 return CreateValueObjectFromAddress(name.GetString(), offset,
126 m_backend.GetExecutionContextRef(),
128}
129
132 m_base = nullptr;
133 m_start = nullptr;
134 m_finish = nullptr;
135
136 CompilerType type = m_backend.GetCompilerType();
137 if (type.GetNumTemplateArguments() == 0)
139
141 if (std::optional<uint64_t> size =
142 llvm::expectedToOptional(m_element_type.GetByteSize(nullptr)))
143 m_element_size = *size;
144
145 if (m_element_size == 0)
147
148 ValueObjectSP vector = m_backend.GetChildMemberWithName("__1d_");
149 if (!vector)
151
152 type = vector->GetCompilerType();
153 if (type.GetNumTemplateArguments() == 0)
155
157 if (std::optional<uint64_t> size =
158 llvm::expectedToOptional(m_element_type_size_t.GetByteSize(nullptr)))
159 m_element_size_size_t = *size;
160
161 if (m_element_size_size_t == 0)
163
164 ValueObjectSP base = m_backend.GetChildMemberWithName("__vp_");
165 ValueObjectSP start = vector->GetChildMemberWithName("__begin_");
166 ValueObjectSP finish = vector->GetChildMemberWithName("__end_");
167 if (!base || !start || !finish)
169
170 m_base = base.get();
171 m_start = start.get();
172 m_finish = finish.get();
173
175}
176
177llvm::Expected<size_t>
180 if (!m_base)
181 return llvm::createStringErrorV("type has no child named '{0}'", name);
182 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
183 if (!optional_idx) {
184 return llvm::createStringErrorV("type has no child named '{0}'", name);
185 }
186 return *optional_idx;
187}
188
192 if (!valobj_sp)
193 return nullptr;
194 return new LibcxxStdProxyArraySyntheticFrontEnd(valobj_sp);
195}
Generic representation of a type in a programming language.
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
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.
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)
Data formatter for libc++'s std::"proxy_array".
uint32_t m_element_size
The sizeof the array's template argument T.
ValueObject * m_finish
A non-owning pointer to the array's __1d_.__end_.
uint32_t m_element_size_size_t
The sizeof the __1d_ array's template argument T (size_t)
CompilerType m_element_type_size_t
The type of the __1d_ array's template argument T (size_t).
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
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
ValueObject * m_start
A non-owning pointer to the array's __1d_.__begin_.
ValueObject * m_base
A non-owning pointer to the array's __vp_.
CompilerType m_element_type
The type of the array's template argument T.
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxStdProxyArraySyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
A class that represents a running process on the host machine.
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