Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
CxxStringTypes.cpp
Generic.cpp
GenericBitset.cpp
GenericInitializerList.cpp
GenericOptional.cpp
LibCxx.cpp
LibCxxAtomic.cpp
LibCxxInitializerList.cpp
LibCxxList.cpp
LibCxxMap.cpp
LibCxxQueue.cpp
Expand Down
16 changes: 11 additions & 5 deletions lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,11 +840,6 @@ static void LoadLibCxxFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
"libc++ std::unordered containers synthetic children",
"^std::__[[:alnum:]]+::unordered_(multi)?(map|set)<.+> >$",
stl_synth_flags, true);
AddCXXSynthetic(
cpp_category_sp,
lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator,
"libc++ std::initializer_list synthetic children",
"^std::initializer_list<.+>$", stl_synth_flags, true);
AddCXXSynthetic(cpp_category_sp, LibcxxQueueFrontEndCreator,
"libc++ std::queue synthetic children",
"^std::__[[:alnum:]]+::queue<.+>$", stl_synth_flags, true);
Expand Down Expand Up @@ -1542,6 +1537,14 @@ static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
},
"MSVC STL/libstdc++ std::wstring summary provider"));

// NOTE: it is loaded as a common formatter because the libc++ version is not
// in the `__1` namespace, hence we need to dispatch based on the class
// layout.
AddCXXSynthetic(cpp_category_sp,
GenericInitializerListSyntheticFrontEndCreator,
"std::initializer_list synthetic children",
"^std::initializer_list<.+>$", stl_synth_flags, true);

stl_summary_flags.SetDontShowChildren(false);
stl_summary_flags.SetSkipPointers(false);

Expand All @@ -1555,6 +1558,9 @@ static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
"std::unique_ptr synthetic children",
"^std::unique_ptr<.+>(( )?&)?$", stl_synth_flags, true);

AddCXXSummary(cpp_category_sp, ContainerSizeSummaryProvider,
"std::initializer_list summary provider",
"^std::initializer_list<.+>$", stl_summary_flags, true);
AddCXXSummary(cpp_category_sp, GenericSmartPointerSummaryProvider,
"MSVC STL/libstdc++ std::shared_ptr summary provider",
"^std::shared_ptr<.+>(( )?&)?$", stl_summary_flags, true);
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Plugins/Language/CPlusPlus/Generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ bool GenericOptionalSummaryProvider(ValueObject &valobj, Stream &stream,
lldb::ValueObjectSP GetDesugaredSmartPointerValue(ValueObject &ptr,
ValueObject &container);

SyntheticChildrenFrontEnd *
GenericInitializerListSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP valobj_sp);
} // namespace formatters
} // namespace lldb_private

Expand Down
145 changes: 145 additions & 0 deletions lldb/source/Plugins/Language/CPlusPlus/GenericInitializerList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//===-- GenericInitializerList.cpp ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/ValueObject/ValueObject.h"
#include <cstddef>
#include <optional>
#include <type_traits>

using namespace lldb;
using namespace lldb_private;

namespace generic_check {
template <class T>
using size_func = decltype(T::GetSizeMember(std::declval<ValueObject &>()));
template <class T>
using start_func = decltype(T::GetStartMember(std::declval<ValueObject &>()));
namespace {
template <typename...> struct check_func : std::true_type {};
} // namespace

template <typename T>
using has_functions = check_func<size_func<T>, start_func<T>>;
} // namespace generic_check

struct LibCxx {
static ValueObjectSP GetStartMember(ValueObject &backend) {
return backend.GetChildMemberWithName("__begin_");
}

static ValueObjectSP GetSizeMember(ValueObject &backend) {
return backend.GetChildMemberWithName("__size_");
}
};

struct LibStdcpp {
static ValueObjectSP GetStartMember(ValueObject &backend) {
return backend.GetChildMemberWithName("_M_array");
}

static ValueObjectSP GetSizeMember(ValueObject &backend) {
return backend.GetChildMemberWithName("_M_len");
}
};

