]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
testsuite: Add 2 new tests
authorJakub Jelinek <jakub@redhat.com>
Sat, 4 Oct 2025 09:19:30 +0000 (11:19 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Sat, 4 Oct 2025 09:19:30 +0000 (11:19 +0200)
The following patch adds a variant of constexpr-new23.C and
constexpr-new4.C tests which are or could be affected by some of
the discussed changes in new expression clobbers.

2025-10-04  Jakub Jelinek  <jakub@redhat.com>

* g++.dg/cpp2a/constexpr-new28.C: New test.
* g++.dg/cpp2a/constexpr-new29.C: New test.

gcc/testsuite/g++.dg/cpp2a/constexpr-new28.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-new29.C [new file with mode: 0644]

diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new28.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new28.C
new file mode 100644 (file)
index 0000000..7828f30
--- /dev/null
@@ -0,0 +1,45 @@
+// PR c++/115645
+// { dg-do compile { target c++20 } }
+
+using size_t = decltype(sizeof(0));
+
+void* operator new(size_t, void* p) { return p; }
+void* operator new[](size_t, void* p) { return p; }
+
+#define VERIFY(C) if (!(C)) throw
+
+namespace std {
+  template<typename T>
+    constexpr T* construct_at(T* p)
+    {
+      if constexpr (__is_array(T))
+        return ::new((void*)p) T[1]();
+      else
+        return ::new((void*)p) T();
+    }
+}
+
+struct S {
+  constexpr S () : s (0) {}
+  constexpr S (int x) : s (x) {}
+  constexpr bool operator== (int x) const { return s == x; }
+  int s;
+};
+
+constexpr void
+test_array()
+{
+  S arr[1] { 99 };
+  std::construct_at(&arr);
+  VERIFY( arr[0] == 0 );
+
+  union U {
+    long long x = -1;
+    S arr[4];
+  } u;
+
+  auto p = std::construct_at(&u.arr);
+  VERIFY( (*p)[0] == 0 );
+}
+
+static_assert( [] { test_array(); return true; }() );
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-new29.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-new29.C
new file mode 100644 (file)
index 0000000..368018d
--- /dev/null
@@ -0,0 +1,30 @@
+// P0784R7
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fdelete-null-pointer-checks" }
+
+struct S
+{
+  constexpr S () : s (0) { s++; }
+  constexpr S (int x) : s (x) { s += 2; }
+  constexpr ~S () { if (s != 35) asm (""); s = 5; }
+  int s;
+};
+
+constexpr bool
+foo (int n)
+{
+  S *p = new S (7);
+  if (p->s != 9) return false;
+  p->s = 35;
+  delete p;
+  p = new S[n] { 11, 13, 15 };
+  if (p[0].s != 13 || p[1].s != 15 || p[2].s != 17) return false;
+  p[0].s = 35;
+  p[2].s = 35;
+  p[1].s = 35;
+  delete[] p;
+  return true;
+}
+
+constexpr bool a = foo (3);
+static_assert (a);