From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 91DE93858D28 for ; Thu, 16 Nov 2023 21:38:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 91DE93858D28 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 91DE93858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700170735; cv=none; b=TekSKkYrYMu/A6PMA23Kttuju8pXe7bTCHkpLuNA8Lc4kjE1msJsQ2kkFHShSViV3Xhh7pFVsFkqq+1L/w3uP1e21ibar8D/PhyxO0QkEw2y0qMOpamXqyB3LjsmwD9HYB35H0IYbpbtMfEbTSsYA7ag5rmgaB+SMI95cJhsZiE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700170735; c=relaxed/simple; bh=enC9Ry4DXNRpW12an8cuk4BbSIm6/DPeEB0YUtRGN/Y=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=ebaLyGbkrCiDAXVNmpRAOs/aGuVmFe5iMDrIaVO+JY0ZdrFFmvsxY7Njy+PU0NcdBbZGQPZsMPWgL21qIxjCo683u3NzxiNxWKqNsgLgRaQrHlaPaPkaYnMCBDWj1XH3mV+Yzq+aLfaYZEU9aSvV6AaknsFhi2PgQx4kO5hiWdQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-171-78.tugraz.at (vra-171-78.tugraz.at [129.27.171.78]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4SWYLv3s5Pz3wkZ; Thu, 16 Nov 2023 22:38:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1700170727; bh=pbzbjWqXCD5U+hEmdt8n4zERR8xgwm1ltTonBxT3tuM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=jA5ftzsS6Od3C6QtFZZn8NBjL0CZWkdy7SN81HrEcxwfWgfuFRcFpVdwcK9sPEs4G ievqqCQvxmHFaXhQWk1Ys+v89EUbu1nnzgUnVykrGFnNlz83JrMecvgmxJ3mWIBh48 vw9uF537K3gvTsPE3U12X3zoT6c+puoxuJAWy1dE= Message-ID: Subject: [PATCH 2/4] c23: tag compatibility rules for enums From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Thu, 16 Nov 2023 22:38:47 +0100 In-Reply-To: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> References: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Allow redefinition of enum types and enumerators. Diagnose nested redefinitions including redefinitions in the enum specifier for enum types with fixed underlying type. gcc/c: * c-tree.h (c_parser_enum_specifier): Add parameter. * c-decl.cc (start_enum): Allow redefinition. (finish_enum): Diagnose conflicts. (build_enumerator): Set context. (diagnose_mismatched_decls): Diagnose conflicting enumerators. (push_decl): Preserve context for enumerators. * c-parser.cc (c_parser_enum_specifier): Remember when seen is from an enum type which is not yet defined. gcc/testsuide/: * gcc.dg/c23-tag-enum-1.c: New test. * gcc.dg/c23-tag-enum-2.c: New test. * gcc.dg/c23-tag-enum-3.c: New test. * gcc.dg/c23-tag-enum-4.c: New test. * gcc.dg/c23-tag-enum-5.c: New test. --- gcc/c/c-decl.cc | 65 +++++++++++++++++++++++---- gcc/c/c-parser.cc | 5 ++- gcc/c/c-tree.h | 3 +- gcc/c/c-typeck.cc | 5 ++- gcc/testsuite/gcc.dg/c23-tag-enum-1.c | 56 +++++++++++++++++++++++ gcc/testsuite/gcc.dg/c23-tag-enum-2.c | 23 ++++++++++ gcc/testsuite/gcc.dg/c23-tag-enum-3.c | 7 +++ gcc/testsuite/gcc.dg/c23-tag-enum-4.c | 22 +++++++++ gcc/testsuite/gcc.dg/c23-tag-enum-5.c | 18 ++++++++ 9 files changed, 192 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/c23-tag-enum-1.c create mode 100644 gcc/testsuite/gcc.dg/c23-tag-enum-2.c create mode 100644 gcc/testsuite/gcc.dg/c23-tag-enum-3.c create mode 100644 gcc/testsuite/gcc.dg/c23-tag-enum-4.c create mode 100644 gcc/testsuite/gcc.dg/c23-tag-enum-5.c diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 194dd595334..e5d48c3fa56 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -2114,9 +2114,24 @@ diagnose_mismatched_decls (tree newdecl, tree olddec= l, given scope. */ if (TREE_CODE (olddecl) =3D=3D CONST_DECL) { - auto_diagnostic_group d; - error ("redeclaration of enumerator %q+D", newdecl); - locate_old_decl (olddecl); + if (flag_isoc23 + && TYPE_NAME (DECL_CONTEXT (newdecl)) + && DECL_CONTEXT (newdecl) !=3D DECL_CONTEXT (olddecl) + && TYPE_NAME (DECL_CONTEXT (newdecl)) =3D=3D TYPE_NAME (DECL_CONTEXT (o= lddecl))) + { + if (!simple_cst_equal (DECL_INITIAL (olddecl), DECL_INITIAL (newdecl))) + { + auto_diagnostic_group d; + error ("conflicting redeclaration of enumerator %q+D", newdecl); + locate_old_decl (olddecl); + } + } + else + { + auto_diagnostic_group d; + error ("redeclaration of enumerator %q+D", newdecl); + locate_old_decl (olddecl); + } return false; } =20 @@ -3277,8 +3292,11 @@ pushdecl (tree x) =20 /* Must set DECL_CONTEXT for everything not at file scope or DECL_FILE_SCOPE_P won't work. Local externs don't count - unless they have initializers (which generate code). */ + unless they have initializers (which generate code). We + also exclude CONST_DECLs because enumerators will get the + type of the enum as context. */ if (current_function_decl + && TREE_CODE (x) !=3D CONST_DECL && (!VAR_OR_FUNCTION_DECL_P (x) || DECL_INITIAL (x) || !TREE_PUBLIC (x))) DECL_CONTEXT (x) =3D current_function_decl; @@ -9737,7 +9755,7 @@ layout_array_type (tree t) =20 tree start_enum (location_t loc, struct c_enum_contents *the_enum, tree name, - tree fixed_underlying_type) + tree fixed_underlying_type, bool potential_nesting_p) { tree enumtype =3D NULL_TREE; location_t enumloc =3D UNKNOWN_LOCATION; @@ -9749,9 +9767,26 @@ start_enum (location_t loc, struct c_enum_contents *= the_enum, tree name, if (name !=3D NULL_TREE) enumtype =3D lookup_tag (ENUMERAL_TYPE, name, true, &enumloc); =20 + if (enumtype !=3D NULL_TREE && TREE_CODE (enumtype) =3D=3D ENUMERAL_TYPE= ) + { + /* If the type is currently being defined or if we have seen an + incomplete version which is now complete, this is a nested + redefinition. The later happens if the redefinition occurs + inside the enum specifier itself. */ + if (C_TYPE_BEING_DEFINED (enumtype) + || (potential_nesting_p && TYPE_VALUES (enumtype) !=3D NULL_TREE)) + error_at (loc, "nested redefinition of %", name); + + /* For C23 we allow redefinitions. We set to zero and check for + consistency later. */ + if (flag_isoc23 && TYPE_VALUES (enumtype) !=3D NULL_TREE) + enumtype =3D NULL_TREE; + } + if (enumtype =3D=3D NULL_TREE || TREE_CODE (enumtype) !=3D ENUMERAL_TYPE= ) { enumtype =3D make_node (ENUMERAL_TYPE); + TYPE_SIZE (enumtype) =3D NULL_TREE; pushtag (loc, name, enumtype); if (fixed_underlying_type !=3D NULL_TREE) { @@ -9779,9 +9814,6 @@ start_enum (location_t loc, struct c_enum_contents *t= he_enum, tree name, DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) =3D loc; } =20 - if (C_TYPE_BEING_DEFINED (enumtype)) - error_at (loc, "nested redefinition of %", name); - C_TYPE_BEING_DEFINED (enumtype) =3D 1; =20 if (TYPE_VALUES (enumtype) !=3D NULL_TREE) @@ -10011,6 +10043,20 @@ finish_enum (tree enumtype, tree values, tree attr= ibutes) && !in_sizeof && !in_typeof && !in_alignof) struct_parse_info->struct_types.safe_push (enumtype); =20 + /* Check for consistency with previous definition */ + if (flag_isoc23) + { + tree vistype =3D previous_tag (enumtype); + if (vistype + && TREE_CODE (vistype) =3D=3D TREE_CODE (enumtype) + && !C_TYPE_BEING_DEFINED (vistype)) + { + TYPE_STUB_DECL (vistype) =3D TYPE_STUB_DECL (enumtype); + if (!comptypes_same_p (enumtype, vistype)) + error("conflicting redefinition of enum %qT", enumtype); + } + } + C_TYPE_BEING_DEFINED (enumtype) =3D 0; =20 return enumtype; @@ -10190,6 +10236,7 @@ build_enumerator (location_t decl_loc, location_t l= oc, =20 decl =3D build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value)); DECL_INITIAL (decl) =3D value; + DECL_CONTEXT (decl) =3D the_enum->enum_type; pushdecl (decl); =20 return tree_cons (decl, value, NULL_TREE); @@ -10206,7 +10253,7 @@ c_simulate_enum_decl (location_t loc, const char *n= ame, =20 struct c_enum_contents the_enum; tree enumtype =3D start_enum (loc, &the_enum, get_identifier (name), - NULL_TREE); + NULL_TREE, false); =20 tree value_chain =3D NULL_TREE; string_int_pair *value; diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 703f9570dbc..ff30ba198ca 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -3667,6 +3667,7 @@ c_parser_enum_specifier (c_parser *parser) { struct c_typespec ret; bool have_std_attrs; + bool potential_nesting_p =3D false; tree std_attrs =3D NULL_TREE; tree attrs; tree ident =3D NULL_TREE; @@ -3706,6 +3707,7 @@ c_parser_enum_specifier (c_parser *parser) if (!ENUM_FIXED_UNDERLYING_TYPE_P (ret.spec)) error_at (enum_loc, "% declared both with and without " "fixed underlying type"); + potential_nesting_p =3D NULL_TREE =3D=3D TYPE_VALUES (ret.spec); } else { @@ -3776,7 +3778,8 @@ c_parser_enum_specifier (c_parser *parser) forward order at the end. */ tree values; timevar_push (TV_PARSE_ENUM); - type =3D start_enum (enum_loc, &the_enum, ident, fixed_underlying_ty= pe); + type =3D start_enum (enum_loc, &the_enum, ident, fixed_underlying_ty= pe, + potential_nesting_p); values =3D NULL_TREE; c_parser_consume_token (parser); while (true) diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 7df4d65bf7a..a5dd9a37944 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -682,7 +682,8 @@ extern void c_warn_unused_attributes (tree); extern tree c_warn_type_attributes (tree); extern void shadow_tag (const struct c_declspecs *); extern void shadow_tag_warned (const struct c_declspecs *, int); -extern tree start_enum (location_t, struct c_enum_contents *, tree, tree); +extern tree start_enum (location_t, struct c_enum_contents *, tree, tree, + bool potential_nesting_p); extern bool start_function (struct c_declspecs *, struct c_declarator *, t= ree); extern tree start_decl (struct c_declarator *, struct c_declspecs *, bool, tree, bool =3D true, location_t * =3D NULL); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index dc8a16df272..8116c9b3e68 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -1419,6 +1419,9 @@ tagged_types_tu_compatible_p (const_tree t1, const_tr= ee t2, { case ENUMERAL_TYPE: { + if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2))) + return false; + /* Speed up the case where the type values are in the same order. */ tree tv1 =3D TYPE_VALUES (t1); tree tv2 =3D TYPE_VALUES (t2); @@ -6948,7 +6951,7 @@ convert_for_assignment (location_t location, location= _t expr_loc, tree type, if (checktype !=3D error_mark_node && TREE_CODE (checktype) =3D=3D ENUMERAL_TYPE && TREE_CODE (type) =3D=3D ENUMERAL_TYPE - && TYPE_MAIN_VARIANT (checktype) !=3D TYPE_MAIN_VARIANT (type)) + && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type))= ) { gcc_rich_location loc (location); warning_at (&loc, OPT_Wenum_conversion, diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-1.c b/gcc/testsuite/gcc.dg/c= 23-tag-enum-1.c new file mode 100644 index 00000000000..0b4829cdbe3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-1.c @@ -0,0 +1,56 @@ +/* + * { dg-do compile } + * { dg-options "-std=3Dc23" } + */ + +// incompatible redeclarations, conflicing redefinitions + + +enum aa { A =3D 1 } *a; +enum bb { B =3D 1 } *b; + +void test(void) +{ + enum aa { A =3D 1 } *c =3D a; + enum bb { B =3D 2 } *d =3D b; /* { dg-warning "incompatible pointer type= " } */ +} + +enum cc { C =3D 1 }; +enum cc { D =3D 1 }; /* { dg-error "conflicting redefinition" } */=09 + +enum dd { E =3D 1 }; +enum dd { E =3D 2 }; /* { dg-error "conflicting redefinition" } */=09 + /* { dg-error "redeclaration of enumerator" "" { target *-*-* } .-1 } = */=09 + + + +void test2(void) +{ + enum ee *a; + enum ee { F =3D 2 } *b; + b =3D a; +} + + +enum ff { G =3D 2 }; +enum gg { G =3D 2 }; /* { dg-error "redeclaration of enumerator" } */ +enum g2 { G =3D 3 }; /* { dg-error "redeclaration of enumerator" } */ + +enum hh { H =3D 1, H =3D 1 }; /* { dg-error "redeclaration of enumerator" = } */ + +enum ss { K =3D 2 }; +enum ss { K =3D 2 }; + +enum tt { R =3D 2 } TT; +enum tt { + R =3D _Generic(&TT, enum tt*: 0, default: 2) +}; + +enum { U =3D 1 }; +enum { U =3D 1 }; /* { dg-error "redeclaration of enumerator" } */ + +enum { V =3D 1 }; +enum { V =3D 2 }; /* { dg-error "redeclaration of enumerator" } */ + + + diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-2.c b/gcc/testsuite/gcc.dg/c= 23-tag-enum-2.c new file mode 100644 index 00000000000..1ced39974f4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-2.c @@ -0,0 +1,23 @@ +/* + * { dg-do compile } + * { dg-options "-std=3Dc23" } + */ + +// incomplete during construction + +enum A { B =3D 7 } y; +enum A { B =3D 7 }; + +enum A { B =3D _Generic(&y, enum A*: 1, default: 7) }; + +void g(void) +{ + enum A { B =3D _Generic(&y, enum A*: 1, default: 7) }; + _Static_assert(7 =3D=3D B, ""); +} + +enum X { E =3D 1, F =3D 1 + 1 }; +enum X { F =3D 2, E =3D 1 }; + + + diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-3.c b/gcc/testsuite/gcc.dg/c= 23-tag-enum-3.c new file mode 100644 index 00000000000..12218a5b911 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-3.c @@ -0,0 +1,7 @@ +/* { dg-do compile } + * { dg-options "-std=3Dc23" } + */ + +enum A { N =3D 0 * sizeof(enum A { M =3D 1 }) }; /* { dg-error "nested" }= */ + + diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-4.c b/gcc/testsuite/gcc.dg/c= 23-tag-enum-4.c new file mode 100644 index 00000000000..f20dc55fc6c --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-4.c @@ -0,0 +1,22 @@ +/* { dg-do compile } + * { dg-options "-std=3Dc23" } + */ + +// fixed underlying types + +enum A : int { N =3D 1 } x1 =3D { }; +enum B : int { M =3D 1 } x2 =3D { }; +enum C { U =3D 1 } x3 =3D { }; + +void f(void) +{ + enum A : int { N =3D 1 } y1 =3D x1; + enum B : short { M =3D 1 } y2; + y2 =3D x2; + enum B : short { M =3D 1 } y2b; + enum Bb : short { V =3D 1 } y2d =3D x2; + enum B : short { M =3D 1 } *y2e =3D &x2; /* { dg-warning "incompatible" }= */ + enum B : short { M =3D 1 } y2c =3D x2; + enum C { U =3D 1 } y3 =3D x3; +} + diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-5.c b/gcc/testsuite/gcc.dg/c= 23-tag-enum-5.c new file mode 100644 index 00000000000..22ce06fb80d --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-5.c @@ -0,0 +1,18 @@ +/* { dg-do compile } + * { dg-options "-std=3Dc23" } */ + +// test for nested redefinitions of enums + +void foo(void) +{ + enum e { A =3D 1 }; + enum e { A =3D 1 /* { dg-error "redeclaration" } */ + + 0 * sizeof(enum e { A =3D 1 }) }; /* { dg-error "nested redefinition" = } */ + =09 +} + +typedef __SIZE_TYPE__ size_t; +enum f : typeof (sizeof (enum f : size_t { B })) { B }; /* { dg-error "nes= ted redefinition" } */ + + + --=20 2.39.2