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]

Re: Patch to Add -Wunused-returns


On 06-Dec-2001, David E. Weekly <dweekly@legato.com> wrote:
> +++ c-common.c 2001/12/07 04:57:44
...
> +/* 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");

I don't see what the point of the second `if' is.  Surely the same
warning should be issued for calls via function pointers as is issued for
ordinary calls.

So I'd just write the body of this function like this:

  if (TREE_CODE (expr) == CALL_EXPR &&
      TREE_TYPE (expr) == void_type_node)
    warning("return value from function call ignored");

On the other hand, if it is an ordinary function call, then it would be
nice if the warning message included the name of the function.
So maybe it would be better to keep the code you have above,
and change the last three lines to something like this
(completely untested):

  if ((TREE_CODE (function) == ADDR_EXPR) &&
      (TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL))
    warning_with_decl(TREE_OPERAND (function, 0),
                      "return value from function `%s' ignored");
  else
    warning("return value from function call ignored");

> Index: gcc/doc/invoke.texi
...
> +@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.

I'm not sure that close() is a good example here, since checking the
return value from close() for files opened in output mode is very
important, and it would be bad to encourage people to omit that.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


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