xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/Language/CPlusPlus/LibCxxQueue.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- LibCxxQueue.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 "lldb/DataFormatters/FormattersHelpers.h"
11 
12 using namespace lldb;
13 using namespace lldb_private;
14 
15 namespace {
16 
17 class QueueFrontEnd : public SyntheticChildrenFrontEnd {
18 public:
QueueFrontEnd(ValueObject & valobj)19   QueueFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
20     Update();
21   }
22 
GetIndexOfChildWithName(ConstString name)23   llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
24     if (m_container_sp)
25       return m_container_sp->GetIndexOfChildWithName(name);
26     return llvm::createStringError("Type has no child named '%s'",
27                                    name.AsCString());
28   }
29 
30   lldb::ChildCacheState Update() override;
31 
CalculateNumChildren()32   llvm::Expected<uint32_t> CalculateNumChildren() override {
33     return m_container_sp ? m_container_sp->GetNumChildren() : 0;
34   }
35 
GetChildAtIndex(uint32_t idx)36   ValueObjectSP GetChildAtIndex(uint32_t idx) override {
37     return m_container_sp ? m_container_sp->GetChildAtIndex(idx)
38                           : nullptr;
39   }
40 
41 private:
42   // The lifetime of a ValueObject and all its derivative ValueObjects
43   // (children, clones, etc.) is managed by a ClusterManager. These
44   // objects are only destroyed when every shared pointer to any of them
45   // is destroyed, so we must not store a shared pointer to any ValueObject
46   // derived from our backend ValueObject (since we're in the same cluster).
47   ValueObject* m_container_sp = nullptr;
48 };
49 } // namespace
50 
Update()51 lldb::ChildCacheState QueueFrontEnd::Update() {
52   m_container_sp = nullptr;
53   ValueObjectSP c_sp = m_backend.GetChildMemberWithName("c");
54   if (!c_sp)
55     return lldb::ChildCacheState::eRefetch;
56   m_container_sp = c_sp->GetSyntheticValue().get();
57   return lldb::ChildCacheState::eRefetch;
58 }
59 
60 SyntheticChildrenFrontEnd *
LibcxxQueueFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)61 formatters::LibcxxQueueFrontEndCreator(CXXSyntheticChildren *,
62                                        lldb::ValueObjectSP valobj_sp) {
63   if (valobj_sp)
64     return new QueueFrontEnd(*valobj_sp);
65   return nullptr;
66 }
67