From a7e72022ce7fbf78637fb8475e9c357f00ff120d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20L=C3=B3pez-Ib=C3=A1=C3=B1ez?= Date: Sun, 19 Oct 2008 13:52:10 +0000 Subject: [PATCH] re PR c/30260 (Enumeration types and enumeration constants erroneously given unsigned types) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2008-10-19 Manuel López-Ibáñez PR c/30260 * c-decl.c (finish_enum): Convert non-integer enumerators to enum type. (build_enumerator): Convert enumerators that fit in integer to integer type. testsuite/ * gcc.dg/pr30260.c: New. From-SVN: r141224 --- gcc/ChangeLog | 20 ++++++++++++++------ gcc/c-decl.c | 32 +++++++++++++++++++++----------- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr30260.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr30260.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 79aee0bb2c0..7791e3b7a58 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2008-10-19 Manuel López-Ibáñez + + PR c/30260 + * c-decl.c (finish_enum): Convert non-integer enumerators to enum + type. + (build_enumerator): Convert enumerators that fit in integer to + integer type. + 2008-10-18 Uros Bizjak * config/i386/i386.md (unnamed peephole2): Do not force memory @@ -8765,7 +8773,7 @@ PR ada/36554 * dwarf2out.c (is_subrange_type): Deal with BOOLEAN_TYPE. -2008-07-30 Rafael Ãvila de Espíndola +2008-07-30 Rafael Ávila de Espíndola PR 36974 * final.c (call_from_call_insn): Handle COND_EXEC. @@ -8977,7 +8985,7 @@ * config/rs6000/rs6000.h (SLOW_UNALIGNED_ACCESS): Add clause for vector modes. -2008-07-30 Rafael Ãvila de Espíndola +2008-07-30 Rafael Ávila de Espíndola * final.c (call_from_call_insn): New. (final_scan_insn): Call assemble_external on FUNCTION_DECLs. @@ -10095,7 +10103,7 @@ (TARGET_OPTION_PRINT): Ditto. (TARGET_CAN_INLINE_P): Ditto. -2008-07-22 Rafael Ãvila de Espíndola +2008-07-22 Rafael Ávila de Espíndola * c-typeck.c (build_external_ref): Don't call assemble_external. * final.c (output_operand): Call assemble_external. @@ -10116,7 +10124,7 @@ highest magnitude if this is still less or equal to the true quotient in magnitude. -2008-07-21 Rafael Ãvila de Espíndola +2008-07-21 Rafael Ávila de Espíndola * Makefile.in: Replace toplev.h with TOPLEV_H. * c-decl.c (merge_decls): Don't set DECL_IN_SYSTEM_HEADER. @@ -10419,7 +10427,7 @@ (m32c_legitimate_address_p): Handle "++rii" addresses created by m32c_legitimize_reload_address. -2007-07-16 Rafael Ãvila de Espíndola +2007-07-16 Rafael Ávila de Espíndola * c-decl.c (merge_decls): Keep DECL_SOURCE_LOCATION and DECL_IN_SYSTEM_HEADER in sync. @@ -10509,7 +10517,7 @@ * emit-rtl.c (set_mem_attributes_minus_bitpos): Improve comment. -2007-07-14 Rafael Ãvila de Espíndola +2007-07-14 Rafael Ávila de Espíndola * c-decl.c (diagnose_mismatched_decls): Don't warn if TREE_NO_WARNING is set. diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 2a6dcf2361c..ee8e45e1c08 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -5926,17 +5926,15 @@ finish_enum (tree enumtype, tree values, tree attributes) /* The ISO C Standard mandates enumerators to have type int, even though the underlying type of an enum type is unspecified. However, GCC allows enumerators of any - integer type as an extensions. Here we convert any - enumerators that fit in an int to type int, to avoid - promotions to unsigned types when comparing integers with - enumerators that fit in the int range. When -pedantic is - given, build_enumerator() would have already warned about - those that don't fit. */ - if (int_fits_type_p (ini, integer_type_node)) - tem = integer_type_node; - else - tem = enumtype; - ini = convert (tem, ini); + integer type as an extensions. build_enumerator() + converts any enumerators that fit in an int to type int, + to avoid promotions to unsigned types when comparing + integers with enumerators that fit in the int range. + When -pedantic is given, build_enumerator() would have + already warned about those that don't fit. Here we + convert the rest to the enumerator type. */ + if (TREE_TYPE (ini) != integer_type_node) + ini = convert (enumtype, ini); DECL_INITIAL (enu) = ini; TREE_PURPOSE (pair) = DECL_NAME (enu); @@ -6026,6 +6024,18 @@ build_enumerator (struct c_enum_contents *the_enum, tree name, tree value, pedwarn (value_loc, OPT_pedantic, "ISO C restricts enumerator values to range of %"); + /* The ISO C Standard mandates enumerators to have type int, even + though the underlying type of an enum type is unspecified. + However, GCC allows enumerators of any integer type as an + extensions. Here we convert any enumerators that fit in an int + to type int, to avoid promotions to unsigned types when comparing + integers with enumerators that fit in the int range. When + -pedantic is given, we would have already warned about those that + don't fit. We have to do this here rather than in finish_enum + because this value may be used to define more enumerators. */ + if (int_fits_type_p (value, integer_type_node)) + value = convert (integer_type_node, value); + /* Set basis for default for next value. */ the_enum->enum_next_value = build_binary_op diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 464a9a65e9c..a08312d98f2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2008-10-19 Manuel López-Ibáñez + + PR c/30260 + * gcc.dg/pr30260.c: New. + 2008-10-19 Paul Thomas PR fortran/37723 diff --git a/gcc/testsuite/gcc.dg/pr30260.c b/gcc/testsuite/gcc.dg/pr30260.c new file mode 100644 index 00000000000..f7373176ed5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr30260.c @@ -0,0 +1,32 @@ +/* PR 30260 */ +/* { dg-do link } */ +/* { dg-options "-pedantic -O" } */ +#include + +enum A { + A1 = 0, + A2 = A1 - 1 +}; +enum B { + B1 = 0u, + B2 = B1 - 1 /* { dg-bogus "ISO C restricts enumerator values to range of 'int'" } */ +}; +int main(void) +{ + enum A a = -1; + enum B b = -1; + + if (!(a < 0)) + link_error (); + if (!(A2 < 0)) + link_error (); + if (!(b < 0)) + link_error (); + if (!(B2 < 0)) + link_error (); + + return 0; +} + +enum E1 { e10 = INT_MAX, e11 }; /* { dg-error "overflow in enumeration values" } */ +enum E2 { e20 = (unsigned) INT_MAX, e21 }; /* { dg-error "overflow in enumeration values" } */ -- 2.30.2