On 19/06/19 19:04 +0200, Michael Weghorn wrote:
Hi everyone,
the Python pretty printer for a 'std::vector<bool>' currently returns
integers as values for the elements, which e.g. leads to the situation
that a 'gdb.Value' constructed from that doesn't have 'bool' type, but
an integer type ('long long' for my test with gdb 8.2.1 on Debian
testing, amd64). Returning bool values ('True'/'False') would make sure
that the type is clear and can thus help improve the displayed type.
More details in [1].
The attached patch changes the behaviour of the pretty printer accordingly.
I'd be glad to receive feedback on this and also notes in case anything
else is needed. (This is my first contribution.)
Thanks, the patch looks fine and is small enough that we can accept it
without a copyright assignment, but if you plan to contribute again
you should look into https://gcc.gnu.org/contribute.html#legal
I think I'd prefer to have the 'elt' variable be the actual element
(not the unsigned long that contains the element) so I'll adjust the
patch to do this instead:
elt = bool(self.item.dereference() & (1 << self.so))
So far, I've tested this with GDB 8.2.1 on Debian testing.
It looks like we don't have any tests in the testsuite for printing
vector<bool>, so I'll add one to verify this behaviour and commit your
patch - thanks!
I've attached what I'm testing and plan to commit.
commit 57396b03636d261fbce3f57cb901ed90405b0c8b
Author: Jonathan Wakely <jwak...@redhat.com>
Date: Wed Jun 19 20:32:45 2019 +0100
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.wegh...@posteo.de>
Jonathan Wakely <jwak...@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>.
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..b7dbcb67d5a 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 of length 4, capacity 100 = \\{true, false, false, true\\}} } }
+
__gnu_cxx::slist<int> sll;
sll.push_front(23);
sll.push_front(47);