Subject: Re: a new KNF (and some comments)
To: None <tech-misc@netbsd.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-misc
Date: 01/25/2000 13:37:37
[Replies to multiple messages here.]

> For example, a function
> 	f1(short arg) { /* ... */ }
> will not link correctly with K&R style compiler, while
> 	f2(arg) short arg; { /* ... */ }
> will.

Not necessarily; there's no guarantee that any two different compilers
are compatible in any respect.

The thing is, the definition above for f1 is not compatible with an
old-style (or implicit) declaration, whereas f2 is.

> The simplest way to avoid this sort of problem is to use K&R style
> (with ANSI prototypes --- current style :-), in my thought.

"That turns out not to be the case."

If we want to be ANSI, you simply can't do that; an f2-style definition
is incompatible with an "f2(short);" prototype.  (I think - I'm sure
seebs or Chris or someone will slap me down if I'm wrong here.)  gcc
allows it as an extension, but it does so by, effectively, rewriting
the definition as a prototype-style definition, which breaks all the
things that an f1-style definition would break anyway.

Of course, you can prototype it as taking int and then define it
f2-style, but then you might as well use an f1-style definition and
type the formal as int.  As Chris Torek pointed out, the problem is
worst in cases where you don't know what the promoted type is.

>>> 	if (error = function(a1, a2))
>> 	if ((error = function(a1, a2)))
>> 	if ((error = function(a1, a2)) != 0)
> Doesn't one of the gcc warnings barf about "testing assignment value"
> or something?  I think there are circumstances where you do need the
> "!= 0" bit to keep it happy.

The warning is "warning: suggest parentheses around assignment used as
truth value", but the extra pair of parens is enough to keep it quiet.

>> But since it will introduces incompatibility with 3rd party
>> applications, I wish that library authors be more careful to keep
>> K&R ABI compatibility.
> I wish there were a way to have gcc warn when an ANSI prototype would
> promote differently in K&R mode.

invoke.texi says, in part,

@item -Wconversion
Warn if a prototype causes a type conversion that is different from what
would happen to the same argument in the absence of a prototype.  This
includes conversions of fixed point to floating and vice versa, and
conversions changing the width or signedness of a fixed point argument
except when the same as the default promotion.

I'm not sure it's quite what you want, but it sounds pretty close.

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B