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"
14#include "lldb/Target/Target.h"
15
16using namespace lldb;
17using namespace lldb_private;
18
20 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
21 stream.Printf(" Has Value=%s ",
22 valobj.GetNumChildrenIgnoringErrors() == 0 ? "false" : "true");
23
24 return true;
25}
26
27// Synthetic Children Provider
28namespace {
29
30class GenericOptionalFrontend : public SyntheticChildrenFrontEnd {
31public:
32 enum class StdLib {
33 LibCxx,
34 LibStdcpp,
35 };
36
37 GenericOptionalFrontend(ValueObject &valobj, StdLib stdlib);
38
39 size_t GetIndexOfChildWithName(ConstString name) override {
41 }
42
43 bool MightHaveChildren() override { return true; }
44 llvm::Expected<uint32_t> CalculateNumChildren() override {
45 return m_has_value ? 1U : 0U;
46 }
47
48 ValueObjectSP GetChildAtIndex(uint32_t idx) override;
50
51private:
52 bool m_has_value = false;
53 StdLib m_stdlib;
54};
55
56} // namespace
57
58GenericOptionalFrontend::GenericOptionalFrontend(ValueObject &valobj,
59 StdLib stdlib)
60 : SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
61 if (auto target_sp = m_backend.GetTargetSP()) {
62 Update();
63 }
64}
65
66lldb::ChildCacheState GenericOptionalFrontend::Update() {
67 ValueObjectSP engaged_sp;
68
69 if (m_stdlib == StdLib::LibCxx)
70 engaged_sp = m_backend.GetChildMemberWithName("__engaged_");
71 else if (m_stdlib == StdLib::LibStdcpp)
72 engaged_sp = m_backend.GetChildMemberWithName("_M_payload")
73 ->GetChildMemberWithName("_M_engaged");
74
75 if (!engaged_sp)
77
78 // _M_engaged/__engaged is a bool flag and is true if the optional contains a
79 // value. Converting it to unsigned gives us a size of 1 if it contains a
80 // value and 0 if not.
81 m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
82
84}
85
86ValueObjectSP GenericOptionalFrontend::GetChildAtIndex(uint32_t _idx) {
87 if (!m_has_value)
88 return ValueObjectSP();
89
90 ValueObjectSP val_sp;
91
92 if (m_stdlib == StdLib::LibCxx)
93 // __val_ contains the underlying value of an optional if it has one.
94 // Currently because it is part of an anonymous union
95 // GetChildMemberWithName() does not peer through and find it unless we are
96 // at the parent itself. We can obtain the parent through __engaged_.
97 val_sp = m_backend.GetChildMemberWithName("__engaged_")
98 ->GetParent()
99 ->GetChildAtIndex(0)
100 ->GetChildMemberWithName("__val_");
101 else if (m_stdlib == StdLib::LibStdcpp) {
102 val_sp = m_backend.GetChildMemberWithName("_M_payload")
103 ->GetChildMemberWithName("_M_payload");
104
105 // In some implementations, _M_value contains the underlying value of an
106 // optional, and in other versions, it's in the payload member.
107 ValueObjectSP candidate = val_sp->GetChildMemberWithName("_M_value");
108 if (candidate)
109 val_sp = candidate;
110 }
111
112 if (!val_sp)
113 return ValueObjectSP();
114
115 CompilerType holder_type = val_sp->GetCompilerType();
116
117 if (!holder_type)
118 return ValueObjectSP();
119
120 return val_sp->Clone(ConstString("Value"));
121}
122
126 if (valobj_sp)
127 return new GenericOptionalFrontend(
128 *valobj_sp, GenericOptionalFrontend::StdLib::LibStdcpp);
129 return nullptr;
130}
131
134 if (valobj_sp)
135 return new GenericOptionalFrontend(*valobj_sp,
136 GenericOptionalFrontend::StdLib::LibCxx);
137 return nullptr;
138}
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:214
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 GetChildAtIndex(uint32_t idx)=0
virtual lldb::ChildCacheState Update()=0
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
virtual size_t GetIndexOfChildWithName(ConstString name)=0
virtual llvm::Expected< uint32_t > CalculateNumChildren()=0
uint32_t GetNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
Like GetNumChildren but returns 0 on error.
SyntheticChildrenFrontEnd * LibStdcppOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
size_t ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxOptionalSyntheticFrontEndCreator(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.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
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
Definition: lldb-forward.h:472