public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-gcc at vinc17 dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/51834] -Wsequence-point fails when convoluted expressions with multiple side effects are used
Date: Thu, 19 Apr 2012 15:07:00 -0000	[thread overview]
Message-ID: <bug-51834-4-hnynAPnLb4@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-51834-4@http.gcc.gnu.org/bugzilla/>

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).


      parent reply	other threads:[~2012-04-19 15:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-12  6:29 [Bug c/51834] New: " prasoonsaurav.nit at gmail dot com
2012-01-12  8:51 ` [Bug c/51834] " rguenth at gcc dot gnu.org
2012-01-12  9:08 ` prasoonsaurav.nit at gmail dot com
2012-01-12  9:20 ` schwab@linux-m68k.org
2012-04-19 15:07 ` vincent-gcc at vinc17 dot net [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-51834-4-hnynAPnLb4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).