From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mailout1.rbg.tum.de (mailout1.rbg.tum.de [131.159.0.201]) by sourceware.org (Postfix) with ESMTPS id 10E893858297; Tue, 6 Sep 2022 21:25:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 10E893858297 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=in.tum.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=in.tum.de Received: from mailrelay1.rbg.tum.de (mailrelay1.in.tum.de [131.159.254.14]) by mailout1.rbg.tum.de (Postfix) with ESMTPS id 1B8D796; Tue, 6 Sep 2022 23:25:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=in.tum.de; s=20220209; t=1662499527; bh=pvuG2CiqFAh2TOrSetDKz/yYRtp1zuTMH0yNvWzcqfc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gsIAPtIRXs/ZmuC8Zv9MqOYzBJUNPuMdAAtrQCrbKK815iIJQku9Ql8yFAynepm2a g3YPl2QJYZgEyfg2dzJDs3pEU7OvMNLl6OSO/YVyjHPF2qCAkLCjKclhQzCysJcDr2 RW2sDBBBu7LrqJ3Jb/sQERl7nqas73EzOMAe2m+dkiz7k+Ebrzarw89htoz+gedcal nwMkaWgAlUiwKnBaS1EydqYDDJeWaw9E2dCVsuUry+MC05XStPBL6vxl2qrYrRq6G5 DZ1fZ09zLIhTv8py6/AGoCvUfvR8hcG+yqATBxizJCtCerwkrUTONaqCFFBy9LicsW j5zCd/0adi+Hw== Received: by mailrelay1.rbg.tum.de (Postfix, from userid 112) id 17B961AC0; Tue, 6 Sep 2022 23:25:27 +0200 (CEST) Received: from mailrelay1.rbg.tum.de (localhost [127.0.0.1]) by mailrelay1.rbg.tum.de (Postfix) with ESMTP id DD8261ABF; Tue, 6 Sep 2022 23:25:26 +0200 (CEST) Received: from mail.in.tum.de (mailproxy.in.tum.de [IPv6:2a09:80c0::78]) by mailrelay1.rbg.tum.de (Postfix) with ESMTPS id DB8F51ABD; Tue, 6 Sep 2022 23:25:26 +0200 (CEST) Received: by mail.in.tum.de (Postfix, from userid 112) id D88014A0246; Tue, 6 Sep 2022 23:25:26 +0200 (CEST) Received: (Authenticated sender: fent) by mail.in.tum.de (Postfix) with ESMTPSA id 461074A0144; Tue, 6 Sep 2022 23:25:26 +0200 (CEST) (Extended-Queue-bit xtech_dd@fff.in.tum.de) From: Philipp Fent To: jwakely@redhat.com Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org, Philipp Fent Subject: [PATCH v2] libstdc++: Add pretty printer for std::stringstreams Date: Tue, 6 Sep 2022 23:24:29 +0200 Message-Id: <20220906212429.42986-1-fent@in.tum.de> X-Mailer: git-send-email 2.37.3 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: To display (o-,i-)stringstreams in the common case, we just print the underlying stringbuf, without the many ios_base members. In the unconventional case that the underlying streambuf was redirected, we report the redirected target. Signed-off-by: Philipp Fent --- libstdc++-v3/python/libstdcxx/v6/printers.py | 56 +++++++++++++++++++ .../libstdc++-prettyprinters/debug.cc | 15 +++++ .../libstdc++-prettyprinters/simple.cc | 15 +++++ .../libstdc++-prettyprinters/simple11.cc | 15 +++++ 4 files changed, 101 insertions(+) diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index d70c8d5d616..bd4289c1c62 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -969,6 +969,57 @@ class StdStringPrinter: def display_hint (self): return 'string' +def access_streambuf_ptrs(streambuf): + "Access the streambuf put area pointers" + pbase = streambuf['_M_out_beg'] + pptr = streambuf['_M_out_cur'] + egptr = streambuf['_M_in_end'] + return pbase, pptr, egptr + +class StdStringBufPrinter: + "Print a std::basic_stringbuf" + + def __init__(self, _, val): + self.val = val + + def to_string(self): + (pbase, pptr, egptr) = access_streambuf_ptrs(self.val) + # Logic from basic_stringbuf::_M_high_mark() + if pptr: + if not egptr or pptr > egptr: + return pbase.string(length = pptr - pbase) + else: + return pbase.string(length = egptr - pbase) + return self.val['_M_string'] + + def display_hint(self): + return 'string' + +class StdStringStreamPrinter: + "Print a std::basic_stringstream" + + def __init__(self, typename, val): + self.val = val + self.typename = typename + + # Check if the stream was redirected: + # This is essentially: val['_M_streambuf'] == val['_M_stringbuf'].address + # However, GDB can't resolve the virtual inheritance, so we do that manually + basetype = [f.type for f in val.type.fields() if f.is_base_class][0] + gdb.set_convenience_variable('__stream', val.cast(basetype).address) + self.streambuf = gdb.parse_and_eval('$__stream->rdbuf()') + self.was_redirected = self.streambuf != val['_M_stringbuf'].address + + def to_string(self): + if self.was_redirected: + return "%s redirected to %s" % (self.typename, self.streambuf.dereference()) + return self.val['_M_stringbuf'] + + def display_hint(self): + if self.was_redirected: + return None + return 'string' + class Tr1HashtableIterator(Iterator): def __init__ (self, hashtable): self.buckets = hashtable['_M_buckets'] @@ -2232,6 +2283,11 @@ def build_libstdcxx_dictionary (): libstdcxx_printer.add_version('std::', 'initializer_list', StdInitializerListPrinter) libstdcxx_printer.add_version('std::', 'atomic', StdAtomicPrinter) + libstdcxx_printer.add_version('std::', 'basic_stringbuf', StdStringBufPrinter) + libstdcxx_printer.add_version('std::__cxx11::', 'basic_stringbuf', StdStringBufPrinter) + for sstream in ('istringstream', 'ostringstream', 'stringstream'): + libstdcxx_printer.add_version('std::', 'basic_' + sstream, StdStringStreamPrinter) + libstdcxx_printer.add_version('std::__cxx11::', 'basic_' + sstream, StdStringStreamPrinter) # std::regex components libstdcxx_printer.add_version('std::__detail::', '_State', diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc index 98bbc182551..3c6195591c5 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -110,6 +111,20 @@ main() __gnu_cxx::slist::iterator slliter = sll.begin(); // { dg-final { note-test slliter {47} } } + std::stringstream sstream; + sstream << "abc"; +// { dg-final { note-test sstream "\"abc\"" } } + std::stringstream ssin("input", std::ios::in); +// { dg-final { note-test ssin "\"input\"" } } + std::istringstream ssin2("input"); +// { dg-final { note-test ssin2 "\"input\"" } } + std::ostringstream ssout; + ssout << "out"; +// { dg-final { note-test ssout "\"out\"" } } + std::stringstream redirected("xxx"); + static_cast&>(redirected).rdbuf(sstream.rdbuf()); +// { dg-final { regexp-test redirected {std::.*stringstream redirected to .*} } } + std::cout << "\n"; return 0; // Mark SPOT } diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc index 1f85775bff0..1609ae2c8db 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -169,6 +170,20 @@ main() __gnu_cxx::slist::iterator slliter0; // { dg-final { note-test slliter0 {non-dereferenceable iterator for __gnu_cxx::slist} } } + std::stringstream sstream; + sstream << "abc"; +// { dg-final { note-test sstream "\"abc\"" } } + std::stringstream ssin("input", std::ios::in); +// { dg-final { note-test ssin "\"input\"" } } + std::istringstream ssin2("input"); +// { dg-final { note-test ssin2 "\"input\"" } } + std::ostringstream ssout; + ssout << "out"; +// { dg-final { note-test ssout "\"out\"" } } + std::stringstream redirected("xxx"); + static_cast&>(redirected).rdbuf(sstream.rdbuf()); +// { dg-final { regexp-test redirected {std::.*stringstream redirected to .*} } } + std::cout << "\n"; return 0; // Mark SPOT } diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc index 6f21675cf41..a4b82e30f9c 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -162,6 +163,20 @@ main() __gnu_cxx::slist::iterator slliter0; // { dg-final { note-test slliter0 {non-dereferenceable iterator for __gnu_cxx::slist} } } + std::stringstream sstream; + sstream << "abc"; +// { dg-final { note-test sstream "\"abc\"" } } + std::stringstream ssin("input", std::ios::in); +// { dg-final { note-test ssin "\"input\"" } } + std::istringstream ssin2("input"); +// { dg-final { note-test ssin2 "\"input\"" } } + std::ostringstream ssout; + ssout << "out"; +// { dg-final { note-test ssout "\"out\"" } } + std::stringstream redirected("xxx"); + static_cast&>(redirected).rdbuf(sstream.rdbuf()); +// { dg-final { regexp-test redirected {std::.*stringstream redirected to .*} } } + std::cout << "\n"; return 0; // Mark SPOT } -- 2.37.3