namespace lldb_private::formatters {

template <class StandardImpl>
class GenericInitializerListSyntheticFrontEnd
: public SyntheticChildrenFrontEnd {
public:
static_assert(generic_check::has_functions<StandardImpl>::value,
"Missing Required Functions.");

GenericInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
: SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() {
if (valobj_sp)
Update();
}

~GenericInitializerListSyntheticFrontEnd() override {
// this needs to stay around because it's a child object who will follow its
// parent's life cycle
// delete m_start;
}

llvm::Expected<uint32_t> CalculateNumChildren() override {
m_num_elements = 0;

const ValueObjectSP size_sp(StandardImpl::GetSizeMember(m_backend));
if (size_sp)
m_num_elements = size_sp->GetValueAsUnsigned(0);
return m_num_elements;
}

lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
if (!m_start)
return {};

uint64_t offset = static_cast<uint64_t>(idx) * m_element_size;
offset = offset + m_start->GetValueAsUnsigned(0);
StreamString name;
name.Printf("[%" PRIu64 "]", (uint64_t)idx);
return CreateValueObjectFromAddress(name.GetString(), offset,
m_backend.GetExecutionContextRef(),
m_element_type);
}

lldb::ChildCacheState Update() override {
m_start = nullptr;
m_num_elements = 0;
m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
if (!m_element_type.IsValid())
return lldb::ChildCacheState::eRefetch;

llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
if (!size_or_err)
LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
"{0}");
else {
m_element_size = *size_or_err;
// Store raw pointers or end up with a circular dependency.
m_start = StandardImpl::GetStartMember(m_backend).get();
}

return lldb::ChildCacheState::eRefetch;
}

llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
if (!m_start) {
return llvm::createStringError("Type has no child named '%s'",
name.AsCString());
}
auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
if (!optional_idx) {
return llvm::createStringError("Type has no child named '%s'",
name.AsCString());
}
return *optional_idx;
}

private:
ValueObject *m_start = nullptr;
CompilerType m_element_type;
uint32_t m_element_size = 0;
size_t m_num_elements = 0;
};

SyntheticChildrenFrontEnd *GenericInitializerListSyntheticFrontEndCreator(
CXXSyntheticChildren * /*unused*/, lldb::ValueObjectSP valobj_sp) {
if (!valobj_sp)
return nullptr;

if (LibCxx::GetStartMember(*valobj_sp) != nullptr)
return new GenericInitializerListSyntheticFrontEnd<LibCxx>(valobj_sp);

return new GenericInitializerListSyntheticFrontEnd<LibStdcpp>(valobj_sp);
}
} // namespace lldb_private::formatters
4 changes: 0 additions & 4 deletions lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ SyntheticChildrenFrontEnd *
LibCxxUnorderedMapIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP);

SyntheticChildrenFrontEnd *
LibcxxInitializerListSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP);

SyntheticChildrenFrontEnd *LibcxxQueueFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP);

Expand Down
124 changes: 0 additions & 124 deletions lldb/source/Plugins/Language/CPlusPlus/LibCxxInitializerList.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ def do_test(self):
substrs=["stopped", "stop reason = breakpoint"],
)

self.expect("frame variable ili", substrs=["[1] = 2", "[4] = 5"])
self.expect(
"frame variable ili",
substrs=["ili = size=5", "[0] = 1", "[1] = 2", "[4] = 5"],
)
self.expect(
"frame variable ils",
substrs=['[4] = "surprise it is a long string!! yay!!"'],
substrs=[
"ils = size=5",
'[0] = "1"',
'[4] = "surprise it is a long string!! yay!!"',
],
)

@add_test_categories(["libc++"])
def test_libcxx(self):
self.build(dictionary={"USE_LIBCPP": 1})
self.do_test()

@add_test_categories(["libstdcxx"])
def test_libstdcpp(self):
self.build(dictionary={"USE_LIBSTDCPP": 1})
self.do_test()
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <initializer_list>
#include <string>
#include <vector>

int main() {
std::initializer_list<int> ili{1, 2, 3, 4, 5};
Expand Down