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

Reply via email to