This is the mail archive of the egcs-bugs@egcs.cygnus.com mailing list for the EGCS project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Too many warnings.


>craig@jcb-sc.com wrote:
>> The point is not that it is common, rather that the *purpose* of `=' is
>> to perform a "side effect", i.e. of modifying one of its operands.
>[snip]
>> `&&' does not modify its operands, so, as the only zero-level operator in
>> a statement, it is entirely appropriate that gcc warn about the statement
>> having, at its zero level anyway, no effect.
>
>Ok.
>
>Alexandre Oliva <oliva@dcc.unicamp.br> pointed out
>
>  i++ ? j=84 : 0
>
>does not give a warning.  The same argument you use regarding `&&'
>could be applied to `?:' here, couldn't it?  So it is still a bit
>inconsistent, isn't it?

I agree.

The main distinction between operators like `=', `+=', `-=', etc., and
operators like `&&', `?:', `&', `+', etc., is that the former modify one
of their operands, the latter do not.

But, the main distinction between operators like `&&', `||', `?:', and
operators like `=', `+', `&', etc., is that the former guarantee evaluation
of one of their operands *entirely* based on the result of evaluating one of
the others.  (In that sense, they not only define "sequence points", the
term I think the C standard(s) uses, but they perform conditional evaluations
of operands.  `,' defines a sequence point, but not a conditional evaluation.)

Therefore, I view a warning about `&&' as a zero-level operator as equally
as valid as one about `?:' as a zero-level operator.

In both cases, they could (as zero-level operators) be easily replaced
via an `if':

  expr1 && expr2;          => if (expr1) expr2;
  expr1 || expr2;          => if (! expr1) expr2;
  expr1 ? expr2 : expr3    => if (expr1) expr2; else expr3;

The "replaceability" illustrates an important distinction between operators
(`=', `&&', `+', etc.) used as zero-level vs. used as nonzero-level ones,
namely, that the value computed as a result of their evaluation goes
unused.  Unused values are normal for `=', since there's no other way
to express `=' *except* via using an operator that also computes a result
value, so we can't *always* warn about them.  Unused values for `+' don't
make much sense, because that means *computation* has been requested but,
for some reason, the results of it not used, hence we warn about that.

With `&&', `||', and `?:' as zero-level operators, the question becomes,
did the user intend *only* the conditional-execution aspect ("spark", to
those few who read my rantings on such subjects ;-)?  Or did he intend
the compute-me-a-value aspect (or both), in which case that value isn't
going to be used?

*Whether* warnings should be issued in such cases therefore a good question,
that I do not feel strongly about (at least, not at the moment).  Given
replaceability, I'd normal lean towards "yes"; given the widespread need for,
and use of, macros (especially in C, so maybe not applicable to C++
or Objective-C), I wouldn't lean quite so far.

So, in summary:

Clearly, there should be no warning for `=' as a zero-level operator, since
there *is* no substitute (no statement equivalent).

And, there *should* be a warning for `+' or `&' as a zero-level operator,
since there's no purpose at all for the operator as zero-level, meaning
it could be simply replaced by `,'.

`&&', `||', and `?:' seem, to me, to occupy a middle ground.  They have
a highly readable direct substitute -- `if' -- but they also have a
purpose as a zero-level operator (they cannot simply be replaced with `,').
They can therefore be used as zero-level within macros that make sense
both as expressions *and* as statements.

The purpose of the warnings we're discussing here seem, to me, to be
limited to making sure the programmer hasn't erroneously assumed that
the operator was *not* zero-level -- i.e. confused precedence issues,
misread (or mistyped) parentheses, etc.

So, I'd say, since it's harder to confuse `if' this way than the equivalent
`&&', `||', or `?:', the warning should still be issued for those when
issued as zero-level.

But it should be issued for all, or none, I agree.

And, perhaps it should be a slightly different warning than for `+'.

E.g. instead of "no effect", wording like "has effect of `if'" could be
used.

>However, all this is not a big deal.

I'd agree it's a *detail*, but, given the huge number of C programmers
plus the bazillions of lines of C code plus the market penetration (current
and future) of GCC plus the maintenance of GCC (including discussions like
this), even a detail this small becomes a big deal.

So, I feel a bit of time and thought invested in these issues, especially
explaining ours views, is probably worthwhile.

I just hope I've added, rather than subtracted, clarity to the big
picture.

        tq vm, (burley)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]