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
This commit is contained in:
parent
019270bcb3
commit
a0a1009abf
|
@ -1,5 +1,14 @@
|
|||
2016-09-19 Jonathan Wakely <jwakely@redhat.com>
|
||||
|
||||
* 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.
|
||||
|
|
|
@ -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<T>',
|
||||
'experimental::fundamentals_v\d::any<(.*)>',
|
||||
'experimental::any<\\1>')
|
||||
add_one_template_type_printer(obj, 'optional<T>',
|
||||
'experimental::fundamentals_v1::optional<(.*)>',
|
||||
'experimental::fundamentals_v\d::optional<(.*)>',
|
||||
'experimental::optional<\\1>')
|
||||
add_one_template_type_printer(obj, 'basic_string_view<C>',
|
||||
'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):
|
||||
|
|
|
@ -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
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Type printers only recognize the old std::string for now.
|
||||
#define _GLIBCXX_USE_CXX11_ABI 0
|
||||
|
||||
#include <any>
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
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<int> o;
|
||||
// { dg-final { note-test o {std::optional<int> [no contained value]} } }
|
||||
optional<bool> ob{false};
|
||||
// { dg-final { note-test ob {std::optional<bool> = {[contained value] = false}} } }
|
||||
optional<int> oi{5};
|
||||
// { dg-final { note-test oi {std::optional<int> = {[contained value] = 5}} } }
|
||||
optional<void*> op{nullptr};
|
||||
// { dg-final { note-test op {std::optional<void *> = {[contained value] = 0x0}} } }
|
||||
optional<std::map<int, double>> om;
|
||||
om = std::map<int, double>{ {1, 2.}, {3, 4.}, {5, 6.} };
|
||||
// { dg-final { note-test om {std::optional<std::map<int, double>> containing std::map with 3 elements = {[1] = 2, [3] = 4, [5] = 6}} } }
|
||||
optional<std::string> os{ "stringy" };
|
||||
// { dg-final { note-test os {std::optional<std::string> = {[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<float, int, string_view> v0;
|
||||
// { dg-final { note-test v0 {std::variant<float, int, std::string_view> [index 0] = {0}} } }
|
||||
variant<float, int, string_view> v1{ 0.5f };
|
||||
// { dg-final { note-test v1 {std::variant<float, int, std::string_view> [index 0] = {0.5}} } }
|
||||
variant<float, int, string_view> v2;
|
||||
try {
|
||||
v2.emplace<1>(S());
|
||||
} catch (int) { }
|
||||
// { dg-final { note-test v2 {std::variant<float, int, std::string_view> [no contained value]} } }
|
||||
variant<float, int, string_view> v3{ 3 };
|
||||
// { dg-final { note-test v3 {std::variant<float, int, std::string_view> [index 1] = {3}} } }
|
||||
variant<float, int, string_view> v4{ str };
|
||||
// { dg-final { note-test v4 {std::variant<float, int, std::string_view> [index 2] = {"string"}} } }
|
||||
|
||||
std::cout << "\n";
|
||||
return 0; // Mark SPOT
|
||||
}
|
||||
|
||||
// { dg-final { gdb-test SPOT } }
|
Loading…
Reference in New Issue