http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51834

--- Comment #4 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> 2012-04-19 
15:06:58 UTC ---
(In reply to comment #3)
> (i++, i) + i is undefined.  The sequence point only orders i++ and i inside 
> the
> parens, but not the operands of +.  The third example is not undefined.

The example is not (i++, i) + i, but (i, i++, i) + i, which is different
because there is a sequence point before and after the i++. Still, there seem
to be disagreements on how to interpret the standard.

There's a discussion "On sequence points and evaluation order" [1] in
comp.std.c in 1995-12 (though that's a bit old), from which there are arguments
to see the above expressions as UB. But "sequence points and evaluation order"
[2] in comp.lang.c in 2006-09 and a message from Keith Thompson [3] in
comp.std.c in 2010-10 both contradict it: they both say something like sin(x) +
cos(x) has defined behavior even if sin() and cos() both modify errno (and that
these functions can be implemented by a macro, as long as it has a sequence
point).

[1]
http://groups.google.com/group/comp.std.c/browse_thread/thread/d133e9c51bef572b/0b6545278c23d37f
[2]
http://groups.google.com/group/comp.lang.c/browse_thread/thread/c4bc836b783b91be/d807a3ad7202b45b
[3] http://groups.google.com/group/comp.std.c/msg/2dc8d2e8a0f4e572

What's strange is that GCC (4.4 to 4.7 at least) complains on

  (i ? (j |= 1, 0) : 0) | (i ? (j |= 1, 0) : 0);

but not on

  (j |= 1, 0) | (j |= 1, 0);

Contrary to GCC, I would say that the latter is UB (because from the root of
the expression, one can evaluate both j |= 1 without getting a sequence point
yet -- GCC should have output a warning, and that's bug 51562), but not the
former (similar to the errno case).

Here's a simple testcase I've used, with more tests:

int i, j;

static inline int set_flag (void)
{
  j |= 1;
  return 0;
}

#define FOO (i ? (j |= 1, 0) : 0)
#define BAR (i ? set_flag () : 0)

void fct (void)
{
  FOO || FOO;
  FOO | FOO;
  BAR | BAR;
  set_flag () + set_flag ();
  j = (++i, j) + (j, ++i);
  return;
}

GCC 4.7.0 warns only for "FOO | FOO;" (and I think that's incorrect, as said
above).

Reply via email to