From patchwork Tue Sep 6 21:24:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Fent X-Patchwork-Id: 1046 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp839276wrt; Tue, 6 Sep 2022 14:26:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR7E7pnOk2dilzmrUmrNY2I6XDia2qQLI1oi4ny4nyKfHQN7cspIYNkNZzop+IdOwDBZ/oGq X-Received: by 2002:a17:907:3f90:b0:741:96fe:6641 with SMTP id hr16-20020a1709073f9000b0074196fe6641mr293333ejc.378.1662499586223; Tue, 06 Sep 2022 14:26:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662499586; cv=none; d=google.com; s=arc-20160816; b=FE2ABXVRZgthT7KCjst3XzYrWY2Gvo5lRxduGMo7BZ3RqWlRVDORCMC574xTOyWF2o yrz7M60o3z7r/6NwHYqE10Sx5uRIFwFtXZhudS1a7WOKTW08dzSGov4B3rKCqVYD6nJ1 1F0AWZj1K5YX9XlL+pBPa419zRiZ+V5SvO7+3twoT1H4tJvXuMZ5nSxMWedA/2CE4tb5 hEiSFCAi4raHUhrO6y/S3oqJADeTkeBxUsPciw0dJXD39bEu7czimkXI7mRPQgi+6oN/ PVwaxsaSuZs2egSYvr/yrzg2FX0gMGitARP9NkAtna838d0WJw9NbgBvB3r6v7JWPNkh wPzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=pvuG2CiqFAh2TOrSetDKz/yYRtp1zuTMH0yNvWzcqfc=; b=dbLJ6ao0KdUXaHRCCIoAnmv205IA2f6N9mo7Jra0EJ8OTkMMt+2BOw1T+dnTSWLjXD EH7vIoJJwZaJjfdPCTponX9mqeAEh3sdollviFOmNrHY9SlnSn9Uos3f23Yz+ODUiN/U ISbI4GoAidUafwwivCghdewBcq++lUnezR3FOQio4J1BXDy1CF3uvCj7AQkLkVDdyS3E UR8kUO3Yp4ZK7S5rQtrIFwJl6//KzcktykWBU6AIzAUOJAFFwGy4y7XVlt/qq5/CW2vp EMCyaFQ4b5mPF5c34johRhLHB2y5JW3PLRweZeoa8PnQ/5IJJh6SeWWgw3Mq00UieVLR VSwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Q9IxRZ6m; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h32-20020a0564020ea000b0044e9afe62ecsi3806549eda.392.2022.09.06.14.26.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 14:26:26 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Q9IxRZ6m; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4588338515FB for ; Tue, 6 Sep 2022 21:26:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4588338515FB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662499574; bh=pvuG2CiqFAh2TOrSetDKz/yYRtp1zuTMH0yNvWzcqfc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Q9IxRZ6mQREV6tjT9AQBFUKZp68fHgLjSvVbFG0T08heCugdcirAM1MEYM/mUW+A3 dNAVxi6uLKiozsu7YyiQkXP4QI8SjAGSr8AOV/qSX+EXvTZftAqBtWDP4TpRDPFOP9 7uJlVaVgX4Fr6oKhcbv1qbiN+iv0QP6jQEqIi4v4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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) 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) To: jwakely@redhat.com 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Philipp Fent via Gcc-patches From: Philipp Fent Reply-To: Philipp Fent Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org, Philipp Fent Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743257166430441508?= X-GMAIL-MSGID: =?utf-8?q?1743257166430441508?= 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 }