From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id A76F63858437; Wed, 13 Oct 2021 19:42:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A76F63858437 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r9-9777] libstdc++: Add pretty printer for std::error_code and std::error_condition X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-9 X-Git-Oldrev: 6732e0e93d7528568085cd3cf8eaaed20ca003a3 X-Git-Newrev: 780aba4a149b3f17a4cee7bf8cc42d1b8a79ec91 Message-Id: <20211013194239.A76F63858437@sourceware.org> Date: Wed, 13 Oct 2021 19:42:39 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Oct 2021 19:42:39 -0000 https://gcc.gnu.org/g:780aba4a149b3f17a4cee7bf8cc42d1b8a79ec91 commit r9-9777-g780aba4a149b3f17a4cee7bf8cc42d1b8a79ec91 Author: Jonathan Wakely Date: Mon Aug 16 17:41:50 2021 +0100 libstdc++: Add pretty printer for std::error_code and std::error_condition Signed-off-by: Jonathan Wakely 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. (cherry picked from commit 2db38d9fcacf522fe9b98ba847e79ba33abdcadc) Diff: --- libstdc++-v3/python/libstdcxx/v6/printers.py | 43 +++++++++++++++++++++- .../testsuite/libstdc++-prettyprinters/cxx11.cc | 19 ++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 0c94a038437..25d9d2e1868 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 @@ -1449,6 +1449,45 @@ class StdPairPrinter: return None +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): @@ -1846,6 +1885,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) diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc index 71a50988924..bff9d1b27de 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc @@ -161,6 +161,25 @@ main() // { dg-final { note-test tpl {std::tuple containing = {[1] = 6, [2] = 7}} } } ExTuple &rtpl = tpl; // { dg-final { note-test rtpl {std::tuple containing = {[1] = 6, [2] = 7}} } } + + std::error_code e0; + // { dg-final { note-test e0 {std::error_code = { }} } } + std::error_condition ec0; + // { dg-final { note-test ec0 {std::error_condition = { }} } } + std::error_code einval = std::make_error_code(std::errc::invalid_argument); + // { dg-final { note-test einval {std::error_code = {"generic": EINVAL}} } } + std::error_condition ecinval = std::make_error_condition(std::errc::invalid_argument); + // { dg-final { note-test ecinval {std::error_condition = {"generic": EINVAL}} } } + + struct custom_cat : std::error_category { + const char* name() const noexcept { return "miaow"; } + std::string message(int) const { return ""; } + } cat; + std::error_code emiaow(42, cat); + // { dg-final { note-test emiaow {std::error_code = {"miaow": 42}} } } + std::error_condition ecmiaow(42, cat); + // { dg-final { note-test ecmiaow {std::error_condition = {"miaow": 42}} } } + placeholder(""); // Mark SPOT use(efl); use(fl);