1 //===-- LibCxxBitset.cpp ----------------------------------------*- C++ -*-===//
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 "lldb/DataFormatters/FormattersHelpers.h"
11 #include "lldb/Symbol/ClangASTContext.h"
12 #include "lldb/Target/Target.h"
15 using namespace lldb_private;
19 class BitsetFrontEnd : public SyntheticChildrenFrontEnd {
21 BitsetFrontEnd(ValueObject &valobj);
23 size_t GetIndexOfChildWithName(ConstString name) override {
24 return formatters::ExtractIndexFromString(name.GetCString());
27 bool MightHaveChildren() override { return true; }
28 bool Update() override;
29 size_t CalculateNumChildren() override { return m_elements.size(); }
30 ValueObjectSP GetChildAtIndex(size_t idx) override;
33 // The lifetime of a ValueObject and all its derivative ValueObjects
34 // (children, clones, etc.) is managed by a ClusterManager. These
35 // objects are only destroyed when every shared pointer to any of them
36 // is destroyed, so we must not store a shared pointer to any ValueObject
37 // derived from our backend ValueObject (since we're in the same cluster).
38 // Value objects created from raw data (i.e. in a different cluster) must
39 // be referenced via shared pointer to keep them alive, however.
40 std::vector<ValueObjectSP> m_elements;
41 ValueObject* m_first = nullptr;
42 CompilerType m_bool_type;
43 ByteOrder m_byte_order = eByteOrderInvalid;
44 uint8_t m_byte_size = 0;
48 BitsetFrontEnd::BitsetFrontEnd(ValueObject &valobj)
49 : SyntheticChildrenFrontEnd(valobj) {
50 m_bool_type = valobj.GetCompilerType().GetBasicTypeFromAST(eBasicTypeBool);
51 if (auto target_sp = m_backend.GetTargetSP()) {
52 m_byte_order = target_sp->GetArchitecture().GetByteOrder();
53 m_byte_size = target_sp->GetArchitecture().GetAddressByteSize();
58 bool BitsetFrontEnd::Update() {
62 TargetSP target_sp = m_backend.GetTargetSP();
65 size_t capping_size = target_sp->GetMaximumNumberOfChildrenToDisplay();
68 if (auto arg = m_backend.GetCompilerType().GetIntegralTemplateArgument(0))
69 size = arg->value.getLimitedValue(capping_size);
71 m_elements.assign(size, ValueObjectSP());
73 m_first = m_backend.GetChildMemberWithName(ConstString("__first_"), true).get();
77 ValueObjectSP BitsetFrontEnd::GetChildAtIndex(size_t idx) {
78 if (idx >= m_elements.size() || !m_first)
79 return ValueObjectSP();
82 return m_elements[idx];
84 ExecutionContext ctx = m_backend.GetExecutionContextRef().Lock(false);
87 // For small bitsets __first_ is not an array, but a plain size_t.
88 if (m_first->GetCompilerType().IsArrayType(&type, nullptr, nullptr)) {
89 llvm::Optional<uint64_t> bit_size =
90 type.GetBitSize(ctx.GetBestExecutionContextScope());
91 if (!bit_size || *bit_size == 0)
93 chunk = m_first->GetChildAtIndex(idx / *bit_size, true);
95 type = m_first->GetCompilerType();
96 chunk = m_first->GetSP();
101 llvm::Optional<uint64_t> bit_size =
102 type.GetBitSize(ctx.GetBestExecutionContextScope());
103 if (!bit_size || *bit_size == 0)
105 size_t chunk_idx = idx % *bit_size;
106 uint8_t value = !!(chunk->GetValueAsUnsigned(0) & (uint64_t(1) << chunk_idx));
107 DataExtractor data(&value, sizeof(value), m_byte_order, m_byte_size);
109 m_elements[idx] = CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(),
110 data, ctx, m_bool_type);
112 return m_elements[idx];
115 SyntheticChildrenFrontEnd *formatters::LibcxxBitsetSyntheticFrontEndCreator(
116 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
118 return new BitsetFrontEnd(*valobj_sp);