From patchwork Tue Oct 18 23:26:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 4365 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp32712wrs; Tue, 18 Oct 2022 16:27:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4QRP4Jv+H30b4lxLymGFTSgpIOkxk2gpnzI9AOXekmem4kTZMvKrrA2jWnuvPSLIa4RD2y X-Received: by 2002:aa7:c58a:0:b0:459:1511:6cff with SMTP id g10-20020aa7c58a000000b0045915116cffmr4807890edq.27.1666135656670; Tue, 18 Oct 2022 16:27:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666135656; cv=none; d=google.com; s=arc-20160816; b=EaW4WShFQno9TfBdZ2eiGlObcCrrGuM+ts+H/eQxZObfSGDXpGUai1axmqViEg/kaM DoQ6zTvzQJNu+PczfwNkGcVSCYfjcXK9zCBwuqqZ2tCEm3s3sMZHhO85gcYvn35CB6T6 H82iFoB9jZaqnaUOrcpnf5TOOkNIpmShyK8SoeYh85gszxvFXbSrY5VnMJjPCu8T3OlD ea3g8pkJkNs8sWcMSGqffodzVzJImTxejFG5iaYN9LJ3yraElHM4hRFejDAfx8rA3eWf 8U26apD1OD2jNYPUtYGT24C2vTLxdZb+imngzpvPDBzjxQFte8NhyEfxzDMEDIsuXn4Q tMow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :message-id:subject:to:from:date:ironport-sdr:dmarc-filter :delivered-to; bh=9k/GBAiE7GZwVY2bKX6jBjdrOgG+oNPEL9dHp2EERWc=; b=KyadpW/DamqhCecI5bTUq2wBuby/VE0Z43j9PTuFZhUaCOcl+N11wWqzJgawTmFn/s EuNOAsqdMCoAhg0wYUklFtl2orKUbV7h2DUQ7x7gXAQR4IsAUfNlJJcSUaZ/3SIxOd3Z zcSpZoD343KivoPfkT6mBv4/2BOBPz5lyp7XkF4c2QlL79wTX/JZrbNvU38mGqTqxv6c 3bkSU+AyG9CLE3Xe7dwICYRPqqwc94qUXS31oZiJCMMwM/eor8TEg22Ychv8lf/QWbWE ruFK+ZdmD82tIT1+mDtG9LVh4LX7KRuMu8BKM4TGD/ky9XQ4Lq+pdj0u82CH+lVUaEdZ DH9g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w3-20020a05640234c300b0045acbc10e93si15846590edc.562.2022.10.18.16.27.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Oct 2022 16:27:36 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4133F3857BBC for ; Tue, 18 Oct 2022 23:27:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747069862499692775?= X-GMAIL-MSGID: =?utf-8?q?1747069862499692775?= 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;