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: Smart pointer pretty printers


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"

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