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: PR libstdc++/90945 Patch to have pretty printer for std::vector<bool> return bool intead of int for elements


On 19/06/19 21:58 +0200, Michael Weghorn wrote:
On 19/06/2019 21.54, Jonathan Wakely wrote:
+  std::vector<bool> vb;
+  vb.reserve(100);
+  vb.push_back(true);
+  vb.push_back(true);
+  vb.push_back(false);
+  vb.push_back(false);
+  vb.push_back(true);
+  vb.erase(vb.begin());
+// { dg-final { regexp-test vb {std::(__debug::)?vector of length 4,
capacity 100 = \\{true, false, false, true\\}} } }
+

This inserts 5 elements, so I'd expect that either "vector of length 5"
and an additional "true" element at the beginning need to be added for
the expected result or one of the two first 'vb.push_back(true)' needs
to be removed.

It inserts five then erases one, the test is right.

Except that it should be capacity=128, because the capacity increases
in units of 64 bits. of course.

Yes, of course. Sorry, I was missing that.

Here's the patch that I've committed to trunk, after testing.

This is probably OK to backport to the release branches as well, so
I'll add that to my TODO list.

Thanks again for the patch!


commit 6c7d761a3f5fd7d19795d1d4b9b027a04b3fe88b
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Wed Jun 19 22:57:06 2019 +0000

    Have std::vector printer's iterator return bool for vector<bool>
    
    Have the pretty-printer for 'std::vector<bool>' return a
    value of type 'bool' rather than an 'int'.
    
    This way, the type is clear and that can be used for better
    display and a 'gdb.Value' constructed from the returned value
    will have type 'bool' again, not e.g. 'long long' as happened
    previously (at least with GDB 8.2.1 on amd64).
    
    2019-06-19  Michael Weghorn  <m.weghorn@posteo.de>
                Jonathan Wakely  <jwakely@redhat.com>
    
            PR libstdc++/90945
            * python/libstdcxx/v6/printers.py (StdVectorPrinter._iterator): Use
            values of type bool for vector<bool> elements.
            * testsuite/libstdc++-prettyprinters/simple.cc: Test vector<bool>.
            * testsuite/libstdc++-prettyprinters/simple11.cc: Likewise.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@272490 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 05143153bee..cd79a1fa6e6 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -362,16 +362,12 @@ class StdVectorPrinter:
             if self.bitvec:
                 if self.item == self.finish and self.so >= self.fo:
                     raise StopIteration
-                elt = self.item.dereference()
-                if elt & (1 << self.so):
-                    obit = 1
-                else:
-                    obit = 0
+                elt = bool(self.item.dereference() & (1 << self.so))
                 self.so = self.so + 1
                 if self.so >= self.isize:
                     self.item = self.item + 1
                     self.so = 0
-                return ('[%d]' % count, obit)
+                return ('[%d]' % count, elt)
             else:
                 if self.item == self.finish:
                     raise StopIteration
@@ -382,7 +378,7 @@ class StdVectorPrinter:
     def __init__(self, typename, val):
         self.typename = strip_versioned_namespace(typename)
         self.val = val
-        self.is_bool = val.type.template_argument(0).code  == gdb.TYPE_CODE_BOOL
+        self.is_bool = val.type.template_argument(0).code == gdb.TYPE_CODE_BOOL
 
     def children(self):
         return self._iterator(self.val['_M_impl']['_M_start'],
@@ -422,6 +418,8 @@ class StdVectorIteratorPrinter:
             return 'non-dereferenceable iterator for std::vector'
         return str(self.val['_M_current'].dereference())
 
+# TODO add printer for vector<bool>'s _Bit_iterator and _Bit_const_iterator
+
 class StdTuplePrinter:
     "Print a std::tuple"
 
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
index 60dfdc597f3..04c1ef683a6 100644
--- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
@@ -116,6 +116,16 @@ main()
   std::vector<int>::iterator viter0;
 // { dg-final { note-test viter0 {non-dereferenceable iterator for std::vector} } }
 
+  std::vector<bool> vb;
+  vb.reserve(100);
+  vb.push_back(true);
+  vb.push_back(true);
+  vb.push_back(false);
+  vb.push_back(false);
+  vb.push_back(true);
+  vb.erase(vb.begin());
+// { dg-final { regexp-test vb {std::(__debug::)?vector<bool> of length 4, capacity 128 = \\{true, false, false, true\\}} } }
+
   __gnu_cxx::slist<int> sll;
   sll.push_front(23);
   sll.push_front(47);
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc
index b18f5cc22a9..ace217cc9e8 100644
--- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc
@@ -109,6 +109,16 @@ main()
   std::vector<int>::iterator viter0;
 // { dg-final { note-test viter0 {non-dereferenceable iterator for std::vector} } }
 
+  std::vector<bool> vb;
+  vb.reserve(100);
+  vb.push_back(true);
+  vb.push_back(true);
+  vb.push_back(false);
+  vb.push_back(false);
+  vb.push_back(true);
+  vb.erase(vb.begin());
+// { dg-final { regexp-test vb {std::(__debug::)?vector<bool> of length 4, capacity 128 = \\{true, false, false, true\\}} } }
+
   __gnu_cxx::slist<int> sll;
   sll.push_front(23);
   sll.push_front(47);

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