public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-7815] c++: alignas and alignof void [PR104944]
Date: Fri, 25 Mar 2022 14:15:31 +0000 (GMT)	[thread overview]
Message-ID: <20220325141531.A04D8388981F@sourceware.org> (raw)

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.

Diff:
---
 gcc/cp/typeck.cc                       | 21 +++++++++++++++------
 gcc/testsuite/g++.dg/cpp0x/alignas20.C | 26 ++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 516fa574ef6..26a7cb4b50d 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -1873,9 +1873,9 @@ compparms (const_tree parms1, const_tree parms2)
 }
 
 \f
-/* 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
@@ -1899,6 +1899,13 @@ cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
       else
 	return error_mark_node;
     }
+  else if (VOID_TYPE_P (type) && std_alignof)
+    {
+      if (complain)
+	error_at (loc, "invalid application of %qs to a void type",
+		  OVL_OP_INFO (false, op)->name);
+      return error_mark_node;
+    }
 
   bool dependent_p = dependent_type_p (type);
   if (!dependent_p)
@@ -2132,11 +2139,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..01a55f3d4a4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/alignas20.C
@@ -0,0 +1,26 @@
+// PR c++/104944
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wpedantic" }
+
+struct inc;
+
+struct alignas(inc) S1 { }; // { dg-error "invalid application" }
+struct alignas(void) S2 { }; // { dg-error "invalid application" }
+
+template <typename T>
+struct alignas(T) S4 {}; // { dg-error "invalid application" }
+
+template <typename T>
+struct alignas(T) S5 {}; // { dg-error "invalid application" }
+
+S4<void> s1;
+S5<inc> s2;
+
+void
+g ()
+{
+  auto s1 = alignof(void); // { dg-error "invalid application" }
+  auto s2 = alignof(const void); // { dg-error "invalid application" }
+  auto s3 = __alignof(void); // { dg-warning "invalid application" }
+  auto s4 = alignof(inc); // { dg-error "invalid application" }
+}


                 reply	other threads:[~2022-03-25 14:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220325141531.A04D8388981F@sourceware.org \
    --to=mpolacek@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).