From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 9531E3858D3C; Tue, 19 Apr 2022 13:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9531E3858D3C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-8193] libstdc++: Add pretty printer for std::span X-Act-Checkin: gcc X-Git-Author: Philipp Fent X-Git-Refname: refs/heads/master X-Git-Oldrev: 214d2860f4e1573f04ef57bfea59da0cc66883ce X-Git-Newrev: fdb3f82fb324c3ddd7464d11c8ea60a98f486a0e Message-Id: <20220419131908.9531E3858D3C@sourceware.org> Date: Tue, 19 Apr 2022 13:19:08 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Apr 2022 13:19:08 -0000 https://gcc.gnu.org/g:fdb3f82fb324c3ddd7464d11c8ea60a98f486a0e commit r12-8193-gfdb3f82fb324c3ddd7464d11c8ea60a98f486a0e Author: Philipp Fent Date: Thu Apr 14 17:06:02 2022 +0100 libstdc++: Add pretty printer for std::span This improves the debug output for C++20 spans. Before: {static extent = 18446744073709551615, _M_ptr = 0x7fffffffb9a8, _M_extent = {_M_extent_value = 2}} Now with StdSpanPrinter: std::span of length 2 = {1, 2} Signed-off-by: Philipp Fent libstdc++-v3/ChangeLog: * python/libstdcxx/v6/printers.py (StdSpanPrinter): Define. * testsuite/libstdc++-prettyprinters/cxx20.cc: Test it. Diff: --- libstdc++-v3/python/libstdcxx/v6/printers.py | 38 ++++++++++++++++++++++ .../testsuite/libstdc++-prettyprinters/cxx20.cc | 11 +++++++ 2 files changed, 49 insertions(+) diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index f7a7f9961a7..6d8b765f2da 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -1654,6 +1654,43 @@ class StdRegexStatePrinter: s = "{}, {}={}".format(s, v, self.val['_M_' + v]) return "{%s}" % (s) +class StdSpanPrinter: + "Print a std::span" + + class _iterator(Iterator): + def __init__(self, begin, size): + self.count = 0 + self.begin = begin + self.size = size + + def __iter__ (self): + return self + + def __next__ (self): + if self.count == self.size: + raise StopIteration + + count = self.count + self.count = self.count + 1 + return '[%d]' % count, (self.begin + count).dereference() + + def __init__(self, typename, val): + self.typename = typename + self.val = val + if val.type.template_argument(1) == gdb.parse_and_eval('static_cast(-1)'): + self.size = val['_M_extent']['_M_extent_value'] + else: + self.size = val.type.template_argument(1) + + def to_string(self): + return '%s of length %d' % (self.typename, self.size) + + def children(self): + return self._iterator(self.val['_M_ptr'], self.size) + + def display_hint(self): + return 'array' + # A "regular expression" printer which conforms to the # "SubPrettyPrinter" protocol from gdb.printing. class RxPrinter(object): @@ -2170,6 +2207,7 @@ def build_libstdcxx_dictionary (): libstdcxx_printer.add_version('std::', 'partial_ordering', StdCmpCatPrinter) libstdcxx_printer.add_version('std::', 'weak_ordering', StdCmpCatPrinter) libstdcxx_printer.add_version('std::', 'strong_ordering', StdCmpCatPrinter) + libstdcxx_printer.add_version('std::', 'span', StdSpanPrinter) # Extensions. libstdcxx_printer.add_version('__gnu_cxx::', 'slist', StdSlistPrinter) diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx20.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx20.cc index b0de25c27ec..76023df93fa 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx20.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx20.cc @@ -18,8 +18,10 @@ // with this library; see the file COPYING3. If not see // . +#include #include #include +#include struct X { @@ -54,6 +56,15 @@ main() auto c10 = 0.0 <=> __builtin_nan(""); // { dg-final { note-test c10 "std::partial_ordering::unordered" } } + auto il = {1, 2}; + auto s1 = std::span(il); + static_assert(s1.extent == std::size_t(-1)); +// { dg-final { note-test s1 {std::span of length 2 = {1, 2}} } } + auto a = std::array{3, 4}; + auto s2 = std::span(a); + static_assert(s2.extent == std::size_t(2)); +// { dg-final { note-test s2 {std::span of length 2 = {3, 4}} } } + std::cout << "\n"; return 0; // Mark SPOT }