Find a file
Joseph Myers 3b3083a598 c: C2x enums wider than int [PR36113]
C2x has two enhancements to enumerations: allowing enumerations whose
values do not all fit in int (similar to an existing extension), and
allowing an underlying type to be specified (as in C++).  This patch
implements the first of those enhancements.

Apart from adjusting diagnostics to reflect this being a standard
feature, there are some semantics differences from the previous
extension:

* The standard feature gives all the values of such an enum the
  enumerated type (while keeping type int if that can represent all
  values of the enumeration), where previously GCC only gave those
  values outside the range of int the enumerated type.  This change
  was previously requested in PR 36113; it seems unlikely that people
  are depending on the detail of the previous extension that some
  enumerators had different types to others depending on whether their
  values could be represented as int, and this patch makes the change
  to types of enumerators unconditionally (if that causes problems in
  practice we could always make it conditional on C2x mode instead).

* The types *while the enumeration is being defined*, for enumerators
  that can't be represented as int, are now based more directly on the
  types of the expressions used, rather than a possibly different type
  with the right precision constructed using c_common_type_for_size.
  Again, this change is made unconditionally.

* Where overflow (or wraparound to 0, in the case of an unsigned type)
  when 1 is implicitly added to determine the value of the next
  enumerator was previously an error, it now results in a wider type
  with the same signedness (as the while-being-defined type of the
  previous enumerator) being chosen, if available.

When a type is chosen in such an overflow case, or when a type is
chosen for the underlying integer type of the enumeration, it's
possible that (unsigned) __int128 is chosen.  Although C2x allows for
such types wider than intmax_t to be considered extended integer
types, we don't have various features required to do so (integer
constant suffixes; sufficient library support would also be needed to
define the associated macros for printf/scanf conversions, and
<stdint.h> typedef names would need to be defined).  Thus, there are
also pedwarns for exceeding the range of intmax_t / uintmax_t, as
while in principle exceeding that range is OK, it's only OK in a
context where the relevant types meet the requirements for extended
integer types, which does not currently apply here.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  Also
manually checked diagnostics for c2x-enum-3.c with -m32 to confirm the
diagnostics in that { target { ! int128 } } test are as expected.

	PR c/36113

gcc/c-family/
	* c-common.cc (c_common_type_for_size): Add fallback to
	widest_unsigned_literal_type_node or
	widest_integer_literal_type_node for precision that may not
	exactly match the precision of those types.

gcc/c/
	* c-decl.cc (finish_enum): If any enumerators do not fit in int,
	convert all to the type of the enumeration.  pedwarn if no integer
	type fits all enumerators and default to
	widest_integer_literal_type_node in that case.  Otherwise pedwarn
	for type wider than intmax_t.
	(build_enumerator): pedwarn for enumerators outside the range of
	uintmax_t or intmax_t, and otherwise use pedwarn_c11 for
	enumerators outside the range of int.  On overflow, attempt to
	find a wider type that can hold the value of the next enumerator.
	Do not convert value to type determined with
	c_common_type_for_size.

gcc/testsuite/
	* gcc.dg/c11-enum-1.c, gcc.dg/c11-enum-2.c, gcc.dg/c11-enum-3.c,
	gcc.dg/c2x-enum-1.c, gcc.dg/c2x-enum-2.c, gcc.dg/c2x-enum-3.c,
	gcc.dg/c2x-enum-4.c, gcc.dg/c2x-enum-5.c: New tests.
	* gcc.dg/pr30260.c: Explicitly use -std=gnu11.  Update expected
	diagnostics.
	* gcc.dg/torture/pr25183.c: Update expected diagnostics.
