Bug ID: 84471
           Summary: Instruction reordering happens in lambdas even with
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot
          Reporter: jengelh at inai dot de
  Target Milestone: ---

Expected behavior:

Objects built with -O0 ought not cause gdb to spuriously "go backwards" in
source (for reasons other than the end of a loop).

Observed behavior:

Despite being explicitly built with no optimizations, I observe that gdb jumps
around inside C++ programs' lambdas that capture something.

#include <cstring>
#include <utility>
#define N 256
int main()
        const char key[] = "keykeykeykeykeykey";
        [&]() {
                unsigned char S[N];
                int len = strlen(key);
                int j = 0;
                for (int i = 0; i < N; ++i)
                        j = (j + S[i] + key[i%len]) % N;

Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-offload-targets=hsa,nvptx-none=/usr/nvptx-none, --without-cuda-driver
--enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/7 --enable-ssp --disable-libssp
--disable-libvtv --disable-libcc1 --enable-plugin
--with-bugurl= --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-7 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 7.3.0 (SUSE Linux) 

$ g++-7 x.cpp -O0 -ggdb3 -fno-reorder-blocks -fno-reorder-blocks-and-partition

$ gdb a.out 
GNU gdb (GDB; openSUSE Tumbleweed) 8.0.1
(gdb) b main
Breakpoint 1 at 0x4005c0: file x.cpp, line 6.
(gdb) r

Breakpoint 1, main () at x.cpp:6
6               const char key[] = "keykeykeykeykeykey";
(gdb) s
13              }();
<lambda()>::operator()(void) const (__closure=0x7fffffffdd98) at x.cpp:9
9                       int len = strlen(key);
(gdb) n
10                      int j = 0;
11                      for (int i = 0; i < N; ++i)
12                              j = (j + S[i] + key[i%len]) % N;
9                       int len = strlen(key);
12                              j = (j + S[i] + key[i%len]) % N;

Expected behavior:

Never reach line 9 again.

Reply via email to