From e6f5e05ebcc46355fe737d8d747f8ed95edc99d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 6 Oct 2020 11:07:53 +0200 Subject: [PATCH] Improve detection of std::uncaught_exceptions support The problem was that Catch2 did not reliably include `` before it checked for the feature test macro for `std::uncaught_exceptions`. To avoid overhead of including `` everywhere, the configuration check was split out into a separate header. Closes #2021 --- .../internal/catch_compiler_capabilities.h | 11 ----- .../catch_config_uncaught_exceptions.hpp | 44 +++++++++++++++++++ .../internal/catch_uncaught_exceptions.cpp | 2 + projects/CMakeLists.txt | 1 + 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 include/internal/catch_config_uncaught_exceptions.hpp diff --git a/include/internal/catch_compiler_capabilities.h b/include/internal/catch_compiler_capabilities.h index 9c6d45c8..93e191c2 100644 --- a/include/internal/catch_compiler_capabilities.h +++ b/include/internal/catch_compiler_capabilities.h @@ -39,10 +39,6 @@ #endif -#if defined(__cpp_lib_uncaught_exceptions) -# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS -#endif - // We have to avoid both ICC and Clang, because they try to mask themselves // as gcc, and we want only GCC in this block #if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) @@ -153,9 +149,6 @@ # define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma( warning(push) ) # define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma( warning(pop) ) -# if _MSC_VER >= 1900 // Visual Studio 2015 or newer -# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS -# endif // Universal Windows platform does not support SEH // Or console colours (or console at all...) @@ -287,10 +280,6 @@ # define CATCH_CONFIG_CPP17_OPTIONAL #endif -#if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) -# define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS -#endif - #if defined(CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_NO_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_CPP17_STRING_VIEW) # define CATCH_CONFIG_CPP17_STRING_VIEW #endif diff --git a/include/internal/catch_config_uncaught_exceptions.hpp b/include/internal/catch_config_uncaught_exceptions.hpp new file mode 100644 index 00000000..eac9078e --- /dev/null +++ b/include/internal/catch_config_uncaught_exceptions.hpp @@ -0,0 +1,44 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +/** \file + * Wrapper for UNCAUGHT_EXCEPTIONS configuration option + * + * For some functionality, Catch2 requires to know whether there is + * an active exception. Because `std::uncaught_exception` is deprecated + * in C++17, we want to use `std::uncaught_exceptions` if possible. + */ + +#ifndef CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP +#define CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP + +#if defined(_MSC_VER) +# if _MSC_VER >= 1900 // Visual Studio 2015 or newer +# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +# endif +#endif + + +#include + +#if defined(__cpp_lib_uncaught_exceptions) \ + && !defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) + +# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +#endif // __cpp_lib_uncaught_exceptions + + +#if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) \ + && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) \ + && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) + +# define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS +#endif + + +#endif // CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP diff --git a/include/internal/catch_uncaught_exceptions.cpp b/include/internal/catch_uncaught_exceptions.cpp index 22b1ed43..cbe7f941 100644 --- a/include/internal/catch_uncaught_exceptions.cpp +++ b/include/internal/catch_uncaught_exceptions.cpp @@ -8,6 +8,8 @@ #include "catch_compiler_capabilities.h" #include "catch_uncaught_exceptions.h" +#include "catch_config_uncaught_exceptions.hpp" + #include namespace Catch { diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index 914af8d6..ece7f0f6 100644 --- a/projects/CMakeLists.txt +++ b/projects/CMakeLists.txt @@ -124,6 +124,7 @@ set(INTERNAL_HEADERS ${HEADER_DIR}/internal/catch_common.h ${HEADER_DIR}/internal/catch_compiler_capabilities.h ${HEADER_DIR}/internal/catch_config.hpp + ${HEADER_DIR}/internal/catch_config_uncaught_exceptions.hpp ${HEADER_DIR}/internal/catch_console_colour.h ${HEADER_DIR}/internal/catch_context.h ${HEADER_DIR}/internal/catch_debug_console.h