public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Philipp Fent <fent@in.tum.de>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH 2/2] libstdc++: Add pretty printer for std::stringstream
Date: Mon, 12 Sep 2022 10:37:10 +0200	[thread overview]
Message-ID: <de37a579-b844-9265-346c-5beb71b20728@in.tum.de> (raw)
In-Reply-To: <CACb0b4kDhRyRBJ1szfDN3Ot8Kewwh8CsXBO+=Pj1P-+H1tojPg@mail.gmail.com>

Hi Jonathan,

I've sent an updated patch addressing your comments here:
https://gcc.gnu.org/pipermail/libstdc++/2022-September/054587.html

Details below.

On 06.09.22 13:27, Jonathan Wakely wrote:
>> +        pbase = self.val['_M_out_beg']
>> +        pptr = self.val['_M_out_cur']
>> +        egptr = self.val['_M_in_end']
> 
> It would be nice to encapsulate this into a generic helper for all
> streambufs, as this part isn't specific to basic_stringbuf. We could
> then reuse it for printing spanbufs and other types in future. It
> could be a function that takes a streambuf and returns a tuple of
> (pbase, pptr, egptr).

I factored this into a reusable access_streambuf_ptrs().

>> +        # 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 = pptr - egptr)
> 
> Shouldn't this be egptr - pbase?
> 
> This suggests the tests are inadequate. I think this bug would have
> been found by a test something like:
> 
> std::stringstream ssin("input", std::ios::in);
> // { dg-final { note-test ssin "\"input\"" } }
> 
> The (egptr - pbase) case is also needed for printing an istringstream.

Good catch! I fixed that bug and added a test for this in the testsuite.

> 
>> +        return self.val['_M_string']
>> +
>> +    def display_hint(self):
>> +        return 'string'
>> +
>> +class StdStringStreamPrinter:
>> +    "Print a std::basic_stringstream"
>> +
>> +    def __init__(self, _, val):
>> +        self.val = val
>> +
>> +    def to_string(self):
>> +        return self.val['_M_stringbuf']
> 
> This assumes that the stream is still using its stringbuf, which is
> probably true 99% of the time, but isn't guaranteed. In the following
> situation, the printer would give potentially misleading output:
> 
> std::stringstream ss("xxx");
> ss.rdbuf(std::cin.rdbuf());
> 
> I wonder if we want to have a check that self.val['_M_streambuf'] ==
> self.val['_M_stringbuf'].address
> 
> Alternatively, don't provide a printer for the stringstream at all,
> just for the stringbuf, and then when GDB uses its default display for
> the stringstream it will show that member.

I didn't know one could redirect a stringstream, since its rdbuf() 
method hides the basic_ios rdbuf() methods. But of course, that's still 
possible via the base class...
The Patch v2 checks that case in the StdStringStreamPrinter constructor, 
and follows that redirect in to_string().

> 
>> +
>> +    def display_hint(self):
>> +        return 'string'
>> +
>>   class Tr1HashtableIterator(Iterator):
>>       def __init__ (self, hashtable):
>>           self.buckets = hashtable['_M_buckets']
>> @@ -2232,6 +2265,10 @@ 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)
>> +    libstdcxx_printer.add_version('std::', 'basic_stringstream', StdStringStreamPrinter)
>> +    libstdcxx_printer.add_version('std::__cxx11::', 'basic_stringstream', StdStringStreamPrinter)
> 
> Wouldn't we want it for ostringstream and istringstream too?

Indeed. The updated patch registers the pretty printer for all flavors.

  parent reply	other threads:[~2022-09-12  8:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-04 18:47 [PATCH 1/2] libstdc++: Fix pretty printer tests of tuple indexes Philipp Fent
2022-09-04 18:47 ` [PATCH 2/2] libstdc++: Add pretty printer for std::stringstream Philipp Fent
2022-09-06 11:27   ` Jonathan Wakely
2022-09-06 21:24     ` [PATCH v2] libstdc++: Add pretty printer for std::stringstreams Philipp Fent
2022-09-14 12:45       ` Jonathan Wakely
2022-09-12  8:37     ` Philipp Fent [this message]
2022-09-06  1:06 ` [PATCH 1/2] libstdc++: Fix pretty printer tests of tuple indexes Will Hawkins
2022-09-06 11:12   ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=de37a579-b844-9265-346c-5beb71b20728@in.tum.de \
    --to=fent@in.tum.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).