Handle removal of old-style function definitions in C2x.
authorJoseph Myers <joseph@codesourcery.com>
Fri, 8 Nov 2019 01:21:40 +0000 (01:21 +0000)
committerJoseph Myers <jsm28@gcc.gnu.org>
Fri, 8 Nov 2019 01:21:40 +0000 (01:21 +0000)
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

16 files changed:
gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-opts.c
gcc/c-family/c.opt
gcc/c/ChangeLog
gcc/c/c-decl.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/c11-old-style-definition-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c11-old-style-definition-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-old-style-definition-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-old-style-definition-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-old-style-definition-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-old-style-definition-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-old-style-definition-5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2x-old-style-definition-6.c [new file with mode: 0644]

index f1f15dadc66ff7e3b7fa48aba9070a13d1fc7803..e723121b8ccb7bb7bb65168a73430fc6dd422cc3 100644 (file)
@@ -1,3 +1,8 @@
+2019-11-08  Joseph Myers  <joseph@codesourcery.com>
+
+       * doc/invoke.texi (-Wold-style-definition): Document () not being
+       considered an old-style definition for C2x.
+
 2019-11-07  John David Anglin  <danglin@gcc.gnu.org>
 
        * config/pa/pa.md (memory_barrier): Revise to use ldcw barriers.
index 39e7d5acb9cd592d01ddc60b365a07ae3983cd31..d3e328fc9a0af1c952395abbf14aa38a14c488e4 100644 (file)
@@ -1,3 +1,9 @@
+2019-11-08  Joseph Myers  <joseph@codesourcery.com>
+
+       * 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  <joseph@codesourcery.com>
 
        * c-attribs.c (parse_tm_stmt_attr): Handle scoped attributes.
index b84f05c64c7aa4b1a4306dea10ce8ad42191b6ef..75b69597be24f0e9f9a5f7ca61bc13368aa3dec1 100644 (file)
@@ -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;
index bb6eeaf1523bcd2385a0efc627c7d3173c565afb..914a2f0ef4425c340331138081f330bc399d1243 100644 (file)
@@ -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
index 29be902ebc63c333946dcb8d877534c1ac6d3612..f1d73e4e73cd1e4613e4154c105803b7a03ae5d8 100644 (file)
@@ -1,3 +1,11 @@
+2019-11-08  Joseph Myers  <joseph@codesourcery.com>
+
+       * 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  <joseph@codesourcery.com>
 
        * c-parser.c (c_parser_attribute_arguments): New function.
index ae0ee3a9c9606a8009c2b60767c5227cd6671043..2841b4f5a777c0b85b3f716c1f0a0f5ffb60dafe 100644 (file)
@@ -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<tree> 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)
index 15fe2284d4210d78947ba3c951cec714268a3a1a..00eb7e7780815a8f5b43143a64f4147c678ba0f4 100644 (file)
@@ -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
index e1a8da60d43f13afcb5142fb6399eff0a0172379..459fd8907ad995dde36c2c8ad8444f76c2fa7d3d 100644 (file)
@@ -1,3 +1,14 @@
+2019-11-08  Joseph Myers  <joseph@codesourcery.com>
+
+       * 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 <bergner@linux.ibm.com>
 
        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 (file)
index 0000000..74164b7
--- /dev/null
@@ -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 (file)
index 0000000..a22f555
--- /dev/null
@@ -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 (file)
index 0000000..c775366
--- /dev/null
@@ -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 (file)
index 0000000..7bd5a60
--- /dev/null
@@ -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 (file)
index 0000000..a250076
--- /dev/null
@@ -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 (file)
index 0000000..b1862b1
--- /dev/null
@@ -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 (file)
index 0000000..0c37a06
--- /dev/null
@@ -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 (file)
index 0000000..fc0e778
--- /dev/null
@@ -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);
+}