Funny mistake

Byron Rakitzis byron at archone.tamu.edu
Fri Mar 22 06:36:24 AEST 1991


In article <15490 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <1991Mar16.195153.15509 at murdoch.acc.Virginia.EDU> gsh7w at astsun.astro.Virginia.EDU (Greg Hennessy) writes:
>-#>>"if (a = b)" instead of "if (a == b)". 
>-#>... I have yet to see a UNIX compiler complain about it
>-Doug Gwyn:
>-#That's good, because it is valid C and the compiler cannot know whether
>-#or not it reflects the programmer's intentions.
>-Then perhaps that is why having an optional flag to inform the user of
>-this sometimes suspicious code fragment may be a good idea.
>
>In the world of UNIX, we normally rely on "lint" to generate warnings
>about *possible* problems like this.  The compilers are expected to
>accept conforming translation units and silently translate them.

Flame on:

I think it's time that compilers assumed a greater responsibility in this
direction. How many people remember to run lint on their code? How many do
it at the last minute? I think that one of the best features of gcc is
the -Wall flag; I compile all my code this way, and it has saved my rear
end at compile time countless times.

A "responsible" compiler will flag this if-business as a warning. However,
this will no longer be an ANSI compiler in the Gwynian sense. It will be a
hand-holding compiler that goes out of its way to detect possible glitches in
the code, but if done tastefully, I think it could promote better coding
style in a way that lint has completely failed to do.

I have had uniformly bad experience with lint. Not only is it an outdated
tool (I have not seen an ansi lint) but many of its warnings are not
pertinent to the code. For example, the "pointer alignment" problem with
every call to malloc, and the "returns a value which is ignored" problem
with every call to printf. A user of lint must wade through a whole bunch
of garbage error messages to evaluate whether the code really needs fixing.
And adding gratuitous casts to void before every printf is a solution that's
worse than the problem, IMHO.

I am presently writing (at a snail's pace, unfortunately) a compiler which
will operate at two levels: "pedantic-mode" (the *default*) where it will
try to be picky (in a reasonable way) about both departures from standard
C and from "normal" coding practises (assignments in if statements are on
the verge of obfuscated C, in my opinion; a human reading the code some
time later has to check very thoroughly to make sure that it is in fact
not a typo); and "silent-mode" in which case all warnings are turned off.

The UNIX many-tools philosophy has its uses (I *AM* a many-tools fan), but
I do not think that lint has a place in that toolchest. For one, the
language that a particular compiler implements is rarely the same language
that a tool like lint thinks its checking (cf. the ANSI problem). I
strongly believe it is up to the compiler to take care about safeguarding
the use of the language which it translates, even if this means flagging
valid code which has been *probably* erroneously typed in.

Flame off.



More information about the Comp.lang.c mailing list