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/ConstString.h" 17 #include "lldb/Core/ValueObject.h" 18 #include "lldb/DataFormatters/FormattersHelpers.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.GetData(), 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 lldb::TemplateArgumentKind kind; 98 m_element_type = m_backend.GetCompilerType().GetTemplateArgument(0, kind); 99 if (kind != lldb::eTemplateArgumentKindType || !m_element_type.IsValid()) 100 return false; 101 102 m_element_size = m_element_type.GetByteSize(nullptr); 103 104 if (m_element_size > 0) 105 m_start = 106 m_backend.GetChildMemberWithName(g___begin_, true) 107 .get(); // store raw pointers or end up with a circular dependency 108 109 return false; 110 } 111 112 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 113 MightHaveChildren() { 114 return true; 115 } 116 117 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd:: 118 GetIndexOfChildWithName(const ConstString &name) { 119 if (!m_start) 120 return UINT32_MAX; 121 return ExtractIndexFromString(name.GetCString()); 122 } 123 124 lldb_private::SyntheticChildrenFrontEnd * 125 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator( 126 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { 127 return (valobj_sp ? new LibcxxInitializerListSyntheticFrontEnd(valobj_sp) 128 : nullptr); 129 } 130