Diagnose calling function with qualified void return type (PR 35210)

Joseph S. Myers joseph@codesourcery.com
Sat Apr 18 20:04:00 GMT 2009


This patch fixes PR 35210, a failure to diagnose a constraint
violation (for both C90 and C99) calling a function with qualified
void return type.  (Defining such a function has a corresponding
constraint, and already gets diagnosed, but declaring such a function
without defining or calling it is valid ISO C.)  In the course of
fixing this bug I noticed and fixed an issue where a call to a
function whose return type was an incomplete structure or union (valid
to declare, but the structure or union needs to be defined before you
can call the function) was not diagnosed in the case where a trap was
generated for a call to a function cast to an incompatible type.

Bootstrapped with no regressions on i686-pc-linux-gnu.  Applied to
mainline.

2009-04-18  Joseph Myers  <joseph@codesourcery.com>

	PR c/35210
	* c-typeck.c (build_function_call): Check for calling a function
	with qualified void return types.  Call require_complete_type when
	generating a trap.

testsuite:
2009-04-18  Joseph Myers  <joseph@codesourcery.com>

	PR c/35210
	* gcc.dg/call-diag-2.c: New test.

Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 146291)
+++ gcc/c-typeck.c	(working copy)
@@ -2498,7 +2498,12 @@ build_function_call (tree function, tree
 	trap = build2 (COMPOUND_EXPR, void_type_node, argarray[i], trap);
 
       if (VOID_TYPE_P (return_type))
-	return trap;
+	{
+	  if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
+	    pedwarn (input_location, 0,
+		     "function with qualified void return type called");
+	  return trap;
+	}
       else
 	{
 	  tree rhs;
@@ -2510,7 +2515,8 @@ build_function_call (tree function, tree
 	  else
 	    rhs = fold_convert (return_type, integer_zero_node);
 
-	  return build2 (COMPOUND_EXPR, return_type, trap, rhs);
+	  return require_complete_type (build2 (COMPOUND_EXPR, return_type,
+						trap, rhs));
 	}
     }
 
@@ -2543,7 +2549,12 @@ build_function_call (tree function, tree
 			       function, nargs, argarray);
 
   if (VOID_TYPE_P (TREE_TYPE (result)))
-    return result;
+    {
+      if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
+	pedwarn (input_location, 0,
+		 "function with qualified void return type called");
+      return result;
+    }
   return require_complete_type (result);
 }
 
Index: gcc/testsuite/gcc.dg/call-diag-2.c
===================================================================
--- gcc/testsuite/gcc.dg/call-diag-2.c	(revision 0)
+++ gcc/testsuite/gcc.dg/call-diag-2.c	(revision 0)
@@ -0,0 +1,17 @@
+/* Test diagnostics for calling function returning qualified void or
+   other incomplete type other than void.  PR 35210.  */
+/* { dg-do compile } */
+/* { dg-options "-pedantic-errors" } */
+
+const void f_cv (void);
+struct s f_s (void);
+void f_v (void);
+
+void g1 (void) { f_cv (); } /* { dg-error "qualified void" } */
+void g2 (void) { f_s (); } /* { dg-error "invalid use of undefined type" } */
+void g3 (void) { ((const void (*) (void)) f_v) (); } /* { dg-error "qualified void" } */
+/* { dg-warning "function called through a non-compatible type" "cast" { target *-*-* } 12 } */
+/* { dg-message "will abort" "abort" { target *-*-* } 12 } */
+void g4 (void) { ((struct s (*) (void)) f_v) (), (void) 0; } /* { dg-error "invalid use of undefined type" } */
+/* { dg-warning "function called through a non-compatible type" "cast" { target *-*-* } 15 } */
+/* { dg-message "will abort" "abort" { target *-*-* } 15 } */

-- 
Joseph S. Myers
joseph@codesourcery.com



More information about the Gcc-patches mailing list