aboutsummaryrefslogtreecommitdiff
path: root/gdb
diff options
context:
space:
mode:
authorCarlos Galvez <carlosgalvezp@gmail.com>2024-12-21 22:25:40 +0100
committerTom Tromey <tom@tromey.com>2024-12-22 10:48:58 -0700
commit4a0b2cb7210c65c8c9f4a56345749bb7294fbfbf (patch)
tree8980ae7350fcc2ad121d56602f797995326e9bb4 /gdb
parent1898ec362a5930c6925d84785b681576acdbcb99 (diff)
downloadgdb-4a0b2cb7210c65c8c9f4a56345749bb7294fbfbf.zip
gdb-4a0b2cb7210c65c8c9f4a56345749bb7294fbfbf.tar.gz
gdb-4a0b2cb7210c65c8c9f4a56345749bb7294fbfbf.tar.bz2
Fix -Wenum-constexpr-conversion in enum-flags.h
This fixes PR 31331: https://sourceware.org/bugzilla/show_bug.cgi?id=31331 Currently, enum-flags.h is suppressing the warning -Wenum-constexpr-conversion coming from recent versions of Clang. This warning is intended to be made a compiler error (non-downgradeable) in future versions of Clang: https://github.com/llvm/llvm-project/issues/59036 The rationale is that casting a value of an integral type into an enumeration is Undefined Behavior if the value does not fit in the range of values of the enum: https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1766 Undefined Behavior is not allowed in constant expressions, leading to an ill-formed program. In this case, in enum-flags.h, we are casting the value -1 to an enum of a positive range only, which is UB as per the Standard and thus not allowed in a constexpr context. The purpose of doing this instead of using std::underlying_type is because, for C-style enums, std::underlying_type typically returns "unsigned int". However, when operating with it arithmetically, the enum is promoted to *signed* int, which is what we want to avoid. This patch solves this issue as follows: * Use std::underlying_type and remove the custom enum_underlying_type. * Ensure that operator~ is called always on an unsigned integer. We do this by casting the input enum into std::size_t, which can fit any unsigned integer. We have the guarantee that the cast is safe, because we have checked that the underlying type is unsigned. If the enum had negative values, the underlying type would be signed. This solves the issue with C-style enums, but also solves a hidden issue: enums with underlying type of std::uint8_t or std::uint16_t are *also* promoted to signed int. Now they are all explicitly casted to the largest unsigned int type and operator~ is safe to use. * There is one more thing that needs fix. Currently, operator~ is implemented as follows: return (enum_type) ~underlying(e); After applying ~underlying(e), the result is a very large value, which we then cast to "enum_type". This cast is Undefined Behavior if the large value does not fit in the range of the enum. For C++ enums (scoped and/or with explicit underlying type), the range of the enum is the entire range of the underlying type, so the cast is safe. However, for C-style enums, the range is the smallest bit-field that can hold all the values of the enumeration. So the range is a lot smaller and casting a large value to the enum would invoke Undefined Behavior. To solve this problem, we create a new trait EnumHasFixedUnderlyingType, to ensure operator~ may only be called on C++-style enums. This behavior is roughly the same as what we had on trunk, but relying on different properties of the enums. * Once this is implemented, the following tests fail to compile: CHECK_VALID (true, int, true ? EF () : EF2 ()) This is because it expects the enums to be promoted to signed int, instead of unsigned int (which is the true underlying type). I propose to remove these tests altogether, because: - The comment nearby say they are not very important. - Comparing 2 enums of different type like that is strange, relies on integer promotions and thus hurts readability. As per comments in the related PR, we likely don't want this type of code in gdb code anyway, so there's no point in testing it. - Most importantly, this type of comparison will be ill-formed in C++26 for regular enums, so enum_flags does not need to emulate that. Since this is the only place where the warning was suppressed, remove also the corresponding macro in include/diagnostics.h. The change has been tested by running the entire gdb test suite (make check) and comparing the results (testsuite/gdb.sum) against trunk. No noticeable differences have been observed. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31331 Tested-by: Keith Seitz <keiths@redhat.com> Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb')
-rw-r--r--gdb/unittests/enum-flags-selftests.c27
1 files changed, 0 insertions, 27 deletions
diff --git a/gdb/unittests/enum-flags-selftests.c b/gdb/unittests/enum-flags-selftests.c
index dddb1e2..f192da3 100644
--- a/gdb/unittests/enum-flags-selftests.c
+++ b/gdb/unittests/enum-flags-selftests.c
@@ -235,33 +235,6 @@ CHECK_VALID (true, UnsignedEnumFlag, ~UnsignedEnumFlag ())
CHECK_VALID (true, EnumFlag, true ? EnumFlag () : RawEnum ())
CHECK_VALID (true, EnumFlag, true ? RawEnum () : EnumFlag ())
-/* These are valid, but it's not a big deal since you won't be able to
- assign the resulting integer to an enum or an enum_flags without a
- cast.
-
- The latter two tests are disabled on older GCCs because they
- incorrectly fail with gcc 4.8 and 4.9 at least. Running the test
- outside a SFINAE context shows:
-
- invalid user-defined conversion from ‘EF’ to ‘RE2’
-
- They've been confirmed to compile/pass with gcc 5.3, gcc 7.1 and
- clang 3.7. */
-
-CHECK_VALID (true, int, true ? EnumFlag () : EnumFlag2 ())
-CHECK_VALID (true, int, true ? EnumFlag2 () : EnumFlag ())
-CHECK_VALID (true, int, true ? EnumFlag () : RawEnum2 ())
-CHECK_VALID (true, int, true ? RawEnum2 () : EnumFlag ())
-
-/* Same, but with an unsigned enum. */
-
-using uns = unsigned int;
-
-CHECK_VALID (true, uns, true ? EnumFlag () : UnsignedEnumFlag ())
-CHECK_VALID (true, uns, true ? UnsignedEnumFlag () : EnumFlag ())
-CHECK_VALID (true, uns, true ? EnumFlag () : UnsignedRawEnum ())
-CHECK_VALID (true, uns, true ? UnsignedRawEnum () : EnumFlag ())
-
/* Unfortunately this can't work due to the way C++ computes the
return type of the ternary conditional operator. int isn't
implicitly convertible to the raw enum type, so the type of the