1//===-- LibCxxRangesRefView.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
11#include "lldb/DataFormatters/FormattersHelpers.h"
12#include "lldb/Utility/ConstString.h"
13#include "lldb/ValueObject/ValueObject.h"
14#include "llvm/ADT/APSInt.h"
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::formatters;
19
20namespace lldb_private {
21namespace formatters {
22
23class LibcxxStdRangesRefViewSyntheticFrontEnd
24 : public SyntheticChildrenFrontEnd {
25public:
26 LibcxxStdRangesRefViewSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
27
28 ~LibcxxStdRangesRefViewSyntheticFrontEnd() override = default;
29
30 llvm::Expected<uint32_t> CalculateNumChildren() override {
31 // __range_ will be the sole child of this type
32 return 1;
33 }
34
35 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
36 // Since we only have a single child, return it
37 assert(idx == 0);
38 return m_range_sp;
39 }
40
41 lldb::ChildCacheState Update() override;
42
43 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
44 // We only have a single child
45 return 0;
46 }
47
48private:
49 /// Pointer to the dereferenced __range_ member
50 lldb::ValueObjectSP m_range_sp = nullptr;
51};
52
53lldb_private::formatters::LibcxxStdRangesRefViewSyntheticFrontEnd::
54 LibcxxStdRangesRefViewSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
55 : SyntheticChildrenFrontEnd(*valobj_sp) {
56 if (valobj_sp)
57 Update();
58}
59
60lldb::ChildCacheState
61lldb_private::formatters::LibcxxStdRangesRefViewSyntheticFrontEnd::Update() {
62 ValueObjectSP range_ptr =
63 GetChildMemberWithName(obj&: m_backend, alternative_names: {ConstString("__range_")});
64 if (!range_ptr)
65 return lldb::ChildCacheState::eRefetch;
66
67 lldb_private::Status error;
68 m_range_sp = range_ptr->Dereference(error);
69
70 return error.Success() ? lldb::ChildCacheState::eReuse
71 : lldb::ChildCacheState::eRefetch;
72}
73
74lldb_private::SyntheticChildrenFrontEnd *
75LibcxxStdRangesRefViewSyntheticFrontEndCreator(CXXSyntheticChildren *,
76 lldb::ValueObjectSP valobj_sp) {
77 if (!valobj_sp)
78 return nullptr;
79 CompilerType type = valobj_sp->GetCompilerType();
80 if (!type.IsValid())
81 return nullptr;
82 return new LibcxxStdRangesRefViewSyntheticFrontEnd(valobj_sp);
83}
84
85} // namespace formatters
86} // namespace lldb_private
87

source code of lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp