Skip to content

Commit

Permalink
Sprinkle some constexpr around to make Jason happy
Browse files Browse the repository at this point in the history
Most of these will not matter in practice due to C++14 imposing
significant limitations on what else we can make constexpr, and we cannot
have references outliving the constexpr context either way.
  • Loading branch information
horenmar committed Sep 13, 2024
1 parent e97ebe6 commit 18df97d
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 122 deletions.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ set(IMPL_SOURCES
${SOURCES_DIR}/internal/catch_random_seed_generation.cpp
${SOURCES_DIR}/internal/catch_reporter_registry.cpp
${SOURCES_DIR}/internal/catch_reporter_spec_parser.cpp
${SOURCES_DIR}/internal/catch_result_type.cpp
${SOURCES_DIR}/internal/catch_reusable_string_stream.cpp
${SOURCES_DIR}/internal/catch_run_context.cpp
${SOURCES_DIR}/internal/catch_section.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/benchmark/catch_outlier_classification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Catch {
int high_mild = 0; // 1.5 to 3 times IQR above Q3
int high_severe = 0; // more than 3 times IQR above Q3

int total() const {
constexpr int total() const {
return low_severe + low_mild + high_mild + high_severe;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/catch_assertion_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Catch {

AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const & _lazyExpression):
AssertionResultData::AssertionResultData(ResultWas::OfType _resultType, LazyExpression const& _lazyExpression):
lazyExpression(_lazyExpression),
resultType(_resultType) {}

Expand Down
16 changes: 6 additions & 10 deletions src/catch2/catch_test_case_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ namespace Catch {
static_assert(sizeof(TestCaseProperties) == sizeof(TCP_underlying_type),
"The size of the TestCaseProperties is different from the assumed size");

TestCaseProperties operator|(TestCaseProperties lhs, TestCaseProperties rhs) {
constexpr TestCaseProperties operator|(TestCaseProperties lhs, TestCaseProperties rhs) {
return static_cast<TestCaseProperties>(
static_cast<TCP_underlying_type>(lhs) | static_cast<TCP_underlying_type>(rhs)
);
}

TestCaseProperties& operator|=(TestCaseProperties& lhs, TestCaseProperties rhs) {
constexpr TestCaseProperties& operator|=(TestCaseProperties& lhs, TestCaseProperties rhs) {
lhs = static_cast<TestCaseProperties>(
static_cast<TCP_underlying_type>(lhs) | static_cast<TCP_underlying_type>(rhs)
);
return lhs;
}

TestCaseProperties operator&(TestCaseProperties lhs, TestCaseProperties rhs) {
constexpr TestCaseProperties operator&(TestCaseProperties lhs, TestCaseProperties rhs) {
return static_cast<TestCaseProperties>(
static_cast<TCP_underlying_type>(lhs) & static_cast<TCP_underlying_type>(rhs)
);
}

bool applies(TestCaseProperties tcp) {
constexpr bool applies(TestCaseProperties tcp) {
static_assert(static_cast<TCP_underlying_type>(TestCaseProperties::None) == 0,
"TestCaseProperties::None must be equal to 0");
return tcp != TestCaseProperties::None;
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace Catch {
return "Anonymous test case " + std::to_string(++counter);
}

StringRef extractFilenamePart(StringRef filename) {
constexpr StringRef extractFilenamePart(StringRef filename) {
size_t lastDot = filename.size();
while (lastDot > 0 && filename[lastDot - 1] != '.') {
--lastDot;
Expand All @@ -98,7 +98,7 @@ namespace Catch {
}

// Returns the upper bound on size of extra tags ([#file]+[.])
size_t sizeOfExtraTags(StringRef filepath) {
constexpr size_t sizeOfExtraTags(StringRef filepath) {
// [.] is 3, [#] is another 3
const size_t extras = 3 + 3;
return extractFilenamePart(filepath).size() + extras;
Expand Down Expand Up @@ -259,8 +259,4 @@ namespace Catch {
return lhs.tags < rhs.tags;
}

TestCaseInfo const& TestCaseHandle::getTestCaseInfo() const {
return *m_info;
}

} // end namespace Catch
6 changes: 4 additions & 2 deletions src/catch2/catch_test_case_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace Catch {
TestCaseInfo* m_info;
ITestInvoker* m_invoker;
public:
TestCaseHandle(TestCaseInfo* info, ITestInvoker* invoker) :
constexpr TestCaseHandle(TestCaseInfo* info, ITestInvoker* invoker) :
m_info(info), m_invoker(invoker) {}

void prepareTestCase() const {
Expand All @@ -124,7 +124,9 @@ namespace Catch {
m_invoker->invoke();
}

TestCaseInfo const& getTestCaseInfo() const;
constexpr TestCaseInfo const& getTestCaseInfo() const {
return *m_info;
}
};

Detail::unique_ptr<TestCaseInfo>
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/catch_translate_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Catch {
class ExceptionTranslator : public IExceptionTranslator {
public:

ExceptionTranslator( std::string(*translateFunction)( T const& ) )
constexpr ExceptionTranslator( std::string(*translateFunction)( T const& ) )
: m_translateFunction( translateFunction )
{}

Expand Down
2 changes: 1 addition & 1 deletion src/catch2/internal/catch_assertion_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Catch {


template<typename T>
void handleExpr( ExprLhs<T> const& expr ) {
constexpr void handleExpr( ExprLhs<T> const& expr ) {
handleExpr( expr.makeUnaryExpr() );
}
void handleExpr( ITransientExpression const& expr );
Expand Down
6 changes: 0 additions & 6 deletions src/catch2/internal/catch_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ namespace Catch {
return *Context::currentContext;
}

void Context::setResultCapture( IResultCapture* resultCapture ) {
m_resultCapture = resultCapture;
}

void Context::setConfig( IConfig const* config ) { m_config = config; }

SimplePcg32& sharedRng() {
static SimplePcg32 s_rng;
return s_rng;
Expand Down
13 changes: 9 additions & 4 deletions src/catch2/internal/catch_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ namespace Catch {
friend void cleanUpContext();

public:
IResultCapture* getResultCapture() const { return m_resultCapture; }
IConfig const* getConfig() const { return m_config; }
void setResultCapture( IResultCapture* resultCapture );
void setConfig( IConfig const* config );
constexpr IResultCapture* getResultCapture() const {
return m_resultCapture;
}
constexpr IConfig const* getConfig() const { return m_config; }
constexpr void setResultCapture( IResultCapture* resultCapture ) {
m_resultCapture = resultCapture;
}
constexpr void setConfig( IConfig const* config ) { m_config = config; }

};

Context& getCurrentMutableContext();
Expand Down
11 changes: 5 additions & 6 deletions src/catch2/internal/catch_decomposer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ namespace Catch {
bool m_isBinaryExpression;
bool m_result;

protected:
~ITransientExpression() = default;

public:
constexpr auto isBinaryExpression() const -> bool { return m_isBinaryExpression; }
constexpr auto getResult() const -> bool { return m_result; }
Expand All @@ -168,17 +171,13 @@ namespace Catch {
m_result( result )
{}

ITransientExpression() = default;
ITransientExpression(ITransientExpression const&) = default;
ITransientExpression& operator=(ITransientExpression const&) = default;
constexpr ITransientExpression( ITransientExpression const& ) = default;
constexpr ITransientExpression& operator=( ITransientExpression const& ) = default;

friend std::ostream& operator<<(std::ostream& out, ITransientExpression const& expr) {
expr.streamReconstructedExpression(out);
return out;
}

protected:
~ITransientExpression() = default;
};

void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs );
Expand Down
27 changes: 15 additions & 12 deletions src/catch2/internal/catch_is_permutation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Catch {
typename Sentinel,
typename T,
typename Comparator>
constexpr
ForwardIter find_sentinel( ForwardIter start,
Sentinel sentinel,
T const& value,
Expand All @@ -33,6 +34,7 @@ namespace Catch {
typename Sentinel,
typename T,
typename Comparator>
constexpr
std::ptrdiff_t count_sentinel( ForwardIter start,
Sentinel sentinel,
T const& value,
Expand All @@ -46,6 +48,7 @@ namespace Catch {
}

template <typename ForwardIter, typename Sentinel>
constexpr
std::enable_if_t<!std::is_same<ForwardIter, Sentinel>::value,
std::ptrdiff_t>
sentinel_distance( ForwardIter iter, const Sentinel sentinel ) {
Expand All @@ -58,8 +61,8 @@ namespace Catch {
}

template <typename ForwardIter>
std::ptrdiff_t sentinel_distance( ForwardIter first,
ForwardIter last ) {
constexpr std::ptrdiff_t sentinel_distance( ForwardIter first,
ForwardIter last ) {
return std::distance( first, last );
}

Expand All @@ -68,11 +71,11 @@ namespace Catch {
typename ForwardIter2,
typename Sentinel2,
typename Comparator>
bool check_element_counts( ForwardIter1 first_1,
const Sentinel1 end_1,
ForwardIter2 first_2,
const Sentinel2 end_2,
Comparator cmp ) {
constexpr bool check_element_counts( ForwardIter1 first_1,
const Sentinel1 end_1,
ForwardIter2 first_2,
const Sentinel2 end_2,
Comparator cmp ) {
auto cursor = first_1;
while ( cursor != end_1 ) {
if ( find_sentinel( first_1, cursor, *cursor, cmp ) ==
Expand Down Expand Up @@ -102,11 +105,11 @@ namespace Catch {
typename ForwardIter2,
typename Sentinel2,
typename Comparator>
bool is_permutation( ForwardIter1 first_1,
const Sentinel1 end_1,
ForwardIter2 first_2,
const Sentinel2 end_2,
Comparator cmp ) {
constexpr bool is_permutation( ForwardIter1 first_1,
const Sentinel1 end_1,
ForwardIter2 first_2,
const Sentinel2 end_2,
Comparator cmp ) {
// TODO: no optimization for stronger iterators, because we would also have to constrain on sentinel vs not sentinel types
// TODO: Comparator has to be "both sides", e.g. a == b => b == a
// This skips shared prefix of the two ranges
Expand Down
6 changes: 3 additions & 3 deletions src/catch2/internal/catch_lazy_expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace Catch {
ITransientExpression const* m_transientExpression = nullptr;
bool m_isNegated;
public:
LazyExpression( bool isNegated ):
constexpr LazyExpression( bool isNegated ):
m_isNegated(isNegated)
{}
LazyExpression(LazyExpression const& other) = default;
constexpr LazyExpression(LazyExpression const& other) = default;
LazyExpression& operator = ( LazyExpression const& ) = delete;

explicit operator bool() const {
constexpr explicit operator bool() const {
return m_transientExpression != nullptr;
}

Expand Down
4 changes: 3 additions & 1 deletion src/catch2/internal/catch_random_integer_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace Catch {
struct ExtendedMultResult {
T upper;
T lower;
bool operator==( ExtendedMultResult const& rhs ) const {
constexpr bool operator==( ExtendedMultResult const& rhs ) const {
return upper == rhs.upper && lower == rhs.lower;
}
};
Expand Down Expand Up @@ -187,6 +187,7 @@ namespace Catch {
* get by simple casting ([0, ..., INT_MAX, INT_MIN, ..., -1])
*/
template <typename OriginalType, typename UnsignedType>
constexpr
std::enable_if_t<std::is_signed<OriginalType>::value, UnsignedType>
transposeToNaturalOrder( UnsignedType in ) {
static_assert(
Expand All @@ -207,6 +208,7 @@ namespace Catch {

template <typename OriginalType,
typename UnsignedType>
constexpr
std::enable_if_t<std::is_unsigned<OriginalType>::value, UnsignedType>
transposeToNaturalOrder(UnsignedType in) {
static_assert(
Expand Down
26 changes: 0 additions & 26 deletions src/catch2/internal/catch_result_type.cpp

This file was deleted.

23 changes: 16 additions & 7 deletions src/catch2/internal/catch_result_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ namespace Catch {

}; };

bool isOk( ResultWas::OfType resultType );
bool isJustInfo( int flags );
constexpr bool isOk( ResultWas::OfType resultType ) {
return ( resultType & ResultWas::FailureBit ) == 0;
}
constexpr bool isJustInfo( int flags ) { return flags == ResultWas::Info; }


// ResultDisposition::Flags enum
Expand All @@ -46,11 +48,18 @@ namespace Catch {
SuppressFail = 0x08 // Failures are reported but do not fail the test
}; };

ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs );

bool shouldContinueOnFailure( int flags );
inline bool isFalseTest( int flags ) { return ( flags & ResultDisposition::FalseTest ) != 0; }
bool shouldSuppressFailure( int flags );
constexpr ResultDisposition::Flags operator|( ResultDisposition::Flags lhs,
ResultDisposition::Flags rhs ) {
return static_cast<ResultDisposition::Flags>( static_cast<int>( lhs ) |
static_cast<int>( rhs ) );
}

constexpr bool isFalseTest( int flags ) {
return ( flags & ResultDisposition::FalseTest ) != 0;
}
constexpr bool shouldSuppressFailure( int flags ) {
return ( flags & ResultDisposition::SuppressFail ) != 0;
}

} // end namespace Catch

Expand Down
2 changes: 1 addition & 1 deletion src/catch2/internal/catch_test_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Catch {
TestType m_testAsFunction;

public:
TestInvokerAsFunction( TestType testAsFunction ) noexcept:
constexpr TestInvokerAsFunction( TestType testAsFunction ) noexcept:
m_testAsFunction( testAsFunction ) {}

void invoke() const override { m_testAsFunction(); }
Expand Down
6 changes: 4 additions & 2 deletions src/catch2/internal/catch_test_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ template<typename C>
class TestInvokerAsMethod : public ITestInvoker {
void (C::*m_testAsMethod)();
public:
TestInvokerAsMethod( void (C::*testAsMethod)() ) noexcept : m_testAsMethod( testAsMethod ) {}
constexpr TestInvokerAsMethod( void ( C::*testAsMethod )() ) noexcept:
m_testAsMethod( testAsMethod ) {}

void invoke() const override {
C obj;
Expand All @@ -53,7 +54,8 @@ class TestInvokerFixture : public ITestInvoker {
Detail::unique_ptr<C> m_fixture = nullptr;

public:
TestInvokerFixture( void ( C::*testAsMethod )() const) noexcept : m_testAsMethod( testAsMethod ) {}
constexpr TestInvokerFixture( void ( C::*testAsMethod )() const ) noexcept:
m_testAsMethod( testAsMethod ) {}

void prepareTestCase() override {
m_fixture = Detail::make_unique<C>();
Expand Down
Loading

0 comments on commit 18df97d

Please sign in to comment.