[Bug c++/104944] [9/10/11/12 Regression] incorrect alignas(void) accepted (with warning if templated)

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Mar 25 14:15:32 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104944

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:d0b938a7612fb7acf1f181da9577235c83ede59e

commit r12-7815-gd0b938a7612fb7acf1f181da9577235c83ede59e
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed Mar 23 17:12:29 2022 -0400

    c++: alignas and alignof void [PR104944]

    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, at least in C++.  However, __alignof__(void)
    is still accepted with a -Wpedantic warning.

    We still say "invalid application of 'alignof'" rather than 'alignas' in
the
    void diagnostic, but I felt that fixing that may not be suitable as part of
    this patch.  The "incomplete type" diagnostic still always prints
    '__alignof__'.

            PR c++/104944

    gcc/cp/ChangeLog:

            * typeck.cc (cxx_sizeof_or_alignof_type): Diagnose alignof(void).
            (cxx_alignas_expr): Call cxx_sizeof_or_alignof_type with
            complain == true.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/alignas20.C: New test.


More information about the Gcc-bugs mailing list