From d02ea5adeecb094966ca09f80e027c9c3c11f502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 13 Nov 2021 23:53:09 +0100 Subject: [PATCH] Cumulative reporter base can be customized to not expand assertions --- .../reporters/catch_reporter_cumulative_base.cpp | 12 ++++++++++-- .../reporters/catch_reporter_cumulative_base.hpp | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/catch2/reporters/catch_reporter_cumulative_base.cpp b/src/catch2/reporters/catch_reporter_cumulative_base.cpp index b8ebcad0..85e9c156 100644 --- a/src/catch2/reporters/catch_reporter_cumulative_base.cpp +++ b/src/catch2/reporters/catch_reporter_cumulative_base.cpp @@ -104,8 +104,16 @@ namespace Catch { // Our section stack copy of the assertionResult will likely outlive the // temporary, so it must be expanded or discarded now to avoid calling // a destroyed object later. - static_cast( - assertionStats.assertionResult.getExpandedExpression() ); + if ( m_shouldStoreFailedAssertions && + !assertionStats.assertionResult.isOk() ) { + static_cast( + assertionStats.assertionResult.getExpandedExpression() ); + } + if ( m_shouldStoreSuccesfulAssertions && + assertionStats.assertionResult.isOk() ) { + static_cast( + assertionStats.assertionResult.getExpandedExpression() ); + } SectionNode& sectionNode = *m_sectionStack.back(); sectionNode.assertionsAndBenchmarks.emplace_back( assertionStats ); } diff --git a/src/catch2/reporters/catch_reporter_cumulative_base.hpp b/src/catch2/reporters/catch_reporter_cumulative_base.hpp index 2db14b09..55abd1e1 100644 --- a/src/catch2/reporters/catch_reporter_cumulative_base.hpp +++ b/src/catch2/reporters/catch_reporter_cumulative_base.hpp @@ -49,6 +49,15 @@ namespace Catch { * If you are deriving from this class and override any testing related * member functions, you should first call into the base's implementation to * avoid breaking the tree construction. + * + * Due to the way this base functions, it has to expand assertions up-front, + * even if they are later unused (e.g. because the deriving reporter does + * not report successful assertions, or because the deriving reporter does + * not use assertion expansion at all). Derived classes can use two + * customization points, `m_shouldStoreSuccesfulAssertions` and + * `m_shouldStoreFailedAssertions`, to disable the expansion and gain extra + * performance. **Accessing the assertion expansions if it wasn't stored is + * UB.** */ struct CumulativeReporterBase : IStreamingReporter { template @@ -116,7 +125,14 @@ namespace Catch { void listTags( std::vector const& tags ) override; protected: + //! Should the cumulative base store the assertion expansion for succesful assertions? + bool m_shouldStoreSuccesfulAssertions = true; + //! Should the cumulative base store the assertion expansion for failed assertions? + bool m_shouldStoreFailedAssertions = true; + + //! Stream to write the output to std::ostream& stream; + // Note: We rely on pointer identity being stable, which is why // we store pointers to the nodes rather than the values. std::vector> m_testCases;