]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: alignas and alignof void [PR104944]
authorMarek Polacek <polacek@redhat.com>
Wed, 23 Mar 2022 21:12:29 +0000 (17:12 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 29 Mar 2022 01:46:14 +0000 (21:46 -0400)
commitfe641f6a44959ff8b9d9f7af11b8b806c5f1375a
tree4f464d664265a830fa3cab0f364e463bff28baac
parent2a2b944c18936f9dfa880e8ff54906cfe90e89e8
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.c (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.

(cherry picked from commit d0b938a7612fb7acf1f181da9577235c83ede59e)
gcc/cp/typeck.c
gcc/testsuite/g++.dg/cpp0x/alignas20.C [new file with mode: 0644]