diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2021-08-16 17:41:50 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2021-08-17 14:22:49 +0100 |
commit | 2db38d9fcacf522fe9b98ba847e79ba33abdcadc (patch) | |
tree | 0e8c2f385a406e1a919b5b17e15a2f8ad3b8b5aa /libstdc++-v3/python | |
parent | 9c560cf23996271ee26dfc4a1d8484b85173cd12 (diff) | |
download | gcc-2db38d9fcacf522fe9b98ba847e79ba33abdcadc.zip gcc-2db38d9fcacf522fe9b98ba847e79ba33abdcadc.tar.gz gcc-2db38d9fcacf522fe9b98ba847e79ba33abdcadc.tar.bz2 |
libstdc++: Add pretty printer for std::error_code and std::error_condition
Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:
* python/libstdcxx/v6/printers.py (StdErrorCodePrinter): Define.
(build_libstdcxx_dictionary): Register printer for
std::error_code and std::error_condition.
* testsuite/libstdc++-prettyprinters/cxx11.cc: Test it.
Diffstat (limited to 'libstdc++-v3/python')
-rw-r--r-- | libstdc++-v3/python/libstdcxx/v6/printers.py | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 550e0ec..e027a69 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -18,7 +18,7 @@ import gdb import itertools import re -import sys +import sys, os, errno ### Python 2 + Python 3 compatibility code @@ -1484,6 +1484,57 @@ class StdCmpCatPrinter: name = names[int(self.val)] return 'std::{}::{}'.format(self.typename, name) +class StdErrorCatPrinter: + "Print an object derived from std::error_category" + + def __init__ (self, typename, val): + self.val = val + self.typename = typename + + def to_string (self): + gdb.set_convenience_variable('__cat', self.val) + name = gdb.parse_and_eval('$__cat->name()').string() + return 'error category = "{}"'.format(name) + +class StdErrorCodePrinter: + "Print a std::error_code or std::error_condition" + + _errno_categories = None # List of categories that use errno values + + def __init__ (self, typename, val): + self.val = val + self.typename = typename + # Do this only once ... + if StdErrorCodePrinter._errno_categories is None: + StdErrorCodePrinter._errno_categories = ['generic'] + try: + import posix + StdErrorCodePrinter._errno_categories.append('system') + except ImportError: + pass + + @staticmethod + def _category_name(cat): + "Call the virtual function that overrides std::error_category::name()" + gdb.set_convenience_variable('__cat', cat) + return gdb.parse_and_eval('$__cat->name()').string() + + def to_string (self): + value = self.val['_M_value'] + category = self._category_name(self.val['_M_cat']) + strval = str(value) + if value == 0: + default_cats = {'error_code':'system', 'error_condition':'generic'} + unqualified = self.typename.split('::')[-1] + if category == default_cats[unqualified]: + return self.typename + ' = { }' # default-constructed value + if value > 0 and category in StdErrorCodePrinter._errno_categories: + try: + strval = errno.errorcode[int(value)] + except: + pass + return '%s = {"%s": %s}' % (self.typename, category, strval) + # A "regular expression" printer which conforms to the # "SubPrettyPrinter" protocol from gdb.printing. class RxPrinter(object): @@ -1886,6 +1937,8 @@ def build_libstdcxx_dictionary (): libstdcxx_printer.add_version('std::__cxx11::', 'basic_string', StdStringPrinter) libstdcxx_printer.add_container('std::', 'bitset', StdBitsetPrinter) libstdcxx_printer.add_container('std::', 'deque', StdDequePrinter) + libstdcxx_printer.add_version('std::', 'error_code', StdErrorCodePrinter) + libstdcxx_printer.add_version('std::', 'error_condition', StdErrorCodePrinter) libstdcxx_printer.add_container('std::', 'list', StdListPrinter) libstdcxx_printer.add_container('std::__cxx11::', 'list', StdListPrinter) libstdcxx_printer.add_container('std::', 'map', StdMapPrinter) |