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