LLDB mainline
LibCxxTuple.cpp
Go to the documentation of this file.
1//===-- LibCxxTuple.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"
11
12using namespace lldb;
13using namespace lldb_private;
14
15namespace {
16
17class TupleFrontEnd: public SyntheticChildrenFrontEnd {
18public:
19 TupleFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
20 Update();
21 }
22
23 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
24 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
25 if (!optional_idx) {
26 return llvm::createStringError("Type has no child named '%s'",
27 name.AsCString());
28 }
29 return *optional_idx;
30 }
31
32 lldb::ChildCacheState Update() override;
33 llvm::Expected<uint32_t> CalculateNumChildren() override {
34 return m_elements.size();
35 }
36 ValueObjectSP GetChildAtIndex(uint32_t idx) override;
37
38private:
39 // The lifetime of a ValueObject and all its derivative ValueObjects
40 // (children, clones, etc.) is managed by a ClusterManager. These
41 // objects are only destroyed when every shared pointer to any of them
42 // is destroyed, so we must not store a shared pointer to any ValueObject
43 // derived from our backend ValueObject (since we're in the same cluster).
44 std::vector<ValueObject*> m_elements;
45 ValueObject* m_base = nullptr;
46};
47}
48
49lldb::ChildCacheState TupleFrontEnd::Update() {
50 m_elements.clear();
51 m_base = nullptr;
52
53 ValueObjectSP base_sp;
54 base_sp = m_backend.GetChildMemberWithName("__base_");
55 if (!base_sp) {
56 // Pre r304382 name of the base element.
57 base_sp = m_backend.GetChildMemberWithName("base_");
58 }
59 if (!base_sp)
61 m_base = base_sp.get();
62 m_elements.assign(base_sp->GetCompilerType().GetNumDirectBaseClasses(),
63 nullptr);
65}
66
67ValueObjectSP TupleFrontEnd::GetChildAtIndex(uint32_t idx) {
68 if (idx >= m_elements.size())
69 return ValueObjectSP();
70 if (!m_base)
71 return ValueObjectSP();
72 if (m_elements[idx])
73 return m_elements[idx]->GetSP();
74
75 CompilerType holder_type =
76 m_base->GetCompilerType().GetDirectBaseClassAtIndex(idx, nullptr);
77 if (!holder_type)
78 return ValueObjectSP();
79 ValueObjectSP holder_sp = m_base->GetChildAtIndex(idx);
80 if (!holder_sp)
81 return ValueObjectSP();
82
83 ValueObjectSP elem_sp = holder_sp->GetChildAtIndex(0);
84 if (elem_sp)
85 m_elements[idx] =
86 elem_sp->Clone(ConstString(llvm::formatv("[{0}]", idx).str())).get();
87
88 if (m_elements[idx])
89 return m_elements[idx]->GetSP();
90 return ValueObjectSP();
91}
92
93SyntheticChildrenFrontEnd *
95 lldb::ValueObjectSP valobj_sp) {
96 if (valobj_sp)
97 return new TupleFrontEnd(*valobj_sp);
98 return nullptr;
99}
static std::optional< size_t > CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type)
Calculates the number of elements stored in a container (with element type 'container_elem_type') as ...
CompilerType GetDirectBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
const char * GetCString() const
Get the string value as a C string.
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx, bool can_create=true)
CompilerType GetCompilerType()
virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true)
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxTupleFrontEndCreator(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