diff mbox

libstdc++: Fix exceptions being generated when compiling with -fno-exceptions

Message ID 1429024679-17856-1-git-send-email-federico.lenarduzzi@tallertechnologies.com
State New
Headers show

Commit Message

Federico Lenarduzzi April 14, 2015, 3:17 p.m. UTC
When the libstdc++ is compiled, the compiler sets the std::terminate_handler function with __verbose_terminate_handler() or std::abort() depending on _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE being true or false.

However, even if we compile with -fno-exceptions, the compiler will use __verbose_terminate_handler(), which uses exceptions. Therefore, the library is not fully exception-free.

This patch adds a check for __EXCEPTIONS to the #if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE condition. If __EXCEPTIONS is defined, the compiler will use __verbose_terminate_handler() as a termination function; otherwise it'll use std::abort() which doesn't have exceptions. It also makes std::uncaught_exception() throw() return false if __EXCEPTIONS is not defined.

libstdc++-v3/
2015-04-14  Federico Lenarduzzi  <federico.lenarduzzi@tallertechnologies.com>

	* libsupc++/eh_catch.cc (std::uncaught_exception() throw()): Add an #else
	which returns false if __EXCEPTIONS is not defined.
	* libsupc++/eh_term_handler.cc: Add a check for __EXCEPTIONS to the #if.

---
 libstdc++-v3/libsupc++/eh_catch.cc        | 4 ++++
 libstdc++-v3/libsupc++/eh_term_handler.cc | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

Comments

Jonathan Wakely April 14, 2015, 7:27 p.m. UTC | #1
On 14 April 2015 at 16:17, Federico Lenarduzzi wrote:
> When the libstdc++ is compiled, the compiler sets the std::terminate_handler function with __verbose_terminate_handler() or std::abort() depending on _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE being true or false.
>
> However, even if we compile with -fno-exceptions, the compiler will use __verbose_terminate_handler(), which uses exceptions. Therefore, the library is not fully exception-free.

I guess this makes sense, but you can just build with
--disable-libstdcxx-verbose to have the same effect.

> It also makes std::uncaught_exception() throw() return false if __EXCEPTIONS is not defined.

This part seems like a good idea, but we'll also need to do it to the
new uncaught_exceptions() function about to be added, so let's wait
until that in committed first (which will be shortly after the 5.1
release).

The checks should use __cpp_exceptions not __EXCEPTIONS, see the current trunk.
diff mbox

Patch

diff --git a/libstdc++-v3/libsupc++/eh_catch.cc b/libstdc++-v3/libsupc++/eh_catch.cc
index 43e875a..6b500f6 100644
--- a/libstdc++-v3/libsupc++/eh_catch.cc
+++ b/libstdc++-v3/libsupc++/eh_catch.cc
@@ -136,6 +136,10 @@  __cxxabiv1::__cxa_end_catch ()
 bool
 std::uncaught_exception() throw()
 {
+#ifdef __EXCEPTIONS  
   __cxa_eh_globals *globals = __cxa_get_globals ();
   return globals->uncaughtExceptions != 0;
+#else
+  return false;
+#endif
 }
diff --git a/libstdc++-v3/libsupc++/eh_term_handler.cc b/libstdc++-v3/libsupc++/eh_term_handler.cc
index c855360..0935193 100644
--- a/libstdc++-v3/libsupc++/eh_term_handler.cc
+++ b/libstdc++-v3/libsupc++/eh_term_handler.cc
@@ -32,7 +32,7 @@ 
    --disable-libstdcxx-verbose and rebuilding the library.
    In a freestanding environment, we default to this latter approach.  */
 
-#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
+#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE && defined(__EXCEPTIONS)
 /* The current installed user handler.  */
 std::terminate_handler __cxxabiv1::__terminate_handler =
 	__gnu_cxx::__verbose_terminate_handler;