From 0eb861b45944e9366e6cf5de3bbb24949456a94a Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Wed, 22 Nov 2023 11:06:31 -0800 Subject: [PATCH] Don't enable `ordinalCount()` or `bitCount()` for bitset enums Summary: Bitfield enums are not sequential, so use of these functions on these enums would be invalid. I looked at whether we could trivially move `bitCount` to template based on `ordinalCount`. `bitCount` must be constexpr, since we use it directly as a bit-field size constant. `log2` and `ceil` to be constexpr, which isn't here until C++ 26. Reviewed By: javache Differential Revision: D51518899 fbshipit-source-id: 256f15bbed517be6f90bf43baa43ce96e9259a71 --- enums.py | 22 +++++++++++----------- yoga/enums/Errata.h | 10 ---------- yoga/enums/PrintOptions.h | 10 ---------- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/enums.py b/enums.py index 749cb825..3755870a 100755 --- a/enums.py +++ b/enums.py @@ -169,19 +169,19 @@ for name, values in sorted(ENUMS.items()): ordinal = value[0] if isinstance(value, tuple) else value f.write(f" {ordinal} = YG{name}{ordinal},\n") f.write("};\n\n") - f.write( - f"YG_DEFINE_ENUM_FLAG_OPERATORS({name})\n\n" if name in BITSET_ENUMS else "" - ) - f.write("template <>\n") - f.write(f"constexpr inline int32_t ordinalCount<{name}>() {{\n") - f.write(f" return {len(values)};\n") - f.write("} \n\n") + if name in BITSET_ENUMS: + f.write(f"YG_DEFINE_ENUM_FLAG_OPERATORS({name})\n\n") + else: + f.write("template <>\n") + f.write(f"constexpr inline int32_t ordinalCount<{name}>() {{\n") + f.write(f" return {len(values)};\n") + f.write("} \n\n") - f.write("template <>\n") - f.write(f"constexpr inline int32_t bitCount<{name}>() {{\n") - f.write(f" return {math.ceil(math.log(len(values), 2))};\n") - f.write("} \n\n") + f.write("template <>\n") + f.write(f"constexpr inline int32_t bitCount<{name}>() {{\n") + f.write(f" return {math.ceil(math.log(len(values), 2))};\n") + f.write("} \n\n") f.write(f"constexpr inline {name} scopedEnum(YG{name} unscoped) {{\n") f.write(f" return static_cast<{name}>(unscoped);\n") diff --git a/yoga/enums/Errata.h b/yoga/enums/Errata.h index 6a085bf5..134f5fff 100644 --- a/yoga/enums/Errata.h +++ b/yoga/enums/Errata.h @@ -26,16 +26,6 @@ enum class Errata : uint32_t { YG_DEFINE_ENUM_FLAG_OPERATORS(Errata) -template <> -constexpr inline int32_t ordinalCount() { - return 6; -} - -template <> -constexpr inline int32_t bitCount() { - return 3; -} - constexpr inline Errata scopedEnum(YGErrata unscoped) { return static_cast(unscoped); } diff --git a/yoga/enums/PrintOptions.h b/yoga/enums/PrintOptions.h index c61fbc63..90d0043c 100644 --- a/yoga/enums/PrintOptions.h +++ b/yoga/enums/PrintOptions.h @@ -23,16 +23,6 @@ enum class PrintOptions : uint32_t { YG_DEFINE_ENUM_FLAG_OPERATORS(PrintOptions) -template <> -constexpr inline int32_t ordinalCount() { - return 3; -} - -template <> -constexpr inline int32_t bitCount() { - return 2; -} - constexpr inline PrintOptions scopedEnum(YGPrintOptions unscoped) { return static_cast(unscoped); }