From patchwork Tue Jun 6 16:48:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 771951 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3whyN71cpKz9s4q for ; Wed, 7 Jun 2017 02:49:57 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="tlSDYemK"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=gEm2GEvFDFj0r4Tcz2vXZaGcVPbdSsOzTTyETWsItwThH34Cv7E04 ctrA0mqyHN78PFYyZCXDL2a83mCNBhbPuj9LVfQykmmv75AL41GLguNQi5GuU4Nr Z5ZHxovtx/GBUme9gYM88xVt5EXZe1aIwtDSlMbzjnUHS8pMotlB/4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=hV3jCVG4w9fIccVkZd7DMi6neOc=; b=tlSDYemK+VhasXQTi9Z8 o1Fy2bZjSdiHkSYwyQCwVJupDNtGoLKOHF+szdl5v/qBWx3Phz0aXzxAniqZBT6i Udpqik0aIHvOW0Q0+Of7SfbOUDLrPYI+4685WB8kmkg3rtrjcaSoZzoxlx6buJGS d00oZ1A2J4j6kICgAVCeI1k= Received: (qmail 25434 invoked by alias); 6 Jun 2017 16:48:47 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 25337 invoked by uid 89); 6 Jun 2017 16:48:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 06 Jun 2017 16:48:35 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 52C8B811A9; Tue, 6 Jun 2017 16:48:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 52C8B811A9 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=polacek@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 52C8B811A9 Received: from redhat.com ([10.40.205.13]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v56GmSkP007447 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 6 Jun 2017 12:48:31 -0400 Date: Tue, 6 Jun 2017 18:48:28 +0200 From: Marek Polacek To: GCC Patches , Joseph Myers Subject: C PATCH to improve enum and struct redefinition diagnostic (PR c/79983) Message-ID: <20170606164828.GP3413@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.8.0 (2017-02-23) This patch brings better enum and struct redefinition diagnostic. In particular, we'll now point to the first definition in the "originally defined here" note, not to the forward declaration. Now, you could argue that we don't have to be setting the location by lookup_tag at all, because we should always have the location in TYPE_STUB_DECL, and then we could get rid of that lookup_tag parameter completely, but I think this is good enough for now. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2017-06-06 Marek Polacek PR c/79983 * c-decl.c (start_struct): Use the location of TYPE_STUB_DECL of ref. (start_enum): Use the location of TYPE_STUB_DECL of enumtype. * gcc.dg/pr79983.c: New test. Marek diff --git gcc/c/c-decl.c gcc/c/c-decl.c index f2b8096..3a0a4f5 100644 --- gcc/c/c-decl.c +++ gcc/c/c-decl.c @@ -7453,6 +7453,9 @@ start_struct (location_t loc, enum tree_code code, tree name, ref = lookup_tag (code, name, true, &refloc); if (ref && TREE_CODE (ref) == code) { + if (TYPE_STUB_DECL (ref)) + refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref)); + if (TYPE_SIZE (ref)) { if (code == UNION_TYPE) @@ -8185,7 +8188,10 @@ start_enum (location_t loc, struct c_enum_contents *the_enum, tree name) /* Update type location to the one of the definition, instead of e.g. a forward declaration. */ else if (TYPE_STUB_DECL (enumtype)) - DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc; + { + enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)); + DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc; + } if (C_TYPE_BEING_DEFINED (enumtype)) error_at (loc, "nested redefinition of %", name); diff --git gcc/testsuite/gcc.dg/pr79983.c gcc/testsuite/gcc.dg/pr79983.c index e69de29..84aae69 100644 --- gcc/testsuite/gcc.dg/pr79983.c +++ gcc/testsuite/gcc.dg/pr79983.c @@ -0,0 +1,15 @@ +/* PR c/79983 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +struct S; +struct S { int i; }; /* { dg-message "originally defined here" } */ +struct S { int i, j; }; /* { dg-error "redefinition of 'struct S'" } */ + +enum E; +enum E { A, B, C }; /* { dg-message "originally defined here" } */ +enum E { D, F }; /* { dg-error "nested redefinition of 'enum E'|redeclaration of 'enum E'" } */ + +union U; +union U { int i; }; /* { dg-message "originally defined here" } */ +union U { int i; double d; }; /* { dg-error "redefinition of 'union U'" } */