Have std::vector printer's iterator return bool for vector<bool>
authorMichael Weghorn <m.weghorn@posteo.de>
Wed, 19 Jun 2019 22:57:06 +0000 (22:57 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 19 Jun 2019 22:57:06 +0000 (23:57 +0100)
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.

Co-Authored-By: Jonathan Wakely <jwakely@redhat.com>
From-SVN: r272490

libstdc++-v3/ChangeLog
libstdc++-v3/python/libstdcxx/v6/printers.py
libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc

index cff842c..a481420 100644 (file)
@@ -1,3 +1,12 @@
+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.
+
 2019-06-19  Jonathan Wakely  <jwakely@redhat.com>
 
        PR libstdc++/90920 partially revert r263433
index 0514315..cd79a1f 100644 (file)
@@ -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"
 
index 60dfdc5..04c1ef6 100644 (file)
@@ -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);
index b18f5cc..ace217c 100644 (file)
@@ -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);