public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107354] New: gcc and clang give different answers for this arithmetic expression
@ 2022-10-22  1:58 luydorarko at vusra dot com
  2022-10-22  2:03 ` [Bug c++/107354] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: luydorarko at vusra dot com @ 2022-10-22  1:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107354

            Bug ID: 107354
           Summary: gcc and clang give different answers for this
                    arithmetic expression
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luydorarko at vusra dot com
  Target Milestone: ---

Consider this test program:

#include <iostream>
int main()
{
  int a = 0;
  int b = ++a + a++;
  std::cout << a << b << '\n';
}


With g++, it gives the answer 23 with no warnings.

With clang++, it gives the answer 22 with this warning:

testfun.cpp:6:11: warning: multiple unsequenced modifications to 'a'
[-Wunsequenced]
  int b = ++a + a++;
          ^      ~~

The same thing happens with the equivalent C program as well:

#include <stdio.h>
int main()
{
  int a = 0;
  int b = ++a + a++;
  printf ("%d%d\n", a, b);
}

gcc gives 23 with no warning, while clang gives 22 with the same warning as
above.

It is not clear which compiler's answer is correct or consistent with the
language standards, but it would be nice if GCC could at least warn about
syntax as well.


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-bootstrap
--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--with-build-config=bootstrap-lto --with-linker-hash-style=gnu
--with-system-zlib --enable-__cxa_atexit --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (GCC) 



$ clang -v
clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation:
/usr/bin/../lib/gcc/x86_64-pc-linux-gnu/12.2.0
Found candidate GCC installation:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/107354] gcc and clang give different answers for this arithmetic expression
  2022-10-22  1:58 [Bug c++/107354] New: gcc and clang give different answers for this arithmetic expression luydorarko at vusra dot com
@ 2022-10-22  2:03 ` pinskia at gcc dot gnu.org
  2022-10-22  2:05 ` pinskia at gcc dot gnu.org
  2022-10-22  2:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-22  2:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107354

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC gives a warning with -Wall:
<source>: In function 'int main()':
<source>:5:11: warning: operation on 'a' may be undefined [-Wsequence-point]
    5 |   int b = ++a + a++;
      |           ^~~

>It is not clear which compiler's answer is correct
Both are correct for C.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/107354] gcc and clang give different answers for this arithmetic expression
  2022-10-22  1:58 [Bug c++/107354] New: gcc and clang give different answers for this arithmetic expression luydorarko at vusra dot com
  2022-10-22  2:03 ` [Bug c++/107354] " pinskia at gcc dot gnu.org
@ 2022-10-22  2:05 ` pinskia at gcc dot gnu.org
  2022-10-22  2:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-22  2:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107354

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Plus from https://gcc.gnu.org/bugs/ :
Before reporting that GCC compiles your code incorrectly, compile it with gcc
-Wall -Wextra and see whether this shows anything wrong with your code.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/107354] gcc and clang give different answers for this arithmetic expression
  2022-10-22  1:58 [Bug c++/107354] New: gcc and clang give different answers for this arithmetic expression luydorarko at vusra dot com
  2022-10-22  2:03 ` [Bug c++/107354] " pinskia at gcc dot gnu.org
  2022-10-22  2:05 ` pinskia at gcc dot gnu.org
@ 2022-10-22  2:06 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-22  2:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107354

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
And even more from https://gcc.gnu.org/bugs/#known :
Increment/decrement operator (++/--) not working as expected - a problem with
many variations.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-22  2:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22  1:58 [Bug c++/107354] New: gcc and clang give different answers for this arithmetic expression luydorarko at vusra dot com
2022-10-22  2:03 ` [Bug c++/107354] " pinskia at gcc dot gnu.org
2022-10-22  2:05 ` pinskia at gcc dot gnu.org
2022-10-22  2:06 ` pinskia at gcc dot gnu.org

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