From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id C415A3865488; Mon, 30 Jan 2023 20:06:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C415A3865488 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675109196; bh=qTkb5TuNykGpaqWkiNkO/nqfDpBPIIqWdUs41KpfS+E=; h=From:To:Subject:Date:From; b=Ze74NZH6134t/YnC2lXmeL9BXv3wupLyAMYax8mI9dP64OwRTuG+yyfj5ttBS28dv KoCYie00uHHyZu8/28TGBgyhPzCs/PlCz70F6eJG3NpL00iu2oRWYRCXXysqTrSK/T dkvKvwn3WauwNEnTHNuWN/Nj5s59ox3NB0BEzLi4= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Simon Marchi To: gdb-cvs@sourceware.org Subject: [binutils-gdb] enum_flags to_string X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: c121e82c39659d1140b1a6a3cfd72c765741b9f5 X-Git-Newrev: 8c4f70ffe7980fe8654660d84592807da2e6f8bc Message-Id: <20230130200636.C415A3865488@sourceware.org> Date: Mon, 30 Jan 2023 20:06:36 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D8c4f70ffe798= 0fe8654660d84592807da2e6f8bc commit 8c4f70ffe7980fe8654660d84592807da2e6f8bc Author: Pedro Alves Date: Tue Oct 25 15:39:37 2022 +0100 enum_flags to_string =20 This commit introduces shared infrastructure that can be used to implement enum_flags -> to_string functions. With this, if we want to support converting a given enum_flags specialization to string, we just need to implement a function that provides the enumerator->string mapping, like so: =20 enum some_flag { SOME_FLAG1 =3D 1 << 0, SOME_FLAG2 =3D 1 << 1, SOME_FLAG3 =3D 1 << 2, }; =20 DEF_ENUM_FLAGS_TYPE (some_flag, some_flags); =20 static std::string to_string (some_flags flags) { static constexpr some_flags::string_mapping mapping[] =3D { MAP_ENUM_FLAG (SOME_FLAG1), MAP_ENUM_FLAG (SOME_FLAG2), MAP_ENUM_FLAG (SOME_FLAG3), }; return flags.to_string (mapping); } =20 .. and then to_string(SOME_FLAG2 | SOME_FLAG3) produces a string like "0x6 [SOME_FLAG2 SOME_FLAG3]". =20 If we happen to forget to update the mapping array when we introduce a new enumerator, then the string representation will pretty-print the flags it knows about, and then the leftover flags in hex (one single number). For example, if we had missed mapping SOME_FLAG2 above, we'd end up with: =20 to_string(SOME_FLAG2 | SOME_FLAG3) =3D> "0x6 [SOME_FLAG2 0x4]"); =20 Other than in the unit tests included, no actual usage of the functionality is added in this commit. =20 Approved-By: Simon Marchi Change-Id: I835de43c33d13bc0c95132f42c3f97318b875779 Diff: --- gdb/unittests/enum-flags-selftests.c | 69 ++++++++++++++++++++++++++++++++= ---- gdbsupport/enum-flags.h | 66 ++++++++++++++++++++++++++++++++= ++ 2 files changed, 129 insertions(+), 6 deletions(-) diff --git a/gdb/unittests/enum-flags-selftests.c b/gdb/unittests/enum-flag= s-selftests.c index 0780fb8acf7..2d3c555d254 100644 --- a/gdb/unittests/enum-flags-selftests.c +++ b/gdb/unittests/enum-flags-selftests.c @@ -359,21 +359,47 @@ CHECK_VALID (true, bool, NF (1) =3D=3D char (1)) =20 enum test_flag { - FLAG1 =3D 1 << 1, - FLAG2 =3D 1 << 2, - FLAG3 =3D 1 << 3, + FLAG1 =3D 1 << 0, + FLAG2 =3D 1 << 1, + FLAG3 =3D 1 << 2, + FLAG4 =3D 1 << 3, }; =20 enum test_uflag : unsigned { - UFLAG1 =3D 1 << 1, - UFLAG2 =3D 1 << 2, - UFLAG3 =3D 1 << 3, + UFLAG1 =3D 1 << 0, + UFLAG2 =3D 1 << 1, + UFLAG3 =3D 1 << 2, + UFLAG4 =3D 1 << 3, }; =20 DEF_ENUM_FLAGS_TYPE (test_flag, test_flags); DEF_ENUM_FLAGS_TYPE (test_uflag, test_uflags); =20 +/* to_string enumerator->string mapping functions used to test + enum_flags::to_string. These intentionally miss mapping a couple + enumerators each (xFLAG2, xFLAG4). */ + +static std::string +to_string_flags (test_flags flags) +{ + static constexpr test_flags::string_mapping mapping[] =3D { + MAP_ENUM_FLAG (FLAG1), + MAP_ENUM_FLAG (FLAG3), + }; + return flags.to_string (mapping); +} + +static std::string +to_string_uflags (test_uflags flags) +{ + static constexpr test_uflags::string_mapping mapping[] =3D { + MAP_ENUM_FLAG (UFLAG1), + MAP_ENUM_FLAG (UFLAG3), + }; + return flags.to_string (mapping); +} + static void self_test () { @@ -581,6 +607,37 @@ self_test () =20 SELF_CHECK (ok); } + + /* Check string conversion. */ + { + SELF_CHECK (to_string_uflags (0) + =3D=3D "0x0 []"); + SELF_CHECK (to_string_uflags (UFLAG1) + =3D=3D "0x1 [UFLAG1]"); + SELF_CHECK (to_string_uflags (UFLAG1 | UFLAG3) + =3D=3D "0x5 [UFLAG1 UFLAG3]"); + SELF_CHECK (to_string_uflags (UFLAG1 | UFLAG2 | UFLAG3) + =3D=3D "0x7 [UFLAG1 UFLAG3 0x2]"); + SELF_CHECK (to_string_uflags (UFLAG2) + =3D=3D "0x2 [0x2]"); + /* Check that even with multiple unmapped flags, we only print one + unmapped hex number (0xa, in this case). */ + SELF_CHECK (to_string_uflags (UFLAG1 | UFLAG2 | UFLAG3 | UFLAG4) + =3D=3D "0xf [UFLAG1 UFLAG3 0xa]"); + + SELF_CHECK (to_string_flags (0) + =3D=3D "0x0 []"); + SELF_CHECK (to_string_flags (FLAG1) + =3D=3D "0x1 [FLAG1]"); + SELF_CHECK (to_string_flags (FLAG1 | FLAG3) + =3D=3D "0x5 [FLAG1 FLAG3]"); + SELF_CHECK (to_string_flags (FLAG1 | FLAG2 | FLAG3) + =3D=3D "0x7 [FLAG1 FLAG3 0x2]"); + SELF_CHECK (to_string_flags (FLAG2) + =3D=3D "0x2 [0x2]"); + SELF_CHECK (to_string_flags (FLAG1 | FLAG2 | FLAG3 | FLAG4) + =3D=3D "0xf [FLAG1 FLAG3 0xa]"); + } } =20 } /* namespace enum_flags_tests */ diff --git a/gdbsupport/enum-flags.h b/gdbsupport/enum-flags.h index 440ffe09734..700037f6126 100644 --- a/gdbsupport/enum-flags.h +++ b/gdbsupport/enum-flags.h @@ -130,6 +130,17 @@ public: typedef E enum_type; typedef typename enum_underlying_type::type underlying_type; =20 + /* For to_string. Maps one enumerator of E to a string. */ + struct string_mapping + { + E flag; + const char *str; + }; + + /* Convenience for to_string implementations, to build a + string_mapping array. */ +#define MAP_ENUM_FLAG(ENUM_FLAG) { ENUM_FLAG, #ENUM_FLAG } + public: /* Allow default construction. */ constexpr enum_flags () @@ -183,6 +194,18 @@ public: /* Binary operations involving some unrelated type (which would be a bug) are implemented as non-members, and deleted. */ =20 + /* 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. + + Note: this is defined outside the template class so it can use + the global operators for enum_type, which are only defined after + the template class. */ + template + std::string to_string (const string_mapping (&mapping)[N]) const; + private: /* Stored as enum_type because GDB knows to print the bit flags neatly if the enum values look like bit flags. */ @@ -415,6 +438,49 @@ template > void operator>> (const enum_flags &, const any_type &) =3D dele= te; =20 +template +template +std::string +enum_flags::to_string (const string_mapping (&mapping)[N]) const +{ + enum_type flags =3D raw (); + std::string res =3D hex_string (flags); + res +=3D " ["; + + bool need_space =3D false; + for (const auto &entry : mapping) + { + if ((flags & entry.flag) !=3D 0) + { + /* Work with an unsigned version of the underlying type, + because if enum_type's underlying type is signed, op~ + won't be defined for it, and, bitwise operations on + signed types are implementation defined. */ + using uns =3D typename std::make_unsigned::type; + flags &=3D (enum_type) ~(uns) entry.flag; + + if (need_space) + res +=3D " "; + res +=3D entry.str; + + need_space =3D true; + } + } + + /* If there were flags not included in the mapping, print them as + a hex number. */ + if (flags !=3D 0) + { + if (need_space) + res +=3D " "; + res +=3D hex_string (flags); + } + + res +=3D "]"; + + return res; +} + #else /* __cplusplus */ =20 /* In C, the flags type is just a typedef for the enum type. */