What should be added to C ( really = in boolean context )

Joe Steffen steffen at ihuxy.UUCP
Thu Jun 5 02:11:24 AEST 1986


> >>Urgh.  Are you talking about removing the assignment-has-a-value feature
> >>completely, or a special case for "if (v = e)"?  (I always write this as
> >>"if ((v = e) != 0)" to make it clear that I didn't mean "if (v == e)".)
> >>
> >
> Somebody mentioned that Lint could warn about using an assignment value
> in a boolean context - unless you said /* BOOLEUSED */. I think the
> warning is a good idea, but the new special comment is not. You can just
> as easily write ( (v=e)!=0 ) and get same code on any non-silly compiler.
> 
> I wouldn't mind a compiler warning if an assignment op were used in
> *any* boolean context ( including being the operand of !, ||, && , and
> the expr. before a ? ).  Note that boolean context is inherited by the
> RHS of a ',' and by e2 and e3 in e1?e2:e3.

I added a simple check to buildtree() in the pcc compilers I support to
warn of and = op directly under a CBRANCH op, which is used by if, while,
and the middle expression in a for statement.  This caught many latent bugs
in our project's code, many which were of the form

	if (rc = f() != 0)
	
The warning can be ignored on

	while (*p++ = *q++)
	
or preferably changed to

	while ((*p++ = *q++) != 0)
	
for clarity, as stated above.  Unfortunately many pcc compiler generate a
useless compare instruction for the latter, since the condition codes are
already set by the move instruction on many machines.  With another simple
change to optim() in the compiler to remove the != 0 under a CBRANCH op, I
solved this objection to adding the != 0, and the compiler generates less
code!
-- 


	Joe Steffen, AT&T Bell Labs, Naperville, IL, (312) 979-5381



More information about the Comp.lang.c mailing list