]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Implement C2Y complex increment/decrement support
authorJoseph Myers <josmyers@redhat.com>
Thu, 13 Jun 2024 22:41:02 +0000 (22:41 +0000)
committerJoseph Myers <josmyers@redhat.com>
Thu, 13 Jun 2024 22:41:59 +0000 (22:41 +0000)
Support for complex increment and decrement (previously supported as
an extension) was voted into C2Y today (paper N3259).  Thus, change
the pedwarn to a pedwarn_c23 and add associated tests.

Note: the type of the 1 to be added / subtracted is underspecified (to
be addressed in a subsequent paper), but understood to be intended to
be a real type (so the sign of a zero imaginary part is never changed)
and this is what is implemented; the tests added include verifying
that there is no undesired change to the sign of a zero imaginary
part.

Bootstrapped with no regressions on x86_64-pc-linux-gnu.

gcc/c/
* c-typeck.cc (build_unary_op): Use pedwarn_c23 for complex
increment and decrement.

gcc/testsuite/
* gcc.dg/c23-complex-1.c, gcc.dg/c23-complex-2.c,
gcc.dg/c23-complex-3.c, gcc.dg/c23-complex-4.c,
gcc.dg/c2y-complex-1.c, gcc.dg/c2y-complex-2.c: New tests.

gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/c23-complex-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c23-complex-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c23-complex-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c23-complex-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-complex-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-complex-2.c [new file with mode: 0644]

