1 //===-- LibCxxInitializerList.cpp -------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "LibCxx.h"
11
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/DataFormatters/FormattersHelpers.h"
14 #include "lldb/Utility/ConstString.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18 using namespace lldb_private::formatters;
19
20 namespace lldb_private {
21 namespace formatters {
22 class LibcxxInitializerListSyntheticFrontEnd
23 : public SyntheticChildrenFrontEnd {
24 public:
25 LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
26
27 ~LibcxxInitializerListSyntheticFrontEnd() override;
28
29 size_t CalculateNumChildren() override;
30
31 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
32
33 bool Update() override;
34
35 bool MightHaveChildren() override;
36
37 size_t GetIndexOfChildWithName(const ConstString &name) override;
38
39 private:
40 ValueObject *m_start;
41 CompilerType m_element_type;
42 uint32_t m_element_size;
43 size_t m_num_elements;
44 };
45 } // namespace formatters
46 } // namespace lldb_private
47
48 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)49 LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
50 : SyntheticChildrenFrontEnd(*valobj_sp), m_start(nullptr), m_element_type(),
51 m_element_size(0), m_num_elements(0) {
52 if (valobj_sp)
53 Update();
54 }
55
56 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
~LibcxxInitializerListSyntheticFrontEnd()57 ~LibcxxInitializerListSyntheticFrontEnd() {
58 // this needs to stay around because it's a child object who will follow its
59 // parent's life cycle
60 // delete m_start;
61 }
62
63 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
CalculateNumChildren()64 CalculateNumChildren() {
65 static ConstString g___size_("__size_");
66 m_num_elements = 0;
67 ValueObjectSP size_sp(m_backend.GetChildMemberWithName(g___size_, true));
68 if (size_sp)
69 m_num_elements = size_sp->GetValueAsUnsigned(0);
70 return m_num_elements;
71 }
72
73 lldb::ValueObjectSP lldb_private::formatters::
GetChildAtIndex(size_t idx)74 LibcxxInitializerListSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
75 if (!m_start)
76 return lldb::ValueObjectSP();
77
78 uint64_t offset = idx * m_element_size;
79 offset = offset + m_start->GetValueAsUnsigned(0);
80 StreamString name;
81 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
82 return CreateValueObjectFromAddress(name.GetString(), offset,
83 m_backend.GetExecutionContextRef(),
84 m_element_type);
85 }
86
87 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
Update()88 Update() {
89 static ConstString g___begin_("__begin_");
90
91 m_start = nullptr;
92 m_num_elements = 0;
93 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
94 if (!m_element_type.IsValid())
95 return false;
96
97 if (llvm::Optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
98 m_element_size = *size;
99 // Store raw pointers or end up with a circular dependency.
100 m_start = m_backend.GetChildMemberWithName(g___begin_, true).get();
101 }
102
103 return false;
104 }
105
106 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
MightHaveChildren()107 MightHaveChildren() {
108 return true;
109 }
110
111 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
GetIndexOfChildWithName(const ConstString & name)112 GetIndexOfChildWithName(const ConstString &name) {
113 if (!m_start)
114 return UINT32_MAX;
115 return ExtractIndexFromString(name.GetCString());
116 }
117
118 lldb_private::SyntheticChildrenFrontEnd *
LibcxxInitializerListSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)119 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator(
120 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
121 return (valobj_sp ? new LibcxxInitializerListSyntheticFrontEnd(valobj_sp)
122 : nullptr);
123 }
124