This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch to warn about qualified function return types


In ISO C, type qualifiers on the return type of a function are
generally allowed but have no effect since the value returned by a
function is not an lvalue (and can't be accessed after the next
sequence point, so restrict qualifiers don't come into play).  Since
such qualifiers won't have whatever effect a programmer might have
intended, this patch adds a warning for them at -W (except for the
"volatile void" GNU extension, which gets warned about at -pedantic).

Bootstrapped with no regressions on i686-pc-linux-gnu.  OK to commit?

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

	* c-decl.c (grokdeclarator): Move warning for qualified void
	return types with -pedantic to when the function type is
	constructed.  At -W, warn in general for qualified function return
	types, except for volatile void.
	* invoke.texi: Document this new warning at -W.

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

	* gcc.dg/qual-return-1.c, gcc.dg/qual-return-2.c: New tests.

--- c-decl.c.orig	Tue Oct 24 19:38:02 2000
+++ c-decl.c	Tue Oct 24 22:02:04 2000
@@ -4577,7 +4577,26 @@ grokdeclarator (declarator, declspecs, d
 	  /* Type qualifiers before the return type of the function
 	     qualify the return type, not the function type.  */
 	  if (type_quals)
-	    type = c_build_qualified_type (type, type_quals);
+	    {
+	      /* Type qualifiers on a function return type are normally
+		 permitted by the standard but have no effect, so give a
+		 warning at -W.  Qualifiers on a void return type have
+		 meaning as a GNU extension, and are banned on function
+		 definitions in ISO C.  FIXME: strictly we shouldn't
+		 pedwarn for qualified void return types except on function
+		 definitions, but not doing so could lead to the undesirable
+		 state of a "volatile void" function return type not being
+		 warned about, and a use of the function being compiled
+		 with GNU semantics, with no diagnostics under -pedantic.  */
+	      if (VOID_TYPE_P (type) && pedantic && !in_system_header)
+		pedwarn ("ISO C forbids qualified void function return type");
+	      else if (extra_warnings
+		       && !(VOID_TYPE_P (type)
+			    && type_quals == TYPE_QUAL_VOLATILE))
+		warning ("type qualifiers ignored on function return type");
+
+	      type = c_build_qualified_type (type, type_quals);
+	    }
 	  type_quals = TYPE_UNQUALIFIED;
 
 	  type = build_function_type (type, arg_types);
@@ -4853,12 +4872,6 @@ grokdeclarator (declarator, declspecs, d
 
 	if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
 	  pedwarn ("ISO C forbids qualified function types");
-
-	if (pedantic
-	    && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))
-	    && TYPE_QUALS (TREE_TYPE (TREE_TYPE (decl)))
-	    && ! DECL_IN_SYSTEM_HEADER (decl))
-	  pedwarn ("ISO C forbids qualified void function return type");
 
 	/* GNU C interprets a `volatile void' return type to indicate
 	   that the function does not return.  */
--- invoke.texi.orig	Tue Oct 24 19:38:02 2000
+++ invoke.texi	Tue Oct 24 21:49:22 2000
@@ -1817,6 +1817,13 @@
 a declaration.  According to the C Standard, this usage is obsolescent.
 
 @item
+The return type of a function has a type qualifier such as @code{const}.
+Such a type qualifier has no effect, since the value returned by a
+function is not an lvalue.  (But don't warn about the GNU extension of
+@code{volatile void} return types.  That extension will be warned about
+if @samp{-pedantic} is specified.)
+
+@item
 If @samp{-Wall} or @samp{-Wunused} is also specified, warn about unused
 arguments.
 
--- testsuite/gcc.dg/qual-return-1.c.orig	Fri Sep 11 11:31:59 1998
+++ testsuite/gcc.dg/qual-return-1.c	Tue Oct 24 21:53:49 2000
@@ -0,0 +1,24 @@
+/* Test for warnings for qualified function return types.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -W" } */
+
+/* Qualifying a function return type makes no sense.  */
+
+const int int_fn (void); /* { dg-warning "qualifiers" "int decl" } */
+const int (*int_ptr) (void); /* { dg-warning "qualifiers" "int ptr" } */
+const int int_fn2 (void) { return 0; } /* { dg-warning "qualifiers" "int defn" } */
+
+const void void_fn (void); /* { dg-warning "qualifiers" "void decl" } */
+const void (*void_ptr) (void); /* { dg-warning "qualifiers" "void ptr" } */
+const void void_fn2 (void) { } /* { dg-warning "qualifiers" "void defn" } */
+
+/* "volatile void" is a GNU extension, so only warn at -pedantic.  */
+
+volatile void vvoid_fn (void);
+volatile void (*vvoid_ptr) (void);
+volatile void vvoid_fn2 (void) { }
+
+int *restrict ip_fn (void); /* { dg-warning "qualifiers" "restrict decl" } */
+int *restrict (*ip_ptr) (void); /* { dg-warning "qualifiers" "restrict ptr" } */
+int *restrict ip_fn2 (void) { return (int *)0; }; /* { dg-warning "qualifiers" "restrict defn" } */
--- testsuite/gcc.dg/qual-return-2.c.orig	Fri Sep 11 11:31:59 1998
+++ testsuite/gcc.dg/qual-return-2.c	Tue Oct 24 21:57:29 2000
@@ -0,0 +1,14 @@
+/* Test for warnings for qualified function return types.  -pedantic test.  */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-pedantic" } */
+
+/* Qualifying a function return type makes no sense.  */
+
+/* "volatile void" is a GNU extension, so only warn at -pedantic.
+   Strictly, the first two of these should warn only if the function is
+   somewhere used or defined.  */
+
+volatile void vvoid_fn (void); /* { dg-warning "qualified" "volatile decl" } */
+volatile void (*vvoid_ptr) (void); /* { dg-warning "qualified" "volatile ptr" } */
+volatile void vvoid_fn2 (void) { } /* { dg-warning "qualified" "volatile defn" } */

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]