forked from catchorg/Catch2
The old code caused warnings to fire under MSVC, and Clang <3.8.
I could not find a GCC version where it worked, but I assume that it
did at some point.
This new code causes all of MSVC, GCC, Clang, in current versions,
to emit signed/unsigned comparison warning in test like this:
```cpp
TEST_CASE() {
int32_t i = -1;
uint32_t j = 1;
REQUIRE(i != j);
}
```
Where previously only MSVC would emit the warning.
Fixes #1880
145 lines
6.4 KiB
C++
145 lines
6.4 KiB
C++
/*
|
|
* Created by Phil on 18/10/2010.
|
|
* Copyright 2010 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_CAPTURE_HPP_INCLUDED
|
|
#define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
|
|
|
|
#include <catch2/internal/catch_assertionhandler.hpp>
|
|
#include <catch2/interfaces/catch_interfaces_capture.hpp>
|
|
#include <catch2/catch_stringref.hpp>
|
|
|
|
// We need this suppression to leak, because it took until GCC 9
|
|
// for the front end to handle local suppression via _Pragma properly
|
|
#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && __GNUC__ < 9
|
|
#pragma GCC diagnostic ignored "-Wparentheses"
|
|
#endif
|
|
|
|
#if !defined(CATCH_CONFIG_DISABLE)
|
|
|
|
#if !defined(CATCH_CONFIG_DISABLE_STRINGIFICATION)
|
|
#define CATCH_INTERNAL_STRINGIFY(...) #__VA_ARGS__
|
|
#else
|
|
#define CATCH_INTERNAL_STRINGIFY(...) "Disabled by CATCH_CONFIG_DISABLE_STRINGIFICATION"
|
|
#endif
|
|
|
|
#if defined(CATCH_CONFIG_FAST_COMPILE) || defined(CATCH_CONFIG_DISABLE_EXCEPTIONS)
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// Another way to speed-up compilation is to omit local try-catch for REQUIRE*
|
|
// macros.
|
|
#define INTERNAL_CATCH_TRY
|
|
#define INTERNAL_CATCH_CATCH( capturer )
|
|
|
|
#else // CATCH_CONFIG_FAST_COMPILE
|
|
|
|
#define INTERNAL_CATCH_TRY try
|
|
#define INTERNAL_CATCH_CATCH( handler ) catch(...) { handler.handleUnexpectedInflightException(); }
|
|
|
|
#endif
|
|
|
|
#define INTERNAL_CATCH_REACT( handler ) handler.complete();
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define INTERNAL_CATCH_TEST( macroName, resultDisposition, ... ) \
|
|
do { \
|
|
/* The expression should not be evaluated, but warnings should hopefully be checked */ \
|
|
CATCH_INTERNAL_IGNORE_BUT_WARN(__VA_ARGS__); \
|
|
Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \
|
|
INTERNAL_CATCH_TRY { \
|
|
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
|
|
CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \
|
|
catchAssertionHandler.handleExpr( Catch::Decomposer() <= __VA_ARGS__ ); \
|
|
CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
|
|
} INTERNAL_CATCH_CATCH( catchAssertionHandler ) \
|
|
INTERNAL_CATCH_REACT( catchAssertionHandler ) \
|
|
} while( false )
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \
|
|
INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \
|
|
if( Catch::getResultCapture().lastAssertionPassed() )
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \
|
|
INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \
|
|
if( !Catch::getResultCapture().lastAssertionPassed() )
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \
|
|
do { \
|
|
Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition ); \
|
|
try { \
|
|
static_cast<void>(__VA_ARGS__); \
|
|
catchAssertionHandler.handleExceptionNotThrownAsExpected(); \
|
|
} \
|
|
catch( ... ) { \
|
|
catchAssertionHandler.handleUnexpectedInflightException(); \
|
|
} \
|
|
INTERNAL_CATCH_REACT( catchAssertionHandler ) \
|
|
} while( false )
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define INTERNAL_CATCH_THROWS( macroName, resultDisposition, ... ) \
|
|
do { \
|
|
Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__), resultDisposition); \
|
|
if( catchAssertionHandler.allowThrows() ) \
|
|
try { \
|
|
static_cast<void>(__VA_ARGS__); \
|
|
catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
|
|
} \
|
|
catch( ... ) { \
|
|
catchAssertionHandler.handleExceptionThrownAsExpected(); \
|
|
} \
|
|
else \
|
|
catchAssertionHandler.handleThrowingCallSkipped(); \
|
|
INTERNAL_CATCH_REACT( catchAssertionHandler ) \
|
|
} while( false )
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
#define INTERNAL_CATCH_THROWS_AS( macroName, exceptionType, resultDisposition, expr ) \
|
|
do { \
|
|
Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(expr) ", " CATCH_INTERNAL_STRINGIFY(exceptionType), resultDisposition ); \
|
|
if( catchAssertionHandler.allowThrows() ) \
|
|
try { \
|
|
static_cast<void>(expr); \
|
|
catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
|
|
} \
|
|
catch( exceptionType const& ) { \
|
|
catchAssertionHandler.handleExceptionThrownAsExpected(); \
|
|
} \
|
|
catch( ... ) { \
|
|
catchAssertionHandler.handleUnexpectedInflightException(); \
|
|
} \
|
|
else \
|
|
catchAssertionHandler.handleThrowingCallSkipped(); \
|
|
INTERNAL_CATCH_REACT( catchAssertionHandler ) \
|
|
} while( false )
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// Although this is matcher-based, it can be used with just a string
|
|
#define INTERNAL_CATCH_THROWS_STR_MATCHES( macroName, resultDisposition, matcher, ... ) \
|
|
do { \
|
|
Catch::AssertionHandler catchAssertionHandler( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, CATCH_INTERNAL_STRINGIFY(__VA_ARGS__) ", " CATCH_INTERNAL_STRINGIFY(matcher), resultDisposition ); \
|
|
if( catchAssertionHandler.allowThrows() ) \
|
|
try { \
|
|
static_cast<void>(__VA_ARGS__); \
|
|
catchAssertionHandler.handleUnexpectedExceptionNotThrown(); \
|
|
} \
|
|
catch( ... ) { \
|
|
Catch::handleExceptionMatchExpr( catchAssertionHandler, matcher, #matcher##_catch_sr ); \
|
|
} \
|
|
else \
|
|
catchAssertionHandler.handleThrowingCallSkipped(); \
|
|
INTERNAL_CATCH_REACT( catchAssertionHandler ) \
|
|
} while( false )
|
|
|
|
#endif // CATCH_CONFIG_DISABLE
|
|
|
|
#endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
|