From patchwork Fri Jan 15 03:01:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 567896 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 D909B140556 for ; Fri, 15 Jan 2016 14:02:15 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=tmWj4TT8; 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 :message-id:date:from:mime-version:to:cc:subject:references :in-reply-to:content-type; q=dns; s=default; b=l36b/dQ/zd0Okk7KY EwAAjeEQ5X1hp4B93GgASUUxrYYGPAdDDopg7gyuF20egUlIlQEHZ7wb6kg0JIsu vj2dt3wU0r9xDGVp8Ha0+BRHqFD0dIns6JmP7be0bsia9zvjO4A2E1Fv8UOI/Pwg F9tOh6PyCgpNZ2b5IPskT0D9mY= 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 :message-id:date:from:mime-version:to:cc:subject:references :in-reply-to:content-type; s=default; bh=jSS7favEp9dzGD4DfGg5VE+ OyYo=; b=tmWj4TT8ilCJrPWfgXlxkqJkQwFVwn2eD2WolHcRcaMibOWzK1hkz+z JQd8K+wNvYUH7VK/+20nWgUve/h4O5rVYe8h+V98/hG/q2I35Kf+riT23mJ9NkZ4 VeUFzMxpr4ulPk2GZZMz2RICfjO0esTCzrTcYF892JRn5xvr9bDw= Received: (qmail 36123 invoked by alias); 15 Jan 2016 03:02:07 -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 36111 invoked by uid 89); 15 Jan 2016 03:02:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL, BAYES_50, FREEMAIL_FROM, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=no version=3.3.2 spammy=anticipation, 1, 49, type_max_value, TYPE_MAX_VALUE X-HELO: mail-qk0-f177.google.com Received: from mail-qk0-f177.google.com (HELO mail-qk0-f177.google.com) (209.85.220.177) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 15 Jan 2016 03:02:05 +0000 Received: by mail-qk0-f177.google.com with SMTP id x1so34203163qkc.1 for ; Thu, 14 Jan 2016 19:02:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=Auf3KwcTr8xU2uoPPAiY4th5ehsyqlXXfB6QJZ/5a64=; b=EdE3FuuL22m3rTcNyGMbsNDbVW53OxJe5+vxwIVeq8l3fK3LI5h7bDbtTInxe8vN0+ 5/p5gpjYnbZvzm+xYoyAFiohaK8WUVCowWMrxFZE1+t4IEpfxsbNsRrjGEa/gmsKXPj0 m7KHp0MN25rXMaozoolCu1xOpZjHVx3hh80DaTl4H0N6Hsyl4emK4zzIyq527nreRjDU 1y1YovZV+aL+a9BQaLJhAGywLNhPHqhq4YRCLCNCpxzTqAHWHLxrT0xGxrN/Uj41G4jc m0KoISAeDV/YKpkpfqpiYMCFzhENveSgxuJz0B3xWw2LHSpGSZjENgUJOkFMesfXPArk 4r/A== X-Gm-Message-State: ALoCoQkk+HHpZlNidEAcS6trdCTLX7M5B6WCxAdpRFrKUJ4cDis+T7cRFn9fJn4i7+8W83pe7PWMSWzr+gWyFIRTV1RTQRiFlQ== X-Received: by 10.55.80.86 with SMTP id e83mr9967014qkb.91.1452826922988; Thu, 14 Jan 2016 19:02:02 -0800 (PST) Received: from [192.168.0.26] (71-212-229-169.hlrn.qwest.net. [71.212.229.169]) by smtp.gmail.com with ESMTPSA id 75sm3768175qkw.19.2016.01.14.19.02.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Jan 2016 19:02:02 -0800 (PST) Message-ID: <56986127.5080406@gmail.com> Date: Thu, 14 Jan 2016 20:01:59 -0700 From: Martin Sebor User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: Jeff Law , Jakub Jelinek CC: Gcc Patch List , Jason Merrill Subject: Re: [PATCH] fix #69277 - [6 Regression] ICE mangling a flexible array member References: <56982542.4050706@gmail.com> <20160114225118.GP3017@tucnak.redhat.com> <56983733.2080605@gmail.com> <56985EF8.5080709@redhat.com> In-Reply-To: <56985EF8.5080709@redhat.com> X-IsSubscribed: yes On 01/14/2016 07:52 PM, Jeff Law wrote: > On 01/14/2016 05:02 PM, Martin Sebor wrote: >> On 01/14/2016 03:51 PM, Jakub Jelinek wrote: >>> On Thu, Jan 14, 2016 at 03:46:26PM -0700, Martin Sebor wrote: >>>> c++/69277 reports an ICE when mangling a template specialization >>>> involving flexible array member. Debugging the problem revealed >>>> that GCC (prior to the ICE), due to treating flexible array >>>> members the same as zero-length arrays, produced the wrong mangling >>>> for the former. >>>> >>>> The attached patch fixes the ICE and also corrects the mangling >>>> (which now also matches Clang's). >>> >>> But then, shouldn't the decision whether to mangle it the old way or >>> the new >>> way depend on -fabi-version= ? >> >> Perhaps it should, I don't know. Jason didn't mention anything >> when we briefly discussed the mangling change on IRC. I can >> certainly modify the patch if it's necessary or a good idea. >> Let me know. > Jason would have the final call here, but I suspect we'll need to do > something. > > While it is a bugfix, we have traditionally queued them up to avoid > regular ABI breakages. It's also the case that we've traditionally > added a warning for this kind of thing (controlled by Wabi) and also > noted something in the manual (I think there's a section dedicated to > this kind of thing). In anticipation of needing to do something I put together the attached patch that rolls this change into version 10, letting version 9 and prior roll it back. I also mention it in the manual. What the patch doesn't do is add a warning. I did notice with the mangling change we already issue a note for one ABI change about flexible arrays: note: the ABI of passing struct with a flexible array member has changed in GCC 4.4 so maybe this is an opportunity to update it. Martin gcc/testsuite/ChangeLog: 2016-01-14 Martin Sebor PR c++/69277 * gcc/testsuite/g++.dg/abi/flexarray-abi-v9.C: New test. * g++.dg/ext/flexarray-mangle-2.C: New test. * g++.dg/ext/flexarray-mangle.C: New test. gcc/cp/ChangeLog: 2016-01-14 Martin Sebor PR c++/69277 * mangle.c (write_array_type): Mangle flexible array members the same as arrays with an unspecified size. gcc/ChangeLog: 2016-01-14 Martin Sebor PR c++/69277 * invoke.texi (-fabi-version=): Update version 10. Index: gcc/cp/mangle.c =================================================================== --- gcc/cp/mangle.c (revision 232296) +++ gcc/cp/mangle.c (working copy) @@ -3280,8 +3280,10 @@ write_template_template_arg (const tree ::= A _ "Array types encode the dimension (number of elements) and the - element type. For variable length arrays, the dimension (but not - the '_' separator) is omitted." */ + element type. For variable length arrays, the dimension (but not + the '_' separator) is omitted." + Note that for flexible array members, like for other arrays of + unspecified size, the dimension is also omitted. */ static void write_array_type (const tree type) @@ -3290,29 +3292,37 @@ write_array_type (const tree type) if (TYPE_DOMAIN (type)) { tree index_type; - tree max; index_type = TYPE_DOMAIN (type); - /* The INDEX_TYPE gives the upper and lower bounds of the - array. */ - max = TYPE_MAX_VALUE (index_type); - if (TREE_CODE (max) == INTEGER_CST) + /* The INDEX_TYPE gives the upper and lower bounds of the array. + It's null for flexible array members which have no upper bound + (this is a change from GCC 5 and prior where such members were + incorrectly mangled as zero-length arrays). */ + if (tree max = TYPE_MAX_VALUE (index_type)) { - /* The ABI specifies that we should mangle the number of - elements in the array, not the largest allowed index. */ - offset_int wmax = wi::to_offset (max) + 1; - /* Truncate the result - this will mangle [0, SIZE_INT_MAX] - number of elements as zero. */ - wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max))); - gcc_assert (wi::fits_uhwi_p (wmax)); - write_unsigned_number (wmax.to_uhwi ()); + if (TREE_CODE (max) == INTEGER_CST) + { + /* The ABI specifies that we should mangle the number of + elements in the array, not the largest allowed index. */ + offset_int wmax = wi::to_offset (max) + 1; + /* Truncate the result - this will mangle [0, SIZE_INT_MAX] + number of elements as zero. */ + wmax = wi::zext (wmax, TYPE_PRECISION (TREE_TYPE (max))); + gcc_assert (wi::fits_uhwi_p (wmax)); + write_unsigned_number (wmax.to_uhwi ()); + } + else + { + max = TREE_OPERAND (max, 0); + write_expression (max); + } } - else + else if (!abi_version_at_least (10)) { - max = TREE_OPERAND (max, 0); - write_expression (max); + /* In ABI versions prior to 10 mangle flexible array members + the same as zero-length arrays for compatibility. */ + write_unsigned_number (0); } - } write_char ('_'); write_type (TREE_TYPE (type)); Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 232296) +++ gcc/doc/invoke.texi (working copy) @@ -2173,7 +2173,8 @@ Version 9, which first appeared in G++ 5 Version 10, which first appeared in G++ 6.1, adds mangling of attributes that affect type identity, such as ia32 calling convention -attributes (e.g. @samp{stdcall}). +attributes (e.g. @samp{stdcall}). This version also corrects the mangling +of flexible array members. See also @option{-Wabi}. Index: gcc/testsuite/g++.dg/ext/flexarray-mangle-2.C =================================================================== --- gcc/testsuite/g++.dg/ext/flexarray-mangle-2.C (revision 0) +++ gcc/testsuite/g++.dg/ext/flexarray-mangle-2.C (working copy) @@ -0,0 +1,48 @@ +// PR c++/69277 - [6 Regression] ICE mangling a flexible array member +// { dg-do compile { target c++11 } } + +struct A { + int n; + char a []; +}; + +// Declare but do not define function templates. +template +void foo (); + +template +void fooref (T&); + +// Rvalue references are a C++ 11 feature. +template +void foorefref (T&&); + +void bar (A a) +{ + // Decltype is also a C++ 11 feature. + // Verify that decltype gets the right type and that foo is + // mangled correctly. + foo(); + + // Verify that function templates taking a reference and an rvalue + // references (as in PR c++/69277) are also mangled correctly. + fooref (a.a); + foorefref (a.a); +} + +// In G++ versions prior to 6 (ABI version 9 and prior), flexible array +// members were incorrectly mangled as arrays of zero elements. Verify +// that flexible array members are mangled correctly as arrays of +// an unspecified number of elements by default (ABI version 10 and +// newer). See g++.dg/abi/flexarray-abi-v9.C for the compatibility +// test. + +// void foo(): +// { dg-final { scan-assembler _Z3fooIA_cEvv } } + +// The following is derived from PR c++/69277: +// void fooref(char (&) []) +// { dg-final { scan-assembler _Z6foorefIA_cEvRT_ } } + +// void foorefref(char (&) []) +// { dg-final { scan-assembler _Z9foorefrefIRA_cEvOT_ } } Index: gcc/testsuite/g++.dg/ext/flexarray-mangle.C =================================================================== --- gcc/testsuite/g++.dg/ext/flexarray-mangle.C (revision 0) +++ gcc/testsuite/g++.dg/ext/flexarray-mangle.C (working copy) @@ -0,0 +1,26 @@ +// PR c++/69277 - [6 Regression] ICE mangling a flexible array member +// { dg-do compile } + +struct A { + int n; + char a []; +}; + +// Declare but do not define function templates. +template +void fooref (T&); + +void bar (A a) +{ + fooref (a.a); +} + +// In G++ versions prior to 6 (ABI version 9 and prior), flexible array +// members were incorrectly mangled as arrays of zero elements. Verify +// that flexible array members are mangled correctly as arrays of +// an unspecified number of elements by default (ABI version 10 and +// newer). See g++.dg/abi/flexarray-abi-v9.C for the compatibility +// test. + +// void fooref(char (&) []) +// { dg-final { scan-assembler _Z6foorefIA_cEvRT_ } } Index: gcc/testsuite/g++.dg/abi/flexarray-abi-v9.C =================================================================== --- gcc/testsuite/g++.dg/abi/flexarray-abi-v9.C (revision 0) +++ gcc/testsuite/g++.dg/abi/flexarray-abi-v9.C (working copy) @@ -0,0 +1,49 @@ +// Test to verify ABI version 9 (and prior) compatibility mangling +// of a flexible array member. See also PR c++/69277. +// { dg-do compile { target c++11 } } +// { dg-options "-fabi-version=9" } + +struct A { + int n; + char a []; +}; + +// Declare but do not define function templates. +template +void foo (); + +template +void fooref (T&); + +// Rvalue references are a C++ 11 feature. +template +void foorefref (T&&); + +void bar (A a) +{ + // Decltype is also a C++ 11 feature. + // Verify that decltype gets the right type and that foo is + // mangled correctly. + foo(); + + // Verify that function templates taking a reference and an rvalue + // references (as in PR c++/69277) are also mangled correctly. + fooref (a.a); + foorefref (a.a); +} + +// In G++ versions prior to 6 (ABI version 10), flexible array members +// were incorrectly mangled as arrays of zero elements. Verify that +// they are still mangled that way with -fabi-version=9 (i.e., unless +// -fabi-version=10 is used. See g++.dg/ext/flexarray-mangle-3.C +// for that test.) + +// void foo(): +// { dg-final { scan-assembler _Z3fooIA0_cEvv } } + +// The following is derived from PR c++/69277: +// void fooref(char (&) [0]) +// { dg-final { scan-assembler _Z6foorefIA0_cEvRT_ } } + +// void foorefref(char (&) [0]) +// { dg-final { scan-assembler _Z9foorefrefIRA0_cEvOT_ } }