LLDB mainline
GenericOptional.cpp
Go to the documentation of this file.
1//===-- GenericOptional.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 "Generic.h"
10#include "LibCxx.h"
11#include "LibStdcpp.h"
12#include "MsvcStl.h"
14#include "lldb/Target/Target.h"
15#include "llvm/Support/ErrorExtras.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
21 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
22 stream.Printf(" Has Value=%s ",
23 valobj.GetNumChildrenIgnoringErrors() == 0 ? "false" : "true");
24
25 return true;
26}
27
28// Synthetic Children Provider
29namespace {
30
31class GenericOptionalFrontend : public SyntheticChildrenFrontEnd {
32public:
33 enum class StdLib {
34 LibCxx,
36 MsvcStl,
37 };
38
39 GenericOptionalFrontend(ValueObject &valobj, StdLib stdlib);
40
41 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
42 if (name == "$$dereference$$")
43 return 0;
44 return llvm::createStringErrorV("type has no child named '{0}'", name);
45 }
46
47 llvm::Expected<uint32_t> CalculateNumChildren() override {
48 return m_has_value ? 1U : 0U;
49 }
50
51 ValueObjectSP GetChildAtIndex(uint32_t idx) override;
52 lldb::ChildCacheState Update() override;
53
54private:
55 bool m_has_value = false;
56 StdLib m_stdlib;
57};
58
59} // namespace
60
61GenericOptionalFrontend::GenericOptionalFrontend(ValueObject &valobj,
62 StdLib stdlib)
63 : SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
64 if (auto target_sp = m_backend.GetTargetSP()) {
65 Update();
66 }
67}
68
69lldb::ChildCacheState GenericOptionalFrontend::Update() {
70 ValueObjectSP engaged_sp;
71
72 if (m_stdlib == StdLib::LibCxx)
73 engaged_sp = m_backend.GetChildMemberWithName("__engaged_");
74 else if (m_stdlib == StdLib::LibStdcpp) {
75 if (ValueObjectSP payload = m_backend.GetChildMemberWithName("_M_payload"))
76 engaged_sp = payload->GetChildMemberWithName("_M_engaged");
77 } else if (m_stdlib == StdLib::MsvcStl)
78 engaged_sp = m_backend.GetChildMemberWithName("_Has_value");
79
80 if (!engaged_sp)
82
83 // _M_engaged/__engaged is a bool flag and is true if the optional contains a
84 // value. Converting it to unsigned gives us a size of 1 if it contains a
85 // value and 0 if not.
86 m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
87
89}
90
91ValueObjectSP GenericOptionalFrontend::GetChildAtIndex(uint32_t _idx) {
92 if (!m_has_value)
93 return ValueObjectSP();
94
95 ValueObjectSP val_sp;
96
97 if (m_stdlib == StdLib::LibCxx)
98 // __val_ contains the underlying value of an optional if it has one.
99 // Currently because it is part of an anonymous union
100 // GetChildMemberWithName() does not peer through and find it unless we are
101 // at the parent itself. We can obtain the parent through __engaged_.
102 val_sp = m_backend.GetChildMemberWithName("__engaged_")
103 ->GetParent()
104 ->GetChildAtIndex(0)
105 ->GetChildMemberWithName("__val_");
106 else if (m_stdlib == StdLib::LibStdcpp) {
107 val_sp = m_backend.GetChildMemberWithName("_M_payload")
108 ->GetChildMemberWithName("_M_payload");
109
110 // In some implementations, _M_value contains the underlying value of an
111 // optional, and in other versions, it's in the payload member.
112 ValueObjectSP candidate = val_sp->GetChildMemberWithName("_M_value");
113 if (candidate)
114 val_sp = candidate;
115 } else if (m_stdlib == StdLib::MsvcStl)
116 // Same issue as with LibCxx
117 val_sp = m_backend.GetChildMemberWithName("_Has_value")
118 ->GetParent()
119 ->GetChildAtIndex(0)
120 ->GetChildMemberWithName("_Value");
121
122 if (!val_sp)
123 return ValueObjectSP();
124
125 CompilerType holder_type = val_sp->GetCompilerType();
126
127 if (!holder_type)
128 return ValueObjectSP();
129
130 return val_sp->Clone("Value");
131}
132
136 if (valobj_sp)
137 return new GenericOptionalFrontend(
138 *valobj_sp, GenericOptionalFrontend::StdLib::LibStdcpp);
139 return nullptr;
140}
141
144 if (valobj_sp)
145 return new GenericOptionalFrontend(*valobj_sp,
146 GenericOptionalFrontend::StdLib::LibCxx);
147 return nullptr;
148}
149
151 if (auto valobj_sp = valobj.GetNonSyntheticValue())
152 return valobj_sp->GetChildMemberWithName("_Has_value") != nullptr;
153 return false;
154}
155
158 if (valobj_sp)
159 return new GenericOptionalFrontend(
160 *valobj_sp, GenericOptionalFrontend::StdLib::MsvcStl);
161 return nullptr;
162}
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 ...
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
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
virtual lldb::ValueObjectSP GetNonSyntheticValue()
uint32_t GetNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
Like GetNumChildren but returns 0 on error.
SyntheticChildrenFrontEnd * LibStdcppOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
bool IsMsvcStlOptional(ValueObject &valobj)
SyntheticChildrenFrontEnd * LibcxxOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
SyntheticChildrenFrontEnd * MsvcStlOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
bool GenericOptionalSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
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