Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Redirect std::uncaught_exception to Catch::uncaught_exception

This means that only one place needs to work with warnings from
the deprecation of `std::uncaught_exception()` in C++17.

Closes #1162.
Commit
5201e9256489233e96d578a932e584e6ebe6423f
Author
Josh Lospinoso ·
Committer
Martin Hořeňovský
Parents
5e484862f219

Files · Download .tar.gz

Changes

 CMakeLists.txt                                 |  2 ++
 include/internal/catch_compiler_capabilities.h | 18 ++++++++++++++++++
 include/internal/catch_message.cpp             | 12 ++----------
 include/internal/catch_section.cpp             | 10 ++--------
 include/internal/catch_uncaught_exceptions.cpp | 22 ++++++++++++++++++++++
 include/internal/catch_uncaught_exceptions.h   | 15 +++++++++++++++
 6 files changed, 61 insertions(+), 18 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65f1927b..c99be0eb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -187,6 +187,7 @@ set(INTERNAL_HEADERS
         ${HEADER_DIR}/internal/catch_timer.h
         ${HEADER_DIR}/internal/catch_tostring.h
         ${HEADER_DIR}/internal/catch_totals.h
+        ${HEADER_DIR}/internal/catch_uncaught_exceptions.h
         ${HEADER_DIR}/internal/catch_user_interfaces.h
         ${HEADER_DIR}/internal/catch_version.h
         ${HEADER_DIR}/internal/catch_wildcard_pattern.h
@@ -247,6 +248,7 @@ set(IMPL_SOURCES
         ${HEADER_DIR}/internal/catch_timer.cpp
         ${HEADER_DIR}/internal/catch_tostring.cpp
         ${HEADER_DIR}/internal/catch_totals.cpp
+        ${HEADER_DIR}/internal/catch_uncaught_exceptions.cpp
         ${HEADER_DIR}/internal/catch_version.cpp
         ${HEADER_DIR}/internal/catch_wildcard_pattern.cpp
         ${HEADER_DIR}/internal/catch_xmlwriter.cpp
diff --git a/include/internal/catch_compiler_capabilities.h b/include/internal/catch_compiler_capabilities.h
index 88180af2..bb409825 100644
--- a/include/internal/catch_compiler_capabilities.h
+++ b/include/internal/catch_compiler_capabilities.h
@@ -31,6 +31,14 @@
 #    define CATCH_CPP14_OR_GREATER
 #  endif
 
+#  if __cplusplus >= 201703L
+#    define CATCH_CPP17_OR_GREATER
+#  endif
+
+#endif
+
+#if defined(CATCH_CPP17_OR_GREATER)
+#  define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
 #endif
 
 #ifdef __clang__
@@ -80,6 +88,11 @@
 // Visual C++
 #ifdef _MSC_VER
 
+
+#  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...)
 #  if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
@@ -112,6 +125,11 @@
 #   define CATCH_CONFIG_POSIX_SIGNALS
 #endif
 
+#if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_INTERNAL_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS)
+#  define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
+#endif
+
+
 #if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS)
 #   define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS
 #   define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS
diff --git a/include/internal/catch_message.cpp b/include/internal/catch_message.cpp
index 513c1776..8684970a 100644
--- a/include/internal/catch_message.cpp
+++ b/include/internal/catch_message.cpp
@@ -8,6 +8,7 @@
 
 #include "catch_message.h"
 #include "catch_interfaces_capture.h"
+#include "catch_uncaught_exceptions.h"
 
 namespace Catch {
 
@@ -49,18 +50,9 @@ namespace Catch {
         getResultCapture().pushScopedMessage( m_info );
     }
 
-#if defined(_MSC_VER)
-#pragma warning(push)
-#pragma warning(disable:4996) // std::uncaught_exception is deprecated in C++17
-#endif
     ScopedMessage::~ScopedMessage() {
-        if ( !std::uncaught_exception() ){
+        if ( !uncaught_exceptions() ){
             getResultCapture().popScopedMessage(m_info);
         }
     }
-#if defined(_MSC_VER)
-#pragma warning(pop)
-#endif
-
-
 } // end namespace Catch
diff --git a/include/internal/catch_section.cpp b/include/internal/catch_section.cpp
index c2572d5e..72a57d51 100644
--- a/include/internal/catch_section.cpp
+++ b/include/internal/catch_section.cpp
@@ -8,6 +8,7 @@
 
 #include "catch_section.h"
 #include "catch_capture.hpp"
+#include "catch_uncaught_exceptions.h"
 
 namespace Catch {
 
@@ -18,22 +19,15 @@ namespace Catch {
         m_timer.start();
     }
 
-#if defined(_MSC_VER)
-#pragma warning(push)
-#pragma warning(disable:4996) // std::uncaught_exception is deprecated in C++17
-#endif
     Section::~Section() {
         if( m_sectionIncluded ) {
             SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() );
-            if( std::uncaught_exception() )
+            if( uncaught_exceptions() )
                 getResultCapture().sectionEndedEarly( endInfo );
             else
                 getResultCapture().sectionEnded( endInfo );
         }
     }
-#if defined(_MSC_VER)
-#pragma warning(pop)
-#endif
 
     // This indicates whether the section should be executed or not
     Section::operator bool() const {
diff --git a/include/internal/catch_uncaught_exceptions.cpp b/include/internal/catch_uncaught_exceptions.cpp
new file mode 100644
index 00000000..0e47a04d
--- /dev/null
+++ b/include/internal/catch_uncaught_exceptions.cpp
@@ -0,0 +1,22 @@
+/*
+ *  Created by Josh on 1/2/2018.
+ *  Copyright 2018 Two Blue Cubes Ltd. All rights reserved.
+ *
+ *  Distributed under the Boost Software License, Version 1.0. (See accompanying
+ *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include "catch_compiler_capabilities.h"
+#include "catch_uncaught_exceptions.h"
+#include <exception>
+
+namespace Catch {
+    bool uncaught_exceptions() {
+// https://github.com/catchorg/Catch2/issues/1162
+#if defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS)
+        return std::uncaught_exceptions() > 0;
+#else
+        return std::uncaught_exception();
+#endif
+  }
+} // end namespace Catch
diff --git a/include/internal/catch_uncaught_exceptions.h b/include/internal/catch_uncaught_exceptions.h
new file mode 100644
index 00000000..c68d680c
--- /dev/null
+++ b/include/internal/catch_uncaught_exceptions.h
@@ -0,0 +1,15 @@
+/*
+ *  Created by Josh on 1/2/2018.
+ *  Copyright 2018 Two Blue Cubes Ltd. All rights reserved.
+ *
+ *  Distributed under the Boost Software License, Version 1.0. (See accompanying
+ *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+ */
+#ifndef TWOBLUECUBES_CATCH_UNCAUGHT_EXCEPTIONS_H_INCLUDED
+#define TWOBLUECUBES_CATCH_UNCAUGHT_EXCEPTIONS_H_INCLUDED
+
+namespace Catch {
+    bool uncaught_exceptions();
+} // end namespace Catch
+
+#endif // TWOBLUECUBES_CATCH_UNCAUGHT_EXCEPTIONS_H_INCLUDED