RFA: fix PR c/48116

Tom Tromey tromey@redhat.com
Wed Mar 16 17:18:00 GMT 2011


This patch fixes PR c/48116.

The bug is that -Wreturn-type does not follow the documentation.  In
particular, it should warn for this code, but does not:

    static void f() {}
    static void g() { return f(); }

I think the bug is that c-typeck.c calls pedwarn with either 0 or
OPT_pedantic, but it should use OPT_Wreturn_type in some cases.

I am not completely sure this is the correct patch.  In particular, this
patch chooses to report -Wreturn-type over -pedantic, e.g.:

    opsy. gcc -pedantic -Wreturn-type --syntax-only q.c
    q.c: In function ‘y’:
    q.c:3:16: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wreturn-type]

This is a somewhat odd situation, in that both -Wreturn-type and
-pedantic must be disabled to eliminate this message.

Also, arguably there should be a different message when -pedantic is not
given.

Bootstrapped and regtested on x86-64 (compile farm).
New test case included.

I did not check to see whether this is a regression.

Tom

2011-03-16  Tom Tromey  <tromey@redhat.com>

	PR c/48116
	* c-typeck.c (c_finish_return): Check warn_return_type.

2011-03-16  Tom Tromey  <tromey@redhat.com>

	* gcc.dg/Wreturn-type3.c: New file.

Index: c-typeck.c
===================================================================
--- c-typeck.c	(revision 170953)
+++ c-typeck.c	(working copy)
@@ -8628,10 +8628,11 @@
     {
       current_function_returns_null = 1;
       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
-	pedwarn (loc, 0,
+	pedwarn (loc, warn_return_type ? OPT_Wreturn_type : 0,
 		 "%<return%> with a value, in function returning void");
       else
-	pedwarn (loc, OPT_pedantic, "ISO C forbids "
+	pedwarn (loc, warn_return_type ? OPT_Wreturn_type : OPT_pedantic,
+		 "ISO C forbids "
 		 "%<return%> with expression, in function returning void");
     }
   else
Index: testsuite/gcc.dg/Wreturn-type3.c
===================================================================
--- testsuite/gcc.dg/Wreturn-type3.c	(revision 0)
+++ testsuite/gcc.dg/Wreturn-type3.c	(revision 0)
@@ -0,0 +1,6 @@
+/* PR c/48116 */
+/* { dg-do compile } */
+/* { dg-options "-Wreturn-type" } */
+
+static void f() {}
+static void g() { return f(); }	/* { dg-warning "forbids .return" "missing return" } */



More information about the Gcc-patches mailing list