LibCxxOptional.cpp revision 360784
1//===-- LibCxxOptional.cpp --------------------------------------*- C++ -*-===// 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 "LibCxx.h" 10#include "lldb/DataFormatters/FormattersHelpers.h" 11 12using namespace lldb; 13using namespace lldb_private; 14 15namespace { 16 17class OptionalFrontEnd : public SyntheticChildrenFrontEnd { 18public: 19 OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) { 20 Update(); 21 } 22 23 size_t GetIndexOfChildWithName(ConstString name) override { 24 return formatters::ExtractIndexFromString(name.GetCString()); 25 } 26 27 bool MightHaveChildren() override { return true; } 28 bool Update() override; 29 size_t CalculateNumChildren() override { return m_size; } 30 ValueObjectSP GetChildAtIndex(size_t idx) override; 31 32private: 33 size_t m_size = 0; 34}; 35} // namespace 36 37bool OptionalFrontEnd::Update() { 38 ValueObjectSP engaged_sp( 39 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)); 40 41 if (!engaged_sp) 42 return false; 43 44 // __engaged_ is a bool flag and is true if the optional contains a value. 45 // Converting it to unsigned gives us a size of 1 if it contains a value 46 // and 0 if not. 47 m_size = engaged_sp->GetValueAsUnsigned(0); 48 49 return false; 50} 51 52ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) { 53 if (idx >= m_size) 54 return ValueObjectSP(); 55 56 // __val_ contains the underlying value of an optional if it has one. 57 // Currently because it is part of an anonymous union GetChildMemberWithName() 58 // does not peer through and find it unless we are at the parent itself. 59 // We can obtain the parent through __engaged_. 60 ValueObjectSP val_sp( 61 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true) 62 ->GetParent() 63 ->GetChildAtIndex(0, true) 64 ->GetChildMemberWithName(ConstString("__val_"), true)); 65 66 if (!val_sp) 67 return ValueObjectSP(); 68 69 CompilerType holder_type = val_sp->GetCompilerType(); 70 71 if (!holder_type) 72 return ValueObjectSP(); 73 74 return val_sp->Clone(ConstString(llvm::formatv("Value").str())); 75} 76 77SyntheticChildrenFrontEnd * 78formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *, 79 lldb::ValueObjectSP valobj_sp) { 80 if (valobj_sp) 81 return new OptionalFrontEnd(*valobj_sp); 82 return nullptr; 83} 84