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 9D3E23854148 for ; Mon, 31 Oct 2022 12:47:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9D3E23854148 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) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 0446E1E0D3; Mon, 31 Oct 2022 08:47:11 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1667220432; bh=g3miqXH8exfpbiZaqmDQi4ie+4eoxIxNaLLPyyGU+To=; h=Date:Subject:To:References:From:In-Reply-To:From; b=uCqMoKy61Of3W8O/SWc4UsCfnDidtYsydfnB6PgI9ekCsZzjxpT/mx7OHwecHY3ln u/CydKEemm0Z6Cizxr0pNYmw+deCgvMQ9gVyS1NESu2KQMuqSZuySdgzWDiWxdlMky G+jVxXmKt4EyAaYO7zgnGGbn2MsY/69VE6D7/nJk= Message-ID: <38cb6f92-34d5-4830-95d8-82bd0a2b113e@simark.ca> Date: Mon, 31 Oct 2022 08:47:11 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: [PATCH v3] 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> From: Simon Marchi In-Reply-To: <47ef6c92-c4f6-448f-c4c3-71da4618107c@palves.net> 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 10/28/22 14:23, Pedro Alves wrote: > On 2022-10-28 4:59 p.m., Simon Marchi wrote: >> On 2022-10-28 07 h 08, Pedro Alves wrote: >>> On 2022-10-28 11:26 a.m., Pedro Alves wrote: > ... >>> + /* Check string conversion. */ >>> + { >>> + SELF_CHECK (to_string (0) == "0x0 []"); >>> + SELF_CHECK (to_string (FLAG1) == "0x2 [FLAG1]"); >>> + SELF_CHECK (to_string (FLAG1 | FLAG3) == "0xa [FLAG1 FLAG3]"); >>> + SELF_CHECK (to_string (FLAG1 | FLAG2 | FLAG3) == "0xe [FLAG1 FLAG3 0x4]"); >>> + SELF_CHECK (to_string (FLAG2) == "0x4 [0x4]"); >>> + SELF_CHECK (to_string (test_flag (0xff)) == "0xff [FLAG1 FLAG3 0xf5]"); >> >> Ish, this crashes with: >> >> /home/smarchi/src/binutils-gdb/gdb/../gdbsupport/enum-flags.h:191:12: runtime error: load of value 255, which is not a valid value for type 'test_flag' > > Ah. That's because I missed that test_flag doesn't have an explicit underlying > type, as in "enum test_flag : underlying". The rules are different if there's > an explicit underlying type or not. enums with an explicit type can hold any value > of the underlying type. Best to fix the testcase. We could either remove > that particular test, which is not really that important, as what it tests is > exercised by the previous two tests, or switch to test_uflags instead, which > does have an explicit underlying type. I did the latter. Ok, didn't know that. This is fine with me. > @@ -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. Simon