diff options
author | Joseph Myers <joseph@codesourcery.com> | 2023-05-16 23:44:56 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2023-05-16 23:46:02 +0000 |
commit | 036b4eb47e5c6212bdcd5fd9bf4fd08a62b2c65a (patch) | |
tree | 03e67ad881e454a8b0c61577b176b087689efd67 /gcc/c | |
parent | 509eef9314b24eff20a5dbdd92f6ab52e2c0c786 (diff) | |
download | gcc-036b4eb47e5c6212bdcd5fd9bf4fd08a62b2c65a.zip gcc-036b4eb47e5c6212bdcd5fd9bf4fd08a62b2c65a.tar.gz gcc-036b4eb47e5c6212bdcd5fd9bf4fd08a62b2c65a.tar.bz2 |
c: Remove restrictions on declarations in 'for' loops for C2X
C2X removes a restriction that the only declarations in the
declaration part of a 'for' loop are declarations of objects with
storage class auto or register. Implement this change, making the
diagnostics into pedwarn_c11 calls instead of errors (as usual for
features added in a new standard version that were invalid code in a
previous version), so now pedwarn-if-pedantic for older standards and
diagnosed also with -Wc11-c2x-compat.
Bootstrapped with no regressions for x86_64-pc-linux-gnu.
gcc/c/
* c-decl.cc (check_for_loop_decls): Use pedwarn_c11 for
diagnostics.
gcc/testsuite/
* gcc.dg/c11-fordecl-1.c, gcc.dg/c11-fordecl-2.c,
gcc.dg/c11-fordecl-3.c, gcc.dg/c11-fordecl-4.c,
gcc.dg/c2x-fordecl-1.c, gcc.dg/c2x-fordecl-2.c,
gcc.dg/c2x-fordecl-3.c, gcc.dg/c2x-fordecl-4.c: New tests.
* gcc.dg/c99-fordecl-2.c: Test diagnostic for typedef declaration
in for loop here.
* gcc.dg/pr67784-2.c, gcc.dg/pr68320.c, objc.dg/foreach-7.m: Do
not expect errors for typedef declaration in for loop.
Diffstat (limited to 'gcc/c')
-rw-r--r-- | gcc/c/c-decl.cc | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 90d7cd2..f8ede36 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -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++; |