From patchwork Mon Sep 19 15:58:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 671826 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3sd9Xv3Zn5z9sXR for ; Tue, 20 Sep 2016 01:58:39 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=HmDFcGNW; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:references:mime-version:content-type :in-reply-to; q=dns; s=default; b=eqyQWuYlvBJPPnhxao5RvWQ415Urtv RCBWcU4qC0SdpzU7l+1wtm2KKKBv8VsSOvXcTthynQ0pPFLZYz+iHp7jS3N29QDv KNafqM2wFjmwV/p6YMDVM0MOPeHqLXG+qbmEio5nR48sU5zTdzTlRzI+yXIsPozF dbWpnBWyZgnkg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:references:mime-version:content-type :in-reply-to; s=default; bh=C/nYp7T8kzAyd17crnOO2T55MdA=; b=HmDF cGNWXAoB3Tdy5L/Pow9q55kT2cuGvvfJLlerYESd+VT31+aqRjIPZBtYWGrx5gdS ej6xCWpvkCed2APoQIR5xd+2ECkTTn8SDQ4dIXI7b6QTURgI3uFOXrbjauVLk+oy J9j9VHOp/rzg5gcQGjZ72+Bj0s8ahqFcydySYWY= Received: (qmail 99471 invoked by alias); 19 Sep 2016 15:58:18 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 99375 invoked by uid 89); 19 Sep 2016 15:58:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.0 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=SPOT, 9866, duration, ios X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 19 Sep 2016 15:58:07 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B0BEEC05A28C; Mon, 19 Sep 2016 15:58:06 +0000 (UTC) Received: from localhost (ovpn-116-66.ams2.redhat.com [10.36.116.66]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8JFw50N007062; Mon, 19 Sep 2016 11:58:06 -0400 Date: Mon, 19 Sep 2016 16:58:05 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Define pretty printers for C++17 library components Message-ID: <20160919155805.GX17376@redhat.com> References: <20160917152016.GA19699@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20160917152016.GA19699@redhat.com> X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.0 (2016-08-17) On 17/09/16 16:20 +0100, Jonathan Wakely wrote: >This tweaks the existing printers for Fundamentals TS components to >work for the C++17 versions, and adds a printer for std::variant. > > * python/libstdcxx/v6/printers.py (StdVariantPrinter): Define. > (StdExpAnyPrinter, StdExpOptionalPrinter, StdExpStringViewPrinter): > Register for C++17 components in namespace std. Strip inline namespace > from typename. This improves the output for the std::variant printer and adds type printers and tests for the C++17 types. I am inclined to change the printers for any and optional so they also use a display_hint of 'array', which means they would show: std::optional = {val} instead of std::optional = {[contained value] = val} IIRC I only made them show the "[contained value]" part because I couldn't figure out how to do {val}. We might also want to consider using 'array' for std::set, because currently it shows fake indices for the elements, e.g. {[0] = x, [1] = y, [2] z} but that would be better shown as {x, y, z} commit b971ec023ef92bd5425df62d20af63317bde430f Author: Jonathan Wakely Date: Mon Sep 19 12:04:29 2016 +0100 Improve pretty printer for std::variant * python/libstdcxx/v6/printers.py (SingleObjContainerPrinter): Allow display_hint to be set by subclasses. (StdVariantPrinter): Use array for display_hint. Adjust output to be more similar to std::any and std::optional output. (register_type_printers): Add type printers for basic_string_view typedefs and experimental::any. Adjust type printers for fundamentals_v1 templates to match fundamentals_v2 and later. * testsuite/libstdc++-prettyprinters/cxx17.cc: New. diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 8c29760..ac529dd 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -879,9 +879,10 @@ class StdForwardListPrinter: class SingleObjContainerPrinter(object): "Base class for printers of containers of single objects" - def __init__ (self, val, viz): + def __init__ (self, val, viz, hint = None): self.contained_value = val self.visualizer = viz + self.hint = hint def _recognize(self, type): """Return TYPE as a string after applying type printers""" @@ -916,7 +917,7 @@ class SingleObjContainerPrinter(object): # if contained value is a map we want to display in the same way if hasattr (self.visualizer, 'children') and hasattr (self.visualizer, 'display_hint'): return self.visualizer.display_hint () - return None + return self.hint class StdExpAnyPrinter(SingleObjContainerPrinter): @@ -985,7 +986,6 @@ class StdVariantPrinter(SingleObjContainerPrinter): def __init__(self, typename, val): alternatives = self._template_args(val) - self.alts = alternatives self.typename = "%s<%s>" % (typename, ', '.join([self._recognize(alt) for alt in alternatives])) self.index = val['_M_index'] if self.index >= len(alternatives): @@ -997,24 +997,25 @@ class StdVariantPrinter(SingleObjContainerPrinter): addr = val['_M_first']['_M_storage'].address contained_value = addr.cast(self.contained_type.pointer()).dereference() visualizer = gdb.default_visualizer(contained_value) - super (StdVariantPrinter, self).__init__(contained_value, visualizer) + super (StdVariantPrinter, self).__init__(contained_value, visualizer, 'array') - def _template_args(self, val): + @staticmethod + def _template_args(val): n = 0 - args = () + args = [] while True: try: - args += (val.type.template_argument(n),) + args.append(val.type.template_argument(n)) except: return args n += 1 def to_string(self): if self.contained_value is None: - return "%s [no value]" % self.typename + return "%s [no contained value]" % self.typename if hasattr(self.visualizer, 'children'): - return "%s [alternative %d] %s" % (self.typename, self.index, self.visualizer.to_string()) - return self.typename + return "%s [index %d] containing %s" % (self.typename, self.index, self.visualizer.to_string()) + return "%s [index %d]" % (self.typename, self.index) class StdExpStringViewPrinter: "Print a std::basic_string_view or std::experimental::basic_string_view" @@ -1262,6 +1263,7 @@ def register_type_printers(obj): for pfx in ('', 'w'): add_one_type_printer(obj, 'basic_string', pfx + 'string') + add_one_type_printer(obj, 'basic_string_view', pfx + 'string_view') add_one_type_printer(obj, 'basic_ios', pfx + 'ios') add_one_type_printer(obj, 'basic_streambuf', pfx + 'streambuf') add_one_type_printer(obj, 'basic_istream', pfx + 'istream') @@ -1296,6 +1298,9 @@ def register_type_printers(obj): add_one_type_printer(obj, 'basic_string', 'u16string') add_one_type_printer(obj, 'basic_string', 'u32string') + add_one_type_printer(obj, 'basic_string_view', 'u16string_view') + add_one_type_printer(obj, 'basic_string_view', 'u32string_view') + for dur in ('nanoseconds', 'microseconds', 'milliseconds', 'seconds', 'minutes', 'hours'): add_one_type_printer(obj, 'duration', dur) @@ -1353,11 +1358,14 @@ def register_type_printers(obj): 'unordered_multiset<{1}>') # strip the "fundamentals_v1" inline namespace from these types + add_one_template_type_printer(obj, 'any', + 'experimental::fundamentals_v\d::any<(.*)>', + 'experimental::any<\\1>') add_one_template_type_printer(obj, 'optional', - 'experimental::fundamentals_v1::optional<(.*)>', + 'experimental::fundamentals_v\d::optional<(.*)>', 'experimental::optional<\\1>') add_one_template_type_printer(obj, 'basic_string_view', - 'experimental::fundamentals_v1::basic_string_view<(.*), std::char_traits<\\1> >', + 'experimental::fundamentals_v\d::basic_string_view<(.*), std::char_traits<\\1> >', 'experimental::basic_string_view<\\1>') def register_libstdcxx_printers (obj): diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx17.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx17.cc new file mode 100644 index 0000000..42d79c0 --- /dev/null +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx17.cc @@ -0,0 +1,91 @@ +// { dg-options "-g -O0 -std=gnu++1z" } +// { dg-do run { target c++1z } } + +// Copyright (C) 2014-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// Type printers only recognize the old std::string for now. +#define _GLIBCXX_USE_CXX11_ABI 0 + +#include +#include +#include +#include +#include +#include +#include + +using std::any; +using std::optional; +using std::variant; +using std::string_view; + +int +main() +{ + string_view str = "string"; +// { dg-final { note-test str "\"string\"" } } + + optional o; +// { dg-final { note-test o {std::optional [no contained value]} } } + optional ob{false}; +// { dg-final { note-test ob {std::optional = {[contained value] = false}} } } + optional oi{5}; +// { dg-final { note-test oi {std::optional = {[contained value] = 5}} } } + optional op{nullptr}; +// { dg-final { note-test op {std::optional = {[contained value] = 0x0}} } } + optional> om; + om = std::map{ {1, 2.}, {3, 4.}, {5, 6.} }; +// { dg-final { note-test om {std::optional> containing std::map with 3 elements = {[1] = 2, [3] = 4, [5] = 6}} } } + optional os{ "stringy" }; +// { dg-final { note-test os {std::optional = {[contained value] = "stringy"}} } } + + any a; +// { dg-final { note-test a {std::any [no contained value]} } } + any ab(false); +// { dg-final { note-test ab {std::any containing bool = {[contained value] = false}} } } + any ai(6); +// { dg-final { note-test ai {std::any containing int = {[contained value] = 6}} } } + any ap = (void*)nullptr; +// { dg-final { note-test ap {std::any containing void * = {[contained value] = 0x0}} } } + any as = *os; +// { dg-final { note-test as {std::any containing std::string = {[contained value] = "stringy"}} } } + any as2("stringiest"); +// { dg-final { regexp-test as2 {std::any containing const char \* = {\[contained value\] = 0x[[:xdigit:]]+ "stringiest"}} } } + any am = *om; +// { dg-final { note-test am {std::any containing std::map with 3 elements = {[1] = 2, [3] = 4, [5] = 6}} } } + + struct S { operator int() { throw 42; }}; + variant v0; +// { dg-final { note-test v0 {std::variant [index 0] = {0}} } } + variant v1{ 0.5f }; +// { dg-final { note-test v1 {std::variant [index 0] = {0.5}} } } + variant v2; + try { + v2.emplace<1>(S()); + } catch (int) { } +// { dg-final { note-test v2 {std::variant [no contained value]} } } + variant v3{ 3 }; +// { dg-final { note-test v3 {std::variant [index 1] = {3}} } } + variant v4{ str }; +// { dg-final { note-test v4 {std::variant [index 2] = {"string"}} } } + + std::cout << "\n"; + return 0; // Mark SPOT +} + +// { dg-final { gdb-test SPOT } }