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"
15#include "lldb/Target/Target.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::createStringError("Type has no child named '%s'",
45 name.AsCString());
46 }
47
48 llvm::Expected<uint32_t> CalculateNumChildren() override {
49 return m_has_value ? 1U : 0U;
50 }
51
52 ValueObjectSP GetChildAtIndex(uint32_t idx) override;
53 lldb::ChildCacheState Update() override;
54
55private:
56 bool m_has_value = false;
57 StdLib m_stdlib;
58};
59
60} // namespace
61
62GenericOptionalFrontend::GenericOptionalFrontend(ValueObject &valobj,
63 StdLib stdlib)
64 : SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
65 if (auto target_sp = m_backend.GetTargetSP()) {
66 Update();
67 }
68}
69
70lldb::ChildCacheState GenericOptionalFrontend::Update() {
71 ValueObjectSP engaged_sp;
72
73 if (m_stdlib == StdLib::LibCxx)
74 engaged_sp = m_backend.GetChildMemberWithName("__engaged_");
75 else if (m_stdlib == StdLib::LibStdcpp) {
76 if (ValueObjectSP payload = m_backend.GetChildMemberWithName("_M_payload"))
77 engaged_sp = payload->GetChildMemberWithName("_M_engaged");
78 } else if (m_stdlib == StdLib::MsvcStl)
79 engaged_sp = m_backend.GetChildMemberWithName("_Has_value");
80
81 if (!engaged_sp)
83
84 // _M_engaged/__engaged is a bool flag and is true if the optional contains a
85 // value. Converting it to unsigned gives us a size of 1 if it contains a
86 // value and 0 if not.
87 m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
88
90}
91
92ValueObjectSP GenericOptionalFrontend::GetChildAtIndex(uint32_t _idx) {
93 if (!m_has_value)
94 return ValueObjectSP();
95
96 ValueObjectSP val_sp;
97
98 if (m_stdlib == StdLib::LibCxx)
99 // __val_ contains the underlying value of an optional if it has one.
100 // Currently because it is part of an anonymous union
101 // GetChildMemberWithName() does not peer through and find it unless we are
102 // at the parent itself. We can obtain the parent through __engaged_.
103 val_sp = m_backend.GetChildMemberWithName("__engaged_")
104 ->GetParent()
105 ->GetChildAtIndex(0)
106 ->GetChildMemberWithName("__val_");
107 else if (m_stdlib == StdLib::LibStdcpp) {
108 val_sp = m_backend.GetChildMemberWithName("_M_payload")
109 ->GetChildMemberWithName("_M_payload");
110
111 // In some implementations, _M_value contains the underlying value of an
112 // optional, and in other versions, it's in the payload member.
113 ValueObjectSP candidate = val_sp->GetChildMemberWithName("_M_value");
114 if (candidate)
115 val_sp = candidate;
116 } else if (m_stdlib == StdLib::MsvcStl)
117 // Same issue as with LibCxx
118 val_sp = m_backend.GetChildMemberWithName("_Has_value")
119 ->GetParent()
120 ->GetChildAtIndex(0)
121 ->GetChildMemberWithName("_Value");
122
123 if (!val_sp)
124 return ValueObjectSP();
125
126 CompilerType holder_type = val_sp->GetCompilerType();
127
128 if (!holder_type)
129 return ValueObjectSP();
130
131 return val_sp->Clone(ConstString("Value"));
132}
133
137 if (valobj_sp)
138 return new GenericOptionalFrontend(
139 *valobj_sp, GenericOptionalFrontend::StdLib::LibStdcpp);
140 return nullptr;
141}
142
145 if (valobj_sp)
146 return new GenericOptionalFrontend(*valobj_sp,
147 GenericOptionalFrontend::StdLib::LibCxx);
148 return nullptr;
149}
150
152 if (auto valobj_sp = valobj.GetNonSyntheticValue())
153 return valobj_sp->GetChildMemberWithName("_Has_value") != nullptr;
154 return false;
155}
156
159 if (valobj_sp)
160 return new GenericOptionalFrontend(
161 *valobj_sp, GenericOptionalFrontend::StdLib::MsvcStl);
162 return nullptr;
163}
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
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
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