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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 72265385483D for ; Thu, 24 Aug 2023 12:41:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 72265385483D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692880917; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TrcAMlrP+K9uvOrAKg3VJVK+wgMSaoLO3il8rC0mrM8=; b=gnGdNKCLMh4NqZiN/WvoyH2OXWdMyRsoVF0Wep4Nr2oQHh/H2MeYtylOx0ZXKNpSCrvfyD yv8re1jxsE+Lvz27ffsMV9ntIOT/mtAHIACfie8TPldzQsfFCVDYjcFHMs7Gop7EISmwVf w73wjfDMNYmJNJlFA/+C1BoRPwE8vj8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-614-NCfHCRw7PDCum9D-fYywcg-1; Thu, 24 Aug 2023 08:41:54 -0400 X-MC-Unique: NCfHCRw7PDCum9D-fYywcg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 81FE1101A528; Thu, 24 Aug 2023 12:41:53 +0000 (UTC) Received: from localhost (unknown [10.42.28.204]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4761E140E963; Thu, 24 Aug 2023 12:41:53 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add pretty printer for std::locale Date: Thu, 24 Aug 2023 13:41:23 +0100 Message-ID: <20230824124152.431392-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 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_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. Maybe worth backporting. -- >8 -- Print the locale's name, except when it uses the same named C locale for all categories except one, in which case print something like: std::locale = "en_GB.UTF-8" with "LC_CTYPE=en_US.UTF-8" libstdc++-v3/ChangeLog: * python/libstdcxx/v6/printers.py (StdLocalePrinter): New printer class. * testsuite/libstdc++-prettyprinters/locale.cc: New test. --- libstdc++-v3/python/libstdcxx/v6/printers.py | 45 +++++++++++++++++++ .../libstdc++-prettyprinters/locale.cc | 36 +++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 libstdc++-v3/testsuite/libstdc++-prettyprinters/locale.cc diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 1a8017adb22..37a447b514b 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -2131,6 +2131,50 @@ class StdChronoTimeZoneRulePrinter: return 'time_zone rule {} from {} to {} starting on {}'.format( self.val['name'], self.val['from'], self.val['to'], start) +class StdLocalePrinter: + "Print a std::locale" + + def __init__(self, typename, val): + self.val = val + self.typename = typename + + def to_string(self): + names = self.val['_M_impl']['_M_names'] + mod = '' + if names[0] == 0: + name = '*' + else: + cats = gdb.parse_and_eval(self.typename + '::_S_categories') + ncat = gdb.parse_and_eval(self.typename + '::_S_categories_size') + n = names[0].string(); + cat = cats[0].string() + name = '{}={}'.format(cat, n) + cat_names = {cat: n} + i = 1 + while i < ncat and names[i] != 0: + n = names[i].string() + cat = cats[i].string() + name = '{};{}={}'.format(name, cat, n) + cat_names[cat] = n + i = i + 1 + uniq_names = set(cat_names.values()) + if len(uniq_names) == 1: + name = n + elif len(uniq_names) == 2: + n1, n2 = (uniq_names) + name_list = list(cat_names.values()) + other = None + if name_list.count(n1) == 1: + name = n2 + other = n1 + elif name_list.count(n2) == 1: + name = n1 + other = n2 + if other is not None: + cat = next(c for c,n in cat_names.items() if n == other) + mod = ' with "{}={}"'.format(cat, other) + return 'std::locale = "{}"{}'.format(name, mod) + # A "regular expression" printer which conforms to the # "SubPrettyPrinter" protocol from gdb.printing. @@ -2585,6 +2629,7 @@ def build_libstdcxx_dictionary (): libstdcxx_printer.add_version('std::', 'unique_ptr', UniquePointerPrinter) libstdcxx_printer.add_container('std::', 'vector', StdVectorPrinter) # vector + libstdcxx_printer.add_version('std::', 'locale', StdLocalePrinter) if hasattr(gdb.Value, 'dynamic_type'): libstdcxx_printer.add_version('std::', 'error_code', diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/locale.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/locale.cc new file mode 100644 index 00000000000..66d42f99432 --- /dev/null +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/locale.cc @@ -0,0 +1,36 @@ +// { dg-do run } +// { dg-options "-g -O0" } +// { dg-require-namedlocale "fr_FR.ISO8859-15" } +// { dg-require-namedlocale "de_DE.ISO8859-15" } +// { dg-require-namedlocale "en_US.ISO8859-1" } + +#include +#include // for ISO_8859 macro + +int main() +{ + std::locale l1 = std::locale::classic(); +// { dg-final { note-test l1 {std::locale = "C"} } } + + std::locale l2(ISO_8859(15,fr_FR)); +// { dg-final { regexp-test l2 {std::locale = "fr_FR.ISO8859-15(@euro)?"} } } + + std::locale l3(l2, ISO_8859(15,de_DE), std::locale::time); +// { dg-final { regexp-test l3 {std::locale = "fr_FR.ISO8859-15(@euro)?" with "LC_TIME=de_DE.ISO8859-15(@euro)?"} } } + + std::locale l4(l3, ISO_8859(1,en_US), std::locale::monetary); +// We don't know which order the categories will occur in the string, +// so test three times, checking for the required substring each time: +// { dg-final { regexp-test l4 {std::locale = "(.*;)?LC_CTYPE=fr_FR.ISO8859-15(@euro)?(;.*)?"} } } + std::locale l5 = l4; +// { dg-final { regexp-test l5 {std::locale = "(.*;)?LC_TIME=de_DE.ISO8859-15(@euro)?(;.*)?"} } } + std::locale l6 = l5; +// { dg-final { regexp-test l6 {std::locale = "(.*;)?LC_MONETARY=en_US.ISO8859-1(;.*)?"} } } + + std::locale l7(l1, &std::use_facet >(l1)); +// { dg-final { regexp-test l7 {std::locale = "\*"} } } + + return 0; // Mark SPOT +} + +// { dg-final { gdb-test SPOT } } -- 2.41.0