1 //===-- GenericBitset.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 #include "LibStdcpp.h"
11 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12 #include "lldb/DataFormatters/FormattersHelpers.h"
13 #include "lldb/Target/Target.h"
14 #include <optional>
15
16 using namespace lldb;
17 using namespace lldb_private;
18
19 namespace {
20
21 /// This class can be used for handling bitsets from both libcxx and libstdcpp.
22 class GenericBitsetFrontEnd : public SyntheticChildrenFrontEnd {
23 public:
24 enum class StdLib {
25 LibCxx,
26 LibStdcpp,
27 };
28
29 GenericBitsetFrontEnd(ValueObject &valobj, StdLib stdlib);
30
GetIndexOfChildWithName(ConstString name)31 size_t GetIndexOfChildWithName(ConstString name) override {
32 return formatters::ExtractIndexFromString(name.GetCString());
33 }
34
MightHaveChildren()35 bool MightHaveChildren() override { return true; }
36 bool Update() override;
CalculateNumChildren()37 size_t CalculateNumChildren() override { return m_elements.size(); }
38 ValueObjectSP GetChildAtIndex(size_t idx) override;
39
40 private:
41 ConstString GetDataContainerMemberName();
42
43 // The lifetime of a ValueObject and all its derivative ValueObjects
44 // (children, clones, etc.) is managed by a ClusterManager. These
45 // objects are only destroyed when every shared pointer to any of them
46 // is destroyed, so we must not store a shared pointer to any ValueObject
47 // derived from our backend ValueObject (since we're in the same cluster).
48 // Value objects created from raw data (i.e. in a different cluster) must
49 // be referenced via shared pointer to keep them alive, however.
50 std::vector<ValueObjectSP> m_elements;
51 ValueObject *m_first = nullptr;
52 CompilerType m_bool_type;
53 ByteOrder m_byte_order = eByteOrderInvalid;
54 uint8_t m_byte_size = 0;
55 StdLib m_stdlib;
56 };
57 } // namespace
58
GenericBitsetFrontEnd(ValueObject & valobj,StdLib stdlib)59 GenericBitsetFrontEnd::GenericBitsetFrontEnd(ValueObject &valobj, StdLib stdlib)
60 : SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
61 m_bool_type = valobj.GetCompilerType().GetBasicTypeFromAST(eBasicTypeBool);
62 if (auto target_sp = m_backend.GetTargetSP()) {
63 m_byte_order = target_sp->GetArchitecture().GetByteOrder();
64 m_byte_size = target_sp->GetArchitecture().GetAddressByteSize();
65 Update();
66 }
67 }
68
GetDataContainerMemberName()69 ConstString GenericBitsetFrontEnd::GetDataContainerMemberName() {
70 switch (m_stdlib) {
71 case StdLib::LibCxx:
72 return ConstString("__first_");
73 case StdLib::LibStdcpp:
74 return ConstString("_M_w");
75 }
76 llvm_unreachable("Unknown StdLib enum");
77 }
78
Update()79 bool GenericBitsetFrontEnd::Update() {
80 m_elements.clear();
81 m_first = nullptr;
82
83 TargetSP target_sp = m_backend.GetTargetSP();
84 if (!target_sp)
85 return false;
86
87 size_t size = 0;
88
89 if (auto arg = m_backend.GetCompilerType().GetIntegralTemplateArgument(0))
90 size = arg->value.getLimitedValue();
91
92 m_elements.assign(size, ValueObjectSP());
93 m_first = m_backend.GetChildMemberWithName(GetDataContainerMemberName(), true)
94 .get();
95 return false;
96 }
97
GetChildAtIndex(size_t idx)98 ValueObjectSP GenericBitsetFrontEnd::GetChildAtIndex(size_t idx) {
99 if (idx >= m_elements.size() || !m_first)
100 return ValueObjectSP();
101
102 if (m_elements[idx])
103 return m_elements[idx];
104
105 ExecutionContext ctx = m_backend.GetExecutionContextRef().Lock(false);
106 CompilerType type;
107 ValueObjectSP chunk;
108 // For small bitsets __first_ is not an array, but a plain size_t.
109 if (m_first->GetCompilerType().IsArrayType(&type)) {
110 std::optional<uint64_t> bit_size =
111 type.GetBitSize(ctx.GetBestExecutionContextScope());
112 if (!bit_size || *bit_size == 0)
113 return {};
114 chunk = m_first->GetChildAtIndex(idx / *bit_size, true);
115 } else {
116 type = m_first->GetCompilerType();
117 chunk = m_first->GetSP();
118 }
119 if (!type || !chunk)
120 return {};
121
122 std::optional<uint64_t> bit_size =
123 type.GetBitSize(ctx.GetBestExecutionContextScope());
124 if (!bit_size || *bit_size == 0)
125 return {};
126 size_t chunk_idx = idx % *bit_size;
127 uint8_t value = !!(chunk->GetValueAsUnsigned(0) & (uint64_t(1) << chunk_idx));
128 DataExtractor data(&value, sizeof(value), m_byte_order, m_byte_size);
129
130 m_elements[idx] = CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(),
131 data, ctx, m_bool_type);
132
133 return m_elements[idx];
134 }
135
LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)136 SyntheticChildrenFrontEnd *formatters::LibStdcppBitsetSyntheticFrontEndCreator(
137 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
138 if (valobj_sp)
139 return new GenericBitsetFrontEnd(*valobj_sp,
140 GenericBitsetFrontEnd::StdLib::LibStdcpp);
141 return nullptr;
142 }
143
LibcxxBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)144 SyntheticChildrenFrontEnd *formatters::LibcxxBitsetSyntheticFrontEndCreator(
145 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
146 if (valobj_sp)
147 return new GenericBitsetFrontEnd(*valobj_sp,
148 GenericBitsetFrontEnd::StdLib::LibCxx);
149 return nullptr;
150 }
151