From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5EB623857B8E for ; Wed, 14 Sep 2022 12:46:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5EB623857B8E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663159569; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=381FO1R48zh6SSSmCKo9Wl0TyDHy3TlOQS50BNLBxiY=; b=TWlKlBq3t0Gyz/zMHa/j+y5Dr+EJOxLkZtyJNQ1o1oUyaQa0USc7ntaelzVHYBiwO952HM ldqeXH5lanFq+n6M1jiQIxmrHGes8glUOSYI31PYrTUNYzgzuWVn5f2bpjEZOIukrAF5CM rRzsKc8+l02KctijO47LNj/SGR0xWvc= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-627-GE8bYeTsP-GrqfNkfDC_oQ-1; Wed, 14 Sep 2022 08:46:08 -0400 X-MC-Unique: GE8bYeTsP-GrqfNkfDC_oQ-1 Received: by mail-qk1-f198.google.com with SMTP id ay10-20020a05620a178a00b006bbcab9d554so12896528qkb.13 for ; Wed, 14 Sep 2022 05:46:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=381FO1R48zh6SSSmCKo9Wl0TyDHy3TlOQS50BNLBxiY=; b=yWLI0gzNvN2n5NWGImkVQjPBnzZhB2kJXEAQb+9EZ952QvpZiMUI/I+adjBiSDuFxb EWOGzps2qvNJ9MVWJNPpfg0p1vWXpIfFUFPFlyMJea8ztBXiDcahH7DXiVpyzRDNFk4+ i3GLb6oOd313vaa+dd9AQCSOHyUfn9QzxpSVNIVlMsPdFfMwrpV8+8xhHorJDbV4wocp dR7LZdFBMvPppk2gB+vP4kOGGpyZhoSGYz27fEfaj83pDKi3Xr6Y4Gl5y3T70aQJe8wT +eWz3PYEAUaJCOGOJ093KksaEKgvL+UEln64A+PHO7/W4/8pEL5KXWckKNMzB/WG3xJL vQ8w== X-Gm-Message-State: ACgBeo2rP5U/OEdAgJvnjkxGze9NfFrKF2q7tyjf2GmY6MA9i2eq8v5D q7BOwP//2i7EPQWA+eiEGjyzIrL35bgzh7RlOMiFxUgHETFk2CAhILOPSWy/9thKLsevloXTV26 YSH0lNfwf0MR67IJYZc4ld+xMBvrMTVY= X-Received: by 2002:a05:6214:1cc5:b0:498:5d76:69f with SMTP id g5-20020a0562141cc500b004985d76069fmr31622048qvd.33.1663159568051; Wed, 14 Sep 2022 05:46:08 -0700 (PDT) X-Google-Smtp-Source: AA6agR6OSTVDo7ja4n+WZDqk7AGT/GqUIpxZ9lY/VB1yrcG4gva6SduVOD4lGVS0Dd1diX3VT5fN5pgZq1sA6UBuMxg= X-Received: by 2002:a05:6214:1cc5:b0:498:5d76:69f with SMTP id g5-20020a0562141cc500b004985d76069fmr31622036qvd.33.1663159567830; Wed, 14 Sep 2022 05:46:07 -0700 (PDT) MIME-Version: 1.0 References: <20220906212429.42986-1-fent@in.tum.de> In-Reply-To: <20220906212429.42986-1-fent@in.tum.de> From: Jonathan Wakely Date: Wed, 14 Sep 2022 13:45:56 +0100 Message-ID: Subject: Re: [PATCH v2] libstdc++: Add pretty printer for std::stringstreams To: Philipp Fent Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,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: On Tue, 6 Sept 2022 at 22:25, Philipp Fent wrote: > > 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 Oh yuck, sorry you had to figure that out. The patch looks good, thanks. I'll get it pushed to trunk. > + 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 >