]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: low -faligned-new [PR102071]
authorJason Merrill <jason@redhat.com>
Sun, 27 Mar 2022 16:36:13 +0000 (12:36 -0400)
committerJason Merrill <jason@redhat.com>
Thu, 12 May 2022 21:21:24 +0000 (17:21 -0400)
This test ICEd after the constexpr new patch (r10-3661) because alloc_call
had a NOP_EXPR around it; fixed by moving the NOP_EXPR to alloc_expr.  And
the PR pointed out that the size_t cookie might need more alignment, so I
fix that as well.

PR c++/102071

gcc/cp/ChangeLog:

* init.c (build_new_1): Include cookie in alignment.  Omit
constexpr wrapper from alloc_call.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/aligned-new9.C: New test.

gcc/cp/init.c
gcc/testsuite/g++.dg/cpp1z/aligned-new9.C [new file with mode: 0644]

index 0036ab60bb13668eba7f21a4cd31153a50a42591..35c142be60fde76aac850d2f5092f88276d14cea 100644 (file)
@@ -3229,7 +3229,13 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 
   tree align_arg = NULL_TREE;
   if (type_has_new_extended_alignment (elt_type))
-    align_arg = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (elt_type));
+    {
+      unsigned align = TYPE_ALIGN_UNIT (elt_type);
+      /* Also consider the alignment of the cookie, if any.  */
+      if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
+       align = MAX (align, TYPE_ALIGN_UNIT (size_type_node));
+      align_arg = build_int_cst (align_type_node, align);
+    }
 
   alloc_fn = NULL_TREE;
 
@@ -3415,18 +3421,19 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
   if (TREE_CODE (alloc_call_expr) == CALL_EXPR)
     CALL_FROM_NEW_OR_DELETE_P (alloc_call_expr) = 1;
 
+  alloc_expr = alloc_call;
   if (cookie_size)
-    alloc_call = maybe_wrap_new_for_constexpr (alloc_call, type,
+    alloc_expr = maybe_wrap_new_for_constexpr (alloc_call, type,
                                               cookie_size);
 
   /* In the simple case, we can stop now.  */
   pointer_type = build_pointer_type (type);
   if (!cookie_size && !is_initialized)
-    return build_nop (pointer_type, alloc_call);
+    return build_nop (pointer_type, alloc_expr);
 
   /* Store the result of the allocation call in a variable so that we can
      use it more than once.  */
-  alloc_expr = get_target_expr (alloc_call);
+  alloc_expr = get_target_expr (alloc_expr);
   alloc_node = TARGET_EXPR_SLOT (alloc_expr);
 
   /* Strip any COMPOUND_EXPRs from ALLOC_CALL.  */
diff --git a/gcc/testsuite/g++.dg/cpp1z/aligned-new9.C b/gcc/testsuite/g++.dg/cpp1z/aligned-new9.C
new file mode 100644 (file)
index 0000000..3fa0ed9
--- /dev/null
@@ -0,0 +1,30 @@
+// PR c++/102071
+// { dg-do run { target c++17 } }
+// { dg-additional-options -faligned-new=2 }
+
+#include <new>
+
+int nalign;
+void *operator new (std::size_t s, std::align_val_t a)
+{
+  nalign = (int)a;
+  return operator new (s);
+}
+
+struct X { ~X(); int c; };
+
+int align = (alignof (X) > alignof (std::size_t)
+            ? alignof (X) : alignof (std::size_t));
+
+int n = 4;
+
+int main()
+{
+  X *p = new X[n];
+  if (nalign != align)
+    __builtin_abort ();
+
+  X *p2 = new X;
+  if (nalign != alignof (X))
+    __builtin_abort ();
+}