LLDB mainline
LibCxxSliceArray.cpp
Go to the documentation of this file.
1//===-- LibCxxSliceArray.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
13#include <optional>
14
15using namespace lldb;
16using namespace lldb_private;
17using namespace lldb_private::formatters;
18
19namespace lldb_private {
20namespace formatters {
21
23 const TypeSummaryOptions &options) {
25 if (!obj)
26 return false;
27
28 ValueObjectSP ptr_sp = obj->GetChildMemberWithName("__size_");
29 if (!ptr_sp)
30 return false;
31 const size_t size = ptr_sp->GetValueAsUnsigned(0);
32
33 ptr_sp = obj->GetChildMemberWithName("__stride_");
34 if (!ptr_sp)
35 return false;
36 const size_t stride = ptr_sp->GetValueAsUnsigned(0);
37
38 stream.Printf("stride=%zu size=%zu", stride, size);
39
40 return true;
41}
42
43/// Data formatter for libc++'s std::slice_array.
44///
45/// A slice_array is created by using:
46/// operator[](std::slice slicearr);
47/// and std::slice is created by:
48/// slice(std::size_t start, std::size_t size, std::size_t stride);
49/// The std::slice_array has the following members:
50/// - __vp_ points to std::valarray::__begin_ + @a start
51/// - __size_ is @a size
52/// - __stride_is @a stride
54public:
56
58
59 llvm::Expected<uint32_t> CalculateNumChildren() override;
60
61 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
62
64
65 bool MightHaveChildren() override;
66
67 size_t GetIndexOfChildWithName(ConstString name) override;
68
69private:
70 /// A non-owning pointer to slice_array.__vp_.
71 ValueObject *m_start = nullptr;
72 /// slice_array.__size_.
73 size_t m_size = 0;
74 /// slice_array.__stride_.
75 size_t m_stride = 0;
76 /// The type of slice_array's template argument T.
78 /// The sizeof slice_array's template argument T.
79 uint32_t m_element_size = 0;
80};
81
82} // namespace formatters
83} // namespace lldb_private
84
87 : SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() {
88 if (valobj_sp)
89 Update();
90}
91
94 // these need to stay around because they are child objects who will follow
95 // their parent's life cycle
96 // delete m_start;
97}
98
99llvm::Expected<uint32_t> lldb_private::formatters::
101 return m_size;
102}
103
106 uint32_t idx) {
107 if (!m_start)
108 return lldb::ValueObjectSP();
109
110 uint64_t offset = idx * m_stride * m_element_size;
111 offset = offset + m_start->GetValueAsUnsigned(0);
112 StreamString name;
113 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
114 return CreateValueObjectFromAddress(name.GetString(), offset,
115 m_backend.GetExecutionContextRef(),
116 m_element_type);
117}
118
121 m_start = nullptr;
122
123 CompilerType type = m_backend.GetCompilerType();
124 if (type.GetNumTemplateArguments() == 0)
125 return ChildCacheState::eRefetch;
126
127 m_element_type = type.GetTypeTemplateArgument(0);
128 if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr))
129 m_element_size = *size;
130
131 if (m_element_size == 0)
132 return ChildCacheState::eRefetch;
133
134 ValueObjectSP start = m_backend.GetChildMemberWithName("__vp_");
135 ValueObjectSP size = m_backend.GetChildMemberWithName("__size_");
136 ValueObjectSP stride = m_backend.GetChildMemberWithName("__stride_");
137
138 if (!start || !size || !stride)
139 return ChildCacheState::eRefetch;
140
141 m_start = start.get();
142 m_size = size->GetValueAsUnsigned(0);
143 m_stride = stride->GetValueAsUnsigned(0);
144
145 return ChildCacheState::eRefetch;
146}
147
150 return true;
151}
152
155 if (!m_start)
156 return std::numeric_limits<size_t>::max();
157 return ExtractIndexFromString(name.GetCString());
158}
159
163 if (!valobj_sp)
164 return nullptr;
165 return new LibcxxStdSliceArraySyntheticFrontEnd(valobj_sp);
166}
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:214
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
virtual lldb::ValueObjectSP GetNonSyntheticValue()
Definition: ValueObject.h:582
Data formatter for libc++'s std::slice_array.
CompilerType m_element_type
The type of slice_array's template argument T.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
uint32_t m_element_size
The sizeof slice_array's template argument T.
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
ValueObject * m_start
A non-owning pointer to slice_array.__vp_.
SyntheticChildrenFrontEnd * LibcxxStdSliceArraySyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
size_t ExtractIndexFromString(const char *item_name)
bool LibcxxStdSliceArraySummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:475