Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Jan 2005 16:41:13 +0200
From:      Giorgos Keramidas <keramida@freebsd.org>
To:        Robert Watson <rwatson@freebsd.org>
Cc:        freebsd-current@freebsd.org
Subject:   Re: gratuitous gcc warnings: unused function arguments?
Message-ID:  <20050116144113.GB66854@gothmog.gr>
In-Reply-To: <Pine.NEB.3.96L.1050116120744.50371A-100000@fledge.watson.org>
References:  <Pine.NEB.3.96L.1050116120744.50371A-100000@fledge.watson.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2005-01-16 12:18, Robert Watson <rwatson@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050116144113.GB66854>