LLDB mainline
MsvcStlExpected.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "MsvcStl.h"
10
12#include "llvm/Support/ErrorExtras.h"
13
14using namespace lldb;
15using namespace lldb_private;
16using namespace lldb_private::formatters;
17
18namespace {
19
20class MsvcStlExpectedFrontend : public SyntheticChildrenFrontEnd {
21public:
22 MsvcStlExpectedFrontend(ValueObject &valobj)
23 : SyntheticChildrenFrontEnd(valobj) {
24 if (valobj.GetTargetSP())
25 Update();
26 }
27
28 llvm::Expected<size_t>
29 GetIndexOfChildWithName(llvm::StringRef name) override {
30 if (!m_active)
31 return llvm::createStringErrorV("type has no child named '{0}'", name);
32 if (m_has_value) {
33 if (name == "Value" || name == "$$dereference$$")
34 return 0;
35 } else if (name == "Unexpected") {
36 return 0;
37 }
38 return llvm::createStringErrorV("type has no child named '{0}'", name);
39 }
40
41 llvm::Expected<uint32_t> CalculateNumChildren() override {
42 return m_active ? 1U : 0U;
43 }
44
45 ValueObjectSP GetChildAtIndex(uint32_t idx) override {
46 if (!m_active || idx != 0)
47 return {};
48 return m_active->Clone(m_has_value ? "Value" : "Unexpected");
49 }
50
51 lldb::ChildCacheState Update() override {
52 m_active = nullptr;
53 m_has_value = false;
54 ValueObjectSP ns = m_backend.GetNonSyntheticValue();
55 if (!ns)
57
58 ValueObjectSP has_sp = ns->GetChildMemberWithName("_Has_value");
59 if (!has_sp)
61
62 m_has_value = has_sp->GetValueAsUnsigned(0) != 0;
63 // expected<void, E> has no value child when engaged.
64 m_active =
65 ns->GetChildMemberWithName(m_has_value ? "_Value" : "_Unexpected")
66 .get();
68 }
69
70private:
71 ValueObject *m_active = nullptr;
72 bool m_has_value = false;
73};
74} // namespace
75
77 if (auto valobj_sp = valobj.GetNonSyntheticValue())
78 return valobj_sp->GetChildMemberWithName("_Has_value") != nullptr;
79 return false;
80}
81
83 Stream &stream,
84 const TypeSummaryOptions &) {
86 if (!ns)
87 return false;
88 ValueObjectSP has_sp = ns->GetChildMemberWithName("_Has_value");
89 if (!has_sp)
90 return false;
91 // Keep the summary consistent with std::optional.
92 stream.Printf(" Has Value=%s ",
93 has_sp->GetValueAsUnsigned(0) ? "true" : "false");
94 return true;
95}
96
99 if (valobj_sp)
100 return new MsvcStlExpectedFrontend(*valobj_sp);
101 return nullptr;
102}
static std::optional< size_t > CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type)
Calculates the number of elements stored in a container (with element type 'container_elem_type') as ...
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
lldb::TargetSP GetTargetSP() const
virtual lldb::ValueObjectSP GetNonSyntheticValue()
bool IsMsvcStlExpected(ValueObject &valobj)
bool MsvcStlExpectedSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
SyntheticChildrenFrontEnd * MsvcStlExpectedSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
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