@@ -11032,7 +11032,9 @@ check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
only applies to those that are. (A question on this in comp.std.c
in November 2000 received no answer.) We implement the strictest
interpretation, to avoid creating an extension which later causes
- problems. */
+ problems.
+
+ This constraint was removed in C2X. */
for (b = current_scope->bindings; b; b = b->prev)
{
@@ -11048,33 +11050,35 @@ check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
{
location_t decl_loc = DECL_SOURCE_LOCATION (decl);
if (TREE_STATIC (decl))
- error_at (decl_loc,
- "declaration of static variable %qD in %<for%> loop "
- "initial declaration", decl);
+ pedwarn_c11 (decl_loc, OPT_Wpedantic,
+ "declaration of static variable %qD in %<for%> "
+ "loop initial declaration", decl);
else if (DECL_EXTERNAL (decl))
- error_at (decl_loc,
- "declaration of %<extern%> variable %qD in %<for%> loop "
- "initial declaration", decl);
+ pedwarn_c11 (decl_loc, OPT_Wpedantic,
+ "declaration of %<extern%> variable %qD in %<for%> "
+ "loop initial declaration", decl);
}
break;
case RECORD_TYPE:
- error_at (loc,
- "%<struct %E%> declared in %<for%> loop initial "
- "declaration", id);
+ pedwarn_c11 (loc, OPT_Wpedantic,
+ "%<struct %E%> declared in %<for%> loop initial "
+ "declaration", id);
break;
case UNION_TYPE:
- error_at (loc,
- "%<union %E%> declared in %<for%> loop initial declaration",
- id);
+ pedwarn_c11 (loc, OPT_Wpedantic,
+ "%<union %E%> declared in %<for%> loop initial "
+ "declaration",
+ id);
break;
case ENUMERAL_TYPE:
- error_at (loc, "%<enum %E%> declared in %<for%> loop "
- "initial declaration", id);
+ pedwarn_c11 (loc, OPT_Wpedantic,
+ "%<enum %E%> declared in %<for%> loop "
+ "initial declaration", id);
break;
default:
- error_at (loc, "declaration of non-variable "
- "%qD in %<for%> loop initial declaration", decl);
+ pedwarn_c11 (loc, OPT_Wpedantic, "declaration of non-variable "
+ "%qD in %<for%> loop initial declaration", decl);
}
n_decls++;
new file mode 100644
@@ -0,0 +1,27 @@
+/* Test for C99 declarations in for loops. Test constraints are diagnosed for
+ C11. Based on c99-fordecl-2.c. */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic-errors" } */
+
+void
+foo (void)
+{
+ int j = 0;
+ for (int i = 1, bar (void); i <= 10; i++) /* { dg-error "bar" } */
+ j += i;
+
+ for (static int i = 1; i <= 10; i++) /* /* { dg-error "static" } */
+ j += i;
+
+ for (extern int i; j <= 500; j++) /* { dg-error "extern" } */
+ j += 5;
+
+ for (enum { FOO } i = FOO; i < 10; i++) /* { dg-error "FOO" } */
+ j += i;
+
+ for (enum BAR { FOO } i = FOO; i < 10; i++) /* { dg-error "FOO" } */
+ /* { dg-error "BAR" "enum tag in for loop" { target *-*-* } .-1 } */
+ j += i;
+ for (typedef int T;;) /* { dg-error "non-variable" } */
+ ;
+}
new file mode 100644
@@ -0,0 +1,14 @@
+/* Test for C99 declarations in for loops. Test constraints are diagnosed for
+ C11: struct and union tags can't be declared there (affirmed in response to
+ DR#277). Based on c99-fordecl-3.c. */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic-errors" } */
+
+void
+foo (void)
+{
+ for (struct s { int p; } *p = 0; ;) /* { dg-error "'struct s' declared in 'for' loop initial declaration" } */
+ ;
+ for (union u { int p; } *p = 0; ;) /* { dg-error "'union u' declared in 'for' loop initial declaration" } */
+ ;
+}
new file mode 100644
@@ -0,0 +1,6 @@
+/* Test restrictions on the kind of declarations permitted in for loops removed
+ in C2X, and thus with -std=c11 -pedantic -Wno-c11-c2x-compat. */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic -Wno-c11-c2x-compat" } */
+
+#include "c99-fordecl-2.c"
new file mode 100644
@@ -0,0 +1,6 @@
+/* Test restrictions on the kind of declarations permitted in for loops removed
+ in C2X, and thus with -std=c11 -pedantic -Wno-c11-c2x-compat. */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic -Wno-c11-c2x-compat" } */
+
+#include "c99-fordecl-3.c"
new file mode 100644
@@ -0,0 +1,6 @@
+/* Test restrictions on the kind of declarations permitted in for loops removed
+ in C2X. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+#include "c99-fordecl-2.c"
new file mode 100644
@@ -0,0 +1,6 @@
+/* Test restrictions on the kind of declarations permitted in for loops removed
+ in C2X. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+#include "c99-fordecl-3.c"
new file mode 100644
@@ -0,0 +1,27 @@
+/* Test for C99 declarations in for loops. Test constraints are diagnosed with
+ -Wc11-c2x-compat for C2X. Based on c99-fordecl-2.c. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors -Wc11-c2x-compat" } */
+
+void
+foo (void)
+{
+ int j = 0;
+ for (int i = 1, bar (void); i <= 10; i++) /* { dg-warning "bar" } */
+ j += i;
+
+ for (static int i = 1; i <= 10; i++) /* /* { dg-warning "static" } */
+ j += i;
+
+ for (extern int i; j <= 500; j++) /* { dg-warning "extern" } */
+ j += 5;
+
+ for (enum { FOO } i = FOO; i < 10; i++) /* { dg-warning "FOO" } */
+ j += i;
+
+ for (enum BAR { FOO } i = FOO; i < 10; i++) /* { dg-warning "FOO" } */
+ /* { dg-warning "BAR" "enum tag in for loop" { target *-*-* } .-1 } */
+ j += i;
+ for (typedef int T;;) /* { dg-warning "non-variable" } */
+ ;
+}
new file mode 100644
@@ -0,0 +1,14 @@
+/* Test for C99 declarations in for loops. Test constraints are diagnosed with
+ -Wc11-c2x-compat for C2X: struct and union tags can't be declared there
+ (affirmed in response to DR#277). Based on c99-fordecl-3.c. */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors -Wc11-c2x-compat" } */
+
+void
+foo (void)
+{
+ for (struct s { int p; } *p = 0; ;) /* { dg-warning "'struct s' declared in 'for' loop initial declaration" } */
+ ;
+ for (union u { int p; } *p = 0; ;) /* { dg-warning "'union u' declared in 'for' loop initial declaration" } */
+ ;
+}
@@ -29,4 +29,6 @@ foo (void)
/* { dg-error "FOO" "enum value in for loop" { target *-*-* } .-1 } */
/* { dg-error "BAR" "enum tag in for loop" { target *-*-* } .-2 } */
j += i;
+ for (typedef int T;;) /* { dg-error "non-variable" } */
+ ;
}
@@ -7,7 +7,7 @@ int T;
void
fn1 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
T *x; /* { dg-error "undeclared" } */
@@ -16,7 +16,7 @@ fn1 (void)
void
fn2 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
T = 1; /* { dg-error "expected expression" } */
T *x; /* { dg-error "undeclared" } */
@@ -25,7 +25,7 @@ fn2 (void)
void
fn3 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
{
}
@@ -35,7 +35,7 @@ fn3 (void)
void
fn4 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
L:
;
@@ -45,7 +45,7 @@ L:
void
fn5 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
else
@@ -5,7 +5,7 @@
void
fn1 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
T x; /* { dg-error "unknown type name" } */
@@ -14,7 +14,7 @@ fn1 (void)
void
fn2 (int i)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
i = 5;
T x; /* { dg-error "unknown type name" } */
@@ -23,7 +23,7 @@ fn2 (int i)
void
fn3 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
{
}
@@ -33,7 +33,7 @@ fn3 (void)
void
fn4 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
T, T; /* { dg-error "undeclared" } */
@@ -42,7 +42,7 @@ fn4 (void)
void
fn5 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
T = 10; /* { dg-error "undeclared" } */
@@ -51,7 +51,7 @@ fn5 (void)
void
fn6 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
T[0]; /* { dg-error "undeclared" } */
@@ -60,7 +60,7 @@ fn6 (void)
void
fn7 (void)
{
- for (typedef int T;;) /* { dg-error "declaration of non-variable" } */
+ for (typedef int T;;)
if (1)
;
T (); /* { dg-warning "implicit declaration" } */
@@ -39,7 +39,7 @@ int main (void)
id array = nil;
id object = nil;
- for (typedef int my_typedef in array) /* { dg-error "declaration of non-variable" } */
+ for (typedef int my_typedef in array)
; /* { dg-error "iterating variable in fast enumeration is not an object" "" { target *-*-* } .-1 } */
for (function () in nil) /* { dg-error "invalid iterating variable in fast enumeration" } */