From: "David E. Weekly" <dweekly@legato.com>
To: <gcc@gnu.org>
Subject: Patch to Add -Wunused-returns
Date: Thu, 06 Dec 2001 22:03:00 -0000 [thread overview]
Message-ID: <0c4f01c17edd$5bbbefc0$5c044589@legato.com> (raw)
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
next reply other threads:[~2001-12-07 5:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-12-06 22:03 David E. Weekly [this message]
2001-12-06 22:14 ` Fergus Henderson
2001-12-06 23:32 ` David E. Weekly
2001-12-07 2:03 ` Neil Booth
2001-12-07 4:15 ` Joseph S. Myers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='0c4f01c17edd$5bbbefc0$5c044589@legato.com' \
--to=dweekly@legato.com \
--cc=gcc@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).