binutils-gdb/gdbsupport
Carlos Galvez 4a0b2cb721 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>
2024-12-22 10:48:58 -07:00
..
.dir-locals.el
.gitattributes
acinclude.m4
aclocal.m4 [gdbsupport] Rerun autoreconf -f 2024-11-23 12:40:36 +01:00
agent.cc
agent.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
alt-stack.h
array-view.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
ax.def
block-signals.h
break-common.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
btrace-common.cc [gdb] Fix some typos 2024-11-18 09:46:31 +01:00
btrace-common.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
buildargv.h
byte-vector.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
ChangeLog-2020-2021
check-defines.el
cleanups.cc
cleanups.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-debug.cc
common-debug.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-defs.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-exceptions.cc
common-exceptions.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-gdbthread.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-inferior.cc
common-inferior.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-regcache.cc
common-regcache.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-types.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common-utils.cc gdb: allow core file containing special characters on the command line 2024-12-09 11:01:00 +00:00
common-utils.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
common.m4 gdb, gdbserver, gdbsupport: flatten and sort some list in configure files 2024-12-02 11:44:37 -05:00
compiler-type.m4
config.in [gdb/build] Fix build breaker on mingw-w64 2024-12-04 21:29:52 +01:00
configure [gdb/build] Fix build breaker on mingw-w64 2024-12-04 21:29:52 +01:00
configure.ac [gdb/build] Fix build breaker on mingw-w64 2024-12-04 21:29:52 +01:00
create-version.sh
def-vector.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
default-init-alloc.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
eintr.h [gdb/build] Fix build breaker on mingw-w64 2024-12-04 21:29:52 +01:00
enum-flags.h Fix -Wenum-constexpr-conversion in enum-flags.h 2024-12-22 10:48:58 -07:00
environ.cc
environ.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
errors.cc
errors.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
event-loop.cc
event-loop.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
event-pipe.cc [gdbsupport] Handle EINTR in event-pipe.cc 2024-11-22 17:44:29 +01:00
event-pipe.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
fileio.cc
fileio.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
filestuff.cc gdb, gdbserver, gdbsupport: remove some unused gdb_vecs.h includes 2024-12-06 12:49:10 -05:00
filestuff.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
filtered-iterator.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
format.cc
format.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
forward-scope-exit.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
function-view.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_assert.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_binary_search.h
gdb_file.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_locale.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_obstack.cc
gdb_obstack.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_proc_service.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_ref_ptr.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_regex.cc
gdb_regex.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_select.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_setjmp.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_signals.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_splay_tree.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_sys_time.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_tilde_expand.cc
gdb_tilde_expand.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_unique_ptr.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_unlinker.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_vecs.cc
gdb_vecs.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb_wait.cc
gdb_wait.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb-checked-static-cast.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb-dlfcn.cc
gdb-dlfcn.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb-hashtab.h
gdb-safe-ctype.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
gdb-sigmask.h
gdb-xfree.h
host-defs.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
intrusive_list.h
iterator-range.h
job-control.cc
job-control.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
libiberty.m4
Makefile.am
Makefile.in
netstuff.cc
netstuff.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
new-op.cc
next-iterator.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
observable.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
offset-type.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
osabi.cc
osabi.def
osabi.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
owning_intrusive_list.h
packed.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
parallel-for.h [gdb/contrib] Add two rules in common-misspellings.txt 2024-11-23 12:20:34 +01:00
pathstuff.cc Pass current directory to gdb_abspath 2024-10-19 13:18:00 -06:00
pathstuff.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
poison.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
preprocessor.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
print-utils.cc
print-utils.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
ptid.cc
ptid.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
ptrace.m4
range-chain.h
README
refcounted-object.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
reference-to-pointer-iterator.h
rsp-low.cc
rsp-low.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
run-time-clock.cc
run-time-clock.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
safe-iterator.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
safe-strerror.cc
scope-exit.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
scoped_fd.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
scoped_ignore_signal.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
scoped_ignore_sigttou.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
scoped_mmap.cc
scoped_mmap.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
scoped_restore.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
scoped_signal_handler.h gdb: Introduce RAII signal handler setter 2024-11-22 17:38:31 -03:00
search.cc
search.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
selftest.cc
selftest.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
selftest.m4
signals-state-save-restore.cc
signals-state-save-restore.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
signals.cc
symbol.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
task-group.cc
task-group.h
tdesc.cc
tdesc.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
thread-pool.cc
thread-pool.h
traits.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
underlying.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
unordered_dense.h gdbsupport: add unordered_dense.h 4.4.0 2024-11-25 22:07:03 -05:00
unordered_map.h gdbsupport: add unordered_dense.h 4.4.0 2024-11-25 22:07:03 -05:00
unordered_set.h gdbsupport: add unordered_dense.h 4.4.0 2024-11-25 22:07:03 -05:00
valid-expr.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
version.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
warning.m4
x86-xstate.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00
xml-utils.cc
xml-utils.h Run check-include-guards.py 2024-12-18 10:00:44 -07:00

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.