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 "llvm/Support/ErrorExtras.h"
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::formatters;
19
20namespace lldb_private {
21namespace formatters {
22
24 const TypeSummaryOptions &options) {
26 if (!obj)
27 return false;
28
29 ValueObjectSP ptr_sp = obj->GetChildMemberWithName("__size_");
30 if (!ptr_sp)
31 return false;
32 const size_t size = ptr_sp->GetValueAsUnsigned(0);
33
34 ptr_sp = obj->GetChildMemberWithName("__stride_");
35 if (!ptr_sp)
36 return false;
37 const size_t stride = ptr_sp->GetValueAsUnsigned(0);
38
39 stream.Printf("stride=%zu size=%zu", stride, size);
40
41 return true;
42}
43
44/// Data formatter for libc++'s std::slice_array.
45///
46/// A slice_array is created by using:
47/// operator[](std::slice slicearr);
48/// and std::slice is created by:
49/// slice(std::size_t start, std::size_t size, std::size_t stride);
50/// The std::slice_array has the following members:
51/// - __vp_ points to std::valarray::__begin_ + @a start
52/// - __size_ is @a size
53/// - __stride_is @a stride
55public:
57
59
60 llvm::Expected<uint32_t> CalculateNumChildren() override;
61
62 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
63
65
66 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
67
68private:
69 /// A non-owning pointer to slice_array.__vp_.
70 ValueObject *m_start = nullptr;
71 /// slice_array.__size_.
72 size_t m_size = 0;
73 /// slice_array.__stride_.
74 size_t m_stride = 0;
75 /// The type of slice_array's template argument T.
77 /// The sizeof slice_array's template argument T.
78 uint32_t m_element_size = 0;
79};
80
81} // namespace formatters
82} // namespace lldb_private
83
90
93 // these need to stay around because they are child objects who will follow
94 // their parent's life cycle
95 // delete m_start;
96}
97
102
105 uint32_t idx) {
106 if (!m_start)
107 return lldb::ValueObjectSP();
108
109 uint64_t offset = idx * m_stride * m_element_size;
110 offset = offset + m_start->GetValueAsUnsigned(0);
111 StreamString name;
112 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
113 return CreateValueObjectFromAddress(name.GetString(), offset,
114 m_backend.GetExecutionContextRef(),
116}
117
120 m_start = nullptr;
121
122 CompilerType type = m_backend.GetCompilerType();
123 if (type.GetNumTemplateArguments() == 0)
125
127 if (std::optional<uint64_t> size =
128 llvm::expectedToOptional(m_element_type.GetByteSize(nullptr)))
129 m_element_size = *size;
130
131 if (m_element_size == 0)
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)
140
141 m_start = start.get();
142 m_size = size->GetValueAsUnsigned(0);
143 m_stride = stride->GetValueAsUnsigned(0);
144
146}
147
148llvm::Expected<size_t>
151 if (!m_start)
152 return llvm::createStringErrorV("type has no child named '{0}'", name);
153 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
154 if (!optional_idx) {
155 return llvm::createStringErrorV("type has no child named '{0}'", name);
156 }
157 return *optional_idx;
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.
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.
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:132
SyntheticChildrenFrontEnd(ValueObject &backend)
lldb::ValueObjectSP CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
virtual lldb::ValueObjectSP GetNonSyntheticValue()
CompilerType m_element_type
The type of slice_array's template argument T.
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
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_.
std::optional< size_t > ExtractIndexFromString(const char *item_name)
bool LibcxxStdSliceArraySummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
SyntheticChildrenFrontEnd * LibcxxStdSliceArraySyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
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