From: Joseph Myers Date: Fri, 8 Nov 2019 01:21:40 +0000 (+0000) Subject: Handle removal of old-style function definitions in C2x. X-Git-Tag: misc/cutover-git~1441 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=017c6491077bee998eed9ed6520026285c906d37;p=thirdparty%2Fgcc.git Handle removal of old-style function definitions in C2x. C2x removes support for old-style function definitions with identifier lists, changing () in function definitions to be equivalent to (void) (while () in declarations that are not definitions still gives an unprototyped type). This patch updates GCC accordingly. The new semantics for () are implemented for C2x mode (meaning () in function definitions isn't diagnosed by -Wold-style-definition in that mode). -Wold-style-definition is enabled by default, and turned into a pedwarn, for C2x. Bootstrapped with no regressions on x86_64-pc-linux-gnu. gcc: * doc/invoke.texi (-Wold-style-definition): Document () not being considered an old-style definition for C2x. gcc/c: * c-decl.c (grokparms): Convert () in a function definition to (void) for C2x. (store_parm_decls_oldstyle): Pedwarn for C2x. (store_parm_decls): Update comment about () not generating a prototype. gcc/c-family: * c.opt (Wold-style-definition): Initialize to -1. * c-opts.c (c_common_post_options): Set warn_old_style_definition to flag_isoc2x if not set explicitly. gcc/testsuite: * gcc.dg/c11-old-style-definition-1.c, gcc.dg/c11-old-style-definition-2.c, gcc.dg/c2x-old-style-definition-1.c, gcc.dg/c2x-old-style-definition-2.c, gcc.dg/c2x-old-style-definition-3.c, gcc.dg/c2x-old-style-definition-4.c, gcc.dg/c2x-old-style-definition-5.c, gcc.dg/c2x-old-style-definition-6.c: New tests. From-SVN: r277945 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f1f15dadc66f..e723121b8ccb 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2019-11-08 Joseph Myers + + * doc/invoke.texi (-Wold-style-definition): Document () not being + considered an old-style definition for C2x. + 2019-11-07 John David Anglin * config/pa/pa.md (memory_barrier): Revise to use ldcw barriers. diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 39e7d5acb9cd..d3e328fc9a0a 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2019-11-08 Joseph Myers + + * c.opt (Wold-style-definition): Initialize to -1. + * c-opts.c (c_common_post_options): Set warn_old_style_definition + to flag_isoc2x if not set explicitly. + 2019-11-07 Joseph Myers * c-attribs.c (parse_tm_stmt_attr): Handle scoped attributes. diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c index b84f05c64c7a..75b69597be24 100644 --- a/gcc/c-family/c-opts.c +++ b/gcc/c-family/c-opts.c @@ -904,6 +904,10 @@ c_common_post_options (const char **pfilename) if (warn_implicit_int == -1) warn_implicit_int = flag_isoc99; + /* -Wold-style-definition is enabled by default for C2X. */ + if (warn_old_style_definition == -1) + warn_old_style_definition = flag_isoc2x; + /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */ if (warn_shift_overflow == -1) warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99; diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index bb6eeaf1523b..914a2f0ef442 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -960,7 +960,7 @@ C ObjC Var(warn_old_style_declaration) Warning EnabledBy(Wextra) Warn for obsolescent usage in a declaration. Wold-style-definition -C ObjC Var(warn_old_style_definition) Warning +C ObjC Var(warn_old_style_definition) Init(-1) Warning Warn if an old-style parameter definition is used. Wopenmp-simd diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 29be902ebc63..f1d73e4e73cd 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,11 @@ +2019-11-08 Joseph Myers + + * c-decl.c (grokparms): Convert () in a function definition to + (void) for C2x. + (store_parm_decls_oldstyle): Pedwarn for C2x. + (store_parm_decls): Update comment about () not generating a + prototype. + 2019-11-07 Joseph Myers * c-parser.c (c_parser_attribute_arguments): New function. diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index ae0ee3a9c960..2841b4f5a777 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -7416,6 +7416,13 @@ grokparms (struct c_arg_info *arg_info, bool funcdef_flag) tree parm, type, typelt; unsigned int parmno; + /* In C2X, convert () in a function definition to (void). */ + if (flag_isoc2x + && funcdef_flag + && !arg_types + && !arg_info->parms) + arg_types = arg_info->types = void_list_node; + /* If there is a parameter of incomplete type in a definition, this is an error. In a declaration this is valid, and a struct or union type may be completed later, before any calls @@ -9261,8 +9268,15 @@ store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info) hash_set seen_args; if (!in_system_header_at (input_location)) - warning_at (DECL_SOURCE_LOCATION (fndecl), - OPT_Wold_style_definition, "old-style function definition"); + { + if (flag_isoc2x) + pedwarn (DECL_SOURCE_LOCATION (fndecl), + OPT_Wold_style_definition, "old-style function definition"); + else + warning_at (DECL_SOURCE_LOCATION (fndecl), + OPT_Wold_style_definition, + "old-style function definition"); + } /* Match each formal parameter name with its declaration. Save each decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ @@ -9578,11 +9592,10 @@ store_parm_decls (void) struct c_arg_info *arg_info = current_function_arg_info; current_function_arg_info = 0; - /* True if this definition is written with a prototype. Note: - despite C99 6.7.5.3p14, we can *not* treat an empty argument - list in a function definition as equivalent to (void) -- an - empty argument list specifies the function has no parameters, - but only (void) sets up a prototype for future calls. */ + /* True if this definition is written with a prototype. In C2X, an + empty argument list was converted to (void) in grokparms; in + older C standard versions, it does not give the function a type + with a prototype for future calls. */ proto = arg_info->types != 0; if (proto) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 15fe2284d421..00eb7e778081 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -7334,7 +7334,10 @@ is also enabled by @option{-Wextra}. @opindex Wold-style-definition @opindex Wno-old-style-definition Warn if an old-style function definition is used. A warning is given -even if there is a previous prototype. +even if there is a previous prototype. A definition using @samp{()} +is not considered an old-style definition in C2X mode, because it is +equivalent to @samp{(void)} in that case, but is considered an +old-style definition for older standards. @item -Wmissing-parameter-type @r{(C and Objective-C only)} @opindex Wmissing-parameter-type diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e1a8da60d43f..459fd8907ad9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,14 @@ +2019-11-08 Joseph Myers + + * gcc.dg/c11-old-style-definition-1.c, + gcc.dg/c11-old-style-definition-2.c, + gcc.dg/c2x-old-style-definition-1.c, + gcc.dg/c2x-old-style-definition-2.c, + gcc.dg/c2x-old-style-definition-3.c, + gcc.dg/c2x-old-style-definition-4.c, + gcc.dg/c2x-old-style-definition-5.c, + gcc.dg/c2x-old-style-definition-6.c: New tests. + 2019-11-07 Peter Bergner PR other/92090 diff --git a/gcc/testsuite/gcc.dg/c11-old-style-definition-1.c b/gcc/testsuite/gcc.dg/c11-old-style-definition-1.c new file mode 100644 index 000000000000..74164b76904c --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-old-style-definition-1.c @@ -0,0 +1,9 @@ +/* Test old-style function definitions not in C2x: allowed in C11. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic-errors" } */ + +void +f (x) + int x; +{ +} diff --git a/gcc/testsuite/gcc.dg/c11-old-style-definition-2.c b/gcc/testsuite/gcc.dg/c11-old-style-definition-2.c new file mode 100644 index 000000000000..a22f555416d2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c11-old-style-definition-2.c @@ -0,0 +1,15 @@ +/* Test old-style function definitions not in C2x: () does not give + type with a prototype for older standards. */ +/* { dg-do compile } */ +/* { dg-options "-std=c11" } */ + +void +f () +{ +} + +void +g (void) +{ + f (1); +} diff --git a/gcc/testsuite/gcc.dg/c2x-old-style-definition-1.c b/gcc/testsuite/gcc.dg/c2x-old-style-definition-1.c new file mode 100644 index 000000000000..c775366d7013 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-old-style-definition-1.c @@ -0,0 +1,9 @@ +/* Test old-style function definitions not in C2x: warnings. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x" } */ + +void +f (x) /* { dg-warning "old-style function definition" } */ + int x; +{ +} diff --git a/gcc/testsuite/gcc.dg/c2x-old-style-definition-2.c b/gcc/testsuite/gcc.dg/c2x-old-style-definition-2.c new file mode 100644 index 000000000000..7bd5a60f6ddd --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-old-style-definition-2.c @@ -0,0 +1,9 @@ +/* Test old-style function definitions not in C2x: errors. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +void +f (x) /* { dg-error "old-style function definition" } */ + int x; +{ +} diff --git a/gcc/testsuite/gcc.dg/c2x-old-style-definition-3.c b/gcc/testsuite/gcc.dg/c2x-old-style-definition-3.c new file mode 100644 index 000000000000..a2500769f87f --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-old-style-definition-3.c @@ -0,0 +1,10 @@ +/* Test old-style function definitions not in C2x: warnings disabled + by -Wno-old-style-definition. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -Wno-old-style-definition" } */ + +void +f (x) + int x; +{ +} diff --git a/gcc/testsuite/gcc.dg/c2x-old-style-definition-4.c b/gcc/testsuite/gcc.dg/c2x-old-style-definition-4.c new file mode 100644 index 000000000000..b1862b101b89 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-old-style-definition-4.c @@ -0,0 +1,15 @@ +/* Test old-style function definitions not in C2x: () gives type with + a prototype. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x" } */ + +void +f () +{ +} + +void +g (void) +{ + f (1); /* { dg-error "too many arguments to function" } */ +} diff --git a/gcc/testsuite/gcc.dg/c2x-old-style-definition-5.c b/gcc/testsuite/gcc.dg/c2x-old-style-definition-5.c new file mode 100644 index 000000000000..0c37a067c9b6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-old-style-definition-5.c @@ -0,0 +1,9 @@ +/* Test old-style function definitions not in C2x: () does not warn + with -Wold-style-definition. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -Wold-style-definition" } */ + +void +f () +{ +} diff --git a/gcc/testsuite/gcc.dg/c2x-old-style-definition-6.c b/gcc/testsuite/gcc.dg/c2x-old-style-definition-6.c new file mode 100644 index 000000000000..fc0e778446d7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-old-style-definition-6.c @@ -0,0 +1,16 @@ +/* Test old-style function definitions not in C2x: () does not give + type with a prototype except for function definitions. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x" } */ + +void f1 (); + +/* Prototyped function returning a pointer to unprototyped function. */ +void (*f2 (void))() { return f1; } + +void +g (void) +{ + f1 (1); + f2 () (1); +}