From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 96660383B825 for ; Tue, 17 Aug 2021 13:26:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 96660383B825 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-201-YBmCf_bvPo2J_YoVHyXa4Q-1; Tue, 17 Aug 2021 09:26:39 -0400 X-MC-Unique: YBmCf_bvPo2J_YoVHyXa4Q-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 96F28760EE; Tue, 17 Aug 2021 13:26:38 +0000 (UTC) Received: from localhost (unknown [10.33.36.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29F0A5C1D5; Tue, 17 Aug 2021 13:26:37 +0000 (UTC) Date: Tue, 17 Aug 2021 14:26:37 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add pretty printer for std::error_code and std::error_condition Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="qI3we38nMx7P8OmR" Content-Disposition: inline X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Aug 2021 13:26:52 -0000 --qI3we38nMx7P8OmR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. Tested powerpc64le-linux. Committed to trunk. --qI3we38nMx7P8OmR Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 2db38d9fcacf522fe9b98ba847e79ba33abdcadc Author: Jonathan Wakely Date: Mon Aug 16 17:41:50 2021 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. diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 550e0ecdd22..e027a69ded9 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) diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc index 05950513ab0..637246b3c12 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/cxx11.cc @@ -155,6 +155,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); --qI3we38nMx7P8OmR--