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> GetIndexOfChildWithName(ConstString name) override {
29 if (!m_active)
30 return llvm::createStringErrorV("type has no child named '{0}'", name);
31 if (m_has_value) {
32 if (name == "Value" || name == "$$dereference$$")
33 return 0;
34 } else if (name == "Unexpected") {
35 return 0;
36 }
37 return llvm::createStringErrorV("type has no child named '{0}'", name);
38 }
39
40 llvm::Expected<uint32_t> CalculateNumChildren() override {
41 return m_active ? 1U : 0U;
42 }
43
44 ValueObjectSP GetChildAtIndex(uint32_t idx) override {
45 if (!m_active || idx != 0)
46 return {};
47 return m_active->Clone(m_has_value ? "Value" : "Unexpected");
48 }
49
50 lldb::ChildCacheState Update() override {
51 m_active = nullptr;
52 m_has_value = false;
53 ValueObjectSP ns = m_backend.GetNonSyntheticValue();
54 if (!ns)
56
57 ValueObjectSP has_sp = ns->GetChildMemberWithName("_Has_value");
58 if (!has_sp)
60
61 m_has_value = has_sp->GetValueAsUnsigned(0) != 0;
62 // expected<void, E> has no value child when engaged.
63 m_active =
64 ns->GetChildMemberWithName(m_has_value ? "_Value" : "_Unexpected")
65 .get();
67 }
68
69private:
70 ValueObject *m_active = nullptr;
71 bool m_has_value = false;
72};
73} // namespace
74
76 if (auto valobj_sp = valobj.GetNonSyntheticValue())
77 return valobj_sp->GetChildMemberWithName("_Has_value") != nullptr;
78 return false;
79}
80
82 Stream &stream,
83 const TypeSummaryOptions &) {
85 if (!ns)
86 return false;
87 ValueObjectSP has_sp = ns->GetChildMemberWithName("_Has_value");
88 if (!has_sp)
89 return false;
90 // Keep the summary consistent with std::optional.
91 stream.Printf(" Has Value=%s ",
92 has_sp->GetValueAsUnsigned(0) ? "true" : "false");
93 return true;
94}
95
98 if (valobj_sp)
99 return new MsvcStlExpectedFrontend(*valobj_sp);
100 return nullptr;
101}
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