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 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
66
67private:
68 /// A non-owning pointer to slice_array.__vp_.
69 ValueObject *m_start = nullptr;
70 /// slice_array.__size_.
71 size_t m_size = 0;
72 /// slice_array.__stride_.
73 size_t m_stride = 0;
74 /// The type of slice_array's template argument T.
76 /// The sizeof slice_array's template argument T.
77 uint32_t m_element_size = 0;
78};
79
80} // namespace formatters
81} // namespace lldb_private
82
89
92 // these need to stay around because they are child objects who will follow
93 // their parent's life cycle
94 // delete m_start;
95}
96
101
104 uint32_t idx) {
105 if (!m_start)
106 return lldb::ValueObjectSP();
107
108 uint64_t offset = idx * m_stride * m_element_size;
109 offset = offset + m_start->GetValueAsUnsigned(0);
110 StreamString name;
111 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
112 return CreateValueObjectFromAddress(name.GetString(), offset,
113 m_backend.GetExecutionContextRef(),
115}
116
119 m_start = nullptr;
120
121 CompilerType type = m_backend.GetCompilerType();
122 if (type.GetNumTemplateArguments() == 0)
124
126 if (std::optional<uint64_t> size =
127 llvm::expectedToOptional(m_element_type.GetByteSize(nullptr)))
128 m_element_size = *size;
129
130 if (m_element_size == 0)
132
133 ValueObjectSP start = m_backend.GetChildMemberWithName("__vp_");
134 ValueObjectSP size = m_backend.GetChildMemberWithName("__size_");
135 ValueObjectSP stride = m_backend.GetChildMemberWithName("__stride_");
136
137 if (!start || !size || !stride)
139
140 m_start = start.get();
141 m_size = size->GetValueAsUnsigned(0);
142 m_stride = stride->GetValueAsUnsigned(0);
143
145}
146
147llvm::Expected<size_t>
150 if (!m_start)
151 return llvm::createStringError("Type has no child named '%s'",
152 name.AsCString());
153 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
154 if (!optional_idx) {
155 return llvm::createStringError("Type has no child named '%s'",
156 name.AsCString());
157 }
158 return *optional_idx;
159}
160
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 * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
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:134
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
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)
std::optional< 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.
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