From 4b2f1da02a37d111b572551f4bd8ed765b06ca28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 19 May 2020 21:16:33 +0200 Subject: [PATCH] Split CATCH_TRANSLATE_EXCEPTION into its own header As far as I know, only a few users actually use it, but these changes allow us to avoid including a surprising amount of code in the main compilation path. --- src/CMakeLists.txt | 1 + src/catch2/catch_all.hpp | 1 + src/catch2/catch_test_macros.hpp | 8 -- src/catch2/catch_translate_exception.hpp | 74 +++++++++++++++++++ .../interfaces/catch_interfaces_exception.hpp | 55 +------------- tests/SelfTest/UsageTests/Exception.tests.cpp | 1 + 6 files changed, 78 insertions(+), 62 deletions(-) create mode 100644 src/catch2/catch_translate_exception.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc7a6753..4f81c000 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -123,6 +123,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/internal/catch_to_string.hpp ${SOURCES_DIR}/catch_tostring.hpp ${SOURCES_DIR}/catch_totals.hpp + ${SOURCES_DIR}/catch_translate_exception.hpp ${SOURCES_DIR}/internal/catch_uncaught_exceptions.hpp ${SOURCES_DIR}/catch_version.hpp ${SOURCES_DIR}/catch_version_macros.hpp diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp index 019c1bfe..b0f31196 100644 --- a/src/catch2/catch_all.hpp +++ b/src/catch2/catch_all.hpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include diff --git a/src/catch2/catch_test_macros.hpp b/src/catch2/catch_test_macros.hpp index 2f1c637f..2514d200 100644 --- a/src/catch2/catch_test_macros.hpp +++ b/src/catch2/catch_test_macros.hpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -202,13 +201,6 @@ #endif // ^^ unprefixed, disabled -// This macro is always prefixed -#if !defined(CATCH_CONFIG_DISABLE) - #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) -#else - #define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) -#endif - // end of user facing macros #endif // TWOBLUECUBES_CATCH_TEST_MACROS_HPP_INCLUDED diff --git a/src/catch2/catch_translate_exception.hpp b/src/catch2/catch_translate_exception.hpp new file mode 100644 index 00000000..0b5a3747 --- /dev/null +++ b/src/catch2/catch_translate_exception.hpp @@ -0,0 +1,74 @@ +#ifndef CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED +#define CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED + +#include + +#include + +namespace Catch { + + class ExceptionTranslatorRegistrar { + template + class ExceptionTranslator : public IExceptionTranslator { + public: + + ExceptionTranslator( std::string(*translateFunction)( T& ) ) + : m_translateFunction( translateFunction ) + {} + + std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { +#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) + try { + if( it == itEnd ) + std::rethrow_exception(std::current_exception()); + else + return (*it)->translate( it+1, itEnd ); + } + catch( T& ex ) { + return m_translateFunction( ex ); + } +#else + return "You should never get here!"; +#endif + } + + protected: + std::string(*m_translateFunction)( T& ); + }; + + public: + template + ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { + getMutableRegistryHub().registerTranslator + ( new ExceptionTranslator( translateFunction ) ); + } + }; + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ + static std::string translatorName( signature ); \ + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ + static std::string translatorName( signature ) + +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +#if defined(CATCH_CONFIG_DISABLE) + #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ + static std::string translatorName( signature ) +#endif + + +// This macro is always prefixed +#if !defined(CATCH_CONFIG_DISABLE) +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) +#else +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) +#endif + + +#endif // CATCH_TRANSLATE_EXCEPTION_HPP_INCLUDED \ No newline at end of file diff --git a/src/catch2/interfaces/catch_interfaces_exception.hpp b/src/catch2/interfaces/catch_interfaces_exception.hpp index 3104ef14..e9fb9ef7 100644 --- a/src/catch2/interfaces/catch_interfaces_exception.hpp +++ b/src/catch2/interfaces/catch_interfaces_exception.hpp @@ -11,12 +11,6 @@ #include #include -#if defined(CATCH_CONFIG_DISABLE) - #define INTERNAL_CATCH_TRANSLATE_EXCEPTION_NO_REG( translatorName, signature) \ - static std::string translatorName( signature ) -#endif - -#include #include #include @@ -37,53 +31,6 @@ namespace Catch { virtual std::string translateActiveException() const = 0; }; - class ExceptionTranslatorRegistrar { - template - class ExceptionTranslator : public IExceptionTranslator { - public: - - ExceptionTranslator( std::string(*translateFunction)( T& ) ) - : m_translateFunction( translateFunction ) - {} - - std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const override { -#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) - try { - if( it == itEnd ) - std::rethrow_exception(std::current_exception()); - else - return (*it)->translate( it+1, itEnd ); - } - catch( T& ex ) { - return m_translateFunction( ex ); - } -#else - return "You should never get here!"; -#endif - } - - protected: - std::string(*m_translateFunction)( T& ); - }; - - public: - template - ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { - getMutableRegistryHub().registerTranslator - ( new ExceptionTranslator( translateFunction ) ); - } - }; -} - -/////////////////////////////////////////////////////////////////////////////// -#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ - static std::string translatorName( signature ); \ - CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ - CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); } \ - CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \ - static std::string translatorName( signature ) - -#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) +} // namespace Catch #endif // TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED diff --git a/tests/SelfTest/UsageTests/Exception.tests.cpp b/tests/SelfTest/UsageTests/Exception.tests.cpp index 8fb3e2f0..f8c628b9 100644 --- a/tests/SelfTest/UsageTests/Exception.tests.cpp +++ b/tests/SelfTest/UsageTests/Exception.tests.cpp @@ -4,6 +4,7 @@ */ #include +#include #include #include