1 //===-- LibCxxInitializerList.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 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "LibCxx.h" 15 16 #include "lldb/Core/ValueObject.h" 17 #include "lldb/DataFormatters/FormattersHelpers.h" 18 #include "lldb/Utility/ConstString.h" 19 20 using namespace lldb; 21 using namespace lldb_private; 22 using namespace lldb_private::formatters; 23 24 namespace lldb_private { 25 namespace formatters { 26 class LibcxxInitializerListSyntheticFrontEnd 27 : public SyntheticChildrenFrontEnd { 28 public: 29 LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp); 30 31 ~LibcxxInitializerListSyntheticFrontEnd() override; 32 33 size_t CalculateNumChildren() override; 34 35 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override; 36 37 bool Update() override; 38 39 bool MightHaveChildren() override; 40 41 size_t GetIndexOfChildWithName(const ConstString &name) override; 42 43 private: 44 ValueObject *m_start; 45 CompilerType m_element_type; 46 uint32_t m_element_size; 47 size_t m_num_elements; 48 }; 49 } // namespace formatters 50 } // namespace lldb_private 51 52 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 53 LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) 54 : SyntheticChildrenFrontEnd(*valobj_sp), m_start(nullptr), m_element_type(), 55 m_element_size(0), m_num_elements(0) { 56 if (valobj_sp) 57 Update(); 58 } 59 60 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 61 ~LibcxxInitializerListSyntheticFrontEnd() { 62 // this needs to stay around because it's a child object who will follow its 63 // parent's life cycle 64 // delete m_start; 65 } 66 67 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 68 CalculateNumChildren() { 69 static ConstString g___size_("__size_"); 70 m_num_elements = 0; 71 ValueObjectSP size_sp(m_backend.GetChildMemberWithName(g___size_, true)); 72 if (size_sp) 73 m_num_elements = size_sp->GetValueAsUnsigned(0); 74 return m_num_elements; 75 } 76 77 lldb::ValueObjectSP lldb_private::formatters:: 78 LibcxxInitializerListSyntheticFrontEnd::GetChildAtIndex(size_t idx) { 79 if (!m_start) 80 return lldb::ValueObjectSP(); 81 82 uint64_t offset = idx * m_element_size; 83 offset = offset + m_start->GetValueAsUnsigned(0); 84 StreamString name; 85 name.Printf("[%" PRIu64 "]", (uint64_t)idx); 86 return CreateValueObjectFromAddress(name.GetString(), offset, 87 m_backend.GetExecutionContextRef(), 88 m_element_type); 89 } 90 91 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 92 Update() { 93 static ConstString g___begin_("__begin_"); 94 95 m_start = nullptr; 96 m_num_elements = 0; 97 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0); 98 if (!m_element_type.IsValid()) 99 return false; 100 101 m_element_size = m_element_type.GetByteSize(nullptr); 102 103 if (m_element_size > 0) 104 m_start = 105 m_backend.GetChildMemberWithName(g___begin_, true) 106 .get(); // store raw pointers or end up with a circular dependency 107 108 return false; 109 } 110 111 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 112 MightHaveChildren() { 113 return true; 114 } 115 116 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 117 GetIndexOfChildWithName(const ConstString &name) { 118 if (!m_start) 119 return UINT32_MAX; 120 return ExtractIndexFromString(name.GetCString()); 121 } 122 123 lldb_private::SyntheticChildrenFrontEnd * 124 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator( 125 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { 126 return (valobj_sp ? new LibcxxInitializerListSyntheticFrontEnd(valobj_sp) 127 : nullptr); 128 } 129