https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71957
Bug ID: 71957 Summary: Invalid code generation with function static objects Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: sebastian.hu...@embedded-brains.de Target Milestone: --- The following test case produces wrong code with GCC 6. The problem is at least visible on x86, ARM, SPARC and PowerPC. class A { public: A(int); }; template <typename T> class B { public: virtual void* h(int) = 0; }; template <typename T> class C : public B<T> { public: explicit C(int); void* h(int); }; C<A>& g(void); class D : public A { static void* f(int); }; void* D::f(int k) { B<D>& j = (reinterpret_cast<C<D>&>(g())); return j.h(k); } int f(void); C<A>& g(void) { static C<A> i(f()); return i; } g++ -Wfatal-errors -Wall -Wextra -S -O3 -fno-exceptions test.cc -o- .file "test.cc" .text .align 2 .p2align 4,,15 .globl _ZN1D1fEi .type _ZN1D1fEi, @function _ZN1D1fEi: .LFB0: .cfi_startproc subq $8, %rsp .cfi_def_cfa_offset 16 movl $_ZGVZ1gvE1i, %edi movzbl _ZGVZ1gvE1i(%rip), %eax call __cxa_guard_acquire <-- ERROR: It must check the return value of __cxa_guard_acquire() call _Z1fv movl $_ZZ1gvE1i, %edi movl %eax, %esi call _ZN1CI1AEC1Ei movl $_ZGVZ1gvE1i, %edi call __cxa_guard_release <-- ERROR Invalid function return .cfi_endproc .LFE0: .size _ZN1D1fEi, .-_ZN1D1fEi .p2align 4,,15 .globl _Z1gv .type _Z1gv, @function _Z1gv: .LFB1: .cfi_startproc movzbl _ZGVZ1gvE1i(%rip), %eax testb %al, %al je .L15 movl $_ZZ1gvE1i, %eax ret .p2align 4,,10 .p2align 3 .L15: subq $8, %rsp .cfi_def_cfa_offset 16 movl $_ZGVZ1gvE1i, %edi call __cxa_guard_acquire testl %eax, %eax je .L5 call _Z1fv movl $_ZZ1gvE1i, %edi movl %eax, %esi call _ZN1CI1AEC1Ei movl $_ZGVZ1gvE1i, %edi call __cxa_guard_release .L5: movl $_ZZ1gvE1i, %eax addq $8, %rsp .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE1: .size _Z1gv, .-_Z1gv .local _ZGVZ1gvE1i .comm _ZGVZ1gvE1i,8,8 .local _ZZ1gvE1i .comm _ZZ1gvE1i,8,8 .ident "GCC: (GNU) 6.1.1 20160705 [gcc-6-branch revision fcd04db:3fed107:ebf5486fa49b6660091b549d17e945f63e698eac]" .section .note.GNU-stack,"",@progbits