From c3657504c642129865fc8cfdeacb98ef2da8d830 Mon Sep 17 00:00:00 2001 From: babenko Date: Sat, 14 Dec 2024 10:23:55 +0300 Subject: Introduce (any use) YT_STATIC_INITIALIZER commit_hash:7d3055f901a21e63f7860f443252a86e9895fd08 --- library/cpp/yt/misc/static_initializer.h | 19 +++++++++++++++++++ yt/yt/core/misc/error_code.h | 13 +++++++------ yt/yt/core/misc/protobuf_helpers.h | 11 +++++------ yt/yt/core/phoenix/type_def-inl.h | 8 ++++---- yt/yt/core/yson/protobuf_interop.h | 22 +++++++++------------- yt/yt/core/ytree/interned_attributes.h | 7 ++----- 6 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 library/cpp/yt/misc/static_initializer.h diff --git a/library/cpp/yt/misc/static_initializer.h b/library/cpp/yt/misc/static_initializer.h new file mode 100644 index 00000000000..ebaa35a1a24 --- /dev/null +++ b/library/cpp/yt/misc/static_initializer.h @@ -0,0 +1,19 @@ +#pragma once + +#include "preprocessor.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +//! Static initializer will be invoked prior to entering |main|. +//! The exact order of these invocations is, of course, undefined. +#define YT_STATIC_INITIALIZER(...) \ + [[maybe_unused]] static inline const void* PP_ANONYMOUS_VARIABLE(StaticInitializer) = [] { \ + __VA_ARGS__; \ + return nullptr; \ + } () + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/yt/yt/core/misc/error_code.h b/yt/yt/core/misc/error_code.h index 2ab243522ea..1c2c08fbb4a 100644 --- a/yt/yt/core/misc/error_code.h +++ b/yt/yt/core/misc/error_code.h @@ -2,13 +2,12 @@ #include #include +#include #include #include -#include - namespace NYT { //////////////////////////////////////////////////////////////////////////////// @@ -90,10 +89,12 @@ void FormatValue( //! NB: This macro should only by used in cpp files. #define YT_DEFINE_ERROR_CODE_RANGE(from, to, namespaceName, formatter) \ - YT_ATTRIBUTE_USED static const void* PP_ANONYMOUS_VARIABLE(RegisterErrorCodeRange) = [] { \ - ::NYT::TErrorCodeRegistry::Get()->RegisterErrorCodeRange(from, to, namespaceName, formatter); \ - return nullptr; \ - } () + YT_STATIC_INITIALIZER( \ + ::NYT::TErrorCodeRegistry::Get()->RegisterErrorCodeRange( \ + from, \ + to, \ + namespaceName, \ + formatter)); //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/core/misc/protobuf_helpers.h b/yt/yt/core/misc/protobuf_helpers.h index a0a8ac569e6..c6e8c716d95 100644 --- a/yt/yt/core/misc/protobuf_helpers.h +++ b/yt/yt/core/misc/protobuf_helpers.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -373,17 +374,15 @@ struct IProtobufExtensionRegistry }; #define REGISTER_PROTO_EXTENSION(type, tag, name) \ - YT_ATTRIBUTE_USED static const void* PP_ANONYMOUS_VARIABLE(RegisterProtoExtension) = [] { \ + YT_STATIC_INITIALIZER( \ NYT::IProtobufExtensionRegistry::Get()->AddAction([] { \ const auto* descriptor = type::default_instance().GetDescriptor(); \ - NYT::IProtobufExtensionRegistry::Get()->RegisterDescriptor({ \ + ::NYT::IProtobufExtensionRegistry::Get()->RegisterDescriptor({ \ .MessageDescriptor = descriptor, \ .Tag = tag, \ - .Name = #name \ + .Name = #name, \ });\ - }); \ - return nullptr; \ - } (); + })); //! Finds and deserializes an extension of the given type. Fails if no matching //! extension is found. diff --git a/yt/yt/core/phoenix/type_def-inl.h b/yt/yt/core/phoenix/type_def-inl.h index ff39a471b16..b7322d79551 100644 --- a/yt/yt/core/phoenix/type_def-inl.h +++ b/yt/yt/core/phoenix/type_def-inl.h @@ -14,7 +14,7 @@ #include -#include +#include #include @@ -78,7 +78,7 @@ namespace NYT::NPhoenix2::NDetail { template <> \ struct TPhoenixTypeInitializer__ \ { \ - [[maybe_unused]] static inline const void* Dummy = &::NYT::NPhoenix2::NDetail::RegisterTypeDescriptorImpl(); \ + YT_STATIC_INITIALIZER(::NYT::NPhoenix2::NDetail::RegisterTypeDescriptorImpl()); \ } #define PHOENIX_DEFINE_TEMPLATE_TYPE(type, typeArgs) \ @@ -88,7 +88,7 @@ namespace NYT::NPhoenix2::NDetail { template <> \ struct TPhoenixTypeInitializer__> \ { \ - [[maybe_unused]] static inline const void* Dummy = &::NYT::NPhoenix2::NDetail::RegisterTypeDescriptorImpl, true>(); \ + YT_STATIC_INITIALIZER(::NYT::NPhoenix2::NDetail::RegisterTypeDescriptorImpl, true>()); \ } #define PHOENIX_DEFINE_OPAQUE_TYPE(type) \ @@ -104,7 +104,7 @@ namespace NYT::NPhoenix2::NDetail { template <> \ struct TPhoenixTypeInitializer__ \ { \ - [[maybe_unused]] static inline const void* Dummy = &::NYT::NPhoenix2::NDetail::RegisterOpaqueTypeDescriptorImpl(); \ + YT_STATIC_INITIALIZER(::NYT::NPhoenix2::NDetail::RegisterOpaqueTypeDescriptorImpl()); \ } #define PHOENIX_REGISTER_FIELD(fieldTag, fieldName) \ diff --git a/yt/yt/core/yson/protobuf_interop.h b/yt/yt/core/yson/protobuf_interop.h index 9b56889654f..6327336075e 100644 --- a/yt/yt/core/yson/protobuf_interop.h +++ b/yt/yt/core/yson/protobuf_interop.h @@ -196,13 +196,11 @@ void RegisterCustomProtobufConverter( const google::protobuf::Descriptor* descriptor, const TProtobufMessageConverter& converter); -#define REGISTER_INTERMEDIATE_PROTO_INTEROP_REPRESENTATION(ProtoType, Type) \ - YT_ATTRIBUTE_USED static const void* PP_ANONYMOUS_VARIABLE(RegisterIntermediateProtoInteropRepresentation) = \ - NYson::DoRegisterIntermediateProtoInteropRepresentation(); +#define REGISTER_INTERMEDIATE_PROTO_INTEROP_REPRESENTATION(ProtoType, Type) \ + YT_STATIC_INITIALIZER(::NYT::NYson::DoRegisterIntermediateProtoInteropRepresentation()); -#define REGISTER_INTERMEDIATE_PROTO_INTEROP_REPRESENTATION_WITH_OPTIONS(ProtoType, Type) \ - YT_ATTRIBUTE_USED static const void* PP_ANONYMOUS_VARIABLE(RegisterIntermediateProtoInteropRepresentationWithOptions) = \ - NYson::DoRegisterIntermediateProtoInteropRepresentation(); +#define REGISTER_INTERMEDIATE_PROTO_INTEROP_REPRESENTATION_WITH_OPTIONS(ProtoType, Type) \ + YT_STATIC_INITIALIZER(::NYT::NYson::DoRegisterIntermediateProtoInteropRepresentation()); //////////////////////////////////////////////////////////////////////////////// @@ -219,10 +217,10 @@ void RegisterCustomProtobufBytesFieldConverter( const TProtobufMessageBytesFieldConverter& converter); #define REGISTER_INTERMEDIATE_PROTO_INTEROP_BYTES_FIELD_REPRESENTATION(ProtoType, FieldNumber, Type) \ - static const void* PP_ANONYMOUS_VARIABLE(RegisterIntermediateProtoInterpBytesFieldRepresentation) = [] { \ - NYT::NYson::AddProtobufConverterRegisterAction([] { \ + YT_STATIC_INITIALIZER( \ + ::NYT::NYson::AddProtobufConverterRegisterAction([] { \ const auto* descriptor = ProtoType::default_instance().GetDescriptor(); \ - NYT::NYson::TProtobufMessageBytesFieldConverter converter; \ + ::NYT::NYson::TProtobufMessageBytesFieldConverter converter; \ converter.Serializer = [] (NYT::NYson::IYsonConsumer* consumer, TStringBuf bytes) { \ Type value; \ FromBytes(&value, bytes); \ @@ -233,10 +231,8 @@ void RegisterCustomProtobufBytesFieldConverter( Deserialize(value, node); \ ToBytes(bytes, value); \ }; \ - NYT::NYson::RegisterCustomProtobufBytesFieldConverter(descriptor, FieldNumber, converter); \ - }); \ - return nullptr; \ - } (); + ::NYT::NYson::RegisterCustomProtobufBytesFieldConverter(descriptor, FieldNumber, converter); \ + })); //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/core/ytree/interned_attributes.h b/yt/yt/core/ytree/interned_attributes.h index b005ed5f54a..85d55e1f108 100644 --- a/yt/yt/core/ytree/interned_attributes.h +++ b/yt/yt/core/ytree/interned_attributes.h @@ -2,7 +2,7 @@ #include "public.h" -#include +#include namespace NYT::NYTree { @@ -43,10 +43,7 @@ void InternAttribute(const TString& uninternedKey, TInternedAttributeKey interne //////////////////////////////////////////////////////////////////////////////// #define REGISTER_INTERNED_ATTRIBUTE(uninternedKey, internedKey) \ - YT_ATTRIBUTE_USED const void* PP_ANONYMOUS_VARIABLE(RegisterInterndAttribute) = [] { \ - ::NYT::NYTree::InternAttribute(#uninternedKey, internedKey); \ - return nullptr; \ - } (); + YT_STATIC_INITIALIZER(::NYT::NYTree::InternAttribute(#uninternedKey, internedKey)); //////////////////////////////////////////////////////////////////////////////// -- cgit v1.3