LLDB mainline
LibCxxValarray.cpp
Go to the documentation of this file.
1//===-- LibCxxValarray.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 {
22public:
24
26
27 llvm::Expected<uint32_t> CalculateNumChildren() override;
28
29 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
30
32
33 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
34
35private:
36 /// A non-owning pointer to valarray's __begin_ member.
37 ValueObject *m_start = nullptr;
38 /// A non-owning pointer to valarray's __end_ member.
40 /// The type of valarray's template argument T.
42 /// The sizeof valarray's template argument T.
43 uint32_t m_element_size = 0;
44};
45
46} // namespace formatters
47} // namespace lldb_private
48
55
58 // these need to stay around because they are child objects who will follow
59 // their parent's life cycle
60 // delete m_start;
61 // delete m_finish;
62}
63
64llvm::Expected<uint32_t> lldb_private::formatters::
66 if (!m_start || !m_finish)
67 return 0;
68 uint64_t start_val = m_start->GetValueAsUnsigned(0);
69 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
70
71 if (start_val == 0 || finish_val == 0)
72 return 0;
73
74 if (start_val >= finish_val)
75 return 0;
76
77 size_t num_children = (finish_val - start_val);
78 if (num_children % m_element_size)
79 return 0;
80 return num_children / m_element_size;
81}
82
85 uint32_t idx) {
86 if (!m_start || !m_finish)
87 return lldb::ValueObjectSP();
88
89 uint64_t offset = idx * m_element_size;
90 offset = offset + m_start->GetValueAsUnsigned(0);
91 StreamString name;
92 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
93 return CreateValueObjectFromAddress(name.GetString(), offset,
94 m_backend.GetExecutionContextRef(),
96}
97
100 m_start = m_finish = nullptr;
101
102 CompilerType type = m_backend.GetCompilerType();
103 if (type.GetNumTemplateArguments() == 0)
105
107 if (std::optional<uint64_t> size =
108 llvm::expectedToOptional(m_element_type.GetByteSize(nullptr)))
109 m_element_size = *size;
110
111 if (m_element_size == 0)
113
114 ValueObjectSP start = m_backend.GetChildMemberWithName("__begin_");
115 ValueObjectSP finish = m_backend.GetChildMemberWithName("__end_");
116
117 if (!start || !finish)
119
120 m_start = start.get();
121 m_finish = finish.get();
122
124}
125
126llvm::Expected<size_t>
129 if (!m_start || !m_finish)
130 return llvm::createStringError("Type has no child named '%s'",
131 name.AsCString());
132 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
133 if (!optional_idx) {
134 return llvm::createStringError("Type has no child named '%s'",
135 name.AsCString());
136 }
137 return *optional_idx;
138}
139
143 if (!valobj_sp)
144 return nullptr;
145 return new LibcxxStdValarraySyntheticFrontEnd(valobj_sp);
146}
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
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)
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 valarray's template argument T.
ValueObject * m_finish
A non-owning pointer to valarray's __end_ member.
ValueObject * m_start
A non-owning pointer to valarray's __begin_ member.
CompilerType m_element_type
The type of valarray's template argument T.
llvm::Expected< uint32_t > CalculateNumChildren() override
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxStdValarraySyntheticFrontEndCreator(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