Smart pointer pretty printers
Juraj Oršulić
juraj.orsulic@fer.hr
Fri Feb 24 16:12:00 GMT 2017
I now realized that I worked on an older version of the printers.py
file. I have produced a patch against a recent version, which had some
changes to the unique pointer printer regarding a newer implementation
of the unique pointer. The patch includes this for both the unique and
shared pointers. Also, I have added the same for the vector iterator
just for your consideration. Do you think the same could be applied
for other STL container iterators?
Also, let me know if you need a copyright assignment.
Regards, Juraj
--- libstdcxx/v6/printers.py
+++ libstdcxx/v6/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"
More information about the Libstdc++
mailing list