Re: gratuitous gcc warnings: unused function arguments?

From: Giorgos Keramidas <keramida_at_freebsd.org>
Date: Sun, 16 Jan 2005 16:41:13 +0200
On 2005-01-16 12:18, Robert Watson <rwatson_at_freebsd.org> wrote:
> At some point in the relatively recent past, we began to generate
> warnings for unused arguments in function definitions.  This is a
> fairly irritating practice, as it interacts poorly with function
> implementations plugged into pluggable function interfaces, such as
> PAM, main(), etc.  Here's an example of a particularly irritating
> instance of the warning:
>
> static void
> usage(void)
> {
>
> 	fprintf(errno, "program: too many arguments\n");
>         exit(-1);
> }
>
> int
> main(int argc, char *argv[])
> {
>
>         if (argc != 1)
>                 usage();
>
> 	dostuff();
> }

In this case, using __unused is a GCC-specific but commonly used way to
specify that argv is unused and we know it's ok.  There is also a more
portable way to specify within the body of the function that argv is
unused:

% int
% main(int argc, char *argv[])
% {
%
%      (void)argv;
%      if (argc != 1)
%          usage();
%      return (0);
% }

This allows the main() function to keep being compliant with the ANSI
standard that specifies only two valid prototypes for main():

	int main(void);
	int main(int argc, char *argv[]);

But it also inhibits the warning from GCC, because argv is 'used', even
though the value of the void expression is immediately thrown away.

I'm not sure if this is "better" than __unused, but it works both on GCC
and on the Forte C/C++ compilers of Sun that I've tried it with.

- Giorgos
Received on Sun Jan 16 2005 - 13:41:34 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:26 UTC