N3517 (array subscripting without decay) has been added to C2y (via a
remote vote in May, not at a meeting).  Implement this in GCC.

The conceptual change, that the array subscripting operator [] no
longer involves an array operand decaying to a pointer, is something
GCC has done for a very long time.  The main effect in terms of what
is made possible in the language, subscripting a register array
(undefined behavior in C23 and before), was available as a GNU
extension, but only with constant indices.  There is also a new
constraint that array indices must not be negative when they are
integer constant expressions and the array operand has array type
(negative indices are fine with pointers) - an access out of bounds of
an array (even when contained within a larger object) has undefined
behavior at runtime when not a constraint violation.

Thus, the previous GCC extension is adapted to allow the cases of
register arrays not previously allowed, clearing DECL_REGISTER on them
as needed (similar to what is done with register declarations of
structures with volatile members) and restricting the pedwarn to
pedwarn_c23.  That pedwarn_c23 is also extended to cover the C23 case
of register compound literals (although not strictly needed since it
was undefined behavior rather than a constraint violation in C23).
The new error is added (only for flag_isoc2y) for negative array
indices with an operand of array type.

N3517 has some specific wording about the type of the result of
non-lvalue array element access.  It's unclear what's actually desired
there in the case where the array element is itself of array type; see
C23 issue 1001 regarding types of qualified members of rvalue
structures and unions more generally.  Rather than implementing the
specific wording about this in N3517, that is deferred until there's
an accepted resolution to issue 1001 and can be dealt with as part of
implementing such a resolution.

Nothing specific is done about the obsolescence in that paper of
writing index[array] or index[pointer] as opposed to array[index] or
pointer[index], although that seems like a reasonable enough thing to
warn about.

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

gcc/c/
        * c-typeck.cc (c_mark_addressable): New parameter
        override_register.
        (build_array_ref): Update calls to c_mark_addressable.  Give error
        in C2Y mode for negative array indices when array expression is an
        array not a pointer.  Use pedwarn_c23 for subscripting register
        array; diagnose that also for register compound literal.
        * c-tree.h (c_mark_addressable): Update prototype.

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

diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h
index fba853c2d446..a06565d1e8bc 100644
--- a/gcc/c/c-tree.h
+++ b/gcc/c/c-tree.h
@@ -813,7 +813,7 @@ extern bool comptypes_same_p (tree, tree);
 extern bool comptypes_equiv_p (tree, tree);
 extern int comptypes_check_different_types (tree, tree, bool *);
 extern int comptypes_check_enum_int (tree, tree, bool *);
-extern bool c_mark_addressable (tree, bool = false);
+extern bool c_mark_addressable (tree, bool = false, bool = false);
 extern void c_incomplete_type_error (location_t, const_tree, const_tree);
 extern tree c_type_promotes_to (tree);
 extern struct c_expr default_function_array_conversion (location_t,
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 67e4c9cfa578..1f04a4d7f4f2 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -3527,7 +3527,7 @@ build_array_ref (location_t loc, tree array, tree index)
          || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
              && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != 
INTEGER_CST))
        {
-         if (!c_mark_addressable (array, true))
+         if (!c_mark_addressable (array, true, true))
            return error_mark_node;
        }
       /* An array that is indexed by a constant value which is not within
@@ -3538,19 +3538,28 @@ build_array_ref (location_t loc, tree array, tree index)
          && TYPE_DOMAIN (TREE_TYPE (array))
          && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
        {
-         if (!c_mark_addressable (array))
+         if (!c_mark_addressable (array, false, true))
            return error_mark_node;
+         /* ISO C2Y disallows a negative integer constant expression index
+            when array subscripting has an operand of array type.  */
+         if (flag_isoc2y
+             && !TREE_OVERFLOW (index)
+             && tree_int_cst_sgn (index) < 0)
+           error_at (loc, "array subscript is negative");
        }
 
