1 //===-- GenericBitset.cpp //-----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "LibStdcpp.h"
11 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12 #include "lldb/DataFormatters/FormattersHelpers.h"
13 #include "lldb/Target/Target.h"
17 using namespace lldb_private
;
21 /// This class can be used for handling bitsets from both libcxx and libstdcpp.
22 class GenericBitsetFrontEnd
: public SyntheticChildrenFrontEnd
{
29 GenericBitsetFrontEnd(ValueObject
&valobj
, StdLib stdlib
);
31 size_t GetIndexOfChildWithName(ConstString name
) override
{
32 return formatters::ExtractIndexFromString(name
.GetCString());
35 bool MightHaveChildren() override
{ return true; }
36 lldb::ChildCacheState
Update() override
;
37 llvm::Expected
<uint32_t> CalculateNumChildren() override
{
38 return m_elements
.size();
40 ValueObjectSP
GetChildAtIndex(uint32_t idx
) override
;
43 llvm::StringRef
GetDataContainerMemberName();
45 // The lifetime of a ValueObject and all its derivative ValueObjects
46 // (children, clones, etc.) is managed by a ClusterManager. These
47 // objects are only destroyed when every shared pointer to any of them
48 // is destroyed, so we must not store a shared pointer to any ValueObject
49 // derived from our backend ValueObject (since we're in the same cluster).
50 // Value objects created from raw data (i.e. in a different cluster) must
51 // be referenced via shared pointer to keep them alive, however.
52 std::vector
<ValueObjectSP
> m_elements
;
53 ValueObject
*m_first
= nullptr;
54 CompilerType m_bool_type
;
55 ByteOrder m_byte_order
= eByteOrderInvalid
;
56 uint8_t m_byte_size
= 0;
61 GenericBitsetFrontEnd::GenericBitsetFrontEnd(ValueObject
&valobj
, StdLib stdlib
)
62 : SyntheticChildrenFrontEnd(valobj
), m_stdlib(stdlib
) {
63 m_bool_type
= valobj
.GetCompilerType().GetBasicTypeFromAST(eBasicTypeBool
);
64 if (auto target_sp
= m_backend
.GetTargetSP()) {
65 m_byte_order
= target_sp
->GetArchitecture().GetByteOrder();
66 m_byte_size
= target_sp
->GetArchitecture().GetAddressByteSize();
71 llvm::StringRef
GenericBitsetFrontEnd::GetDataContainerMemberName() {
72 static constexpr llvm::StringLiteral
s_libcxx_case("__first_");
73 static constexpr llvm::StringLiteral
s_libstdcpp_case("_M_w");
77 case StdLib::LibStdcpp
:
78 return s_libstdcpp_case
;
80 llvm_unreachable("Unknown StdLib enum");
83 lldb::ChildCacheState
GenericBitsetFrontEnd::Update() {
87 TargetSP target_sp
= m_backend
.GetTargetSP();
89 return lldb::ChildCacheState::eRefetch
;
93 if (auto arg
= m_backend
.GetCompilerType().GetIntegralTemplateArgument(0))
94 size
= arg
->value
.getLimitedValue();
96 m_elements
.assign(size
, ValueObjectSP());
98 m_backend
.GetChildMemberWithName(GetDataContainerMemberName()).get();
99 return lldb::ChildCacheState::eRefetch
;
102 ValueObjectSP
GenericBitsetFrontEnd::GetChildAtIndex(uint32_t idx
) {
103 if (idx
>= m_elements
.size() || !m_first
)
104 return ValueObjectSP();
107 return m_elements
[idx
];
109 ExecutionContext ctx
= m_backend
.GetExecutionContextRef().Lock(false);
112 // For small bitsets __first_ is not an array, but a plain size_t.
113 if (m_first
->GetCompilerType().IsArrayType(&type
)) {
114 std::optional
<uint64_t> bit_size
=
115 type
.GetBitSize(ctx
.GetBestExecutionContextScope());
116 if (!bit_size
|| *bit_size
== 0)
118 chunk
= m_first
->GetChildAtIndex(idx
/ *bit_size
);
120 type
= m_first
->GetCompilerType();
121 chunk
= m_first
->GetSP();
126 std::optional
<uint64_t> bit_size
=
127 type
.GetBitSize(ctx
.GetBestExecutionContextScope());
128 if (!bit_size
|| *bit_size
== 0)
130 size_t chunk_idx
= idx
% *bit_size
;
131 uint8_t value
= !!(chunk
->GetValueAsUnsigned(0) & (uint64_t(1) << chunk_idx
));
132 DataExtractor
data(&value
, sizeof(value
), m_byte_order
, m_byte_size
);
134 m_elements
[idx
] = CreateValueObjectFromData(llvm::formatv("[{0}]", idx
).str(),
135 data
, ctx
, m_bool_type
);
137 return m_elements
[idx
];
140 SyntheticChildrenFrontEnd
*formatters::LibStdcppBitsetSyntheticFrontEndCreator(
141 CXXSyntheticChildren
*, lldb::ValueObjectSP valobj_sp
) {
143 return new GenericBitsetFrontEnd(*valobj_sp
,
144 GenericBitsetFrontEnd::StdLib::LibStdcpp
);
148 SyntheticChildrenFrontEnd
*formatters::LibcxxBitsetSyntheticFrontEndCreator(
149 CXXSyntheticChildren
*, lldb::ValueObjectSP valobj_sp
) {
151 return new GenericBitsetFrontEnd(*valobj_sp
,
152 GenericBitsetFrontEnd::StdLib::LibCxx
);