From e3aaa40fe7b88bcb72e51b95f0820dd3b79d21a9 Mon Sep 17 00:00:00 2001 From: HoseynHeydari Date: Thu, 14 Apr 2022 17:23:59 +0430 Subject: [PATCH] Add MSVC compiler flag. (see #2094) --- src/catch2/benchmark/catch_optimizer.hpp | 7 ++++--- src/catch2/catch_config.cpp | 6 ++++-- src/catch2/catch_tostring.hpp | 8 ++++---- src/catch2/internal/catch_compiler_capabilities.hpp | 8 ++++++-- .../internal/catch_config_uncaught_exceptions.hpp | 4 +++- src/catch2/internal/catch_container_nonmembers.hpp | 2 +- src/catch2/internal/catch_debugger.cpp | 4 +++- src/catch2/internal/catch_debugger.hpp | 2 +- src/catch2/internal/catch_decomposer.hpp | 4 ++-- src/catch2/internal/catch_output_redirect.cpp | 10 ++++++---- src/catch2/internal/catch_output_redirect.hpp | 2 +- src/catch2/internal/catch_platform.hpp | 4 +++- src/catch2/internal/catch_random_number_generator.cpp | 6 ++++-- src/catch2/reporters/catch_reporter_combined_tu.cpp | 4 +++- src/catch2/reporters/catch_reporter_console.cpp | 4 ++-- src/catch2/reporters/catch_reporter_junit.cpp | 3 ++- src/catch2/reporters/catch_reporter_xml.cpp | 5 +++-- tests/SelfTest/IntrospectiveTests/Details.tests.cpp | 5 +++-- tests/SelfTest/UsageTests/Compilation.tests.cpp | 4 ++-- tests/SelfTest/UsageTests/Condition.tests.cpp | 2 +- tests/SelfTest/UsageTests/Exception.tests.cpp | 2 +- tests/SelfTest/UsageTests/Matchers.tests.cpp | 2 +- tests/SelfTest/UsageTests/Message.tests.cpp | 4 ++-- tests/SelfTest/UsageTests/Tricky.tests.cpp | 2 +- third_party/clara.hpp | 4 +++- 25 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/catch2/benchmark/catch_optimizer.hpp b/src/catch2/benchmark/catch_optimizer.hpp index b327852c..549b3e85 100644 --- a/src/catch2/benchmark/catch_optimizer.hpp +++ b/src/catch2/benchmark/catch_optimizer.hpp @@ -10,11 +10,12 @@ #ifndef CATCH_OPTIMIZER_HPP_INCLUDED #define CATCH_OPTIMIZER_HPP_INCLUDED -#if defined(_MSC_VER) +#include + +#if defined(CATCH_COMPILER_MSC) # include // atomic_thread_fence #endif -#include #include #include @@ -33,7 +34,7 @@ namespace Catch { namespace Detail { inline void optimizer_barrier() { keep_memory(); } } // namespace Detail -#elif defined(_MSC_VER) +#elif defined(CATCH_COMPILER_MSC) #pragma optimize("", off) template diff --git a/src/catch2/catch_config.cpp b/src/catch2/catch_config.cpp index 79ec33f8..852f0e46 100644 --- a/src/catch2/catch_config.cpp +++ b/src/catch2/catch_config.cpp @@ -6,7 +6,9 @@ // SPDX-License-Identifier: BSL-1.0 #include + #include +#include #include #include #include @@ -68,14 +70,14 @@ namespace Catch { // during test, Bazel will not generate a default XML output. // This allows the XML output file to contain higher level of detail // than what is possible otherwise. -# if defined( _MSC_VER ) +# if defined( CATCH_COMPILER_MSC ) // On Windows getenv throws a warning as there is no input validation, // since the key is hardcoded, this should not be an issue. # pragma warning( push ) # pragma warning( disable : 4996 ) # endif const auto bazelOutputFilePtr = std::getenv( "XML_OUTPUT_FILE" ); -# if defined( _MSC_VER ) +# if defined( CATCH_COMPILER_MSC ) # pragma warning( pop ) # endif if ( bazelOutputFilePtr != nullptr ) { diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index 9182a119..b6d416c1 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -25,7 +25,7 @@ #include #endif -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(push) #pragma warning(disable:4180) // We attempt to stream a function (address) by const&, which MSVC complains about but is harmless #endif @@ -625,7 +625,7 @@ struct ratio_string { static std::string convert(std::chrono::time_point const& time_point) { auto converted = std::chrono::system_clock::to_time_t(time_point); -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC std::tm timeInfo = {}; gmtime_s(&timeInfo, &converted); #else @@ -636,7 +636,7 @@ struct ratio_string { char timeStamp[timeStampSize]; const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); #else std::strftime(timeStamp, timeStampSize, fmt, timeInfo); @@ -660,7 +660,7 @@ namespace Catch { \ #define CATCH_REGISTER_ENUM( enumName, ... ) INTERNAL_CATCH_REGISTER_ENUM( enumName, __VA_ARGS__ ) -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(pop) #endif diff --git a/src/catch2/internal/catch_compiler_capabilities.hpp b/src/catch2/internal/catch_compiler_capabilities.hpp index c98a2791..91c28baa 100644 --- a/src/catch2/internal/catch_compiler_capabilities.hpp +++ b/src/catch2/internal/catch_compiler_capabilities.hpp @@ -32,10 +32,14 @@ #define CATCH_COMPILER_GCC #endif -#if defined(__clang__) && !defined(_MSC_VER) +#if defined(__clang__) #define CATCH_COMPILER_CLANG #endif +#if defined(_MSC_VER) && !defined(__clang__) +#define CATCH_COMPILER_MSC +#endif + #ifdef __cplusplus # if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) @@ -164,7 +168,7 @@ //////////////////////////////////////////////////////////////////////////////// // Visual C++ -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) # define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma( warning(push) ) # define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma( warning(pop) ) diff --git a/src/catch2/internal/catch_config_uncaught_exceptions.hpp b/src/catch2/internal/catch_config_uncaught_exceptions.hpp index c853e673..beabd31d 100644 --- a/src/catch2/internal/catch_config_uncaught_exceptions.hpp +++ b/src/catch2/internal/catch_config_uncaught_exceptions.hpp @@ -17,7 +17,9 @@ #ifndef CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED #define CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED -#if defined(_MSC_VER) +#include + +#if defined(CATCH_COMPILER_MSC) # if _MSC_VER >= 1900 // Visual Studio 2015 or newer # define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS # endif diff --git a/src/catch2/internal/catch_container_nonmembers.hpp b/src/catch2/internal/catch_container_nonmembers.hpp index d6f10eb5..b828415c 100644 --- a/src/catch2/internal/catch_container_nonmembers.hpp +++ b/src/catch2/internal/catch_container_nonmembers.hpp @@ -17,7 +17,7 @@ // for C++14 or C++ libraries with incomplete support. // We also have to handle that MSVC std lib will happily provide these // under older standards. -#if defined(CATCH_CPP17_OR_GREATER) || defined(_MSC_VER) +#if defined(CATCH_CPP17_OR_GREATER) || defined(CATCH_COMPILER_MSC) // We are already using this header either way, so there shouldn't // be much additional overhead in including it to get the feature diff --git a/src/catch2/internal/catch_debugger.cpp b/src/catch2/internal/catch_debugger.cpp index 3c80173a..a07cb788 100644 --- a/src/catch2/internal/catch_debugger.cpp +++ b/src/catch2/internal/catch_debugger.cpp @@ -6,6 +6,8 @@ // SPDX-License-Identifier: BSL-1.0 #include + +#include #include #include #include @@ -99,7 +101,7 @@ return false; } } // namespace Catch -#elif defined(_MSC_VER) +#elif defined(CATCH_COMPILER_MSC) extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); namespace Catch { bool isDebuggerActive() { diff --git a/src/catch2/internal/catch_debugger.hpp b/src/catch2/internal/catch_debugger.hpp index 9bb80194..b16c14ba 100644 --- a/src/catch2/internal/catch_debugger.hpp +++ b/src/catch2/internal/catch_debugger.hpp @@ -47,7 +47,7 @@ namespace Catch { #define CATCH_TRAP() raise(SIGTRAP) #endif -#elif defined(_MSC_VER) +#elif defined(CATCH_COMPILER_MSC) #define CATCH_TRAP() __debugbreak() #elif defined(__MINGW32__) extern "C" __declspec(dllimport) void __stdcall DebugBreak(); diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index 8d64eded..68390e72 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -15,7 +15,7 @@ #include -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(push) #pragma warning(disable:4389) // '==' : signed/unsigned mismatch #pragma warning(disable:4018) // more "signed/unsigned mismatch" @@ -257,7 +257,7 @@ namespace Catch { } // end namespace Catch -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(pop) #endif #ifdef CATCH_COMPILER_CLANG diff --git a/src/catch2/internal/catch_output_redirect.cpp b/src/catch2/internal/catch_output_redirect.cpp index 49cfaa0d..235cc07a 100644 --- a/src/catch2/internal/catch_output_redirect.cpp +++ b/src/catch2/internal/catch_output_redirect.cpp @@ -6,6 +6,8 @@ // SPDX-License-Identifier: BSL-1.0 #include + +#include #include #include @@ -13,7 +15,7 @@ #include #if defined(CATCH_CONFIG_NEW_CAPTURE) - #if defined(_MSC_VER) + #if defined(CATCH_COMPILER_MSC) #include //_dup and _dup2 #define dup _dup #define dup2 _dup2 @@ -59,7 +61,7 @@ namespace Catch { #if defined(CATCH_CONFIG_NEW_CAPTURE) -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) TempFile::TempFile() { if (tmpnam_s(m_buffer)) { CATCH_RUNTIME_ERROR("Could not get a temp filename"); @@ -87,7 +89,7 @@ namespace Catch { std::fclose(m_file); // We manually create the file on Windows only, on Linux // it will be autodeleted -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) std::remove(m_buffer); #endif } @@ -137,7 +139,7 @@ namespace Catch { } // namespace Catch #if defined(CATCH_CONFIG_NEW_CAPTURE) - #if defined(_MSC_VER) + #if defined(CATCH_COMPILER_MSC) #undef dup #undef dup2 #undef fileno diff --git a/src/catch2/internal/catch_output_redirect.hpp b/src/catch2/internal/catch_output_redirect.hpp index fd7234ef..1ccce57e 100644 --- a/src/catch2/internal/catch_output_redirect.hpp +++ b/src/catch2/internal/catch_output_redirect.hpp @@ -85,7 +85,7 @@ namespace Catch { private: std::FILE* m_file = nullptr; - #if defined(_MSC_VER) + #if defined(CATCH_COMPILER_MSC) char m_buffer[L_tmpnam] = { 0 }; #endif }; diff --git a/src/catch2/internal/catch_platform.hpp b/src/catch2/internal/catch_platform.hpp index 8ecf01ad..3416123a 100644 --- a/src/catch2/internal/catch_platform.hpp +++ b/src/catch2/internal/catch_platform.hpp @@ -8,6 +8,8 @@ #ifndef CATCH_PLATFORM_HPP_INCLUDED #define CATCH_PLATFORM_HPP_INCLUDED +#include + // See e.g.: // https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/TargetConditionals.h.auto.html #ifdef __APPLE__ @@ -22,7 +24,7 @@ #elif defined(linux) || defined(__linux) || defined(__linux__) # define CATCH_PLATFORM_LINUX -#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) +#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(CATCH_COMPILER_MSC) || defined(__MINGW32__) # define CATCH_PLATFORM_WINDOWS #endif diff --git a/src/catch2/internal/catch_random_number_generator.cpp b/src/catch2/internal/catch_random_number_generator.cpp index f71c2323..e154446d 100644 --- a/src/catch2/internal/catch_random_number_generator.cpp +++ b/src/catch2/internal/catch_random_number_generator.cpp @@ -7,11 +7,13 @@ // SPDX-License-Identifier: BSL-1.0 #include +#include + namespace Catch { namespace { -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(push) #pragma warning(disable:4146) // we negate uint32 during the rotate #endif @@ -22,7 +24,7 @@ namespace { return (val >> count) | (val << (-count & mask)); } -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(pop) #endif diff --git a/src/catch2/reporters/catch_reporter_combined_tu.cpp b/src/catch2/reporters/catch_reporter_combined_tu.cpp index 23c738cd..fdd10649 100644 --- a/src/catch2/reporters/catch_reporter_combined_tu.cpp +++ b/src/catch2/reporters/catch_reporter_combined_tu.cpp @@ -17,8 +17,10 @@ * of Catch2 has its own combined TU like this. */ + #include #include +#include #include #include #include @@ -68,7 +70,7 @@ namespace Catch { // Save previous errno, to prevent sprintf from overwriting it ErrnoGuard guard; -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC size_t printedLength = static_cast( sprintf_s( buffer, "%.3f", duration ) ); #else diff --git a/src/catch2/reporters/catch_reporter_console.cpp b/src/catch2/reporters/catch_reporter_console.cpp index f0b7751e..20d7c145 100644 --- a/src/catch2/reporters/catch_reporter_console.cpp +++ b/src/catch2/reporters/catch_reporter_console.cpp @@ -23,7 +23,7 @@ #include -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(push) #pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch // Note that 4062 (not all labels are handled and default is missing) is enabled @@ -711,7 +711,7 @@ void ConsoleReporter::printTestFilters() { } // end namespace Catch -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(pop) #endif diff --git a/src/catch2/reporters/catch_reporter_junit.cpp b/src/catch2/reporters/catch_reporter_junit.cpp index 37546cd7..0d71da68 100644 --- a/src/catch2/reporters/catch_reporter_junit.cpp +++ b/src/catch2/reporters/catch_reporter_junit.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -28,7 +29,7 @@ namespace Catch { std::time(&rawtime); std::tm timeInfo = {}; -#if defined (_MSC_VER) || defined (__MINGW32__) +#if defined (CATCH_COMPILER_MSC) || defined (__MINGW32__) gmtime_s(&timeInfo, &rawtime); #else gmtime_r(&rawtime, &timeInfo); diff --git a/src/catch2/reporters/catch_reporter_xml.cpp b/src/catch2/reporters/catch_reporter_xml.cpp index b34d7679..e34af221 100644 --- a/src/catch2/reporters/catch_reporter_xml.cpp +++ b/src/catch2/reporters/catch_reporter_xml.cpp @@ -10,12 +10,13 @@ #include #include #include +#include #include #include #include #include -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(push) #pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch // Note that 4062 (not all labels are handled @@ -300,6 +301,6 @@ namespace Catch { } // end namespace Catch -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(pop) #endif diff --git a/tests/SelfTest/IntrospectiveTests/Details.tests.cpp b/tests/SelfTest/IntrospectiveTests/Details.tests.cpp index 987910a9..d2a290e2 100644 --- a/tests/SelfTest/IntrospectiveTests/Details.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Details.tests.cpp @@ -6,12 +6,13 @@ // SPDX-License-Identifier: BSL-1.0 +#include #include #include #include #include -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(push) #pragma warning(disable:4702) // unreachable code in the macro expansions #endif @@ -24,7 +25,7 @@ TEST_CASE("Check that our error handling macros throw the right exceptions", "[! REQUIRE_NOTHROW([](){CATCH_ENFORCE(true, "");}()); } -#if defined(_MSC_VER) +#if defined(CATCH_COMPILER_MSC) #pragma warning(pop) // unreachable code in the macro expansions #endif diff --git a/tests/SelfTest/UsageTests/Compilation.tests.cpp b/tests/SelfTest/UsageTests/Compilation.tests.cpp index 2c049a32..5b57e098 100644 --- a/tests/SelfTest/UsageTests/Compilation.tests.cpp +++ b/tests/SelfTest/UsageTests/Compilation.tests.cpp @@ -197,7 +197,7 @@ TEST_CASE("#1548", "[compilation]") { inline static void synchronizing_callback( void * ) { } } -#if defined (_MSC_VER) +#if defined (CATCH_COMPILER_MSC) #pragma warning(push) // The function pointer comparison below triggers warning because of // calling conventions @@ -207,7 +207,7 @@ TEST_CASE("#1548", "[compilation]") { TestClass test; REQUIRE(utility::synchronizing_callback != test.testMethod_uponComplete_arg); } -#if defined (_MSC_VER) +#if defined (CATCH_COMPILER_MSC) #pragma warning(pop) #endif diff --git a/tests/SelfTest/UsageTests/Condition.tests.cpp b/tests/SelfTest/UsageTests/Condition.tests.cpp index da7ac0e7..edd122d4 100644 --- a/tests/SelfTest/UsageTests/Condition.tests.cpp +++ b/tests/SelfTest/UsageTests/Condition.tests.cpp @@ -230,7 +230,7 @@ TEST_CASE( "Comparisons with int literals don't warn when mixing signed/ unsigne #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" #endif -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(disable:4389) // '==' : signed/unsigned mismatch #endif diff --git a/tests/SelfTest/UsageTests/Exception.tests.cpp b/tests/SelfTest/UsageTests/Exception.tests.cpp index 6e0d3221..9e8b7314 100644 --- a/tests/SelfTest/UsageTests/Exception.tests.cpp +++ b/tests/SelfTest/UsageTests/Exception.tests.cpp @@ -14,7 +14,7 @@ #include #include -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(disable:4702) // Unreachable code -- unconditional throws and so on #endif #ifdef CATCH_COMPILER_CLANG diff --git a/tests/SelfTest/UsageTests/Matchers.tests.cpp b/tests/SelfTest/UsageTests/Matchers.tests.cpp index cee4efe7..f70e5602 100644 --- a/tests/SelfTest/UsageTests/Matchers.tests.cpp +++ b/tests/SelfTest/UsageTests/Matchers.tests.cpp @@ -41,7 +41,7 @@ namespace { static bool alwaysTrue( int ) { return true; } static bool alwaysFalse( int ) { return false; } -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC # pragma warning( disable : 4702 ) // Unreachable code -- MSVC 19 (VS 2015) // sees right through the indirection #endif diff --git a/tests/SelfTest/UsageTests/Message.tests.cpp b/tests/SelfTest/UsageTests/Message.tests.cpp index 0612f380..5a7dd355 100644 --- a/tests/SelfTest/UsageTests/Message.tests.cpp +++ b/tests/SelfTest/UsageTests/Message.tests.cpp @@ -221,7 +221,7 @@ TEST_CASE( "CAPTURE can deal with complex expressions", "[messages][capture]" ) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-value" // All the comma operators are side-effect free #endif -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(push) #pragma warning(disable:4709) // comma in indexing operator #endif @@ -283,6 +283,6 @@ TEST_CASE("CAPTURE parses string and character constants", "[messages][capture]" #ifdef CATCH_COMPILER_GCC #pragma GCC diagnostic pop #endif -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning(pop) #endif diff --git a/tests/SelfTest/UsageTests/Tricky.tests.cpp b/tests/SelfTest/UsageTests/Tricky.tests.cpp index c304e617..5108c6a5 100644 --- a/tests/SelfTest/UsageTests/Tricky.tests.cpp +++ b/tests/SelfTest/UsageTests/Tricky.tests.cpp @@ -12,7 +12,7 @@ #pragma clang diagnostic ignored "-Wpadded" #endif -#ifdef _MSC_VER +#ifdef CATCH_COMPILER_MSC #pragma warning (disable : 4702) // Disable unreachable code warning for the last test // that is triggered when compiling as Win32|Release #endif diff --git a/third_party/clara.hpp b/third_party/clara.hpp index eb4c7275..6f661355 100644 --- a/third_party/clara.hpp +++ b/third_party/clara.hpp @@ -47,6 +47,8 @@ #include #include +#include + #ifndef CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH #define CLARA_TEXTFLOW_CONFIG_CONSOLE_WIDTH 80 #endif @@ -379,7 +381,7 @@ namespace clara { namespace TextFlow { #include #include -#if !defined(CLARA_PLATFORM_WINDOWS) && ( defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) ) +#if !defined(CLARA_PLATFORM_WINDOWS) && ( defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(CATCH_COMPILER_MSC) ) #define CLARA_PLATFORM_WINDOWS #endif