In comp.lang.c Eric Sosman said:
S.Tobias wrote On 10/13/05 17:19,:
In comp.lang.c Thad Smith said:
Albert van der Horst wrote:
[snip]
Make clean code. Catch the warnings in a file.
Carefully inspect the warnings and head them.
Catch the warnings on the resulting cleaner code.
Now in the regression test, expect the warnings to remain
the same, i.e. make a diff with the file with the warnings
of the previous test.
Only act on warnings changes, indicative of new warnings.
That's an interesting idea! I suppose I should strip the line numbers
on the warnings so that code insertion/deletion doesn't trigger an
avalanche in the diff. It's time for a little scripting. I'll have
to give that a try. I could even fail a make if the warning file
changed.
This idea has been wandering around me for some time, too.
It could work like this: programmer puts #pramas in the code,
which contain verbatim quotes (or regexes, or identifires) of warnings
that are to be suppressed for the next line. The utility program (or
a script) calculates line numbers and produces a list of warnings
(with the line numbers) to cut out from the compiler output.
Like this:
#pragma nowarn t.c:%n: warning: comparison between signed and unsigned
if (u>s)
Ugh. Ugh ugh ugh.
That's how great ideas die - someone says "ugh"... ;-)
First, while the use of warning-suppressing tags in the
code goes back at least to lint, using #pragma to express
them is a truly terrible idea. Somewhere there'll be a
compiler that actually recognizes "#pragma nowarn" and does
something with it -- for example, suppressing all warning
messages for the entire translation unit, or turning on
ARN (automatic name rewriting) NOW.
Embed such tags in
specially-formatted comments that are linquistically inert,
not in constructs that might at any moment turn into toxic
chemicals and rot your program.
That's true. All #pragmas (with a small exception) suffer from this
disease. OTOH the whole concept is subject to implementation behaviour
and is not (universally) portable. But I agree, it's always better to
avoid a mine-field, and take a quiet and safe path.
(Only, what if the compiler uses the same tags embedded in comments
for its own purposes, too?)
What might actually be bad in practice with #pragmas is that a
compiler might issue warnings about unrecognized #pragmas, thus
we'd be chasing our tail.
Let's make that:
/* $ nowarn: ... $ */
(Actually, only `$ nowarn: ... $' part is recognized by the utility.)
Second, a far better way to suppress the warning you
mention is
if (u > (unsigned)s)
Sometimes it can be a bad thing for a "last resort" mechanism
to exist: it's too easy for people to give up searching for
the "first resort."
That was just a poor example how it could be used. Anyway,
you had to clutter the code to suppress the warning, too.
Sometimes there's no way of "fixing" warnings for all compilers,
you do it for one, and some other issue arises for another.
Consider this (semi-real-life example):
off_t off;
size_t siz;
if (siz > off) ;
may cause:
warning: signed/unsigned in comparison
on one implementation, whereas
if (siz > (size_t)off) ;
on another may issue:
warning: possible loss of data in cast
(besides that casting always makes me feel uneasy).
It's not obvious which side to cast to which.
(I actually solved it this way (through a macro):
if(siz + (off_t)0 > off + (size_t)0) ;
(clue: usual arithmetic conversions).
)
Finally, and it's been said before: The goal of turning
of ALL warnings from ALL compilers is ultimately futile,
because compilers are allowed to complain about anything
they feel like. "Warning: Source was modified at 2:37 AM;
sleep-deprived programmer may have made more mistakes than
usaul."
Well, yes, but my goal is not to turn off ALL warnings, but
only *known* and *inspected* warnings. The idea is that
I don't waste my work when line numbers change.