LLDB mainline
MsvcStlTuple.cpp
Go to the documentation of this file.
1//===-- MsvcStlTuple.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 "MsvcStl.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};
46
47} // namespace
48
49lldb::ChildCacheState TupleFrontEnd::Update() {
50 m_elements.clear();
51
52 size_t n_elements = 0;
53 for (CompilerType ty = m_backend.GetCompilerType();
54 ty.GetNumDirectBaseClasses() > 0;
55 ty = ty.GetDirectBaseClassAtIndex(0, nullptr))
56 ++n_elements;
57
58 m_elements.assign(n_elements, nullptr);
60}
61
62ValueObjectSP TupleFrontEnd::GetChildAtIndex(uint32_t idx) {
63 if (idx >= m_elements.size())
64 return nullptr;
65 if (m_elements[idx])
66 return m_elements[idx]->GetSP();
67
68 CompilerType holder_ty = m_backend.GetCompilerType();
69 for (uint32_t i = 0; i < idx; i++) {
70 holder_ty = holder_ty.GetDirectBaseClassAtIndex(0, nullptr);
71 if (!holder_ty.IsValid())
72 return nullptr;
73 }
74
75 ValueObjectSP holder_sp = m_backend.Cast(holder_ty);
76 if (!holder_sp)
77 return nullptr;
78 holder_sp = holder_sp->GetChildMemberWithName("_Myfirst");
79
80 if (!holder_sp)
81 return nullptr;
82
83 ValueObjectSP val_sp = holder_sp->GetChildMemberWithName("_Val");
84 if (!val_sp)
85 return nullptr;
86
87 m_elements[idx] =
88 val_sp->Clone(ConstString(llvm::formatv("[{0}]", idx).str())).get();
89 return m_elements[idx]->GetSP();
90}
91
93 // This returns false for empty tuples, but the libstdc++ formatter handles
94 // this correctly.
95 if (auto valobj_sp = valobj.GetNonSyntheticValue())
96 return valobj_sp->GetChildMemberWithName("_Myfirst") != nullptr;
97 return false;
98}
99
102 if (valobj_sp)
103 return new TupleFrontEnd(*valobj_sp);
104 return nullptr;
105}
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 GetNonSyntheticValue()
std::optional< size_t > ExtractIndexFromString(const char *item_name)
bool IsMsvcStlTuple(ValueObject &valobj)
SyntheticChildrenFrontEnd * MsvcStlTupleSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
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