Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralize compiler detection for GCC, clang, MSVC and MINGW32. #2410

Draft
wants to merge 9 commits into
base: devel
Choose a base branch
from
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_detections.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_detections.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_detections.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
1 change: 1 addition & 0 deletions src/catch2/catch_all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <catch2/internal/catch_clara.hpp>
#include <catch2/internal/catch_commandline.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_compiler_detections.hpp>
#include <catch2/internal/catch_config_android_logwrite.hpp>
#include <catch2/internal/catch_config_counter.hpp>
#include <catch2/internal/catch_config_uncaught_exceptions.hpp>
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_detections.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_stringref.hpp>
#include <catch2/internal/catch_string_manip.hpp>
Expand Down Expand Up @@ -65,14 +67,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_detections.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_detections.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_detections.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
9 changes: 5 additions & 4 deletions src/catch2/catch_tostring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <string.h>

#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_compiler_detections.hpp>
#include <catch2/internal/catch_config_wchar.hpp>
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <catch2/internal/catch_void_type.hpp>
Expand All @@ -25,7 +26,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 +626,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 +637,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 +661,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
10 changes: 6 additions & 4 deletions src/catch2/internal/catch_clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
#ifndef CATCH_CLARA_HPP_INCLUDED
#define CATCH_CLARA_HPP_INCLUDED

#if defined( __clang__ )
#include <catch2/internal/catch_compiler_detections.hpp>

#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 Down Expand Up @@ -697,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
3 changes: 0 additions & 3 deletions src/catch2/internal/catch_combined_tu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ namespace Catch {
//////////////////////////////////////////

#include <catch2/internal/catch_polyfills.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <cmath>

namespace Catch {
Expand Down Expand Up @@ -74,7 +73,6 @@ namespace Catch {
// vvv formerly catch_uncaught_exceptions.cpp vvv //
////////////////////////////////////////////////////

#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_uncaught_exceptions.hpp>
#include <catch2/internal/catch_config_uncaught_exceptions.hpp>

Expand Down Expand Up @@ -130,7 +128,6 @@ namespace Catch {
// vvv formerly catch_startup_exception_registry.cpp vvv //
///////////////////////////////////////////////////////////
#include <catch2/internal/catch_startup_exception_registry.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>

namespace Catch {
#if !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
Expand Down
11 changes: 6 additions & 5 deletions src/catch2/internal/catch_compiler_capabilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// 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_detections.hpp>
#include <catch2/internal/catch_platform.hpp>
#include <catch2/catch_user_config.hpp>

Expand All @@ -41,7 +42,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 +58,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 +100,7 @@
# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \
_Pragma( "clang diagnostic ignored \"-Wunused-template\"" )

#endif // __clang__
#endif // CATCH_COMPILER_CLANG


////////////////////////////////////////////////////////////////////////////////
Expand All @@ -126,7 +127,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 +156,7 @@

////////////////////////////////////////////////////////////////////////////////
// Visual C++
#if defined(_MSC_VER)
#if defined(CATCH_COMPILER_MSVC)

# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma( warning(push) )
# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma( warning(pop) )
Expand Down
26 changes: 26 additions & 0 deletions src/catch2/internal/catch_compiler_detections.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// 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)

#ifndef CATCH_COMPILER_DETECTIONS_HPP_INCLUDED
#define CATCH_COMPILER_DETECTIONS_HPP_INCLUDED

#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && !defined(__LCC__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach isn't going to scale well, as pretty much every new or obscure compiler masquerades for GCC, so this line is going to keep getting longer and longer.

Instead, let's have a macro CATCH_COMPILER_DETECTED, that get's set when a compiler was picked. This would simplify GCC (and also clang) to something like

#if defined(__GNUC__) and !defined(CATCH_COMPILER_DETECTED)
#define CATCH_COMPILER_GCC
#endif

#define CATCH_COMPILER_GCC
#endif

#if defined(__clang__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely doesn't filter out all the different compilers that like to pretend they are Clang, e.g. IBM XL. See above for a good way to fix this.

#define CATCH_COMPILER_CLANG
#endif

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

#if defined(__MINGW32__)
#define CATCH_COMPILER_MINGW32
#endif

#endif // CATCH_COMPILER_DETECTIONS_HPP_INCLUDED
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_detections.hpp>

#if defined(CATCH_COMPILER_MSVC)
# if _MSC_VER >= 1900 // Visual Studio 2015 or newer
# define CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
# endif
Expand Down
8 changes: 5 additions & 3 deletions src/catch2/internal/catch_console_colour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

#include <catch2/internal/catch_console_colour.hpp>
#include <catch2/internal/catch_compiler_detections.hpp>

// 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_console_colour.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_errno_guard.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
Expand Down Expand Up @@ -280,7 +282,7 @@ namespace Catch {

} // end namespace Catch

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

Loading