From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1105) id 469A23858D32; Tue, 18 Oct 2022 23:26:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 469A23858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666135596; bh=eusUTDid1fWpVPgskoENMJwtBd0YESm9/XfxrdeqKFw=; h=From:To:Subject:Date:From; b=dfXp7hf6QyjuNxmeKSP16DaQU3ymShqnZss6clGylKgdA4Hl3Lj1RP4P7BuFF6U2v NW7HZVViPReIDVFESODHUgajXARqpJHBOkQ8h5Ogxi7k3ATiFzVv4UjecO86n2IzzZ Fumkyl/uPVR+ZdZt7P9Y0KbuLc1DYzICfSistkHo= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Joseph Myers To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3366] c: Diagnose "enum tag;" after definition [PR107164] X-Act-Checkin: gcc X-Git-Author: Joseph Myers X-Git-Refname: refs/heads/master X-Git-Oldrev: cc694f45087c892e69ebbb177203c708f00b1bc7 X-Git-Newrev: f5f1d92fe2e1d75c3fae34497929a1965af704ae Message-Id: <20221018232636.469A23858D32@sourceware.org> Date: Tue, 18 Oct 2022 23:26:36 +0000 (GMT) List-Id: https://gcc.gnu.org/g:f5f1d92fe2e1d75c3fae34497929a1965af704ae commit r13-3366-gf5f1d92fe2e1d75c3fae34497929a1965af704ae Author: Joseph Myers Date: Tue Oct 18 23:25:47 2022 +0000 c: Diagnose "enum tag;" after definition [PR107164] 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: --- gcc/c/c-decl.cc | 14 ++++++++++++++ gcc/testsuite/gcc.dg/c99-tag-4.c | 8 ++++++++ gcc/testsuite/gcc.dg/c99-tag-5.c | 8 ++++++++ gcc/testsuite/gcc.dg/c99-tag-6.c | 9 +++++++++ 4 files changed, 39 insertions(+) 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;