From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 862453858C2C for ; Thu, 24 Mar 2022 21:12:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 862453858C2C Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-39-cyuBb9XoPwKzHuQNJ-M-Nw-1; Thu, 24 Mar 2022 17:12:15 -0400 X-MC-Unique: cyuBb9XoPwKzHuQNJ-M-Nw-1 Received: by mail-qt1-f197.google.com with SMTP id f3-20020ac84983000000b002e22396acfbso4635845qtq.18 for ; Thu, 24 Mar 2022 14:12:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=6nzpdcQzpES+lTz842oGKMOMy2keeAEZ8gve8itFTFA=; b=WJg48fhHtsqm5E7AMUMw7gShFjVqhay3xOSFFM8qES81k48z2I1Q4qFQjwkvQI9WwH w7cs6F6U6pmOb22x7mEehvvYkaCRl5Uscoela6zQpttFQ2wBognhf3ycCRT77Kxy9ipm qDdNtZOvEcknidvcoAymY49tbE2nUW4Ept8X4n9MzZXJczyMHvHBObEeNWTyNuZw40of WDONgM5BteQqXkhL6ofjZnoTEsdVscocffQvP/16bQXF2L7VyH3Q8qxUW6N88zlfFqaG vNI6hCXdu7oDgsAJ4cxlz46yrZBV8Wg0c0RYb/ZXa+/UEZDf5l8jN+4bpK+YjHxUgTEj iCvA== X-Gm-Message-State: AOAM5318fTtGGZWLJKRDy6O76xu6fViTxszbYF3e1M9v/UyNdoCYoaLV N89b9ICyeGXaCQ4QF1DYKtw9SjyOYowYPbfJ/x6aRTFcQofdqiIWEtT9BhBiM26TmFxnLmpjH0+ EsVofS04FwlhT5qMSPA== X-Received: by 2002:ac8:59c3:0:b0:2e1:a305:cc6 with SMTP id f3-20020ac859c3000000b002e1a3050cc6mr6385488qtf.429.1648156334046; Thu, 24 Mar 2022 14:12:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLM4K2vFfW7ZF03me/Lv20gwo24AJp0h1iAQHZVxOO9YkXjjQiMU0wW3m0dnFCHsIavvxF0g== X-Received: by 2002:ac8:59c3:0:b0:2e1:a305:cc6 with SMTP id f3-20020ac859c3000000b002e1a3050cc6mr6385462qtf.429.1648156333576; Thu, 24 Mar 2022 14:12:13 -0700 (PDT) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id q8-20020a05622a04c800b002e06d7c1eabsm3228266qtx.16.2022.03.24.14.12.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Mar 2022 14:12:12 -0700 (PDT) Message-ID: Date: Thu, 24 Mar 2022 17:12:12 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v2] c++: alignas and alignof void [PR104944] To: Marek Polacek Cc: Joseph Myers , GCC Patches References: <20220324154922.478547-1-polacek@redhat.com> <354fcb2b-5cea-a48b-4af9-93f08c12104a@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-26.3 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , X-List-Received-Date: Thu, 24 Mar 2022 21:12:20 -0000 On 3/24/22 15:56, Marek Polacek wrote: > On Thu, Mar 24, 2022 at 12:02:29PM -0400, Jason Merrill wrote: >> On 3/24/22 11:49, Marek Polacek wrote: >>> I started looking into this PR because in GCC 4.9 we were able to >>> detect the invalid >>> >>> struct alignas(void) S{}; >>> >>> but I broke it in r210262. >>> >>> It's ill-formed code in C++: >>> [dcl.align]/3: "An alignment-specifier of the form alignas(type-id) has >>> the same effect as alignas(alignof(type-id))", and [expr.align]/1: >>> "The operand shall be a type-id representing a complete object type, >>> or an array thereof, or a reference to one of those types." and void >>> is not a complete type. >>> >>> It's also invalid in C: >>> 6.7.5: _Alignas(type-name) is equivalent to _Alignas(_Alignof(type-name)) >>> 6.5.3.4: "The _Alignof operator shall not be applied to a function type >>> or an incomplete type." >>> >>> We have a GNU extension whereby we treat sizeof(void) as 1, but I assume >>> it doesn't apply to alignof, so I'd like to reject it in C too. >> >> That makes sense to me in principle, but we've allowed it since the >> beginning of version control, back when c_alignof was a separate function. >> Changing that seems questionable for a regression fix. > > Ok, that makes sense. How about rejecting alignof(void) in C++ only > now (where it is a regression), and maybe come back to this in GCC 13 for C? I'd probably just leave it alone for C and __alignof. > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > I started looking into this PR because in GCC 4.9 we were able to > detect the invalid > > struct alignas(void) S{}; > > but I broke it in r210262. > > It's ill-formed code in C++: > [dcl.align]/3: "An alignment-specifier of the form alignas(type-id) has > the same effect as alignas(alignof(type-id))", and [expr.align]/1: > "The operand shall be a type-id representing a complete object type, > or an array thereof, or a reference to one of those types." and void > is not a complete type. > > It's also invalid in C: > 6.7.5: _Alignas(type-name) is equivalent to _Alignas(_Alignof(type-name)) > 6.5.3.4: "The _Alignof operator shall not be applied to a function type > or an incomplete type." > > We have a GNU extension whereby we treat sizeof(void) as 1, but I assume > it doesn't apply to alignof, so I'd like to reject it in C too, but it's > probably not a good time to do so in stage4. > > (We still say "invalid application of '__alignof__'" rather than > 'alignas' but I felt that fixing that may not be suitable as part of this > patch.) > > PR c++/104944 > > gcc/c-family/ChangeLog: > > * c-common.cc (c_sizeof_or_alignof_type): Do not allow alignof(void) > in C++. > > gcc/cp/ChangeLog: > > * typeck.cc (cxx_alignas_expr): Call cxx_sizeof_or_alignof_type with > complain == true. This hunk is OK. But let's put the diagnostic in cxx_sizeof_or_alignof_type, where it can depend on std_alignof. > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/alignas20.C: New test. > --- > gcc/c-family/c-common.cc | 20 +++++++++++++++----- > gcc/cp/typeck.cc | 14 ++++++++------ > gcc/testsuite/g++.dg/cpp0x/alignas20.C | 24 ++++++++++++++++++++++++ > 3 files changed, 47 insertions(+), 11 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/alignas20.C > > diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc > index d034837bb5b..f99717f540b 100644 > --- a/gcc/c-family/c-common.cc > +++ b/gcc/c-family/c-common.cc > @@ -3849,7 +3849,7 @@ c_common_get_alias_set (tree t) > the IS_SIZEOF parameter indicates which operator is being applied. > The COMPLAIN flag controls whether we should diagnose possibly > ill-formed constructs or not. LOC is the location of the SIZEOF or > - TYPEOF operator. If MIN_ALIGNOF, the least alignment required for > + ALIGNOF operator. If MIN_ALIGNOF, the least alignment required for > a type in any context should be returned, rather than the normal > alignment for that type. */ > > @@ -3891,10 +3891,20 @@ c_sizeof_or_alignof_type (location_t loc, > } > else if (type_code == VOID_TYPE || type_code == ERROR_MARK) > { > - if (type_code == VOID_TYPE > - && complain && warn_pointer_arith) > - pedwarn (loc, OPT_Wpointer_arith, > - "invalid application of %qs to a void type", op_name); > + if (type_code == VOID_TYPE && complain) > + { > + /* sizeof(void) is a GNU extension. */ > + if (is_sizeof || !c_dialect_cxx ()) > + pedwarn (loc, OPT_Wpointer_arith, > + "invalid application of %qs to a void type", op_name); > + /* But alignof(void) is not (in C++). */ > + else > + { > + error_at (loc, "invalid application of %qs to a void type", > + op_name); > + return error_mark_node; > + } > + } > else if (!complain) > return error_mark_node; > value = size_one_node; > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 516fa574ef6..96653c4f96e 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -1873,9 +1873,9 @@ compparms (const_tree parms1, const_tree parms2) > } > > > -/* Process a sizeof or alignof expression where the operand is a > - type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment) > - or GNU (preferred alignment) semantics; it is ignored if op is > +/* Process a sizeof or alignof expression where the operand is a type. > + STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment) > + or GNU (preferred alignment) semantics; it is ignored if OP is > SIZEOF_EXPR. */ > > tree > @@ -2132,11 +2132,13 @@ cxx_alignas_expr (tree e) > /* [dcl.align]/3: > > When the alignment-specifier is of the form > - alignas(type-id ), it shall have the same effect as > - alignas(alignof(type-id )). */ > + alignas(type-id), it shall have the same effect as > + alignas(alignof(type-id)). */ > > return cxx_sizeof_or_alignof_type (input_location, > - e, ALIGNOF_EXPR, true, false); > + e, ALIGNOF_EXPR, > + /*std_alignof=*/true, > + /*complain=*/true); > > /* If we reach this point, it means the alignas expression if of > the form "alignas(assignment-expression)", so we should follow > diff --git a/gcc/testsuite/g++.dg/cpp0x/alignas20.C b/gcc/testsuite/g++.dg/cpp0x/alignas20.C > new file mode 100644 > index 00000000000..f04f27927e2 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/alignas20.C > @@ -0,0 +1,24 @@ > +// PR c++/104944 > +// { dg-do compile { target c++11 } } > + > +struct inc; > + > +struct alignas(inc) S1 { }; // { dg-error "invalid application" } > +struct alignas(void) S2 { }; // { dg-error "invalid application" } > + > +template > +struct alignas(T) S4 {}; // { dg-error "invalid application" } > + > +template > +struct alignas(T) S5 {}; // { dg-error "invalid application" } > + > +S4 s1; > +S5 s2; > + > +void > +g () > +{ > + auto s1 = alignof(void); // { dg-error "invalid application" } > + auto s2 = alignof(const void); // { dg-error "invalid application" } > + auto s3 = alignof(inc); // { dg-error "invalid application" } > +} > > base-commit: 346ab5a54a831ad9c78afcbd8dfe98e0e07e3070