2022-10-18 14:07:27 +00:00
c++tools Daily bump. 2022-03-19 00:16:22 +00:00
config Daily bump. 2022-09-01 00:17:39 +00:00
contrib Daily bump. 2022-10-08 00:17:29 +00:00
fixincludes Daily bump. 2022-10-08 00:17:29 +00:00
gcc c: C2x enums wider than int [PR36113] 2022-10-18 14:07:27 +00:00
gnattools Daily bump. 2022-09-01 00:17:39 +00:00
gotools Daily bump. 2022-08-31 00:16:45 +00:00
include Daily bump. 2022-10-14 00:16:35 +00:00
INSTALL
intl Daily bump. 2021-11-30 00:16:44 +00:00
libada Daily bump. 2022-08-26 00:16:21 +00:00
libatomic Daily bump. 2022-10-13 00:17:37 +00:00
libbacktrace Daily bump. 2022-10-13 00:17:37 +00:00
libcc1 Daily bump. 2022-10-13 00:17:37 +00:00
libcody Daily bump. 2022-06-04 00:16:27 +00:00
libcpp Daily bump. 2022-10-15 00:17:38 +00:00
libdecnumber Daily bump. 2022-10-08 00:17:29 +00:00
libffi Daily bump. 2022-10-13 00:17:37 +00:00
libgcc libgcc: Quote variable in Makefile.in 2022-10-18 12:09:53 +01:00
libgfortran Daily bump. 2022-10-13 00:17:37 +00:00
libgo runtime: portable access to sigev_notify_thread_id 2022-09-27 09:30:23 -07:00
libgomp Daily bump. 2022-10-18 00:17:40 +00:00
libiberty Daily bump. 2022-10-15 00:17:38 +00:00
libitm Daily bump. 2022-10-14 00:16:35 +00:00
libobjc Daily bump. 2022-10-13 00:17:37 +00:00
liboffloadmic Daily bump. 2022-10-13 00:17:37 +00:00
libphobos Daily bump. 2022-10-13 00:17:37 +00:00
libquadmath Daily bump. 2022-10-13 00:17:37 +00:00
libsanitizer Daily bump. 2022-10-13 00:17:37 +00:00
libssp Daily bump. 2022-10-13 00:17:37 +00:00
libstdc++-v3 libstdc++: Partial library support for std::float{16,32,64,128}_t and std::bfloat16_t 2022-10-18 11:37:13 +02:00
libvtv Daily bump. 2022-10-13 00:17:37 +00:00
lto-plugin Daily bump. 2022-10-13 00:17:37 +00:00
maintainer-scripts Daily bump. 2022-07-29 00:16:21 +00:00
zlib Daily bump. 2022-10-13 00:17:37 +00:00
.dir-locals.el dir-locals: Use https for bug references 2021-07-20 11:40:34 +01:00
.gitattributes
.gitignore .gitignore: do not ignore config.h 2022-07-19 17:07:04 +03:00
ABOUT-NLS
ar-lib
ChangeLog Daily bump. 2022-10-13 00:17:37 +00:00
ChangeLog.jit
ChangeLog.tree-ssa
compile
config-ml.in
config.guess
config.rpath
config.sub config.sub: change mode to 755. 2021-12-21 09:10:57 +01:00
configure Arrange to --disable-shared by default for VxWorks 2022-09-30 10:43:13 +00:00
configure.ac Arrange to --disable-shared by default for VxWorks 2022-09-30 10:43:13 +00:00
COPYING
COPYING.LIB
COPYING.RUNTIME
COPYING3
COPYING3.LIB
depcomp
install-sh
libtool-ldflags
libtool.m4 Generic configury support for shared libs on VxWorks 2022-10-11 07:31:07 +00:00
ltgcc.m4
ltmain.sh
ltoptions.m4
ltsugar.m4
ltversion.m4
lt~obsolete.m4
MAINTAINERS MAINTAINERS: Add myself for write after approval 2022-10-12 17:12:05 +08:00
Makefile.def Makefile.def: drop remnants of unused libelf 2022-08-18 09:37:09 +01:00
Makefile.in Makefile.def: drop remnants of unused libelf 2022-08-18 09:37:09 +01:00
Makefile.tpl Makefile.def: drop remnants of unused libelf 2022-08-18 09:37:09 +01:00
missing
mkdep
mkinstalldirs
move-if-change
multilib.am
README
symlink-tree
test-driver
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.