From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12069 invoked by alias); 7 Dec 2001 05:10:20 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 12034 invoked from network); 7 Dec 2001 05:10:17 -0000 Received: from unknown (HELO fencepost.gnu.org) (199.232.76.164) by sources.redhat.com with SMTP; 7 Dec 2001 05:10:17 -0000 Received: from smtp1.legato.com ([137.69.200.1]) by fencepost.gnu.org with esmtp (Exim 3.22 #1 (Debian)) id 16CDH7-0004BR-00 for ; Fri, 07 Dec 2001 00:10:17 -0500 Received: from mta1.legato.com (mta1.legato.com [137.69.1.14]) by smtp1.legato.com (Switch-2.1.3/Switch-2.1.0) with ESMTP id fB75AEM12942 for ; Thu, 6 Dec 2001 21:10:14 -0800 (PST) Received: from mail.legato.com (mail [137.69.1.58]) by mta1.legato.com (Switch-2.1.3/Switch-2.1.0) with ESMTP id fB75AEu20615 for ; Thu, 6 Dec 2001 21:10:14 -0800 (PST) Received: from gadzooks (trilluser@gadzooks.legato.com [137.69.4.92]) by mail.legato.com (SendmailServer-1.0.4/8.11.1) with SMTP id fB759kk19233 for ; Thu, 6 Dec 2001 21:09:46 -0800 (PST) Message-ID: <0c4f01c17edd$5bbbefc0$5c044589@legato.com> From: "David E. Weekly" To: Subject: Patch to Add -Wunused-returns Date: Thu, 06 Dec 2001 22:03:00 -0000 Organization: Legato Systems, Inc. MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-SW-Source: 2001-12/txt/msg00349.txt.bz2 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