This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Define pretty printers for C++17 library components


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<Foo> = {val}

instead of
 std::optional<Foo> = {[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 <jwakely@redhat.com>
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<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):
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
+// <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 } }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]