Skip to content

Commit

Permalink
Centralize compiler detection for GCC,
Browse files Browse the repository at this point in the history
clang, MSVC and MINGW32. (see catchorg#2094)
  • Loading branch information
HoseynHeydari committed Apr 14, 2022
1 parent a243cba commit c31170d
Show file tree
Hide file tree
Showing 49 changed files with 192 additions and 118 deletions.
4 changes: 3 additions & 1 deletion examples/231-Cfg-OutputStreams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <sstream>
#include <cstdio>

#include <catch2/internal/catch_compiler_capabilities.hpp>

class out_buff : public std::stringbuf {
std::FILE* m_stream;
public:
Expand All @@ -31,7 +33,7 @@ class out_buff : public std::stringbuf {

out_buff::~out_buff() { pubsync(); }

#if defined(__clang__)
#if defined(CATCH_COMPILER_CLANG)
#pragma clang diagnostic ignored "-Wexit-time-destructors" // static variables in cout/cerr/clog
#endif

Expand Down
8 changes: 5 additions & 3 deletions src/catch2/benchmark/catch_optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#ifndef CATCH_OPTIMIZER_HPP_INCLUDED
#define CATCH_OPTIMIZER_HPP_INCLUDED

#if defined(_MSC_VER)
#include <catch2/internal/catch_compiler_capabilities.hpp>

#if defined(CATCH_COMPILER_MSC)
# include <atomic> // atomic_thread_fence
#endif

Expand All @@ -20,7 +22,7 @@

namespace Catch {
namespace Benchmark {
#if defined(__GNUC__) || defined(__clang__)
#if defined(CATCH_COMPILER_GCC) || defined(CATCH_COMPILER_CLANG)
template <typename T>
inline void keep_memory(T* p) {
asm volatile("" : : "g"(p) : "memory");
Expand All @@ -32,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 <typename T>
Expand Down
5 changes: 3 additions & 2 deletions src/catch2/benchmark/detail/catch_estimate_clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <catch2/benchmark/detail/catch_measure.hpp>
#include <catch2/benchmark/detail/catch_run_for_at_least.hpp>
#include <catch2/benchmark/catch_clock.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>

#include <algorithm>
Expand Down Expand Up @@ -95,12 +96,12 @@ namespace Catch {

template <typename Clock>
Environment<FloatDuration<Clock>> measure_environment() {
#if defined(__clang__)
#if defined(CATCH_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif
static Catch::Detail::unique_ptr<Environment<FloatDuration<Clock>>> env;
#if defined(__clang__)
#if defined(CATCH_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif
if (env) {
Expand Down
6 changes: 4 additions & 2 deletions src/catch2/catch_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

// SPDX-License-Identifier: BSL-1.0
#include <catch2/catch_config.hpp>

#include <catch2/catch_user_config.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_stream.hpp>
#include <catch2/internal/catch_stringref.hpp>
Expand Down Expand Up @@ -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 ) {
Expand Down
4 changes: 3 additions & 1 deletion src/catch2/catch_template_test_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#ifndef CATCH_TEMPLATE_TEST_MACROS_HPP_INCLUDED
#define CATCH_TEMPLATE_TEST_MACROS_HPP_INCLUDED

#include <catch2/internal/catch_compiler_capabilities.hpp>

// We need this suppression to leak, because it took until GCC 10
// for the front end to handle local suppression via _Pragma properly
// inside templates (so `TEMPLATE_TEST_CASE` and co).
// **THIS IS DIFFERENT FOR STANDARD TESTS, WHERE GCC 9 IS SUFFICIENT**
#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && __GNUC__ < 10
#if defined(CATCH_COMPILER_GCC) && __GNUC__ < 10
#pragma GCC diagnostic ignored "-Wparentheses"
#endif

Expand Down
5 changes: 3 additions & 2 deletions src/catch2/catch_test_case_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef CATCH_TEST_CASE_INFO_HPP_INCLUDED
#define CATCH_TEST_CASE_INFO_HPP_INCLUDED

#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_source_line_info.hpp>
#include <catch2/internal/catch_noncopyable.hpp>
#include <catch2/internal/catch_stringref.hpp>
Expand All @@ -18,7 +19,7 @@
#include <string>
#include <vector>

#ifdef __clang__
#ifdef CATCH_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
#endif
Expand Down Expand Up @@ -123,7 +124,7 @@ namespace Catch {
SourceLineInfo const& lineInfo );
}

#ifdef __clang__
#ifdef CATCH_COMPILER_CLANG
#pragma clang diagnostic pop
#endif

Expand Down
6 changes: 4 additions & 2 deletions src/catch2/catch_test_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#ifndef CATCH_TEST_SPEC_HPP_INCLUDED
#define CATCH_TEST_SPEC_HPP_INCLUDED

#ifdef __clang__
#include <catch2/internal/catch_compiler_capabilities.hpp>

#ifdef CATCH_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
#endif
Expand Down Expand Up @@ -81,7 +83,7 @@ namespace Catch {
};
}

#ifdef __clang__
#ifdef CATCH_COMPILER_CLANG
#pragma clang diagnostic pop
#endif

Expand Down
8 changes: 4 additions & 4 deletions src/catch2/catch_tostring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <string_view>
#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
Expand Down Expand Up @@ -625,7 +625,7 @@ struct ratio_string<std::milli> {
static std::string convert(std::chrono::time_point<std::chrono::system_clock, Duration> 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
Expand All @@ -636,7 +636,7 @@ struct ratio_string<std::milli> {
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);
Expand All @@ -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

Expand Down
9 changes: 5 additions & 4 deletions src/catch2/internal/catch_clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#ifndef CATCH_CLARA_HPP_INCLUDED
#define CATCH_CLARA_HPP_INCLUDED

#if defined( __clang__ )
#if defined( CATCH_COMPILER_CLANG )
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wweak-vtables"
# pragma clang diagnostic ignored "-Wshadow"
# pragma clang diagnostic ignored "-Wdeprecated"
#endif

#if defined( __GNUC__ )
#if defined( CATCH_COMPILER_GCC )
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
Expand All @@ -29,6 +29,7 @@
# endif
#endif

#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/internal/catch_noncopyable.hpp>
#include <catch2/internal/catch_void_type.hpp>
Expand Down Expand Up @@ -698,11 +699,11 @@ namespace Catch {
} // namespace Clara
} // namespace Catch

#if defined( __clang__ )
#if defined( CATCH_COMPILER_CLANG )
# pragma clang diagnostic pop
#endif

#if defined( __GNUC__ )
#if defined( CATCH_COMPILER_GCC )
# pragma GCC diagnostic pop
#endif

Expand Down
27 changes: 22 additions & 5 deletions src/catch2/internal/catch_compiler_capabilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,26 @@
// Many features, at point of detection, define an _INTERNAL_ macro, so they
// can be combined, en-mass, with the _NO_ forms later.

#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_platform.hpp>
#include <catch2/catch_user_config.hpp>

#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && !defined(__LCC__)
#define CATCH_COMPILER_GCC
#endif

#if defined(__clang__)
#define CATCH_COMPILER_CLANG
#endif

#if defined(_MSC_VER) && !defined(__clang__)
#define CATCH_COMPILER_MSC
#endif

#if defined(__MINGW32__)
#define CATCH_COMPILER_MINGW32
#endif

#ifdef __cplusplus

# if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
Expand All @@ -41,7 +58,7 @@

// Only GCC compiler should be used in this block, so other compilers trying to
// mask themselves as GCC should be ignored.
#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && !defined(__LCC__)
#if defined(CATCH_COMPILER_GCC)
# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic push" )
# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic pop" )

Expand All @@ -57,7 +74,7 @@

#endif

#if defined(__clang__) && !defined(_MSC_VER)
#if defined(CATCH_COMPILER_CLANG)

# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic push" )
# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic pop" )
Expand Down Expand Up @@ -99,7 +116,7 @@
# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \
_Pragma( "clang diagnostic ignored \"-Wunused-template\"" )

#endif // __clang__
#endif // CATCH_COMPILER_CLANG


////////////////////////////////////////////////////////////////////////////////
Expand All @@ -126,7 +143,7 @@

////////////////////////////////////////////////////////////////////////////////
// Not all Windows environments support SEH properly
#if defined(__MINGW32__)
#if defined(CATCH_COMPILER_MINGW32)
# define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH
#endif

Expand Down Expand Up @@ -155,7 +172,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) )
Expand Down
4 changes: 3 additions & 1 deletion src/catch2/internal/catch_config_uncaught_exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#ifndef CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED
#define CATCH_CONFIG_UNCAUGHT_EXCEPTIONS_HPP_INCLUDED

#if defined(_MSC_VER)
#include <catch2/internal/catch_compiler_capabilities.hpp>

#if defined(CATCH_COMPILER_MSC)
# if _MSC_VER >= 1900 // Visual Studio 2015 or newer
# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
# endif
Expand Down
5 changes: 3 additions & 2 deletions src/catch2/internal/catch_console_colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0
#if defined(__clang__)
#if defined(CATCH_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif


#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_console_colour.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_errno_guard.hpp>
Expand Down Expand Up @@ -277,7 +278,7 @@ namespace Catch {

} // end namespace Catch

#if defined(__clang__)
#if defined(CATCH_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif

2 changes: 1 addition & 1 deletion src/catch2/internal/catch_container_nonmembers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/catch2/internal/catch_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_debugger.hpp>

#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_errno_guard.hpp>
#include <catch2/internal/catch_stream.hpp>
#include <catch2/internal/catch_platform.hpp>
Expand Down Expand Up @@ -99,14 +101,14 @@
return false;
}
} // namespace Catch
#elif defined(_MSC_VER)
#elif defined(CATCH_COMPILER_MSC)
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
namespace Catch {
bool isDebuggerActive() {
return IsDebuggerPresent() != 0;
}
}
#elif defined(__MINGW32__)
#elif defined(CATCH_COMPILER_MINGW32)
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
namespace Catch {
bool isDebuggerActive() {
Expand Down
Loading

0 comments on commit c31170d

Please sign in to comment.