[cfe-dev] clang, noexcept and terminate
hhinnant at apple.com
Mon Jan 30 18:04:49 PST 2012
On Jan 30, 2012, at 4:11 PM, Howard Hinnant wrote:
> Wait, it gets worse. The compiler can't just do:
> In C++98/03, the terminate_handler that gets used in a call to terminate during exception unwinding is the terminate_handler in effect at the time the exception is thrown, not when the implementation "catches" the exception and calls terminate. libc++abi-like libraries handle this by storing the terminate_handler and unexpected_handler in the thrown exception object at the time the exception object is created.
I think I've changed my mind on this. With a modified std::terminate() that detected caught-but-unhandled exceptions, and called the right handler based on that detection, I tried to write a test case that would get the wrong answer if we just did the above, and I'm having trouble doing so. The above answer is easy and ABI compatible.
clang just needs to start emitting a call to __cxa_begin_catch prior to calling terminate. If I'm wrong, and there is a test case where this doesn't work, such code is going to be vanishingly rare, and far rarer than code that is broken by our current state: not calling __cxa_begin_catch prior to calling terminate.
Here's a test case that I would like to run:
std::cout << "handler 1\n";
std::cout << "handler 2\n";
throw std::runtime_error("throw in f2");
void f1() throw()
Whether f1() has a noexcept or a throw(), the output should be:
A wrong output is:
(for either noexcept or throw())
I've just checked in a new std::terminate in libc++abi that will make this possible *if* clang calls __cxa_begin_catch prior to calling std::terminate (revision 149329).
to track this work.
More information about the cfe-dev