In Joseph's patch for bug 52023, making _Alignof(double) 4 rather than
8 on x86, he deliberately didn't change the C++ behavior, based on
this wording in the C++ standard:

--
The alignment required for a type might be different when it is used
as the type of a complete object and when it is used as the type of a
subobject. [ Example:
  struct B { long double d; };
  struct D : virtual B { char c; };
When D is the type of a complete object, it will have a subobject of
type B, so it must be aligned appropriately for a long double. If D
appears as a subobject of another object that also has B as a virtual
base class, the B subobject might be part of a different subobject,
reducing the alignment requirements on the D subobject. — end example
] The result of the alignof operator reflects the alignment
requirement of the type in the complete-object case.
--

This suggests that alignof shouldn't consider alignment of non-static
data members.  I think that this wording is wrong, that "subobject"
was intended only to refer to base subobjects.

But really this is beside the point: the x86 ABI says that the
alignment of double is 4, so alignof(double) should be 4 regardless of
what GCC wants to do internally.  And I think the same is true of
__alignof__.

Thoughts?
Jason
commit 3ca02d94a346c46edc08185897aebfda03d17969
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Apr 10 13:09:37 2018 -0400

            PR c++/69763 - wrong alignof(double) on x86.
    
            * typeck.c (cxx_sizeof_or_alignof_type): Request min_align
            from c_sizeof_or_alignof_type.

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index e5ad54dbcd1..442cf9df282 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -1641,7 +1641,7 @@ cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
     }
 
   return c_sizeof_or_alignof_type (input_location, complete_type (type),
-				   op == SIZEOF_EXPR, false,
+				   op == SIZEOF_EXPR, true,
 				   complain);
 }
 
diff --git a/gcc/testsuite/g++.dg/abi/align2.C b/gcc/testsuite/g++.dg/abi/align2.C
new file mode 100644
index 00000000000..bc993b24a1e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/abi/align2.C
@@ -0,0 +1,5 @@
+// PR c++/69763
+// { dg-do compile { target { { i?86-*-* } && c++11 } } }
+
+#define SA(X) static_assert ((X), #X)
+SA(alignof(double) == 4);

Reply via email to