mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2025-02-05 12:53:16 +08:00
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> |
||
---|---|---|
.. | ||
.dir-locals.el | ||
.gitattributes | ||
acinclude.m4 | ||
aclocal.m4 | ||
agent.cc | ||
agent.h | ||
alt-stack.h | ||
array-view.h | ||
ax.def | ||
block-signals.h | ||
break-common.h | ||
btrace-common.cc | ||
btrace-common.h | ||
buildargv.h | ||
byte-vector.h | ||
ChangeLog-2020-2021 | ||
check-defines.el | ||
cleanups.cc | ||
cleanups.h | ||
common-debug.cc | ||
common-debug.h | ||
common-defs.h | ||
common-exceptions.cc | ||
common-exceptions.h | ||
common-gdbthread.h | ||
common-inferior.cc | ||
common-inferior.h | ||
common-regcache.cc | ||
common-regcache.h | ||
common-types.h | ||
common-utils.cc | ||
common-utils.h | ||
common.m4 | ||
compiler-type.m4 | ||
config.in | ||
configure | ||
configure.ac | ||
create-version.sh | ||
def-vector.h | ||
default-init-alloc.h | ||
eintr.h | ||
enum-flags.h | ||
environ.cc | ||
environ.h | ||
errors.cc | ||
errors.h | ||
event-loop.cc | ||
event-loop.h | ||
event-pipe.cc | ||
event-pipe.h | ||
fileio.cc | ||
fileio.h | ||
filestuff.cc | ||
filestuff.h | ||
filtered-iterator.h | ||
format.cc | ||
format.h | ||
forward-scope-exit.h | ||
function-view.h | ||
gdb_assert.h | ||
gdb_binary_search.h | ||
gdb_file.h | ||
gdb_locale.h | ||
gdb_obstack.cc | ||
gdb_obstack.h | ||
gdb_proc_service.h | ||
gdb_ref_ptr.h | ||
gdb_regex.cc | ||
gdb_regex.h | ||
gdb_select.h | ||
gdb_setjmp.h | ||
gdb_signals.h | ||
gdb_splay_tree.h | ||
gdb_sys_time.h | ||
gdb_tilde_expand.cc | ||
gdb_tilde_expand.h | ||
gdb_unique_ptr.h | ||
gdb_unlinker.h | ||
gdb_vecs.cc | ||
gdb_vecs.h | ||
gdb_wait.cc | ||
gdb_wait.h | ||
gdb-checked-static-cast.h | ||
gdb-dlfcn.cc | ||
gdb-dlfcn.h | ||
gdb-hashtab.h | ||
gdb-safe-ctype.h | ||
gdb-sigmask.h | ||
gdb-xfree.h | ||
host-defs.h | ||
intrusive_list.h | ||
iterator-range.h | ||
job-control.cc | ||
job-control.h | ||
libiberty.m4 | ||
Makefile.am | ||
Makefile.in | ||
netstuff.cc | ||
netstuff.h | ||
new-op.cc | ||
next-iterator.h | ||
observable.h | ||
offset-type.h | ||
osabi.cc | ||
osabi.def | ||
osabi.h | ||
owning_intrusive_list.h | ||
packed.h | ||
parallel-for.h | ||
pathstuff.cc | ||
pathstuff.h | ||
poison.h | ||
preprocessor.h | ||
print-utils.cc | ||
print-utils.h | ||
ptid.cc | ||
ptid.h | ||
ptrace.m4 | ||
range-chain.h | ||
README | ||
refcounted-object.h | ||
reference-to-pointer-iterator.h | ||
rsp-low.cc | ||
rsp-low.h | ||
run-time-clock.cc | ||
run-time-clock.h | ||
safe-iterator.h | ||
safe-strerror.cc | ||
scope-exit.h | ||
scoped_fd.h | ||
scoped_ignore_signal.h | ||
scoped_ignore_sigttou.h | ||
scoped_mmap.cc | ||
scoped_mmap.h | ||
scoped_restore.h | ||
scoped_signal_handler.h | ||
search.cc | ||
search.h | ||
selftest.cc | ||
selftest.h | ||
selftest.m4 | ||
signals-state-save-restore.cc | ||
signals-state-save-restore.h | ||
signals.cc | ||
symbol.h | ||
task-group.cc | ||
task-group.h | ||
tdesc.cc | ||
tdesc.h | ||
thread-pool.cc | ||
thread-pool.h | ||
traits.h | ||
underlying.h | ||
unordered_dense.h | ||
unordered_map.h | ||
unordered_set.h | ||
valid-expr.h | ||
version.h | ||
warning.m4 | ||
x86-xstate.h | ||
xml-utils.cc | ||
xml-utils.h |
This is a helper library that is used by gdb and gdbserver. To send patches, follow the gdb patch submission instructions in ../gdb/CONTRIBUTE. For maintainers, see ../gdb/MAINTAINERS.