index a5ca9ea7db6ab85b21edb7e797806b0a04ce63c0..ffcab7df4d3b1f24f0a4eff41982436eaf502b59 100644 (file)
@@ -5079,8 +5079,9 @@ build_unary_op (location_t location, enum tree_code code, tree xarg,
        {
          tree real, imag;
 
-         pedwarn (location, OPT_Wpedantic,
-                  "ISO C does not support %<++%> and %<--%> on complex types");
+         pedwarn_c23 (location, OPT_Wpedantic,
+                      "ISO C does not support %<++%> and %<--%> on complex "
+                      "types before C2Y");
 
          if (!atomic_op)
            {
diff --git a/gcc/testsuite/gcc.dg/c23-complex-1.c b/gcc/testsuite/gcc.dg/c23-complex-1.c
new file mode 100644 (file)
index 0000000..3607336
--- /dev/null
@@ -0,0 +1,14 @@
+/* Test C2Y complex increment and decrement: disallowed for C23.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+_Complex float a;
+
+void
+f (void)
+{
+  a++; /* { dg-error "does not support" } */
+  ++a; /* { dg-error "does not support" } */
+  a--; /* { dg-error "does not support" } */
+  --a; /* { dg-error "does not support" } */
+}
diff --git a/gcc/testsuite/gcc.dg/c23-complex-2.c b/gcc/testsuite/gcc.dg/c23-complex-2.c
new file mode 100644 (file)
index 0000000..301b668
--- /dev/null
@@ -0,0 +1,15 @@
+/* Test C2Y complex increment and decrement: disallowed for C23 (warning with
+   -pedantic).  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic" } */
+
+_Complex float a;
+
+void
+f (void)
+{
+  a++; /* { dg-warning "does not support" } */
+  ++a; /* { dg-warning "does not support" } */
+  a--; /* { dg-warning "does not support" } */
+  --a; /* { dg-warning "does not support" } */
+}
diff --git a/gcc/testsuite/gcc.dg/c23-complex-3.c b/gcc/testsuite/gcc.dg/c23-complex-3.c
new file mode 100644 (file)
index 0000000..6fef301
--- /dev/null
@@ -0,0 +1,15 @@
+/* Test C2Y complex increment and decrement: allowed for C23 with
+   -Wno-c23-c2y-compat.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors -Wno-c23-c2y-compat" } */
+
+_Complex float a;
+
+void
+f (void)
+{
+  a++;
+  ++a;
+  a--;
+  --a;
+}
diff --git a/gcc/testsuite/gcc.dg/c23-complex-4.c b/gcc/testsuite/gcc.dg/c23-complex-4.c
new file mode 100644 (file)
index 0000000..61d50e9
--- /dev/null
@@ -0,0 +1,15 @@
+/* Test C2Y complex increment and decrement: allowed for C23 by default (not
+   pedantic).  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23" } */
+
+_Complex float a;
+
+void
+f (void)
+{
+  a++;
+  ++a;
+  a--;
+  --a;
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-complex-1.c b/gcc/testsuite/gcc.dg/c2y-complex-1.c
new file mode 100644 (file)
index 0000000..29a8c27
--- /dev/null
@@ -0,0 +1,232 @@
+/* Test C2Y complex increment and decrement.  */
+/* { dg-do run } */
+/* { dg-options "-std=c2y -pedantic-errors" } */
+
+extern void abort (void);
+extern void exit (int);
+
+_Complex float a, ax;
+_Complex double b, bx;
+_Complex long double c, cx;
+
+int
+main ()
+{
+  ax = a++;
+  if (ax != 0
+      || a != 1
+      || __builtin_signbit (__builtin_crealf (ax))
+      || __builtin_signbit (__builtin_cimagf (ax))
+      || __builtin_signbit (__builtin_crealf (a))
+      || __builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = __builtin_complex (0.0f, -0.0f);
+  ax = a++;
+  if (ax != 0
+      || a != 1
+      || __builtin_signbit (__builtin_crealf (ax))
+      || !__builtin_signbit (__builtin_cimagf (ax))
+      || __builtin_signbit (__builtin_crealf (a))
+      || !__builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = 0;
+  ax = ++a;
+  if (ax != 1
+      || a != 1
+      || __builtin_signbit (__builtin_crealf (ax))
+      || __builtin_signbit (__builtin_cimagf (ax))
+      || __builtin_signbit (__builtin_crealf (a))
+      || __builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = __builtin_complex (0.0f, -0.0f);
+  ax = ++a;
+  if (ax != 1
+      || a != 1
+      || __builtin_signbit (__builtin_crealf (ax))
+      || !__builtin_signbit (__builtin_cimagf (ax))
+      || __builtin_signbit (__builtin_crealf (a))
+      || !__builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = 0;
+  ax = a--;
+  if (ax != 0
+      || a != -1
+      || __builtin_signbit (__builtin_crealf (ax))
+      || __builtin_signbit (__builtin_cimagf (ax))
+      || !__builtin_signbit (__builtin_crealf (a))
+      || __builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = __builtin_complex (0.0f, -0.0f);
+  ax = a--;
+  if (ax != 0
+      || a != -1
+      || __builtin_signbit (__builtin_crealf (ax))
+      || !__builtin_signbit (__builtin_cimagf (ax))
+      || !__builtin_signbit (__builtin_crealf (a))
+      || !__builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = 0;
+  ax = --a;
+  if (ax != -1
+      || a != -1
+      || !__builtin_signbit (__builtin_crealf (ax))
+      || __builtin_signbit (__builtin_cimagf (ax))
+      || !__builtin_signbit (__builtin_crealf (a))
+      || __builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+  a = __builtin_complex (0.0f, -0.0f);
+  ax = --a;
+  if (ax != -1
+      || a != -1
+      || !__builtin_signbit (__builtin_crealf (ax))
+      || !__builtin_signbit (__builtin_cimagf (ax))
+      || !__builtin_signbit (__builtin_crealf (a))
+      || !__builtin_signbit (__builtin_cimagf (a)))
+    abort ();
+
+  bx = b++;
+  if (bx != 0
+      || b != 1
+      || __builtin_signbit (__builtin_creal (bx))
+      || __builtin_signbit (__builtin_cimag (bx))
+      || __builtin_signbit (__builtin_creal (b))
+      || __builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = __builtin_complex (0.0, -0.0);
+  bx = b++;
+  if (bx != 0
+      || b != 1
+      || __builtin_signbit (__builtin_creal (bx))
+      || !__builtin_signbit (__builtin_cimag (bx))
+      || __builtin_signbit (__builtin_creal (b))
+      || !__builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = 0;
+  bx = ++b;
+  if (bx != 1
+      || b != 1
+      || __builtin_signbit (__builtin_creal (bx))
+      || __builtin_signbit (__builtin_cimag (bx))
+      || __builtin_signbit (__builtin_creal (b))
+      || __builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = __builtin_complex (0.0, -0.0);
+  bx = ++b;
+  if (bx != 1
+      || b != 1
+      || __builtin_signbit (__builtin_creal (bx))
+      || !__builtin_signbit (__builtin_cimag (bx))
+      || __builtin_signbit (__builtin_creal (b))
+      || !__builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = 0;
+  bx = b--;
+  if (bx != 0
+      || b != -1
+      || __builtin_signbit (__builtin_creal (bx))
+      || __builtin_signbit (__builtin_cimag (bx))
+      || !__builtin_signbit (__builtin_creal (b))
+      || __builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = __builtin_complex (0.0f, -0.0f);
+  bx = b--;
+  if (bx != 0
+      || b != -1
+      || __builtin_signbit (__builtin_creal (bx))
+      || !__builtin_signbit (__builtin_cimag (bx))
+      || !__builtin_signbit (__builtin_creal (b))
+      || !__builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = 0;
+  bx = --b;
+  if (bx != -1
+      || b != -1
+      || !__builtin_signbit (__builtin_creal (bx))
+      || __builtin_signbit (__builtin_cimag (bx))
+      || !__builtin_signbit (__builtin_creal (b))
+      || __builtin_signbit (__builtin_cimag (b)))
+    abort ();
+  b = __builtin_complex (0.0f, -0.0f);
+  bx = --b;
+  if (bx != -1
+      || b != -1
+      || !__builtin_signbit (__builtin_creal (bx))
+      || !__builtin_signbit (__builtin_cimag (bx))
+      || !__builtin_signbit (__builtin_creal (b))
+      || !__builtin_signbit (__builtin_cimag (b)))
+    abort ();
+
+  cx = c++;
+  if (cx != 0
+      || c != 1
+      || __builtin_signbit (__builtin_creall (cx))
+      || __builtin_signbit (__builtin_cimagl (cx))
+      || __builtin_signbit (__builtin_creall (c))
+      || __builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = __builtin_complex (0.0L, -0.0L);
+  cx = c++;
+  if (cx != 0
+      || c != 1
+      || __builtin_signbit (__builtin_creall (cx))
+      || !__builtin_signbit (__builtin_cimagl (cx))
+      || __builtin_signbit (__builtin_creall (c))
+      || !__builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = 0;
+  cx = ++c;
+  if (cx != 1
+      || c != 1
+      || __builtin_signbit (__builtin_creall (cx))
+      || __builtin_signbit (__builtin_cimagl (cx))
+      || __builtin_signbit (__builtin_creall (c))
+      || __builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = __builtin_complex (0.0L, -0.0L);
+  cx = ++c;
+  if (cx != 1
+      || c != 1
+      || __builtin_signbit (__builtin_creall (cx))
+      || !__builtin_signbit (__builtin_cimagl (cx))
+      || __builtin_signbit (__builtin_creall (c))
+      || !__builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = 0;
+  cx = c--;
+  if (cx != 0
+      || c != -1
+      || __builtin_signbit (__builtin_creall (cx))
+      || __builtin_signbit (__builtin_cimagl (cx))
+      || !__builtin_signbit (__builtin_creall (c))
+      || __builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = __builtin_complex (0.0L, -0.0L);
+  cx = c--;
+  if (cx != 0
+      || c != -1
+      || __builtin_signbit (__builtin_creall (cx))
+      || !__builtin_signbit (__builtin_cimagl (cx))
+      || !__builtin_signbit (__builtin_creall (c))
+      || !__builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = 0;
+  cx = --c;
+  if (cx != -1
+      || c != -1
+      || !__builtin_signbit (__builtin_creall (cx))
+      || __builtin_signbit (__builtin_cimagl (cx))
+      || !__builtin_signbit (__builtin_creall (c))
+      || __builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+  c = __builtin_complex (0.0L, -0.0L);
+  cx = --c;
+  if (cx != -1
+      || c != -1
+      || !__builtin_signbit (__builtin_creall (cx))
+      || !__builtin_signbit (__builtin_cimagl (cx))
+      || !__builtin_signbit (__builtin_creall (c))
+      || !__builtin_signbit (__builtin_cimagl (c)))
+    abort ();
+
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-complex-2.c b/gcc/testsuite/gcc.dg/c2y-complex-2.c
new file mode 100644 (file)
index 0000000..0ca8949
--- /dev/null
@@ -0,0 +1,14 @@
+/* Test C2Y complex increment and decrement: warning with -Wc23-c2y-compat.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2y -pedantic-errors -Wc23-c2y-compat" } */
+
+_Complex float a;
+
+void
+f (void)
+{
+  a++; /* { dg-warning "does not support" } */
+  ++a; /* { dg-warning "does not support" } */
+  a--; /* { dg-warning "does not support" } */
+  --a; /* { dg-warning "does not support" } */
+}