-      if ((pedantic || warn_c90_c99_compat)
+      if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
          && ! was_vector)
        {
          tree foo = array;
          while (TREE_CODE (foo) == COMPONENT_REF)
            foo = TREE_OPERAND (foo, 0);
-         if (VAR_P (foo) && C_DECL_REGISTER (foo))
-           pedwarn (loc, OPT_Wpedantic,
-                    "ISO C forbids subscripting %<register%> array");
+         if ((VAR_P (foo) && C_DECL_REGISTER (foo))
+             || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
+                 && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
+           pedwarn_c23 (loc, OPT_Wpedantic,
+                        "ISO C forbids subscripting %<register%> array "
+                        "before C2Y");
          else if (!lvalue_p (foo))
            pedwarn_c90 (loc, OPT_Wpedantic,
                         "ISO C90 forbids subscripting non-lvalue "
@@ -6257,10 +6266,11 @@ lvalue_or_else (location_t loc, const_tree ref, enum 
lvalue_use use)
    is for ARRAY_REF construction - in that case we don't want
    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
    it is fine to use ARRAY_REFs for vector subscripts on vector
-   register variables.  */
+   register variables.  If OVERRIDE_REGISTER, clear DECL_REGISTER rather
+   than producing an error for taking the address of a register.  */
 
 bool
-c_mark_addressable (tree exp, bool array_ref_p)
+c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
 {
   tree x = exp;
 
@@ -6293,8 +6303,13 @@ c_mark_addressable (tree exp, bool array_ref_p)
       case COMPOUND_LITERAL_EXPR:
        if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
          {
-           error ("address of register compound literal requested");
-           return false;
+           if (override_register)
+             DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
+           else
+             {
+               error ("address of register compound literal requested");
+               return false;
+             }
          }
        TREE_ADDRESSABLE (x) = 1;
        TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
@@ -6323,6 +6338,13 @@ c_mark_addressable (tree exp, bool array_ref_p)
          {
            if (TREE_PUBLIC (x) || is_global_var (x))
              error ("address of global register variable %qD requested", x);
+           else if (override_register && !DECL_HARD_REGISTER (x))
+             {
+               DECL_REGISTER (x) = 0;
+               TREE_ADDRESSABLE (x) = 1;
+               mark_decl_used (x, true);
+               return true;
+             }
            else
              error ("address of register variable %qD requested", x);
            return false;
diff --git a/gcc/testsuite/gcc.dg/c23-array-negative-1.c 
b/gcc/testsuite/gcc.dg/c23-array-negative-1.c
new file mode 100644
index 000000000000..c7d083300181
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-array-negative-1.c
@@ -0,0 +1,46 @@
+/* Test C2y constraint against negative array indices does not apply in
+   C23.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+int a[1], b[10];
+struct s { int a[2]; } x;
+void *p;
+
+void
+f ()
+{
+  (void) a[0];
+  (void) a[1];
+  (void) a[12345];
+  (void) a[-1];
+  (void) a[-__LONG_LONG_MAX__];
+  (void) b[0];
+  (void) b[10];
+  (void) b[12345];
+  (void) b[-1];
+  (void) b[-__LONG_LONG_MAX__];
+  (void) x.a[0];
+  (void) x.a[1];
+  (void) x.a[12345];
+  (void) x.a[-1];
+  (void) x.a[-__LONG_LONG_MAX__];
+  int c[1];
+  (void) c[0];
+  (void) c[1];
+  (void) c[12345];
+  (void) c[-1];
+  (void) c[-__LONG_LONG_MAX__];
+  (void) (*(int (*)[1]) p)[0];
+  (void) (*(int (*)[1]) p)[1];
+  (void) (*(int (*)[1]) p)[12345];
+  (void) (*(int (*)[1]) p)[-1];
+  (void) (*(int (*)[1]) p)[-__LONG_LONG_MAX__];
+  /* This index is not an integer constant expression, so the constraint
+     against negative indices does not apply even in C2y.  */
+  (void) a[__LONG_LONG_MAX__ + 2];
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  /* Likewise, this is only an arithmetic constant expression, not an integer
+     constant expression.  */
+  (void) a[(int)-1.0];
+}
diff --git a/gcc/testsuite/gcc.dg/c23-register-array-1.c 
b/gcc/testsuite/gcc.dg/c23-register-array-1.c
new file mode 100644
index 000000000000..4aa482a4ee9d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-register-array-1.c
@@ -0,0 +1,48 @@
+/* Test C2y register array element access: not in C23.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+extern void abort ();
+extern void exit (int);
+
+struct s { int a[10]; };
+
+int
+main ()
+{
+  int n = 10;
+  register int a[1], b[20], c[n];
+  register struct s v;
+  a[0] = 0; /* { dg-error "ISO C forbids subscripting 'register' array before 
C2Y" } */
+  for (int i = 0; i < n; i++)
+    c[i] = i; /* { dg-error "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 20; i++)
+    b[i] = i; /* { dg-error "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 10; i++)
+    v.a[i] = i; /* { dg-error "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    if (c[i] != i) /* { dg-error "ISO C forbids subscripting 'register' array 
before C2Y" } */
+      abort ();
+  for (int i = 0; i < 20; i++)
+    if (b[i] != i) /* { dg-error "ISO C forbids subscripting 'register' array 
before C2Y" } */
+      abort ();
+  for (int i = 0; i < 10; i++)
+    if (v.a[i] != i) /* { dg-error "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  if (a[0] != 0) /* { dg-error "ISO C forbids subscripting 'register' array 
before C2Y" } */
+    abort ();
+  if ((register int[2]) { 2, 3 }[n / 10] != 3) /* { dg-error "ISO C forbids 
subscripting 'register' array before C2Y" } */
+    abort ();
+  if ((register struct s) { 1, 2 }.a[n / 10] != 2) /* { dg-error "ISO C 
forbids subscripting 'register' array before C2Y" } */
+    abort ();
+  if (false)
+    (void) a[12345]; /* { dg-error "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    (void) b[23456]; /* { dg-error "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    /* This index is not an integer constant expression, so the constraint
+       against negative indices does not apply.  */
+    (void) a[__INT_MAX__ + 2]; /* { dg-error "ISO C forbids subscripting 
'register' array before C2Y" } */
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c23-register-array-2.c 
b/gcc/testsuite/gcc.dg/c23-register-array-2.c
new file mode 100644
index 000000000000..225b6ca7a5df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-register-array-2.c
@@ -0,0 +1,48 @@
+/* Test C2y register array element access: C23 warning with -pedantic.  */
+/* { dg-do run } */
+/* { dg-options "-std=c23 -pedantic" } */
+
+extern void abort ();
+extern void exit (int);
+
+struct s { int a[10]; };
+
+int
+main ()
+{
+  int n = 10;
+  register int a[1], b[20], c[n];
+  register struct s v;
+  a[0] = 0; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    c[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 20; i++)
+    b[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 10; i++)
+    v.a[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    if (c[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  for (int i = 0; i < 20; i++)
+    if (b[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  for (int i = 0; i < 10; i++)
+    if (v.a[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  if (a[0] != 0) /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+    abort ();
+  if ((register int[2]) { 2, 3 }[n / 10] != 3) /* { dg-warning "ISO C forbids 
subscripting 'register' array before C2Y" } */
+    abort ();
+  if ((register struct s) { 1, 2 }.a[n / 10] != 2) /* { dg-warning "ISO C 
forbids subscripting 'register' array before C2Y" } */
+    abort ();
+  if (false)
+    (void) a[12345]; /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    (void) b[23456]; /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    /* This index is not an integer constant expression, so the constraint
+       against negative indices does not apply.  */
+    (void) a[__INT_MAX__ + 2]; /* { dg-warning "ISO C forbids subscripting 
'register' array before C2Y" } */
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c23-register-array-3.c 
b/gcc/testsuite/gcc.dg/c23-register-array-3.c
new file mode 100644
index 000000000000..2312cfe694df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-register-array-3.c
@@ -0,0 +1,49 @@
+/* Test C2y register array element access: C23 warning with
+   -Wc23-c2y-compat.  */
+/* { dg-do run } */
+/* { dg-options "-std=c23 -Wc23-c2y-compat" } */
+
+extern void abort ();
+extern void exit (int);
+
+struct s { int a[10]; };
+
+int
+main ()
+{
+  int n = 10;
+  register int a[1], b[20], c[n];
+  register struct s v;
+  a[0] = 0; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    c[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 20; i++)
+    b[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 10; i++)
+    v.a[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    if (c[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  for (int i = 0; i < 20; i++)
+    if (b[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  for (int i = 0; i < 10; i++)
+    if (v.a[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  if (a[0] != 0) /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+    abort ();
+  if ((register int[2]) { 2, 3 }[n / 10] != 3) /* { dg-warning "ISO C forbids 
subscripting 'register' array before C2Y" } */
+    abort ();
+  if ((register struct s) { 1, 2 }.a[n / 10] != 2) /* { dg-warning "ISO C 
forbids subscripting 'register' array before C2Y" } */
+    abort ();
+  if (false)
+    (void) a[12345]; /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    (void) b[23456]; /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    /* This index is not an integer constant expression, so the constraint
+       against negative indices does not apply.  */
+    (void) a[__INT_MAX__ + 2]; /* { dg-warning "ISO C forbids subscripting 
'register' array before C2Y" } */
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c23-register-array-4.c 
b/gcc/testsuite/gcc.dg/c23-register-array-4.c
new file mode 100644
index 000000000000..46156bc6cad6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-register-array-4.c
@@ -0,0 +1,49 @@
+/* Test C2y register array element access: no warning in C23 mode with
+   -Wno-c23-c2y-compat.  */
+/* { dg-do run } */
+/* { dg-options "-std=c23 -pedantic-errors -Wno-c23-c2y-compat" } */
+
+extern void abort ();
+extern void exit (int);
+
+struct s { int a[10]; };
+
+int
+main ()
+{
+  int n = 10;
+  register int a[1], b[20], c[n];
+  register struct s v;
+  a[0] = 0;
+  for (int i = 0; i < n; i++)
+    c[i] = i;
+  for (int i = 0; i < 20; i++)
+    b[i] = i;
+  for (int i = 0; i < 10; i++)
+    v.a[i] = i;
+  for (int i = 0; i < n; i++)
+    if (c[i] != i)
+      abort ();
+  for (int i = 0; i < 20; i++)
+    if (b[i] != i)
+      abort ();
+  for (int i = 0; i < 10; i++)
+    if (v.a[i] != i)
+      abort ();
+  if (a[0] != 0)
+    abort ();
+  if ((register int[2]) { 2, 3 }[n / 10] != 3)
+    abort ();
+  if ((register struct s) { 1, 2 }.a[n / 10] != 2)
+    abort ();
+  if (false)
+    (void) a[12345];
+  if (false)
+    (void) b[23456];
+  if (false)
+    /* This index is not an integer constant expression, so the constraint
+       against negative indices does not apply.  */
+    (void) a[__INT_MAX__ + 2];
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-array-negative-1.c 
b/gcc/testsuite/gcc.dg/c2y-array-negative-1.c
new file mode 100644
index 000000000000..d745bf2bcf96
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2y-array-negative-1.c
@@ -0,0 +1,45 @@
+/* Test C2y constraint against negative array indices.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2y -pedantic-errors" } */
+
+int a[1], b[10];
+struct s { int a[2]; } x;
+void *p;
+
+void
+f ()
+{
+  (void) a[0];
+  (void) a[1];
+  (void) a[12345];
+  (void) a[-1]; /* { dg-error "array subscript is negative" } */
+  (void) a[-__LONG_LONG_MAX__]; /* { dg-error "array subscript is negative" } 
*/
+  (void) b[0];
+  (void) b[10];
+  (void) b[12345];
+  (void) b[-1]; /* { dg-error "array subscript is negative" } */
+  (void) b[-__LONG_LONG_MAX__]; /* { dg-error "array subscript is negative" } 
*/
+  (void) x.a[0];
+  (void) x.a[1];
+  (void) x.a[12345];
+  (void) x.a[-1]; /* { dg-error "array subscript is negative" } */
+  (void) x.a[-__LONG_LONG_MAX__]; /* { dg-error "array subscript is negative" 
} */
+  int c[1];
+  (void) c[0];
+  (void) c[1];
+  (void) c[12345];
+  (void) c[-1]; /* { dg-error "array subscript is negative" } */
+  (void) c[-__LONG_LONG_MAX__]; /* { dg-error "array subscript is negative" } 
*/
+  (void) (*(int (*)[1]) p)[0];
+  (void) (*(int (*)[1]) p)[1];
+  (void) (*(int (*)[1]) p)[12345];
+  (void) (*(int (*)[1]) p)[-1]; /* { dg-error "array subscript is negative" } 
*/
+  (void) (*(int (*)[1]) p)[-__LONG_LONG_MAX__]; /* { dg-error "array subscript 
is negative" } */
+  /* This index is not an integer constant expression, so the constraint
+     against negative indices does not apply.  */
+  (void) a[__LONG_LONG_MAX__ + 2];
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  /* Likewise, this is only an arithmetic constant expression, not an integer
+     constant expression.  */
+  (void) a[(int)-1.0];
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-register-array-2.c 
b/gcc/testsuite/gcc.dg/c2y-register-array-2.c
new file mode 100644
index 000000000000..dad0f4c9fb62
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2y-register-array-2.c
@@ -0,0 +1,48 @@
+/* Test C2y register array element access.  Execution tests.  */
+/* { dg-do run } */
+/* { dg-options "-std=c2y -pedantic-errors" } */
+
+extern void abort ();
+extern void exit (int);
+
+struct s { int a[10]; };
+
+int
+main ()
+{
+  int n = 10;
+  register int a[1], b[20], c[n];
+  register struct s v;
+  a[0] = 0;
+  for (int i = 0; i < n; i++)
+    c[i] = i;
+  for (int i = 0; i < 20; i++)
+    b[i] = i;
+  for (int i = 0; i < 10; i++)
+    v.a[i] = i;
+  for (int i = 0; i < n; i++)
+    if (c[i] != i)
+      abort ();
+  for (int i = 0; i < 20; i++)
+    if (b[i] != i)
+      abort ();
+  for (int i = 0; i < 10; i++)
+    if (v.a[i] != i)
+      abort ();
+  if (a[0] != 0)
+    abort ();
+  if ((register int[2]) { 2, 3 }[n / 10] != 3)
+    abort ();
+  if ((register struct s) { 1, 2 }.a[n / 10] != 2)
+    abort ();
+  if (false)
+    (void) a[12345];
+  if (false)
+    (void) b[23456];
+  if (false)
+    /* This index is not an integer constant expression, so the constraint
+       against negative indices does not apply.  */
+    (void) a[__INT_MAX__ + 2];
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-register-array-3.c 
b/gcc/testsuite/gcc.dg/c2y-register-array-3.c
new file mode 100644
index 000000000000..010e738ca41a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2y-register-array-3.c
@@ -0,0 +1,49 @@
+/* Test C2y register array element access.  Test warnings with
+   -Wc23-c2y-compat.  */
+/* { dg-do run } */
+/* { dg-options "-std=c2y -pedantic-errors -Wc23-c2y-compat" } */
+
+extern void abort ();
+extern void exit (int);
+
+struct s { int a[10]; };
+
+int
+main ()
+{
+  int n = 10;
+  register int a[1], b[20], c[n];
+  register struct s v;
+  a[0] = 0; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    c[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 20; i++)
+    b[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < 10; i++)
+    v.a[i] = i; /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+  for (int i = 0; i < n; i++)
+    if (c[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  for (int i = 0; i < 20; i++)
+    if (b[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  for (int i = 0; i < 10; i++)
+    if (v.a[i] != i) /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+      abort ();
+  if (a[0] != 0) /* { dg-warning "ISO C forbids subscripting 'register' array 
before C2Y" } */
+    abort ();
+  if ((register int[2]) { 2, 3 }[n / 10] != 3) /* { dg-warning "ISO C forbids 
subscripting 'register' array before C2Y" } */
+    abort ();
+  if ((register struct s) { 1, 2 }.a[n / 10] != 2) /* { dg-warning "ISO C 
forbids subscripting 'register' array before C2Y" } */
+    abort ();
+  if (false)
+    (void) a[12345]; /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    (void) b[23456]; /* { dg-warning "ISO C forbids subscripting 'register' 
array before C2Y" } */
+  if (false)
+    /* This index is not an integer constant expression, so the constraint
+       against negative indices does not apply.  */
+    (void) a[__INT_MAX__ + 2]; /* { dg-warning "ISO C forbids subscripting 
'register' array before C2Y" } */
+  /* { dg-warning "integer overflow in expression" "overflow" { target *-*-* } 
.-1 } */
+  exit (0);
+}

-- 
Joseph S. Myers
josmy...@redhat.com

Reply via email to