From patchwork Wed Dec 14 22:14:18 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 131484 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]) by ozlabs.org (Postfix) with SMTP id BBEB31007D7 for ; Thu, 15 Dec 2011 09:14:35 +1100 (EST) Received: (qmail 24252 invoked by alias); 14 Dec 2011 22:14:33 -0000 Received: (qmail 24243 invoked by uid 22791); 14 Dec 2011 22:14:32 -0000 X-SWARE-Spam-Status: No, hits=-5.3 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_CX X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 14 Dec 2011 22:14:20 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pBEMEJK9005835 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 14 Dec 2011 17:14:20 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id pBEMEJs7028268 for ; Wed, 14 Dec 2011 17:14:19 -0500 Received: from [0.0.0.0] (ovpn-113-21.phx2.redhat.com [10.3.113.21]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id pBEMEIKU013477 for ; Wed, 14 Dec 2011 17:14:19 -0500 Message-ID: <4EE91FBA.1070204@redhat.com> Date: Wed, 14 Dec 2011 17:14:18 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111112 Thunderbird/8.0 MIME-Version: 1.0 To: gcc-patches List Subject: RFA: PATCH to handle_transparent_union_attribute for c++/51228 (ICE on bogus use of transparent_union) 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 It doesn't make sense to mark a type with no fields as a transparent union, and indeed handle_transparent_union checks for that. But this testcase was slipping past the checks because we're applying the attribute in place but after the type has been completed. This patch adjusts the check logic so that we check for a suitable field even when applying the attribute in place if we've already done finish_struct. Tested x86_64-pc-linux-gnu. OK for trunk? This is apparently a regression back to 4.5.0, but doesn't seem worth fixing in older releases since this isn't a valid use of the attribute. commit f3b9daf04363159ed8707df50ed77989e1ee31be Author: Jason Merrill Date: Wed Dec 14 14:05:57 2011 -0500 PR c++/51228 * c-common.c (handle_transparent_union_attribute): Check the first field if the type is complete. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index fbbcb38..87eee79 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -6286,13 +6286,21 @@ handle_transparent_union_attribute (tree *node, tree name, if (TREE_CODE (type) == UNION_TYPE) { - /* When IN_PLACE is set, leave the check for FIELDS and MODE to - the code in finish_struct. */ + /* Make sure that the first field will work for a transparent union. + If the type isn't complete yet, leave the check to the code in + finish_struct. */ + if (TYPE_SIZE (type)) + { + tree first = first_field (type); + if (first == NULL_TREE + || TYPE_MODE (type) != DECL_MODE (first)) + goto ignored; + } + if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) { - if (TYPE_FIELDS (type) == NULL_TREE - || c_dialect_cxx () - || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type))) + /* build_duplicate_type doesn't work for C++. */ + if (c_dialect_cxx ()) goto ignored; /* A type variant isn't good enough, since we don't a cast diff --git a/gcc/testsuite/c-c++-common/transparent-union-1.c b/gcc/testsuite/c-c++-common/transparent-union-1.c new file mode 100644 index 0000000..3fb6e78 --- /dev/null +++ b/gcc/testsuite/c-c++-common/transparent-union-1.c @@ -0,0 +1,5 @@ +/* PR c++/51228 */ + +typedef union {} U __attribute__((transparent_union)); /* { dg-warning "ignored" } */ + +void foo(U u) {}