From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from esa4.mentor.iphmx.com (esa4.mentor.iphmx.com [68.232.137.252]) by sourceware.org (Postfix) with ESMTPS id B4ED03857C7D for ; Tue, 18 Oct 2022 23:26:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B4ED03857C7D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.95,194,1661846400"; d="scan'208";a="84997294" Received: from orw-gwy-01-in.mentorg.com ([192.94.38.165]) by esa4.mentor.iphmx.com with ESMTP; 18 Oct 2022 15:26:46 -0800 IronPort-SDR: H+kP2mTcXcR9lWJ+frs/UMdMRnJ6LW/8kni8hrrGBUg2NEHH9pMl8qbVRoXsmmXEHrccpPKBvv aA2QfNwkOGRNDKrLYFb8ty6tCqZ44IZjV50ysbnjSEteW4qU0p0rsyf8kO9krgeE8kz02OA8aZ WFCI9zvavh+dPj57oOnuBCszq/8o/9pRW3tL7zvxUe6kWCe2Yj4N9ShYOBmSu5tiE56T7Wo7x9 HjXlGLf+rIlk4sO9ZAHGRTXz7ZKXxNcsVPy39Dk0w3WBhuHN6wEofwLTHU1JM/uogCMhQFCSWm +vg= Date: Tue, 18 Oct 2022 23:26:40 +0000 From: Joseph Myers X-X-Sender: jsm28@digraph.polyomino.org.uk To: Subject: [committed] c: Diagnose "enum tag;" after definition [PR107164] Message-ID: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-07.mgc.mentorg.com (139.181.222.7) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-3116.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,SPF_HELO_PASS,SPF_PASS,TXREP 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: As noted in bug 101764, a declaration "enum tag;" is invalid in standard C after a definition, as well as when no definition is visible; we had a pedwarn-if-pedantic for the forward declaration case, but were missing one for the other case. Add that missing diagnostic (if pedantic only). (These diagnostics will need to be appropriately conditioned when support is added for C2x enums with fixed underlying type, since "enum tag : type;" is OK both before and after a definition.) Bootstrapped with no regressions for x86_64-pc-linux-gnu. PR c/107164 gcc/c/ * c-decl.cc (shadow_tag_warned): If pedantic, diagnose "enum tag;" with previous declaration visible. gcc/testsuite/ * gcc.dg/c99-tag-4.c, gcc.dg/c99-tag-5.c, gcc.dg/c99-tag-6.c: New tests. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index bcb4d7b66fe..80f6e912187 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -4814,6 +4814,20 @@ shadow_tag_warned (const struct c_declspecs *declspecs, int warned) warned = 1; pending_xref_error (); } + else if (declspecs->typespec_kind != ctsk_tagdef + && declspecs->typespec_kind != ctsk_tagfirstref + && declspecs->typespec_kind != ctsk_tagfirstref_attrs + && code == ENUMERAL_TYPE) + { + bool warned_enum = false; + if (warned != 1) + warned_enum = pedwarn (input_location, OPT_Wpedantic, + "empty declaration of % type " + "does not redeclare tag"); + if (warned_enum) + warned = 1; + pending_xref_error (); + } else { pending_invalid_xref = NULL_TREE; diff --git a/gcc/testsuite/gcc.dg/c99-tag-4.c b/gcc/testsuite/gcc.dg/c99-tag-4.c new file mode 100644 index 00000000000..9ff3ccb8d4b --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-tag-4.c @@ -0,0 +1,8 @@ +/* Test for handling of tags. "enum foo;" is invalid after an existing + declaration (does not redeclare the tag) as well as before: bug 107164. */ +/* { dg-do compile } */ +/* { dg-options "-std=c99 -pedantic-errors" } */ + +enum e1; /* { dg-error "ISO C forbids forward references to 'enum' types" } */ +enum e2 { E }; +enum e2; /* { dg-error "empty declaration of 'enum' type does not redeclare tag" } */ diff --git a/gcc/testsuite/gcc.dg/c99-tag-5.c b/gcc/testsuite/gcc.dg/c99-tag-5.c new file mode 100644 index 00000000000..97fcc75bc26 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-tag-5.c @@ -0,0 +1,8 @@ +/* Test for handling of tags. "enum foo;" is invalid after an existing + declaration (does not redeclare the tag) as well as before: bug 107164. */ +/* { dg-do compile } */ +/* { dg-options "-std=c99 -pedantic" } */ + +enum e1; /* { dg-warning "ISO C forbids forward references to 'enum' types" } */ +enum e2 { E }; +enum e2; /* { dg-warning "empty declaration of 'enum' type does not redeclare tag" } */ diff --git a/gcc/testsuite/gcc.dg/c99-tag-6.c b/gcc/testsuite/gcc.dg/c99-tag-6.c new file mode 100644 index 00000000000..8307217523c --- /dev/null +++ b/gcc/testsuite/gcc.dg/c99-tag-6.c @@ -0,0 +1,9 @@ +/* Test for handling of tags. "enum foo;" is invalid after an existing + declaration (does not redeclare the tag) as well as before: bug 107164. + Test this is not diagnosed without -pedantic. */ +/* { dg-do compile } */ +/* { dg-options "-std=c99" } */ + +enum e1; +enum e2 { E }; +enum e2; -- Joseph S. Myers joseph@codesourcery.com