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: Improved pretty printing for smart pointers


Tom,

I took your advice into account, see the attached patch.

Notice that I also improved pretty printing for weak pointers:

* no children when there are no reference counters
* no managed value node when use count is zero

Michael


On Monday 29 April 2013 17:21:47 Tom Tromey wrote:
> >>>>> "Michael" == Michael Marte <informarte@freenet.de> writes:
> Michael> creating an iterator is necessary in gdb 7.4.1.
> 
> Yeah, I forgot about that.  It was a gdb bug:
> http://sourceware.org/bugzilla/show_bug.cgi?id=14386
> 
> Michael> Actually, this is what I tried first:
> [...]
> Michael>             return [('Use count',
> self.val['_M_refcount']['_M_pi']['_M_use_count']), Michael>               
>      ('Weak count', self.val['_M_refcount']['_M_pi']['_M_weak_count']),
> Michael>                     ('Managed value', managedValue)]
> 
> You can 'return iter([...])'.
> With a comment about the gdb bug I think this would be easier to
> understand than having a separate iterator class.
> 
> Tom
Index: libstdcxx/v6/printers.py
===================================================================
--- libstdcxx/v6/printers.py	(revision 197535)
+++ libstdcxx/v6/printers.py	(working copy)
@@ -61,17 +61,32 @@
         self.typename = typename
         self.val = val
 
+    def children (self):
+        result = []
+        refCounts = self.val['_M_refcount']['_M_pi']
+        if refCounts != 0:
+            useCount = refCounts['_M_use_count']
+            weakCount = refCounts['_M_weak_count']
+            result.append(('Use count', useCount))
+            result.append(('Weak count', weakCount))
+            if (useCount != 0):
+                managedValue = self.val['_M_ptr']
+                result.append(('Managed value', managedValue))
+        # We need the iter function to work around http://sourceware.org/bugzilla/show_bug.cgi?id=14386.
+        return iter(result)
+
     def to_string (self):
         state = 'empty'
-        refcounts = self.val['_M_refcount']['_M_pi']
-        if refcounts != 0:
-            usecount = refcounts['_M_use_count']
-            weakcount = refcounts['_M_weak_count']
-            if usecount == 0:
-                state = 'expired, weak %d' % weakcount
+        refCounts = self.val['_M_refcount']['_M_pi']
+        if refCounts != 0:
+            useCount = refCounts['_M_use_count']
+            weakCount = refCounts['_M_weak_count']
+            if useCount == 0:
+                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)
+        managedValue = self.val['_M_ptr']
+        return '%s<%s> (%s) to %s' % (self.typename, str(managedValue.type.target()), state, managedValue)
 
 class UniquePointerPrinter:
     "Print a unique_ptr"
@@ -79,10 +94,17 @@
     def __init__ (self, typename, val):
         self.val = val
 
+    def children (self):
+        result = []
+        managedValue = self.val['_M_t']['_M_head_impl']
+        if managedValue != 0:
+            result = [('Managed value', managedValue)]
+        # We need the iter function to work around http://sourceware.org/bugzilla/show_bug.cgi?id=14386.
+        return iter(result)
+
     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)))
+        managedValue = self.val['_M_t']['_M_head_impl']
+        return ('std::unique_ptr<%s> to %s' % (str(managedValue.type.target()), str(managedValue)))
 
 class StdListPrinter:
     "Print a std::list"

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