From 1d269211bd603d4a50c44b741617db754b790e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 28 Dec 2020 20:29:05 +0100 Subject: [PATCH] Split CATCH_CONFIG_WCHAR into its own header Part of #2041 --- src/CMakeLists.txt | 1 + src/catch2/catch_all.hpp | 1 + src/catch2/catch_session.hpp | 2 +- src/catch2/catch_tostring.hpp | 5 +-- .../internal/catch_compiler_capabilities.hpp | 9 ----- src/catch2/internal/catch_config_wchar.hpp | 33 +++++++++++++++++++ src/catch2/internal/catch_main.cpp | 1 + tests/SelfTest/UsageTests/Misc.tests.cpp | 5 +-- 8 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/catch2/internal/catch_config_wchar.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab764872..eced39f5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -53,6 +53,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/catch_config.hpp ${SOURCES_DIR}/internal/catch_config_android_logwrite.hpp ${SOURCES_DIR}/internal/catch_config_uncaught_exceptions.hpp + ${SOURCES_DIR}/internal/catch_config_wchar.hpp ${SOURCES_DIR}/internal/catch_console_colour.hpp ${SOURCES_DIR}/internal/catch_context.hpp ${SOURCES_DIR}/internal/catch_debug_console.hpp diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp index 770c7244..72fa940c 100644 --- a/src/catch2/catch_all.hpp +++ b/src/catch2/catch_all.hpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include diff --git a/src/catch2/catch_session.hpp b/src/catch2/catch_session.hpp index 49dafc45..b2d3eb0b 100644 --- a/src/catch2/catch_session.hpp +++ b/src/catch2/catch_session.hpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include namespace Catch { diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index 0462bc6b..d0f799a0 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -162,7 +163,7 @@ namespace Catch { static std::string convert(char * str); }; -#ifdef CATCH_CONFIG_WCHAR +#if defined(CATCH_CONFIG_WCHAR) template<> struct StringMaker { static std::string convert(const std::wstring& wstr); @@ -183,7 +184,7 @@ namespace Catch { struct StringMaker { static std::string convert(wchar_t * str); }; -#endif +#endif // CATCH_CONFIG_WCHAR // TBD: Should we use `strnlen` to ensure that we don't go out of the buffer, // while keeping string semantics? diff --git a/src/catch2/internal/catch_compiler_capabilities.hpp b/src/catch2/internal/catch_compiler_capabilities.hpp index e8de815b..b98921ae 100644 --- a/src/catch2/internal/catch_compiler_capabilities.hpp +++ b/src/catch2/internal/catch_compiler_capabilities.hpp @@ -186,11 +186,6 @@ # define CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED #endif -//////////////////////////////////////////////////////////////////////////////// -// DJGPP -#ifdef __DJGPP__ -# define CATCH_INTERNAL_CONFIG_NO_WCHAR -#endif // __DJGPP__ //////////////////////////////////////////////////////////////////////////////// // Embarcadero C++Build @@ -273,10 +268,6 @@ #if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) # define CATCH_CONFIG_POSIX_SIGNALS #endif -// This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. -#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) -# define CATCH_CONFIG_WCHAR -#endif #if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) # define CATCH_CONFIG_CPP11_TO_STRING diff --git a/src/catch2/internal/catch_config_wchar.hpp b/src/catch2/internal/catch_config_wchar.hpp new file mode 100644 index 00000000..fb64daf2 --- /dev/null +++ b/src/catch2/internal/catch_config_wchar.hpp @@ -0,0 +1,33 @@ + +// 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 the WCHAR configuration option + * + * We want to support platforms that do not provide `wchar_t`, so we + * sometimes have to disable providing wchar_t overloads through Catch2, + * e.g. the StringMaker specialization for `std::wstring`. + */ + +#ifndef CATCH_CONFIG_WCHAR_HPP_INCLUDED +#define CATCH_CONFIG_WCHAR_HPP_INCLUDED + +// We assume that WCHAR should be enabled by default, and only disabled +// for a shortlist (so far only DJGPP) of compilers. + +#if defined(__DJGPP__) +# define CATCH_INTERNAL_CONFIG_NO_WCHAR +#endif // __DJGPP__ + +#if !defined( CATCH_INTERNAL_CONFIG_NO_WCHAR ) && \ + !defined( CATCH_CONFIG_NO_WCHAR ) && \ + !defined( CATCH_CONFIG_WCHAR ) +# define CATCH_CONFIG_WCHAR +#endif + +#endif // CATCH_CONFIG_WCHAR_HPP_INCLUDED diff --git a/src/catch2/internal/catch_main.cpp b/src/catch2/internal/catch_main.cpp index b80bae0b..940ac3bc 100644 --- a/src/catch2/internal/catch_main.cpp +++ b/src/catch2/internal/catch_main.cpp @@ -7,6 +7,7 @@ // SPDX-License-Identifier: BSL-1.0 #include #include +#include #include #include diff --git a/tests/SelfTest/UsageTests/Misc.tests.cpp b/tests/SelfTest/UsageTests/Misc.tests.cpp index aa8829d9..277723a8 100644 --- a/tests/SelfTest/UsageTests/Misc.tests.cpp +++ b/tests/SelfTest/UsageTests/Misc.tests.cpp @@ -5,6 +5,7 @@ #include #include +#include #ifdef __clang__ # pragma clang diagnostic ignored "-Wc++98-compat" @@ -423,7 +424,7 @@ TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) { } -#ifdef CATCH_CONFIG_WCHAR +#if defined(CATCH_CONFIG_WCHAR) TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) { const wchar_t * const s = L"wide load"; std::string result = ::Catch::Detail::stringify( s ); @@ -447,7 +448,7 @@ TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) { std::string result = ::Catch::Detail::stringify( s ); CHECK( result == "\"wide load\"" ); } -#endif +#endif // CATCH_CONFIG_WCHAR TEST_CASE( "long long" ) { long long l = std::numeric_limits::max();