This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Smart pointer pretty printers
- From: Juraj Oršulić <juraj dot orsulic at fer dot hr>
- To: Jonathan Wakely <jwakely at redhat dot com>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 2 Mar 2017 19:10:13 +0100
- Subject: Re: Smart pointer pretty printers
- Authentication-results: sourceware.org; auth=none
- References: <CAEPqvowX8wC0AE40DR2EMpAq_J7Z+q+mbOu+KAKNrrwaAOnEZQ@mail.gmail.com> <c95d0000162d4d7da2fc613a23501cbd@MAIL.fer.hr> <CAEPqvox1VGohEeo0z7jV6tsdNrU4PmcXmGe7euzZ7vk4XDH3cg@mail.gmail.com> <308301db772b4f7c95404d69db52baff@MAIL.fer.hr> <CAEPqvoxuAJtsWuXvha3Qrsek1k1OsR05OUERvcym_EkkOwDq-A@mail.gmail.com> <CAEPqvoyhD4+GzZ1rOX_qQ2brQYQH9pwvDKrf-BQ7qeb3F=Y-+Q@mail.gmail.com> <4c6aef27483e44dc9aa409dace8490c8@MAIL.fer.hr>
On Fri, Feb 24, 2017 at 5:36 PM, Jonathan Wakely <jwakely@redhat.com> wrote:
> For a patch this size we do, so I'm not going to look at your patch.
> It will probably be simpler to just do it myself, and not worry about
> paperwork.
>
> If you want to contribue in future then please do complete the
> necessary paperwork anyway:
> https://gcc.gnu.org/onlinedocs/libstdc++/manual/appendix_contributing.html#contrib.list
Hi Jonathan,
I have completed the assignment.
I am resubmitting the patch from the last time, this time as an
attachment (since the previous was broken by newlines). As before,
there are two versions of the patch: the one that I obtained by editing
printers.py on Ubuntu 16.04, which ships with gcc 5.4.0; and the
other, for the current printers.py from the gcc repo (I assume it's gcc
6?), which needs a slightly different patch because the unique_ptr
printer has significantly changed.
I have been using the gcc 5 patch without problems. I have not tested
the gcc 6 patch, but since it is very similar, I don't expect any
problems.
Let me know if you want me to expand this for other iterators.
Regards,
Juraj
--- printers.py
+++ printers.py
@@ -106,6 +106,10 @@ class SharedPointerPrinter:
def __init__ (self, typename, val):
self.typename = typename
self.val = val
+ self.pointer = val['_M_ptr']
+
+ def children (self):
+ return [('get()', self.pointer)]
def to_string (self):
state = 'empty'
@@ -116,20 +120,22 @@ class SharedPointerPrinter:
if usecount == 0:
state = 'expired, weak %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()), state)
class UniquePointerPrinter:
"Print a unique_ptr"
def __init__ (self, typename, val):
self.val = val
+ self.pointer = self.val['_M_t']['_M_head_impl']
- def to_string (self):
- v = self.val['_M_t']['_M_head_impl']
- return ('std::unique_ptr<%s> containing %s' % (str(v.type.target()),
- str(v)))
+ def children (self):
+ return [('get()', self.pointer)]
+
+ def to_string (self):
+ return ('std::unique_ptr<%s>' % (str(self.pointer.type.target())))
def get_value_from_list_node(node):
"""Returns the value held in an _List_node<_Val>"""
try:
@@ -322,9 +328,17 @@ class StdVectorIteratorPrinter:
def __init__(self, typename, val):
self.val = val
+ self.pointer = self.val['_M_current']
+
+ def children(self):
+ if not self.pointer:
+ return []
+ return [('operator->()', self.pointer)]
def to_string(self):
- return self.val['_M_current'].dereference()
+ if not self.pointer:
+ return 'non-dereferenceable iterator for std::vector'
+ return ('std::vector<%s>::iterator' % (str(self.pointer.type.target())))
class StdTuplePrinter:
"Print a std::tuple"
--- printers.py
+++ printers.py
@@ -121,6 +121,10 @@ class SharedPointerPrinter:
def __init__ (self, typename, val):
self.typename = strip_versioned_namespace(typename)
self.val = val
+ self.pointer = val['_M_ptr']
+
+ def children (self):
+ return [('get()', self.pointer)]
def to_string (self):
state = 'empty'
@@ -131,25 +135,27 @@ class SharedPointerPrinter:
if usecount == 0:
state = 'expired, weak %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()), 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
if is_specialization_of(impl_type, '__uniq_ptr_impl'): # New implementation
- v = self.val['_M_t']['_M_t']['_M_head_impl']
+ self.pointer = self.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 = self.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))
+
+ def children (self):
+ return [('get()', 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."""
@@ -350,11 +356,17 @@ class StdVectorIteratorPrinter:
def __init__(self, typename, val):
self.val = val
+ self.pointer = self.val['_M_current']
+
+ def children(self):
+ if not self.pointer:
+ return []
+ return [('operator->()', self.pointer)]
def to_string(self):
- if not self.val['_M_current']:
+ if not self.pointer:
return 'non-dereferenceable iterator for std::vector'
- return str(self.val['_M_current'].dereference())
+ return ('std::vector<%s>::iterator' % (str(self.pointer.type.target())))
class StdTuplePrinter:
"Print a std::tuple"