Eric Sosman
7/22/2011 12:53:00 AM
On 7/21/2011 6:35 PM, pozz wrote:
> Is there a universal method (valid for all C compilers) that avoids the
> compiler to generate a warning about a not used parameter of a function?
No, because a compiler is always permitted to issue diagnostics
about any random condition its implementors think of. It cannot
refuse to translate a correct program simply because it's told you
"foo.c(37): E1943 Not tonight, I have a headache", but it's free to
issue the diagnostic anyhow.
> I think it isn't because compiler warnings aren't regulated by the
> standard. So I'm wondering what could be the best or the better methods
> to face this situation.
The Standard does in fact regulate diagnostics, but the regulatory
burden is light. Some transgressions are required to provoke
diagnostics, some are not (but may), and as mentioned above it is
entirely all right for the compiler to produce diagnostics even for
valid C programs. (Warning about an unused `defualt:' label in a
`switch' statement, for example, is probably a Good Thing even when
the code is perfectly valid as it stands.)
Another point is that the Standard speaks only of "diagnostics,"
not of "errors" and "warnings," nor of "errors" and "warnings" and
"stylistic abominations" and "queasy feelings." Even a required
diagnostic need not prompt an implementation to refuse a program; it
can say "Syntax error: Proceeding as if there were a `,' here" and
translate the program anyhow. (Exception: It must refuse a program
with a "live" `#error' directive.)
> I tried with the following on my compiler:
>
> int sum(int a, int b, int unused) {
> unused;
> return a + b;
> }
>
> but it works only with a low level of warnings. Otherwise I can do:
>
> int sum(int a, int b, int unused) {
> unused = unused;
> return a + b;
> }
>
> but it generates an unuseful instruction without optimization.
>
> What do you use for that goal?
Some compilers will be quiet if you write `(void)unused;' to
"use" the unused parameter. Others, unfortunately, will issue new
warnings of the "Statement with no effect" kind ...
One possibility is to use a macro whose definition is tuned to
the particular compiler in use. You'd write
int sum(int a, int b, int unused) {
UNUSED(unused)
...
and in a header file somewhere you'd have something like
#ifdef _FROBOZZ_MAGIC_C_
#define UNUSED(x) _Pragma(x,__PARAM_UNUSED__)
#elif defined _O_SAY_CAN_YOU_C_
#define UNUSED(x) /* empty */
#else
/* Hope for the best ... */
#define UNUSED(x) void(x);
#endif
This list of compiler-specific dodges can be updated as experience
dictates.
--
Eric Sosman
esosman@ieee-dot-org.invalid