Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2026-04-08 10:26:57 +0200
committerMartin Hořeňovský2026-04-08 12:57:09 +0200
commit47200ddbee03eaf4a9d3d99d18cb692381d8f5bc (patch)
treeec947545e5f35b38ea5592c6fafcd7861081648c /src
parente51dcdcc595371828de2b1d28985e8d8c1895345 (diff)
downloadcatch3-47200ddbee03eaf4a9d3d99d18cb692381d8f5bc.tar.gz
Rework internals of CATCH_REGISTER_ENUM
The old internals reached into the global hub to stash the allocation(s) for enum value -> string value there, then kept around a potentially invalid (in case the hub was cleaned up) reference into it, going through whole bunch of virtual dispatch in the process. The new internals just store the data in a static variable inside the `StringMaker` specialization. This avoids potential lifetime issues, avoids all virtual dispatch and (almost) reduces the include bloat in the main header path. The reason for (almost) there is that for full include correctness, `EnumInfo` needs `<utility>` include for `std::pair`. However, this brings in things like `std::relops`, because the std headers in C++ are dumb. As this was not included before, and instead we relied on `std::pair` existing in an internal stdlib header that we transitively included, the full include size ends up bigger than before.
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt5
-rw-r--r--src/catch2/catch_all.hpp2
-rw-r--r--src/catch2/catch_message.hpp1
-rw-r--r--src/catch2/catch_registry_hub.cpp5
-rw-r--r--src/catch2/catch_session.cpp1
-rw-r--r--src/catch2/catch_tostring.hpp29
-rw-r--r--src/catch2/interfaces/catch_interfaces_all.hpp1
-rw-r--r--src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp47
-rw-r--r--src/catch2/interfaces/catch_interfaces_registry_hub.hpp1
-rw-r--r--src/catch2/internal/catch_assertion_handler.cpp1
-rw-r--r--src/catch2/internal/catch_enum_info.cpp (renamed from src/catch2/internal/catch_enum_values_registry.cpp)28
-rw-r--r--src/catch2/internal/catch_enum_info.hpp48
-rw-r--r--src/catch2/internal/catch_enum_values_registry.hpp36
-rw-r--r--src/catch2/internal/catch_run_context.cpp1
-rw-r--r--src/catch2/meson.build5
15 files changed, 84 insertions, 127 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 11089ff5..0ed64ebf 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -87,7 +87,7 @@ set(IMPL_HEADERS
${SOURCES_DIR}/internal/catch_decomposer.hpp
${SOURCES_DIR}/internal/catch_deprecation_macro.hpp
${SOURCES_DIR}/internal/catch_enforce.hpp
- ${SOURCES_DIR}/internal/catch_enum_values_registry.hpp
+ ${SOURCES_DIR}/internal/catch_enum_info.hpp
${SOURCES_DIR}/internal/catch_errno_guard.hpp
${SOURCES_DIR}/internal/catch_exception_translator_registry.hpp
${SOURCES_DIR}/internal/catch_fatal_condition_handler.hpp
@@ -181,7 +181,7 @@ set(IMPL_SOURCES
${SOURCES_DIR}/internal/catch_debugger.cpp
${SOURCES_DIR}/internal/catch_decomposer.cpp
${SOURCES_DIR}/internal/catch_enforce.cpp
- ${SOURCES_DIR}/internal/catch_enum_values_registry.cpp
+ ${SOURCES_DIR}/internal/catch_enum_info.cpp
${SOURCES_DIR}/internal/catch_errno_guard.cpp
${SOURCES_DIR}/internal/catch_exception_translator_registry.cpp
${SOURCES_DIR}/internal/catch_fatal_condition_handler.cpp
@@ -227,7 +227,6 @@ set(INTERFACE_HEADERS
${SOURCES_DIR}/interfaces/catch_interfaces_all.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_capture.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_config.hpp
- ${SOURCES_DIR}/interfaces/catch_interfaces_enum_values_registry.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_exception.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_registry_hub.hpp
diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp
index 97226dd1..82b11405 100644
--- a/src/catch2/catch_all.hpp
+++ b/src/catch2/catch_all.hpp
@@ -68,7 +68,7 @@
#include <catch2/internal/catch_decomposer.hpp>
#include <catch2/internal/catch_deprecation_macro.hpp>
#include <catch2/internal/catch_enforce.hpp>
-#include <catch2/internal/catch_enum_values_registry.hpp>
+#include <catch2/internal/catch_enum_info.hpp>
#include <catch2/internal/catch_errno_guard.hpp>
#include <catch2/internal/catch_exception_translator_registry.hpp>
#include <catch2/internal/catch_fatal_condition_handler.hpp>
diff --git a/src/catch2/catch_message.hpp b/src/catch2/catch_message.hpp
index 4958716d..4582654f 100644
--- a/src/catch2/catch_message.hpp
+++ b/src/catch2/catch_message.hpp
@@ -13,6 +13,7 @@
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <catch2/internal/catch_stream_end_stop.hpp>
#include <catch2/internal/catch_message_info.hpp>
+#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/catch_tostring.hpp>
#include <string>
diff --git a/src/catch2/catch_registry_hub.cpp b/src/catch2/catch_registry_hub.cpp
index 925468fa..25aecdde 100644
--- a/src/catch2/catch_registry_hub.cpp
+++ b/src/catch2/catch_registry_hub.cpp
@@ -15,7 +15,6 @@
#include <catch2/internal/catch_tag_alias_registry.hpp>
#include <catch2/internal/catch_startup_exception_registry.hpp>
#include <catch2/internal/catch_singletons.hpp>
-#include <catch2/internal/catch_enum_values_registry.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/internal/catch_noncopyable.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
@@ -72,9 +71,6 @@ namespace Catch {
CATCH_INTERNAL_ERROR("Attempted to register active exception under CATCH_CONFIG_DISABLE_EXCEPTIONS!");
#endif
}
- IMutableEnumValuesRegistry& getMutableEnumValuesRegistry() override {
- return m_enumValuesRegistry;
- }
private:
TestRegistry m_testCaseRegistry;
@@ -82,7 +78,6 @@ namespace Catch {
ExceptionTranslatorRegistry m_exceptionTranslatorRegistry;
TagAliasRegistry m_tagAliasRegistry;
StartupExceptionRegistry m_exceptionRegistry;
- Detail::EnumValuesRegistry m_enumValuesRegistry;
};
}
diff --git a/src/catch2/catch_session.cpp b/src/catch2/catch_session.cpp
index 042b7f4d..0487fe75 100644
--- a/src/catch2/catch_session.cpp
+++ b/src/catch2/catch_session.cpp
@@ -21,6 +21,7 @@
#include <catch2/reporters/catch_reporter_multi.hpp>
#include <catch2/internal/catch_reporter_registry.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
+#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/internal/catch_stdstreams.hpp>
#include <catch2/internal/catch_istream.hpp>
diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp
index b005212f..16c4c496 100644
--- a/src/catch2/catch_tostring.hpp
+++ b/src/catch2/catch_tostring.hpp
@@ -18,7 +18,8 @@
#include <catch2/internal/catch_config_wchar.hpp>
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <catch2/internal/catch_void_type.hpp>
-#include <catch2/interfaces/catch_interfaces_enum_values_registry.hpp>
+#include <catch2/internal/catch_enum_info.hpp>
+#include <catch2/internal/catch_stringref.hpp>
#ifdef CATCH_CONFIG_CPP17_STRING_VIEW
#include <string_view>
@@ -630,17 +631,21 @@ struct ratio_string<std::milli> {
};
}
-#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
-
-#define INTERNAL_CATCH_REGISTER_ENUM( enumName, ... ) \
-namespace Catch { \
- template<> struct StringMaker<enumName> { \
- static std::string convert( enumName value ) { \
- static const auto& enumInfo = ::Catch::getMutableRegistryHub().getMutableEnumValuesRegistry().registerEnum( #enumName, #__VA_ARGS__, { __VA_ARGS__ } ); \
- return static_cast<std::string>(enumInfo.lookup( static_cast<int>( value ) )); \
- } \
- }; \
-}
+#define INTERNAL_CATCH_REGISTER_ENUM( enumName, ... ) \
+ namespace Catch { \
+ template <> \
+ struct StringMaker<enumName> { \
+ static std::string convert( enumName value ) { \
+ CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \
+ CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \
+ static const auto enumInfo = ::Catch::Detail::makeEnumInfo( \
+ #enumName, #__VA_ARGS__, { __VA_ARGS__ } ); \
+ CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION \
+ return static_cast<std::string>( \
+ enumInfo.lookup( static_cast<int64_t>( value ) ) ); \
+ } \
+ }; \
+ }
#define CATCH_REGISTER_ENUM( enumName, ... ) INTERNAL_CATCH_REGISTER_ENUM( enumName, __VA_ARGS__ )
diff --git a/src/catch2/interfaces/catch_interfaces_all.hpp b/src/catch2/interfaces/catch_interfaces_all.hpp
index a99fdcdc..87eb259b 100644
--- a/src/catch2/interfaces/catch_interfaces_all.hpp
+++ b/src/catch2/interfaces/catch_interfaces_all.hpp
@@ -24,7 +24,6 @@
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
-#include <catch2/interfaces/catch_interfaces_enum_values_registry.hpp>
#include <catch2/interfaces/catch_interfaces_exception.hpp>
#include <catch2/interfaces/catch_interfaces_generatortracker.hpp>
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
diff --git a/src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp b/src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp
deleted file mode 100644
index 38b052d9..00000000
--- a/src/catch2/interfaces/catch_interfaces_enum_values_registry.hpp
+++ /dev/null
@@ -1,47 +0,0 @@
-
-// Copyright Catch2 Authors
-// Distributed under the Boost Software License, Version 1.0.
-// (See accompanying file LICENSE.txt or copy at
-// https://www.boost.org/LICENSE_1_0.txt)
-
-// SPDX-License-Identifier: BSL-1.0
-#ifndef CATCH_INTERFACES_ENUM_VALUES_REGISTRY_HPP_INCLUDED
-#define CATCH_INTERFACES_ENUM_VALUES_REGISTRY_HPP_INCLUDED
-
-#include <catch2/internal/catch_stringref.hpp>
-
-#include <vector>
-
-namespace Catch {
-
- namespace Detail {
- struct EnumInfo {
- StringRef m_name;
- std::vector<std::pair<int, StringRef>> m_values;
-
- ~EnumInfo();
-
- StringRef lookup( int value ) const;
- };
- } // namespace Detail
-
- class IMutableEnumValuesRegistry {
- public:
- virtual ~IMutableEnumValuesRegistry(); // = default;
-
- virtual Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector<int> const& values ) = 0;
-
- template<typename E>
- Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list<E> values ) {
- static_assert(sizeof(int) >= sizeof(E), "Cannot serialize enum to int");
- std::vector<int> intValues;
- intValues.reserve( values.size() );
- for( auto enumValue : values )
- intValues.push_back( static_cast<int>( enumValue ) );
- return registerEnum( enumName, allEnums, intValues );
- }
- };
-
-} // Catch
-
-#endif // CATCH_INTERFACES_ENUM_VALUES_REGISTRY_HPP_INCLUDED
diff --git a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp
index 113f223e..3695a192 100644
--- a/src/catch2/interfaces/catch_interfaces_registry_hub.hpp
+++ b/src/catch2/interfaces/catch_interfaces_registry_hub.hpp
@@ -53,7 +53,6 @@ namespace Catch {
virtual void registerTranslator( Detail::unique_ptr<IExceptionTranslator>&& translator ) = 0;
virtual void registerTagAlias( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) = 0;
virtual void registerStartupException() noexcept = 0;
- virtual IMutableEnumValuesRegistry& getMutableEnumValuesRegistry() = 0;
};
IRegistryHub const& getRegistryHub();
diff --git a/src/catch2/internal/catch_assertion_handler.cpp b/src/catch2/internal/catch_assertion_handler.cpp
index a0fcfcb4..5a4806e9 100644
--- a/src/catch2/internal/catch_assertion_handler.cpp
+++ b/src/catch2/internal/catch_assertion_handler.cpp
@@ -8,6 +8,7 @@
#include <catch2/internal/catch_assertion_handler.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
+#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/internal/catch_context.hpp>
#include <catch2/internal/catch_debugger.hpp>
#include <catch2/internal/catch_test_failure_exception.hpp>
diff --git a/src/catch2/internal/catch_enum_values_registry.cpp b/src/catch2/internal/catch_enum_info.cpp
index a94b6088..0793abd9 100644
--- a/src/catch2/internal/catch_enum_values_registry.cpp
+++ b/src/catch2/internal/catch_enum_info.cpp
@@ -5,15 +5,14 @@
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
-#include <catch2/internal/catch_enum_values_registry.hpp>
+
+#include <catch2/internal/catch_enum_info.hpp>
#include <catch2/internal/catch_string_manip.hpp>
#include <cassert>
namespace Catch {
- IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() = default;
-
namespace Detail {
namespace {
@@ -39,9 +38,7 @@ namespace Catch {
return parsed;
}
- EnumInfo::~EnumInfo() = default;
-
- StringRef EnumInfo::lookup( int value ) const {
+ StringRef EnumInfo::lookup( int64_t value ) const {
for( auto const& valueToName : m_values ) {
if( valueToName.first == value )
return valueToName.second;
@@ -49,25 +46,20 @@ namespace Catch {
return "{** unexpected enum value **}"_sr;
}
- Catch::Detail::unique_ptr<EnumInfo> makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
- auto enumInfo = Catch::Detail::make_unique<EnumInfo>();
- enumInfo->m_name = enumName;
- enumInfo->m_values.reserve( values.size() );
+ EnumInfo makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int64_t> const& values ) {
+ EnumInfo enumInfo;
+ enumInfo.m_name = enumName;
+ enumInfo.m_values.reserve( values.size() );
const auto valueNames = Catch::Detail::parseEnums( allValueNames );
assert( valueNames.size() == values.size() );
- std::size_t i = 0;
- for( auto value : values )
- enumInfo->m_values.emplace_back(value, valueNames[i++]);
+ for (size_t i = 0; i < values.size(); ++i) {
+ enumInfo.m_values.emplace_back( values[i], valueNames[i] );
+ }
return enumInfo;
}
- EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
- m_enumInfos.push_back(makeEnumInfo(enumName, allValueNames, values));
- return *m_enumInfos.back();
- }
-
} // Detail
} // Catch
diff --git a/src/catch2/internal/catch_enum_info.hpp b/src/catch2/internal/catch_enum_info.hpp
new file mode 100644
index 00000000..b3036a82
--- /dev/null
+++ b/src/catch2/internal/catch_enum_info.hpp
@@ -0,0 +1,48 @@
+
+// Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.txt or copy at
+// https://www.boost.org/LICENSE_1_0.txt)
+
+// SPDX-License-Identifier: BSL-1.0
+#ifndef CATCH_ENUM_INFO_HPP_INCLUDED
+#define CATCH_ENUM_INFO_HPP_INCLUDED
+
+#include <catch2/internal/catch_stringref.hpp>
+
+#include <cstdint>
+#include <utility>
+#include <vector>
+
+namespace Catch {
+ namespace Detail {
+
+ struct EnumInfo {
+ StringRef m_name;
+ std::vector<std::pair<int64_t, StringRef>> m_values;
+
+ StringRef lookup( int64_t value ) const;
+ };
+
+ EnumInfo makeEnumInfo( StringRef enumName,
+ StringRef allValueNames,
+ std::vector<int64_t> const& values );
+ template <typename E>
+ Detail::EnumInfo makeEnumInfo( StringRef enumName,
+ StringRef allEnums,
+ std::initializer_list<E> values ) {
+ static_assert( sizeof( int64_t ) >= sizeof( E ),
+ "Cannot serialize enum to int64_t" );
+ std::vector<int64_t> intValues;
+ intValues.reserve( values.size() );
+ for ( auto enumValue : values )
+ intValues.push_back( static_cast<int64_t>( enumValue ) );
+ return makeEnumInfo( enumName, allEnums, intValues );
+ }
+
+ std::vector<StringRef> parseEnums( StringRef enums );
+
+ } // namespace Detail
+} // namespace Catch
+
+#endif // CATCH_ENUM_INFO_HPP_INCLUDED
diff --git a/src/catch2/internal/catch_enum_values_registry.hpp b/src/catch2/internal/catch_enum_values_registry.hpp
deleted file mode 100644
index 7b5e7a72..00000000
--- a/src/catch2/internal/catch_enum_values_registry.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-
-// Copyright Catch2 Authors
-// Distributed under the Boost Software License, Version 1.0.
-// (See accompanying file LICENSE.txt or copy at
-// https://www.boost.org/LICENSE_1_0.txt)
-
-// SPDX-License-Identifier: BSL-1.0
-#ifndef CATCH_ENUM_VALUES_REGISTRY_HPP_INCLUDED
-#define CATCH_ENUM_VALUES_REGISTRY_HPP_INCLUDED
-
-#include <catch2/interfaces/catch_interfaces_enum_values_registry.hpp>
-#include <catch2/internal/catch_unique_ptr.hpp>
-#include <catch2/internal/catch_stringref.hpp>
-
-#include <vector>
-
-namespace Catch {
-
- namespace Detail {
-
- Catch::Detail::unique_ptr<EnumInfo> makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int> const& values );
-
- class EnumValuesRegistry final : public IMutableEnumValuesRegistry {
-
- std::vector<Catch::Detail::unique_ptr<EnumInfo>> m_enumInfos;
-
- EnumInfo const& registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values) override;
- };
-
- std::vector<StringRef> parseEnums( StringRef enums );
-
- } // Detail
-
-} // Catch
-
-#endif // CATCH_ENUM_VALUES_REGISTRY_HPP_INCLUDED
diff --git a/src/catch2/internal/catch_run_context.cpp b/src/catch2/internal/catch_run_context.cpp
index 547ff9d8..e7e22c58 100644
--- a/src/catch2/internal/catch_run_context.cpp
+++ b/src/catch2/internal/catch_run_context.cpp
@@ -11,6 +11,7 @@
#include <catch2/generators/catch_generators_throw.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
#include <catch2/interfaces/catch_interfaces_generatortracker.hpp>
+#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_context.hpp>
diff --git a/src/catch2/meson.build b/src/catch2/meson.build
index f491c602..94e0496a 100644
--- a/src/catch2/meson.build
+++ b/src/catch2/meson.build
@@ -65,7 +65,6 @@ internal_headers = [
'interfaces/catch_interfaces_all.hpp',
'interfaces/catch_interfaces_capture.hpp',
'interfaces/catch_interfaces_config.hpp',
- 'interfaces/catch_interfaces_enum_values_registry.hpp',
'interfaces/catch_interfaces_exception.hpp',
'interfaces/catch_interfaces_generatortracker.hpp',
'interfaces/catch_interfaces_registry_hub.hpp',
@@ -95,7 +94,7 @@ internal_headers = [
'internal/catch_decomposer.hpp',
'internal/catch_deprecation_macro.hpp',
'internal/catch_enforce.hpp',
- 'internal/catch_enum_values_registry.hpp',
+ 'internal/catch_enum_info.hpp',
'internal/catch_errno_guard.hpp',
'internal/catch_exception_translator_registry.hpp',
'internal/catch_fatal_condition_handler.hpp',
@@ -224,7 +223,7 @@ internal_sources = files(
'internal/catch_debugger.cpp',
'internal/catch_decomposer.cpp',
'internal/catch_enforce.cpp',
- 'internal/catch_enum_values_registry.cpp',
+ 'internal/catch_enum_info.cpp',
'internal/catch_errno_guard.cpp',
'internal/catch_exception_translator_registry.cpp',
'internal/catch_fatal_condition_handler.cpp',