This is an archive of the discontinued LLVM Phabricator instance.

[LLDB][formatters] Add formatter for libc++'s std::span
ClosedPublic

Authored by Michael137 on Jun 10 2022, 3:30 AM.

Details

Summary

This patch adds a libcxx formatter for std::span. The
implementation is based on the libcxx formatter for
std::vector. The main difference is the fact that
std::span conditionally has a __size member based
on whether it has a static or dynamic extent.

Example output of formatted span:

(std::span<const int, 18446744073709551615>) $0 = size=6 {
  [0] = 0
  [1] = 1
  [2] = 2
  [3] = 3
  [4] = 4
  [5] = 5
}

The second template parameter here is actually std::dynamic_extent,
but the type declaration we get back from the TypeSystemClang is the
actual value (which in this case is (size_t)-1). This is consistent
with diagnostics from clang, which doesn't desugar this value either.
E.g.,:

span.cpp:30:31: error: implicit instantiation of undefined template
    'Undefined<std::span<int, 18446744073709551615>>'

Testing:

  • Added API-tests
  • Confirmed manually using LLDB cli that printing spans works in various scenarios

Diff Detail

Event Timeline

Michael137 created this revision.Jun 10 2022, 3:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2022, 3:30 AM
Herald added a subscriber: mgorny. · View Herald Transcript
Michael137 requested review of this revision.Jun 10 2022, 3:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2022, 3:30 AM

Removed redundant comment

Michael137 added inline comments.Jun 10 2022, 3:45 AM
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/Makefile
6

Might need to have some compiler version checks here since by default we assume c++11

Thanks, this looks really good! I have a couple of small comments inline.

lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
77

/// Formatter for libc++ std::span<>.

lldb/source/Plugins/Language/CPlusPlus/LibCxxSpan.cpp
41

///< First element of span.

56

// This ...

57

.

58

I would just delete this last line IMHO, it's more confusing than helping. Or maybe just say that m_start is owned by s shared_ptr elsewhere.

81

I would move this into a doxygen comment on the function declaration.

83

dymanic

104

// Get element type.

112

dito

117

Technically LLVM doesn't use {} on single-statement bodies.

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/Makefile
6

The -O0 should not be necessary.

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py
54

We probably need to skip this on earlier compilers that don't support C++20.

72

I think the cleanups are no longer necessary because we run every test in isolation now.

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/main.cpp
11

a more robust way to ensure code exists on this line would be
printf(" Stop here to check by ref")

  • Update to use Doxygen-style comments
  • Improve test robustness
Michael137 marked 13 inline comments as done.Jun 10 2022, 11:22 AM
aprantl accepted this revision.Jun 10 2022, 12:26 PM
This revision is now accepted and ready to land.Jun 10 2022, 12:26 PM
aprantl added inline comments.Jun 10 2022, 12:40 PM
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/span/TestDataFormatterLibcxxSpan.py
24

Something you could consider for a follow-up commit:
A more robust and canonical way to test for synthetic children would be to check the SBAPI directly.
https://lldb.llvm.org/python_api.html

var = frame.FindVariable(var_name)
self.assertEquals(var.GetNumChildren(), 5)
  • Updated tests to avoid using self.expect and instead the more specific assertions from lldbtest and SB API

Redundant destructor definition

This revision was automatically updated to reflect the committed changes.