Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Jan 2005 12:18:22 +0000 (GMT)
From:      Robert Watson <rwatson@FreeBSD.org>
To:        current@FreeBSD.org
Subject:   gratuitous gcc warnings: unused function arguments?
Message-ID:  <Pine.NEB.3.96L.1050116120744.50371A-100000@fledge.watson.org>

next in thread | raw e-mail | index | archive | help

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();
}

Just to be clear: this program accepts no arguments, and is considered to
be used in error if passed an argument.  This is because someday the
program will likely accept arguments and not consider them to be no-ops,
so in the interests of command line compatibility, we reject arguments
today as an error in usage.  If compiled with WARNS=3, the following error
occurs:

cc -O -pipe  -Wsystem-headers -Werror -Wall -Wno-format-y2k -W
-Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith
-Wno-uninitialized -c program.c
program.c:48: warning: unused parameter 'argv'
*** Error code 1

However, gcc also knows that correct use of the C language doesn't permit
us to only declare argc as an argument, resulting in a similar warning if
argv is removed:

cc -O -pipe  -Wsystem-headers -Werror -Wall -Wno-format-y2k -W
-Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith
-Wno-uninitialized -c program.c
program.c:49: warning: 'main' takes only zero or two arguments
*** Error code 1

Graffiting large piles of code with "__unused" strikes me as equally high
up there on the irritating scale, as we have a non-trivial number of
situations where functions implement APIs where some of the arguments
provided to the API are not required for a full implementation of the API.
For example, PAM entry points for PAM modules contain many arguments, and
not all of those arguments are used by every implementation --
pam_kerberosIV doesn't need all the information passed, for example, to
the session management interface.  One of the reasons why I find __unused
irritating is the following sort of situation:

int
dummyfunction(int arg1, int arg2, char *argv)
{

#ifdef DUMMY_USES_ARGV
	if (argv != NULL)
		printf("dummyfunction: %s\n", argv);
#endif
	return (arg1 + arg2);
}

With the new world order, we would have to ifdef the function definition
to conditionally use __unused in order to allow the function to compile
with or without the #ifdef.

I'm not sure what is required to disable this warning, but I'd like to see
us make it optional and not keyed to lower warning levels (such as 3).

Robert N M Watson



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.96L.1050116120744.50371A-100000>