Patch for void foo(const void) and similar

Joseph S. Myers jsm28@cam.ac.uk
Sun Jul 30 06:49:00 GMT 2000


When a parameter list of 'void' alone is used to signify no parameters
to a function in C, it makes no sense for the 'void' to be qualified
or to have a storage class specifier, and such cases aren't covered by
the definition of this handling of 'void' in the standard.  The
informative list of undefined behavior in C99 includes this (undefined
through the lack of any definition of the significance of qualified
void parameters or of storage class specifiers applied to such).  This
patch converts such cases into errors by not treating them as the
special case of 'void' alone in a parameter list.

(Other type qualifiers (restrict) already get errors when applied to
void; other storage class specifiers than register (auto, extern,
static) already get errors when used in a parameter list.)

Bootstrapped with no regressions on i686-pc-linux-gnu.

gcc/ChangeLog:
2000-07-30  Joseph S. Myers  <jsm28@cam.ac.uk>

	* c-decl.c (get_parm_info): Don't treat 'const void', 'volatile
	void' or 'register void' as being the special case of 'void' alone
	in a parameter list.

gcc/testsuite/ChangeLog:
2000-07-30  Joseph S. Myers  <jsm28@cam.ac.uk>

	* gcc.dg/noncompile/voidparam-1.c: New test.

--- c-decl.c.orig	Thu Jul 27 19:53:51 2000
+++ c-decl.c	Sun Jul 30 00:25:36 2000
@@ -4985,10 +4985,18 @@
   tree new_parms = 0;
   tree order = current_binding_level->parm_order;
 
-  /* Just `void' (and no ellipsis) is special.  There are really no parms.  */
+  /* Just `void' (and no ellipsis) is special.  There are really no parms.
+     But if the `void' is qualified (by `const' or `volatile') or has a
+     storage class specifier (`register'), then the behavior is undefined;
+     by not counting it as the special case of `void' we will cause an
+     error later.  Typedefs for `void' are OK (see DR#157).
+  */
   if (void_at_end && parms != 0
       && TREE_CHAIN (parms) == 0
       && VOID_TYPE_P (TREE_TYPE (parms))
+      && ! TREE_THIS_VOLATILE (parms)
+      && ! TREE_READONLY (parms)
+      && ! DECL_REGISTER (parms)
       && DECL_NAME (parms) == 0)
     {
       parms = NULL_TREE;
--- testsuite/gcc.dg/noncompile/voidparam-1.c.orig	Fri Sep 11 11:31:59 1998
+++ testsuite/gcc.dg/noncompile/voidparam-1.c	Sun Jul 30 02:00:08 2000
@@ -0,0 +1,17 @@
+/* Test for bad uses of 'void' in parameter lists.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+
+typedef const void cv;
+typedef volatile void vv;
+
+void foo0 (const void); /* { dg-error "parameter" "const void decl" } */
+void foo0a (cv); /* { dg-error "parameter" "const void decl" } */
+void foo1 (volatile void); /* { dg-error "parameter" "volatile void decl" } */
+void foo1a (vv); /* { dg-error "parameter" "volatile void decl" } */
+void foo2 (register void); /* { dg-error "parameter" "register void decl" } */
+
+void bar0 (const void) { } /* { dg-error "parameter" "const void defn" } */
+void bar0a (cv) { } /* { dg-error "parameter" "const void defn" } */
+void bar1 (volatile void) { } /* { dg-error "parameter" "volatile void defn" } */
+void bar1a (vv) { } /* { dg-error "parameter" "volatile void defn" } */
+void bar2 (register void) { } /* { dg-error "parameter" "register void defn" } */

-- 
Joseph S. Myers
jsm28@cam.ac.uk



More information about the Gcc-patches mailing list