From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 8F4B73858D35 for ; Mon, 7 Nov 2022 18:29:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8F4B73858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca Received: from [10.0.0.11] (unknown [217.28.27.60]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 08FB51E0CB; Mon, 7 Nov 2022 13:29:57 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1667845798; bh=DGYsgC8ImahJUrmAVHAh/eV5cpD0Dm8HX9bGzwtJ2Uw=; h=Date:Subject:To:References:From:In-Reply-To:From; b=ZcutP2tp0APu2ErPYQzqdTXK8JLDGuFBlGWRUwWetj3PmnjsgfprcoHknC/qfe+jL kFZ1EjbrLq0Zef7Sc12hlIC34DnBvODnLzrj8vP6eaKFMz1svGfS/slHJWLdrIL3qW Kfyp6FWWzMEMaMxNRnKTDf2xPRUtMfm30HYO8YXU= Message-ID: <57afccc8-d308-0b5e-e471-0b944b8cdff9@simark.ca> Date: Mon, 7 Nov 2022 13:29:57 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v5] enum_flags to_string (was: Re: [PATCH v2 07/29] Thread options & clone events (core + remote)) Content-Language: en-US To: Pedro Alves , gdb-patches@sourceware.org References: <20220713222433.374898-1-pedro@palves.net> <20220713222433.374898-8-pedro@palves.net> <5d68cd36-e8d6-e8ad-5428-863e79742062@simark.ca> <6b91f8f6-d3b5-c44b-297c-ce1c3a1c80f6@palves.net> <6aee3c00-b334-97e2-62f7-0434822bcf7a@simark.ca> <47ef6c92-c4f6-448f-c4c3-71da4618107c@palves.net> <38cb6f92-34d5-4830-95d8-82bd0a2b113e@simark.ca> From: Simon Marchi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_PASS,SPF_PASS,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: On 11/7/22 12:26, Pedro Alves wrote: > On 2022-10-31 12:47 p.m., Simon Marchi wrote: >> On 10/28/22 14:23, Pedro Alves wrote: > > ... > >>> @@ -183,6 +194,52 @@ class enum_flags >>> /* Binary operations involving some unrelated type (which would be a >>> bug) are implemented as non-members, and deleted. */ >>> >>> + /* Convert this object to a std::string, using MAPPING as >>> + enumerator-to-string mapping array. This is not meant to be >>> + called directly. Instead, enum_flags specializations should have >>> + their own to_string function wrapping this one, thus hidding the >>> + mapping array from callers. */ >>> + >>> + template >>> + std::string >>> + to_string (const string_mapping (&mapping)[N]) const >>> + { >>> + enum_type flags = raw (); >>> + std::string res = hex_string (flags); >>> + res += " ["; >>> + >>> + bool need_space = false; >>> + for (const auto &entry : mapping) >>> + { >>> + if ((flags & entry.flag) != 0) >>> + { >>> + /* Do op~ in the underlying type, because if enum_type's >>> + underlying type is signed, op~ won't be defined for >>> + it. */ >>> + flags &= (enum_type) ~(underlying_type) entry.flag; >> >> This line gives me (GCC 12): >> >> CXX unittests/enum-flags-selftests.o >> In file included from /home/simark/src/binutils-gdb/gdb/defs.h:65, >> from /home/simark/src/binutils-gdb/gdb/unittests/enum-flags-selftests.c:20: >> /home/simark/src/binutils-gdb/gdb/../gdbsupport/enum-flags.h: In instantiation of ‘std::string enum_flags::to_string(const string_mapping (&)[N]) const [with long unsigned int N = 2; E = selftests::enum_flags_tests::test_uflag; std::string = std::__cxx11::basic_string]’: >> /home/simark/src/binutils-gdb/gdb/unittests/enum-flags-selftests.c:388:26: required from here >> /home/simark/src/binutils-gdb/gdb/../gdbsupport/enum-flags.h:219:19: error: invalid conversion from ‘unsigned int’ to ‘enum_flags::enum_type’ {aka ‘selftests::enum_flags_tests::test_uflag’} [-fpermissive] >> 219 | flags &= (enum_type) ~(underlying_type) entry.flag; >> | ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> | | >> | unsigned int >> >> This builds though: >> >> flags = (enum_type) (flags & (enum_type) (~(underlying_type) entry.flag)); >> >> I'm not sure why, the operator & between an enum_type and an enum_type >> is supposed to return an enum_type already. >> > > Man... The problem is that enum_flags::to_string is defined inside the template > class, before the global operators for enum_type are defined, so the to_string > method is being compiled as if the global operators don't exist. I guess newer > GCC is more stringent than the GCC 9.4 from Ubuntu 20.04, which is what I was > using. The fix is then to move the method outside the class, after the operators. Geez, I would not have found that. > Here's a new version... > > I'm now also doing the op~ using an unsigned type, in case ubsan complains > or something. > > And that made me realize, that only testing with the unsigned enum completely bypasses > the need for these casts in the first place, so better re-add testing with the signed > enum. Better yet, test both signed and unsigned. > > I also tweaked the testcase to no longer need "test_flag (0xff)" -- I remembered what > was the original reason I had added that -- it was to check whether multiple > leftover unmapped flags end up printed with a single hex. > > I also changed the numbers behind FLAG1, FLAG2, etc., to make it easier to > come up with the numbers for the tests. > > Tested GCC 9, 13 (some self-built trunk version from a few months back), clang 10 and clang 13. Thanks, I gave it a quick test, LGTM: Approved-By: Simon Marchi Simon