LLDB mainline
LibStdcppTuple.cpp
Go to the documentation of this file.
1//===-- LibStdcppTuple.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 "LibStdcpp.h"
10
15
16#include <memory>
17#include <vector>
18
19using namespace lldb;
20using namespace lldb_private;
21using namespace lldb_private::formatters;
22
23namespace {
24
25class LibStdcppTupleSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
26public:
27 explicit LibStdcppTupleSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
28
29 llvm::Expected<uint32_t> CalculateNumChildren() override;
30
31 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
32
33 lldb::ChildCacheState Update() override;
34
35 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
36
37private:
38 // The lifetime of a ValueObject and all its derivative ValueObjects
39 // (children, clones, etc.) is managed by a ClusterManager. These
40 // objects are only destroyed when every shared pointer to any of them
41 // is destroyed, so we must not store a shared pointer to any ValueObject
42 // derived from our backend ValueObject (since we're in the same cluster).
43 std::vector<ValueObject*> m_members;
44};
45
46} // end of anonymous namespace
47
48LibStdcppTupleSyntheticFrontEnd::LibStdcppTupleSyntheticFrontEnd(
49 lldb::ValueObjectSP valobj_sp)
50 : SyntheticChildrenFrontEnd(*valobj_sp) {
51 Update();
52}
53
54lldb::ChildCacheState LibStdcppTupleSyntheticFrontEnd::Update() {
55 m_members.clear();
56
57 ValueObjectSP valobj_backend_sp = m_backend.GetSP();
58 if (!valobj_backend_sp)
60
61 ValueObjectSP next_child_sp = valobj_backend_sp->GetNonSyntheticValue();
62 while (next_child_sp != nullptr) {
63 ValueObjectSP current_child = next_child_sp;
64 next_child_sp = nullptr;
65
66 size_t child_count = current_child->GetNumChildrenIgnoringErrors();
67 for (size_t i = 0; i < child_count; ++i) {
68 ValueObjectSP child_sp = current_child->GetChildAtIndex(i);
69 if (!child_sp)
70 continue;
71 llvm::StringRef name_str = child_sp->GetName().GetStringRef();
72 if (name_str.starts_with("std::_Tuple_impl<")) {
73 next_child_sp = child_sp;
74 } else if (name_str.starts_with("std::_Head_base<")) {
75 ValueObjectSP value_sp =
76 child_sp->GetChildMemberWithName("_M_head_impl");
77 if (value_sp) {
78 StreamString name;
79 name.Printf("[%zd]", m_members.size());
80 m_members.push_back(value_sp->Clone(ConstString(name.GetString())).get());
81 }
82 }
83 }
84 }
85
87}
88
90LibStdcppTupleSyntheticFrontEnd::GetChildAtIndex(uint32_t idx) {
91 if (idx < m_members.size() && m_members[idx])
92 return m_members[idx]->GetSP();
93 return lldb::ValueObjectSP();
94}
95
96llvm::Expected<uint32_t>
97LibStdcppTupleSyntheticFrontEnd::CalculateNumChildren() {
98 return m_members.size();
99}
100
101llvm::Expected<size_t>
102LibStdcppTupleSyntheticFrontEnd::GetIndexOfChildWithName(ConstString name) {
103 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
104 if (!optional_idx) {
105 return llvm::createStringError("Type has no child named '%s'",
106 name.AsCString());
107 }
108 return *optional_idx;
109}
110
111SyntheticChildrenFrontEnd *
113 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
114 return (valobj_sp ? new LibStdcppTupleSyntheticFrontEnd(valobj_sp) : nullptr);
115}
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 ...
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.
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibStdcppTupleSyntheticFrontEndCreator(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