https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68922
Bug ID: 68922 Summary: g++ fails to generate code for catch clause with specific optimizations enabled Product: gcc Version: 5.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: alban.lefebvre at gmail dot com Target Milestone: --- Hello, The following code fails to generate a try/catch clause on g++ 5.2.1, 4.9.2, 4.8.4, 4.8.2: #include <iostream> #include <exception> class Base { public: virtual ~Base() {} }; class C1 : public virtual Base { }; class C2 : public virtual Base { public: virtual void foo() = 0; }; class D : public C1, public C2 { public: virtual void foo() { throw std::exception(); } }; int main() { C2 * c2 = new D(); try { c2->foo(); } catch (...) { std::cout << "Caught some exception" << std::endl; } return 0; } when compiled with O2 optimization g++ main.cpp -Wall -Wextra -pedantic -O2 && ./a.out I get the following error when executing it: terminate called after throwing an instance of 'std::exception' what(): std::exception Aborted (core dumped) It seems that __cxa_begin_catch, __cxa_end_catch calls do not get generated: main: sub $0x8,%rsp mov $0x10,%edi callq 0x400850 <_Znwm@plt> lea 0x8(%rax),%rdi movq $0x400ea0,(%rax) movq $0x400ed8,0x8(%rax) callq 0x400b10 <_ZThn8_N1D3fooEv> xor %eax,%eax add $0x8,%rsp retq It looks similar but possibly not the same as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68184 When we remove the virtual inheritance (by removing Base), the bug doesn't occur anymore. As far as which flags seem to trigger the issue, here what I've tried: -O1 => OK -O2 => BUG -O3 => BUG -O1 -ftree-pre -ftree-vrp => BUG -ftree-pre -ftree-vrp => OK Thank you