LLDB mainline
LibCxxVariant.cpp
Go to the documentation of this file.
1//===-- LibCxxVariant.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 "LibCxxVariant.h"
10#include "LibCxx.h"
14
15#include "llvm/ADT/ScopeExit.h"
16#include <optional>
17
18using namespace lldb;
19using namespace lldb_private;
20
21// libc++ variant implementation contains two members that we care about both
22// are contained in the __impl member.
23// - __index which tells us which of the variadic template types is the active
24// type for the variant
25// - __data is a variadic union which recursively contains itself as member
26// which refers to the tailing variadic types.
27// - __head which refers to the leading non pack type
28// - __value refers to the actual value contained
29// - __tail which refers to the remaining pack types
30//
31// e.g. given std::variant<int,double,char> v1
32//
33// (lldb) frame var -R v1.__impl.__data
34//(... __union<... 0, int, double, char>) v1.__impl.__data = {
35// ...
36// __head = {
37// __value = ...
38// }
39// __tail = {
40// ...
41// __head = {
42// __value = ...
43// }
44// __tail = {
45// ...
46// __head = {
47// __value = ...
48// ...
49//
50// So given
51// - __index equal to 0 the active value is contained in
52//
53// __data.__head.__value
54//
55// - __index equal to 1 the active value is contained in
56//
57// __data.__tail.__head.__value
58//
59// - __index equal to 2 the active value is contained in
60//
61// __data.__tail.__tail.__head.__value
62//
63
64namespace {
65// libc++ std::variant index could have one of three states
66// 1) Valid, we can obtain it and its not variant_npos
67// 2) Invalid, we can't obtain it or it is not a type we expect
68// 3) NPos, its value is variant_npos which means the variant has no value
69enum class LibcxxVariantIndexValidity { Valid, Invalid, NPos };
70
71uint64_t VariantNposValue(uint64_t index_byte_size) {
72 switch (index_byte_size) {
73 case 1:
74 return static_cast<uint8_t>(-1);
75 case 2:
76 return static_cast<uint16_t>(-1);
77 case 4:
78 return static_cast<uint32_t>(-1);
79 }
80 lldbassert(false && "Unknown index type size");
81 return static_cast<uint32_t>(-1); // Fallback to stable ABI type.
82}
83
84LibcxxVariantIndexValidity
85LibcxxVariantGetIndexValidity(ValueObjectSP &impl_sp) {
86 ValueObjectSP index_sp(impl_sp->GetChildMemberWithName("__index", true));
87
88 if (!index_sp)
89 return LibcxxVariantIndexValidity::Invalid;
90
91 // In the stable ABI, the type of __index is just int.
92 // In the unstable ABI, where _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION is
93 // enabled, the type can either be unsigned char/short/int depending on
94 // how many variant types there are.
95 // We only need to do this here when comparing against npos, because npos is
96 // just `-1`, but that translates to different unsigned values depending on
97 // the byte size.
98 CompilerType index_type = index_sp->GetCompilerType();
99
100 std::optional<uint64_t> index_type_bytes = index_type.GetByteSize(nullptr);
101 if (!index_type_bytes)
102 return LibcxxVariantIndexValidity::Invalid;
103
104 uint64_t npos_value = VariantNposValue(*index_type_bytes);
105 uint64_t index_value = index_sp->GetValueAsUnsigned(0);
106
107 if (index_value == npos_value)
108 return LibcxxVariantIndexValidity::NPos;
109
110 return LibcxxVariantIndexValidity::Valid;
111}
112
113std::optional<uint64_t> LibcxxVariantIndexValue(ValueObjectSP &impl_sp) {
114 ValueObjectSP index_sp(impl_sp->GetChildMemberWithName("__index", true));
115
116 if (!index_sp)
117 return {};
118
119 return {index_sp->GetValueAsUnsigned(0)};
120}
121
122ValueObjectSP LibcxxVariantGetNthHead(ValueObjectSP &impl_sp, uint64_t index) {
123 ValueObjectSP data_sp(impl_sp->GetChildMemberWithName("__data", true));
124
125 if (!data_sp)
126 return ValueObjectSP{};
127
128 ValueObjectSP current_level = data_sp;
129 for (uint64_t n = index; n != 0; --n) {
130 ValueObjectSP tail_sp(
131 current_level->GetChildMemberWithName("__tail", true));
132
133 if (!tail_sp)
134 return ValueObjectSP{};
135
136 current_level = tail_sp;
137 }
138
139 return current_level->GetChildMemberWithName("__head", true);
140}
141} // namespace
142
143namespace lldb_private {
144namespace formatters {
146 const TypeSummaryOptions &options) {
147 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
148 if (!valobj_sp)
149 return false;
150
151 ValueObjectSP impl_sp = GetChildMemberWithName(
152 *valobj_sp, {ConstString("__impl_"), ConstString("__impl")});
153
154 if (!impl_sp)
155 return false;
156
157 LibcxxVariantIndexValidity validity = LibcxxVariantGetIndexValidity(impl_sp);
158
159 if (validity == LibcxxVariantIndexValidity::Invalid)
160 return false;
161
162 if (validity == LibcxxVariantIndexValidity::NPos) {
163 stream.Printf(" No Value");
164 return true;
165 }
166
167 auto optional_index_value = LibcxxVariantIndexValue(impl_sp);
168
169 if (!optional_index_value)
170 return false;
171
172 uint64_t index_value = *optional_index_value;
173
174 ValueObjectSP nth_head = LibcxxVariantGetNthHead(impl_sp, index_value);
175
176 if (!nth_head)
177 return false;
178
179 CompilerType head_type = nth_head->GetCompilerType();
180
181 if (!head_type)
182 return false;
183
184 CompilerType template_type = head_type.GetTypeTemplateArgument(1);
185
186 if (!template_type)
187 return false;
188
189 stream << " Active Type = " << template_type.GetDisplayTypeName() << " ";
190
191 return true;
192}
193} // namespace formatters
194} // namespace lldb_private
195
196namespace {
197class VariantFrontEnd : public SyntheticChildrenFrontEnd {
198public:
199 VariantFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
200 Update();
201 }
202
203 size_t GetIndexOfChildWithName(ConstString name) override {
205 }
206
207 bool MightHaveChildren() override { return true; }
208 bool Update() override;
209 size_t CalculateNumChildren() override { return m_size; }
210 ValueObjectSP GetChildAtIndex(size_t idx) override;
211
212private:
213 size_t m_size = 0;
214};
215} // namespace
216
217bool VariantFrontEnd::Update() {
218 m_size = 0;
219 ValueObjectSP impl_sp = formatters::GetChildMemberWithName(
220 m_backend, {ConstString("__impl_"), ConstString("__impl")});
221 if (!impl_sp)
222 return false;
223
224 LibcxxVariantIndexValidity validity = LibcxxVariantGetIndexValidity(impl_sp);
225
226 if (validity == LibcxxVariantIndexValidity::Invalid)
227 return false;
228
229 if (validity == LibcxxVariantIndexValidity::NPos)
230 return true;
231
232 m_size = 1;
233
234 return false;
235}
236
237ValueObjectSP VariantFrontEnd::GetChildAtIndex(size_t idx) {
238 if (idx >= m_size)
239 return {};
240
241 ValueObjectSP impl_sp = formatters::GetChildMemberWithName(
242 m_backend, {ConstString("__impl_"), ConstString("__impl")});
243 if (!impl_sp)
244 return {};
245
246 auto optional_index_value = LibcxxVariantIndexValue(impl_sp);
247
248 if (!optional_index_value)
249 return {};
250
251 uint64_t index_value = *optional_index_value;
252
253 ValueObjectSP nth_head = LibcxxVariantGetNthHead(impl_sp, index_value);
254
255 if (!nth_head)
256 return {};
257
258 CompilerType head_type = nth_head->GetCompilerType();
259
260 if (!head_type)
261 return {};
262
263 CompilerType template_type = head_type.GetTypeTemplateArgument(1);
264
265 if (!template_type)
266 return {};
267
268 ValueObjectSP head_value(nth_head->GetChildMemberWithName("__value", true));
269
270 if (!head_value)
271 return {};
272
273 return head_value->Clone(ConstString("Value"));
274}
275
278 lldb::ValueObjectSP valobj_sp) {
279 if (valobj_sp)
280 return new VariantFrontEnd(*valobj_sp);
281 return nullptr;
282}
#define lldbassert(x)
Definition: LLDBAssert.h:13
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
ConstString GetDisplayTypeName() const
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:221
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
virtual size_t GetIndexOfChildWithName(ConstString name)=0
virtual lldb::ValueObjectSP GetChildAtIndex(size_t idx)=0
virtual lldb::ValueObjectSP GetNonSyntheticValue()
Definition: ValueObject.h:591
lldb::ValueObjectSP GetChildMemberWithName(ValueObject &obj, llvm::ArrayRef< ConstString > alternative_names)
Find a child member of obj_sp, trying all alternative names in order.
Definition: LibCxx.cpp:38
size_t ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxVariantFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
bool LibcxxVariantSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15