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"""
# 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):
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):
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"
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')
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)
'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):
--- /dev/null
+// { 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 } }