LLDB mainline
LibCxxInitializerList.cpp
Go to the documentation of this file.
1//===-- LibCxxInitializerList.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
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::formatters;
19
20namespace lldb_private {
21namespace formatters {
24public:
26
28
29 llvm::Expected<uint32_t> CalculateNumChildren() override;
30
31 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
32
34
35 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
36
37private:
38 ValueObject *m_start = nullptr;
40 uint32_t m_element_size = 0;
41 size_t m_num_elements = 0;
42};
43} // namespace formatters
44} // namespace lldb_private
45
52
55 // this needs to stay around because it's a child object who will follow its
56 // parent's life cycle
57 // delete m_start;
58}
59
60llvm::Expected<uint32_t> lldb_private::formatters::
63 ValueObjectSP size_sp(m_backend.GetChildMemberWithName("__size_"));
64 if (size_sp)
65 m_num_elements = size_sp->GetValueAsUnsigned(0);
66 return m_num_elements;
67}
68
71 if (!m_start)
72 return lldb::ValueObjectSP();
73
74 uint64_t offset = idx * m_element_size;
75 offset = offset + m_start->GetValueAsUnsigned(0);
76 StreamString name;
77 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
78 return CreateValueObjectFromAddress(name.GetString(), offset,
79 m_backend.GetExecutionContextRef(),
81}
82
85 m_start = nullptr;
87 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
88 if (!m_element_type.IsValid())
90
91 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
92 if (!size_or_err)
93 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
94 "{0}");
95 else {
96 m_element_size = *size_or_err;
97 // Store raw pointers or end up with a circular dependency.
98 m_start = m_backend.GetChildMemberWithName("__begin_").get();
99 }
100
102}
103
104llvm::Expected<size_t>
107 if (!m_start) {
108 return llvm::createStringError("Type has no child named '%s'",
109 name.AsCString());
110 }
111 auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
112 if (!optional_idx) {
113 return llvm::createStringError("Type has no child named '%s'",
114 name.AsCString());
115 }
116 return *optional_idx;
117}
118
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:408
Generic representation of a type in a programming language.
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)
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...
std::optional< size_t > ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxInitializerListSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
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