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 "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 {
23public:
25
27
28 llvm::Expected<uint32_t> CalculateNumChildren() override;
29
30 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
31
33
34 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
35
36private:
37 /// A non-owning pointer to valarray's __begin_ member.
38 ValueObject *m_start = nullptr;
39 /// A non-owning pointer to valarray's __end_ member.
41 /// The type of valarray's template argument T.
43 /// The sizeof valarray's template argument T.
44 uint32_t m_element_size = 0;
45};
46
47} // namespace formatters
48} // namespace lldb_private
49
56
59 // these need to stay around because they are child objects who will follow
60 // their parent's life cycle
61 // delete m_start;
62 // delete m_finish;
63}
64
65llvm::Expected<uint32_t> lldb_private::formatters::
67 if (!m_start || !m_finish)
68 return 0;
69 uint64_t start_val = m_start->GetValueAsUnsigned(0);
70 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
71
72 if (start_val == 0 || finish_val == 0)
73 return 0;
74
75 if (start_val >= finish_val)
76 return 0;
77
78 size_t num_children = (finish_val - start_val);
79 if (num_children % m_element_size)
80 return 0;
81 return num_children / m_element_size;
82}
83
86 uint32_t idx) {
87 if (!m_start || !m_finish)
88 return lldb::ValueObjectSP();
89
90 uint64_t offset = idx * m_element_size;
91 offset = offset + m_start->GetValueAsUnsigned(0);
92 StreamString name;
93 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
94 return CreateValueObjectFromAddress(name.GetString(), offset,
95 m_backend.GetExecutionContextRef(),
97}
98
101 m_start = m_finish = nullptr;
102
103 CompilerType type = m_backend.GetCompilerType();
104 if (type.GetNumTemplateArguments() == 0)
106
108 if (std::optional<uint64_t> size =
109 llvm::expectedToOptional(m_element_type.GetByteSize(nullptr)))
110 m_element_size = *size;
111
112 if (m_element_size == 0)
114
115 ValueObjectSP start = m_backend.GetChildMemberWithName("__begin_");
116 ValueObjectSP finish = m_backend.GetChildMemberWithName("__end_");
117
118 if (!start || !finish)
120
121 m_start = start.get();
122 m_finish = finish.get();
123
125}
126
127llvm::Expected<size_t>
130 if (!m_start || !m_finish)
131 return llvm::createStringErrorV("type has no child named '{0}'", name);
132 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
133 if (!optional_idx) {
134 return llvm::createStringErrorV("type has no child named '{0}'", name);
135 }
136 return *optional_idx;
137}
138
142 if (!valobj_sp)
143 return nullptr;
144 return new LibcxxStdValarraySyntheticFrontEnd(valobj_sp);
145}
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
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)
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
Determine the index of a named child.
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