This is the mail archive of the gcc@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]
Other format: [Raw text]

Patch to Add -Wunused-returns


GCC Folks,

Thanks for your wide response (generally along the lines of "okay, if you
want it - do it!"). I've resurrected Gray Watson's patch
(http://256.com/gray/) and 95% of the credit of this patch belongs to him.
(I obtained his permission to resurrect this patch.) I basically just
slapped his code in there my way and gave it a quick check. Seems to work on
some simple test cases just fine.

Incidentally, when running my diff it became clear that some files are in
CVS that oughtn't be -- i.e., are autogenerated! Here they are:

    fastjar/Makefile.in (made from fastjar/Makefile.am)
    fastjar/configure   (made from configure.in)

Needless to say, having these auto-generated files in CVS makes diffing
annoying. =)

There were also numerous warnings when compiling GCC from GCC 2.96 and XGCC
itself. Is this okay, or is the hope to someday have a smooth, clean build?

Here is a contextual diff for readability.

Cheers,
 -david




Index: gcc/c-common.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.279
diff -u -r1.279 c-common.c
--- c-common.c 2001/12/05 23:19:54 1.279
+++ c-common.c 2001/12/07 04:57:44
@@ -1244,6 +1244,26 @@
   return value;
 }

+/* Check to see if a non-void function's return value is ignored. */
+
+void
+check_for_unused_returns (expr)
+     tree expr;
+{
+  tree function;
+
+  /* Make sure our expression is a function. */
+  if (TREE_CODE (expr) != CALL_EXPR ||
+      TREE_TYPE (expr) == void_type_node)
+    return;
+
+  function = TREE_OPERAND (expr, 0);
+
+  if ((TREE_CODE (function) == ADDR_EXPR) &&
+      (TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL))
+    warning("return value from function ignored");
+}
+
 /* Return an integer type with BITS bits of precision,
    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */

Index: gcc/c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.281
diff -u -r1.281 c-decl.c
--- c-decl.c 2001/12/05 23:19:55 1.281
+++ c-decl.c 2001/12/07 04:57:45
@@ -419,6 +419,10 @@

 int warn_unknown_pragmas = 0; /* Tri state variable.  */

+/* Warn about unused return values from non-void functions. */
+
+int warn_unused_returns = 0;
+
 /* Warn about comparison of signed and unsigned values.
    If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified.
*/

@@ -765,6 +769,10 @@
     warn_unknown_pragmas = 2;
   else if (!strcmp (p, "-Wno-unknown-pragmas"))
     warn_unknown_pragmas = 0;
+  else if (!strcmp (p, "-Wunused-returns"))
+    warn_unused_returns = 1;
+  else if (!strcmp (p, "-Wno-unused-returns"))
+    warn_unused_returns = 0;
   else if (!strcmp (p, "-Wall"))
     {
       /* We save the value of warn_uninitialized, since if they put
Index: gcc/c-parse.in
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-parse.in,v
retrieving revision 1.117
diff -u -r1.117 c-parse.in
--- c-parse.in 2001/12/04 22:55:37 1.117
+++ c-parse.in 2001/12/07 04:57:46
@@ -2314,6 +2314,8 @@
  { stmt_count++; $$ = $1; }
  | expr ';'
  { stmt_count++;
+   if (warn_unused_returns)
+     check_for_unused_returns ($1);
    $$ = c_expand_expr_stmt ($1); }
  | c99_block_start select_or_iter_stmt c99_block_end
  { if (flag_isoc99)
Index: gcc/c-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-tree.h,v
retrieving revision 1.77
diff -u -r1.77 c-tree.h
--- c-tree.h 2001/12/04 22:55:37 1.77
+++ c-tree.h 2001/12/07 04:57:46
@@ -183,6 +183,7 @@
 extern int  c_decode_option                     PARAMS ((int, char **));
 extern void c_mark_varargs                      PARAMS ((void));
 extern void check_for_loop_decls                PARAMS ((void));
+extern void check_for_unused_returns            PARAMS ((tree));
 extern tree check_identifier                    PARAMS ((tree, tree));
 extern void clear_parm_order                    PARAMS ((void));
 extern int  complete_array_type                 PARAMS ((tree, tree, int));
@@ -361,6 +362,10 @@
 /* Warn about multicharacter constants.  */

 extern int warn_multichar;
+
+/* Warn about unused return values from non-void functions. */
+
+extern int warn_unused_returns;

 /* Nonzero means we are reading code that came from a system header file.
*/

Index: gcc/doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.85
diff -u -r1.85 invoke.texi
--- invoke.texi 2001/12/06 11:49:45 1.85
+++ invoke.texi 2001/12/07 04:57:50
@@ -235,7 +235,7 @@
 @item C-only Warning Options
 @gccoptlist{
 -Wbad-function-cast  -Wmissing-prototypes  -Wnested-externs @gol
--Wstrict-prototypes  -Wtraditional}
+-Wstrict-prototypes  -Wtraditional  -Wunused-returns}

 @item Debugging Options
 @xref{Debugging Options,,Options for Debugging Your Program or GCC}.
@@ -2068,6 +2068,14 @@
 In order to get a warning about an unused function parameter, you must
 either specify @samp{-W -Wunused} or separately specify
 @option{-Wunused-parameter}.
+
+@item -Wunused-returns @r{(C only)}
+@opindex Wunused-returns
+Warn whenever the result of a non-void function is implicitly cast away.
+It is not included in the above, because it requires @code{void} casting
+returns of all sorts of regular functions, like @code{close} and
+@code{printf}, whose return value you usually don't care about. This makes
+GCC more like Lint.

 @item -Wuninitialized
 @opindex Wuninitialized


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