From d2dfcf823c0ba2b654f8d25a9ec46f59612cb60c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Juraj=20Or=C5=A1uli=C4=87?= Date: Tue, 9 Jan 2018 18:49:57 +0000 Subject: [PATCH] PR libstdc++/59253 Improve pretty printers for smart pointers PR libstdc++/59253 (partial) * python/libstdcxx/v6/printers.py (SmartPtrIterator): Common iterator type for pointer stored by shared_ptr, weak_ptr and unique_ptr. (SharedPointerPrinter, UniquePointerPrinter): Treat stored values as children. * testsuite/libstdc++-prettyprinters/cxx11.cc: Update expected output of unique_ptr printer. * testsuite/libstdc++-prettyprinters/shared_ptr.cc: Update expected output of shared_ptr printer. From-SVN: r256390 --- libstdc++-v3/ChangeLog | 12 ++++++ libstdc++-v3/python/libstdcxx/v6/printers.py | 43 ++++++++++++++----- .../libstdc++-prettyprinters/cxx11.cc | 8 ++-- .../libstdc++-prettyprinters/shared_ptr.cc | 12 +++--- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index b074111d003..df96fcb3944 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2018-01-09 Jonathan Wakely + + PR libstdc++/59253 (partial) + * python/libstdcxx/v6/printers.py (SmartPtrIterator): Common iterator + type for pointer stored by shared_ptr, weak_ptr and unique_ptr. + (SharedPointerPrinter, UniquePointerPrinter): Treat stored values as + children. + * testsuite/libstdc++-prettyprinters/cxx11.cc: Update expected output + of unique_ptr printer. + * testsuite/libstdc++-prettyprinters/shared_ptr.cc: Update expected + output of shared_ptr printer. + 2018-01-05 Jonathan Wakely PR libstdc++/83626 diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 6da8508d944..e9f7359d63f 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -114,12 +114,31 @@ def strip_versioned_namespace(typename): return typename.replace(_versioned_namespace, '') return typename +class SmartPtrIterator(Iterator): + "An iterator for smart pointer types with a single 'child' value" + + def __init__(self, val): + self.val = val + + def __iter__(self): + return self + + def __next__(self): + if self.val is None: + raise StopIteration + self.val, val = None, self.val + return ('get()', val) + class SharedPointerPrinter: "Print a shared_ptr or weak_ptr" def __init__ (self, typename, val): self.typename = strip_versioned_namespace(typename) self.val = val + self.pointer = val['_M_ptr'] + + def children (self): + return SmartPtrIterator(self.pointer) def to_string (self): state = 'empty' @@ -128,27 +147,29 @@ class SharedPointerPrinter: usecount = refcounts['_M_use_count'] weakcount = refcounts['_M_weak_count'] if usecount == 0: - state = 'expired, weak %d' % weakcount + state = 'expired, weak count %d' % weakcount else: - state = 'count %d, weak %d' % (usecount, weakcount - 1) - return '%s (%s) %s' % (self.typename, state, self.val['_M_ptr']) + state = 'use count %d, weak count %d' % (usecount, weakcount - 1) + return '%s<%s> (%s)' % (self.typename, str(self.pointer.type.target().strip_typedefs()), state) class UniquePointerPrinter: "Print a unique_ptr" def __init__ (self, typename, val): self.val = val - - def to_string (self): - impl_type = self.val.type.fields()[0].type.tag + impl_type = val.type.fields()[0].type.tag if is_specialization_of(impl_type, '__uniq_ptr_impl'): # New implementation - v = self.val['_M_t']['_M_t']['_M_head_impl'] + self.pointer = val['_M_t']['_M_t']['_M_head_impl'] elif is_specialization_of(impl_type, 'tuple'): - v = self.val['_M_t']['_M_head_impl'] + self.pointer = val['_M_t']['_M_head_impl'] else: - raise ValueError("Unsupported implementation for unique_ptr: %s" % self.val.type.fields()[0].type.tag) - return 'std::unique_ptr<%s> containing %s' % (str(v.type.target()), - str(v)) + raise ValueError("Unsupported implementation for unique_ptr: %s" % impl_type) + + def children (self): + return SmartPtrIterator(self.pointer) + + def to_string (self): + return ('std::unique_ptr<%s>' % (str(self.pointer.type.target()))) def get_value_from_aligned_membuf(buf, valtype): """Returns the value held in a __gnu_cxx::__aligned_membuf.""" diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc index 64119155282..3af564f2bf7 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc @@ -127,14 +127,14 @@ main() std::unique_ptr uptr (new datum); uptr->s = "hi bob"; uptr->i = 23; -// { dg-final { regexp-test uptr {std::unique_ptr.datum. containing 0x.*} } } +// { dg-final { regexp-test uptr {std::unique_ptr.datum. = {get\(\) = 0x.*}} } } std::unique_ptr &ruptr = uptr; -// { dg-final { regexp-test ruptr {std::unique_ptr.datum. containing 0x.*} } } +// { dg-final { regexp-test ruptr {std::unique_ptr.datum. = {get\(\) = 0x.*}} } } ExTuple tpl(6,7); -// { dg-final { note-test tpl {std::tuple containing = {[1] = 6, [2] = 7}} } } +// { dg-final { note-test tpl {std::tuple containing = {[1] = 6, [2] = 7}} } } ExTuple &rtpl = tpl; -// { dg-final { note-test rtpl {std::tuple containing = {[1] = 6, [2] = 7}} } } +// { dg-final { note-test rtpl {std::tuple containing = {[1] = 6, [2] = 7}} } } placeholder(""); // Mark SPOT use(efl); use(fl); diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc index 556ecd1cc35..27c2e405fa4 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc @@ -49,25 +49,25 @@ main() typedef std::weak_ptr weak; shared esp; -// { dg-final { note-test esp "std::shared_ptr (empty) 0x0" } } +// { dg-final { note-test esp "std::shared_ptr (empty) = {get() = 0x0}" } } weak ewp1; -// { dg-final { note-test ewp1 "std::weak_ptr (empty) 0x0" } } +// { dg-final { note-test ewp1 "std::weak_ptr (empty) = {get() = 0x0}" } } weak ewp2 = esp; -// { dg-final { note-test ewp2 "std::weak_ptr (empty) 0x0" } } +// { dg-final { note-test ewp2 "std::weak_ptr (empty) = {get() = 0x0}" } } shared sp1 = make(0x12345678); shared sp2 = sp1; -// { dg-final { note-test sp1 "std::shared_ptr (count 2, weak 0) 0x12345678" } } +// { dg-final { note-test sp1 "std::shared_ptr (use count 2, weak count 0) = {get() = 0x12345678}" } } shared sp3 = make(0x12344321); weak sp4 = sp3; weak wp1 = sp3; -// { dg-final { note-test wp1 "std::weak_ptr (count 1, weak 2) 0x12344321" } } +// { dg-final { note-test wp1 "std::weak_ptr (use count 1, weak count 2) = {get() = 0x12344321}" } } shared sp5 = make(0x56788765); weak wp2 = sp5; sp5.reset(); -// { dg-final { note-test wp2 "std::weak_ptr (expired, weak 1) 0x56788765" } } +// { dg-final { note-test wp2 "std::weak_ptr (expired, weak count 1) = {get() = 0x56788765}" } } placeholder(""); // Mark SPOT use(esp); -- 2.30.2