From: Jonathan Wakely Date: Mon, 19 Sep 2016 15:49:27 +0000 (+0100) Subject: Improve pretty printer for std::variant X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a0a1009abf4802538f9f8687e99a27be0ab1dc17;p=gcc.git 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. From-SVN: r240234 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 8bbd29c720c..8ee94555b1a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,14 @@ 2016-09-19 Jonathan Wakely + * 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. + PR libstdc++/77645 * python/libstdcxx/v6/xmethods.py (DequeWorkerBase.index): Rename argument. diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 8c29760e01c..ac529ddbece 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 00000000000..42d79c05e64 --- /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 } }