Skip to content

Commit 24cf7b3

Browse files
committed
[lldb] add libstdcpp span formatter
1 parent 5407e62 commit 24cf7b3

File tree

5 files changed

+145
-7
lines changed

5 files changed

+145
-7
lines changed

lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
3131
LibCxxValarray.cpp
3232
LibCxxVector.cpp
3333
LibStdcpp.cpp
34+
LibStdcppSpan.cpp
3435
LibStdcppTuple.cpp
3536
LibStdcppUniquePointer.cpp
3637
MsvcStl.cpp

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,10 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
14111411
stl_synth_flags,
14121412
"lldb.formatters.cpp.gnu_libstdcpp.StdForwardListSynthProvider")));
14131413

1414+
AddCXXSynthetic(cpp_category_sp, LibStdcppSpanSyntheticFrontEndCreator,
1415+
"libstdc++ std::span synthetic children", "^std::span<.+>$",
1416+
stl_deref_flags, true);
1417+
14141418
stl_summary_flags.SetDontShowChildren(false);
14151419
stl_summary_flags.SetSkipPointers(false);
14161420

@@ -1501,6 +1505,11 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
15011505
lldb_private::formatters::StdlibCoroutineHandleSummaryProvider,
15021506
"libstdc++ std::coroutine_handle summary provider",
15031507
libstdcpp_std_coroutine_handle_regex, stl_summary_flags, true);
1508+
1509+
AddCXXSummary(cpp_category_sp,
1510+
lldb_private::formatters::ContainerSizeSummaryProvider,
1511+
"libstdc++ std::span summary provider", "^std::span<.+>$",
1512+
stl_summary_flags, true);
15041513
}
15051514

15061515
static lldb_private::SyntheticChildrenFrontEnd *

lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ SyntheticChildrenFrontEnd *
3737
LibstdcppMapIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,
3838
lldb::ValueObjectSP);
3939

40+
SyntheticChildrenFrontEnd *
41+
LibStdcppSpanSyntheticFrontEndCreator(CXXSyntheticChildren *,
42+
lldb::ValueObjectSP);
43+
4044
SyntheticChildrenFrontEnd *
4145
LibStdcppTupleSyntheticFrontEndCreator(CXXSyntheticChildren *,
4246
lldb::ValueObjectSP);
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===---------------------------------------------------------------------===//
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 "LibStdcpp.h"
10+
11+
#include "lldb/DataFormatters/FormattersHelpers.h"
12+
#include "lldb/Utility/ConstString.h"
13+
#include "lldb/ValueObject/ValueObject.h"
14+
#include "llvm/ADT/APSInt.h"
15+
#include "llvm/Support/Error.h"
16+
#include <cstddef>
17+
#include <optional>
18+
19+
using namespace lldb;
20+
21+
namespace lldb_private::formatters {
22+
23+
class LibStdcppSpanSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
24+
public:
25+
LibStdcppSpanSyntheticFrontEnd(const lldb::ValueObjectSP &valobj_sp)
26+
: SyntheticChildrenFrontEnd(*valobj_sp) {
27+
if (valobj_sp)
28+
Update();
29+
}
30+
31+
~LibStdcppSpanSyntheticFrontEnd() override = default;
32+
33+
llvm::Expected<uint32_t> CalculateNumChildren() override {
34+
return m_num_elements;
35+
}
36+
37+
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
38+
if (!m_start)
39+
return {};
40+
41+
uint64_t offset = (static_cast<uint64_t>(idx) * m_element_size);
42+
offset += m_start->GetValueAsUnsigned(0);
43+
const std::string name = llvm::formatv("[{0}]", idx);
44+
return CreateValueObjectFromAddress(
45+
name, offset, m_backend.GetExecutionContextRef(), m_element_type);
46+
}
47+
48+
lldb::ChildCacheState Update() override {
49+
const ValueObjectSP element_ptr =
50+
m_backend.GetChildMemberWithName(ConstString("_M_ptr"));
51+
if (!element_ptr)
52+
return lldb::ChildCacheState::eRefetch;
53+
54+
m_element_type = element_ptr->GetCompilerType().GetPointeeType();
55+
56+
// Get element size.
57+
llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
58+
if (!size_or_err) {
59+
LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
60+
"{0}");
61+
return lldb::ChildCacheState::eReuse;
62+
}
63+
64+
m_element_size = *size_or_err;
65+
if (m_element_size > 0) {
66+
m_start = element_ptr.get();
67+
}
68+
69+
// Get number of elements.
70+
if (auto size_sp = m_backend.GetChildAtNamePath(
71+
{ConstString("_M_extent"), ConstString("_M_extent_value")})) {
72+
m_num_elements = size_sp->GetValueAsUnsigned(0);
73+
} else if (auto arg =
74+
m_backend.GetCompilerType().GetIntegralTemplateArgument(1)) {
75+
76+
m_num_elements = arg->value.GetAPSInt().getLimitedValue();
77+
}
78+
79+
return lldb::ChildCacheState::eReuse;
80+
}
81+
82+
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
83+
if (!m_start)
84+
return llvm::createStringError("Type has no child named '%s'",
85+
name.AsCString());
86+
auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
87+
if (!optional_idx) {
88+
return llvm::createStringError("Type has no child named '%s'",
89+
name.AsCString());
90+
}
91+
return *optional_idx;
92+
}
93+
94+
private:
95+
ValueObject *m_start = nullptr; /// First element of span. Held, not owned.
96+
CompilerType m_element_type; /// Type of span elements.
97+
size_t m_num_elements = 0; /// Number of elements in span.
98+
uint32_t m_element_size = 0; /// Size in bytes of each span element.
99+
};
100+
101+
SyntheticChildrenFrontEnd *
102+
LibStdcppSpanSyntheticFrontEndCreator(CXXSyntheticChildren * /*unused*/,
103+
lldb::ValueObjectSP valobj_sp) {
104+
if (!valobj_sp)
105+
return nullptr;
106+
const CompilerType type = valobj_sp->GetCompilerType();
107+
if (!type || type.GetNumTemplateArguments() != 2)
108+
return nullptr;
109+
return new LibStdcppSpanSyntheticFrontEnd(valobj_sp);
110+
}
111+
112+
} // namespace lldb_private::formatters

lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/span/TestDataFormatterStdSpan.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def do_test(self):
7474
result_summary="item 0 is 1",
7575
)
7676

77-
self.runCmd("type summary delete span")
77+
self.runCmd("type summary clear")
7878

7979
# New span with strings
8080
lldbutil.continue_to_breakpoint(process, bkpt)
@@ -155,12 +155,6 @@ def do_test(self):
155155
)
156156
self.check_size("nested", 2)
157157

158-
@skipIf(compiler="clang", compiler_version=["<", "11.0"])
159-
@add_test_categories(["libc++"])
160-
def test_libcxx(self):
161-
self.build(dictionary={"USE_LIBCPP": 1})
162-
self.do_test()
163-
164158
def do_test_ref_and_ptr(self):
165159
"""Test that std::span is correctly formatted when passed by ref and ptr"""
166160
(self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
@@ -174,8 +168,26 @@ def do_test_ref_and_ptr(self):
174168

175169
self.expect("frame variable ptr", patterns=["ptr = 0x[0-9a-f]+ size=5"])
176170

171+
@skipIf(compiler="clang", compiler_version=["<", "11.0"])
172+
@add_test_categories(["libc++"])
173+
def test_libcxx(self):
174+
self.build(dictionary={"USE_LIBCPP": 1})
175+
self.do_test()
176+
177177
@skipIf(compiler="clang", compiler_version=["<", "11.0"])
178178
@add_test_categories(["libc++"])
179179
def test_ref_and_ptr_libcxx(self):
180180
self.build(dictionary={"USE_LIBCPP": 1})
181181
self.do_test_ref_and_ptr()
182+
183+
@skipIf(compiler="clang", compiler_version=["<", "11.0"])
184+
@add_test_categories(["libstdcxx"])
185+
def test_libstdcxx(self):
186+
self.build(dictionary={"USE_LIBSTDCPP": 1})
187+
self.do_test()
188+
189+
@skipIf(compiler="clang", compiler_version=["<", "11.0"])
190+
@add_test_categories(["libstdcxx"])
191+
def test_ref_and_ptr_libstdcxx(self):
192+
self.build(dictionary={"USE_LIBSTDCPP": 1})
193+
self.do_test_ref_and_ptr()

0 commit comments

Comments
 (0)