libstdc++: Pretty printers for _Bit_reference and _Bit_iterator
authorMichael Weghorn <m.weghorn@posteo.de>
Tue, 1 Dec 2020 21:19:20 +0000 (21:19 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 1 Dec 2020 21:35:32 +0000 (21:35 +0000)
'std::_Bit_iterator' and 'std::_Bit_const_iterator' are the iterators
used by 'std::vector<bool>'.
'std::_Bit_reference' is e.g. used in range-based for loops over
'std::vector<bool>'  like

    std::vector<bool> vb {true, false, false};
    for (auto b : vb) {
        // b is of type std::_Bit_reference here
        // ...
    }

Like iterators of vectors for other types, the actual value is printed.

libstdc++-v3/ChangeLog:

* python/libstdcxx/v6/printers.py (StdBitIteratorPrinter)
(StdBitReferencePrinter): Add pretty-printers for
_Bit_reference, _Bit_iterator and _Bit_const_iterator.
* testsuite/libstdc++-prettyprinters/simple.cc: Test
std::_Bit_reference, std::_Bit_iterator and
std::_Bit_const_iterator.
* testsuite/libstdc++-prettyprinters/simple11.cc: Likewise.

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

index c0f061f79c1f03715d10091f8b2955d86bca9ad2..478e44eefdfde02d9b1980f7a7513299298ee9df 100644 (file)
@@ -479,7 +479,27 @@ 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 StdBitIteratorPrinter:
+    "Print std::vector<bool>'s _Bit_iterator and _Bit_const_iterator"
+
+    def __init__(self, typename, val):
+        self.val = val
+
+    def to_string(self):
+        if not self.val['_M_p']:
+            return 'non-dereferenceable iterator for std::vector<bool>'
+        return bool(self.val['_M_p'].dereference() & (1 << self.val['_M_offset']))
+
+class StdBitReferencePrinter:
+    "Print std::_Bit_reference"
+
+    def __init__(self, typename, val):
+        self.val = val
+
+    def to_string(self):
+        if not self.val['_M_p']:
+            return 'invalid std::_Bit_reference'
+        return bool(self.val['_M_p'].dereference() & (self.val['_M_mask']))
 
 class StdTuplePrinter:
     "Print a std::tuple"
@@ -1965,6 +1985,12 @@ def build_libstdcxx_dictionary ():
                                         StdDequeIteratorPrinter)
         libstdcxx_printer.add_version('__gnu_cxx::', '__normal_iterator',
                                       StdVectorIteratorPrinter)
+        libstdcxx_printer.add_version('std::', '_Bit_iterator',
+                                      StdBitIteratorPrinter)
+        libstdcxx_printer.add_version('std::', '_Bit_const_iterator',
+                                      StdBitIteratorPrinter)
+        libstdcxx_printer.add_version('std::', '_Bit_reference',
+                                      StdBitReferencePrinter)
         libstdcxx_printer.add_version('__gnu_cxx::', '_Slist_iterator',
                                       StdSlistIteratorPrinter)
         libstdcxx_printer.add_container('std::', '_Fwd_list_iterator',
index 4b44be594f504dc5d50c68226a4acdeac5a08ce6..9821d1805cf1968771a1a08f8bfc22c4aad457eb 100644 (file)
@@ -127,6 +127,37 @@ main()
   vb.erase(vb.begin());
 // { dg-final { regexp-test vb {std::(__debug::)?vector<bool> of length 5, capacity 128 = \\{true, true, false, false, true\\}} } }
 
+  std::vector<bool>::iterator vbIt = vb.begin();
+// { dg-final { note-test vbIt {true} } }
+  std::vector<bool>::iterator vbIt2 = ++vbIt;
+// { dg-final { note-test vbIt2 {true} } }
+  std::vector<bool>::iterator vbIt3 = ++vbIt;
+// { dg-final { note-test vbIt3 {false} } }
+  std::vector<bool>::iterator vbIt4 = ++vbIt;
+// { dg-final { note-test vbIt4 {false} } }
+  std::vector<bool>::iterator vbIt5 = ++vbIt;
+// { dg-final { note-test vbIt5 {true} } }
+
+  std::vector<bool>::const_iterator vbcIt = vb.begin();
+// { dg-final { note-test vbcIt {true} } }
+
+  std::vector<bool>::iterator vbIt0;
+// { dg-final { note-test vbIt0 {non-dereferenceable iterator for std::vector<bool>} } }
+
+  std::_Bit_reference br = *vb.begin();
+// { dg-final { note-test br {true} } }
+  std::_Bit_reference br2 = *vbIt2;
+// { dg-final { note-test br2 {true} } }
+  std::_Bit_reference br3 = *vbIt3;
+// { dg-final { note-test br3 {false} } }
+  std::_Bit_reference br4 = *vbIt4;
+// { dg-final { note-test br4 {false} } }
+  std::_Bit_reference br5 = *vbIt5;
+// { dg-final { note-test br5 {true} } }
+
+ std::_Bit_reference br0;
+// { dg-final { note-test br0 {invalid std::_Bit_reference} } }
+
   __gnu_cxx::slist<int> sll;
   sll.push_front(23);
   sll.push_front(47);
index 0ebd80a42e914eda98d51d8c23b6f5d4fe4ca024..519565693b75c6173e8eb8b04f58fc3612ea3187 100644 (file)
@@ -120,6 +120,37 @@ main()
   vb.erase(vb.begin());
 // { dg-final { regexp-test vb {std::(__debug::)?vector<bool> of length 5, capacity 128 = \\{true, true, false, false, true\\}} } }
 
+  std::vector<bool>::iterator vbIt = vb.begin();
+// { dg-final { note-test vbIt {true} } }
+  std::vector<bool>::iterator vbIt2 = ++vbIt;
+// { dg-final { note-test vbIt2 {true} } }
+  std::vector<bool>::iterator vbIt3 = ++vbIt;
+// { dg-final { note-test vbIt3 {false} } }
+  std::vector<bool>::iterator vbIt4 = ++vbIt;
+// { dg-final { note-test vbIt4 {false} } }
+  std::vector<bool>::iterator vbIt5 = ++vbIt;
+// { dg-final { note-test vbIt5 {true} } }
+
+  std::vector<bool>::const_iterator vbcIt = vb.cbegin();
+// { dg-final { note-test vbcIt {true} } }
+
+  std::vector<bool>::iterator vbIt0;
+// { dg-final { note-test vbIt0 {non-dereferenceable iterator for std::vector<bool>} } }
+
+  std::_Bit_reference br = *vb.begin();
+// { dg-final { note-test br {true} } }
+  std::_Bit_reference br2 = *vbIt2;
+// { dg-final { note-test br2 {true} } }
+  std::_Bit_reference br3 = *vbIt3;
+// { dg-final { note-test br3 {false} } }
+  std::_Bit_reference br4 = *vbIt4;
+// { dg-final { note-test br4 {false} } }
+  std::_Bit_reference br5 = *vbIt5;
+// { dg-final { note-test br5 {true} } }
+
+ std::_Bit_reference br0;
+// { dg-final { note-test br0 {invalid std::_Bit_reference} } }
+
   __gnu_cxx::slist<int> sll;
   sll.push_front(23);
   sll.push_front(47);