diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index 4dcfef611aa92..d09961fdd5a56 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc @@ -592,7 +592,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) { }; }; - PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT$ dllexport_decl$ + PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT_WITH_PTR$ dllexport_decl$ PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 const $type$ $name$; )cc"); } @@ -620,7 +620,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) { }; }; - PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT$ dllexport_decl$ + PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT_WITH_PTR$ dllexport_decl$ PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 $type$ $name$; )cc"); } else if (UsingImplicitWeakDescriptor(file_, options_)) { @@ -646,7 +646,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) { file_default_instances + $index$, sizeof($type$)}; }; - PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT$ dllexport_decl$ + PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT_WITH_PTR$ dllexport_decl$ PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 $type$ $name$ __attribute__((section("$section$"))); )cc"); @@ -666,7 +666,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) { }; }; - PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT$ dllexport_decl$ + PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT_WITH_PTR$ dllexport_decl$ PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 $type$ $name$; )cc"); } diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 5325fb671fd8d..2566bec49c3ce 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -4174,7 +4174,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) { }; } - PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 const + PROTOBUF_CONSTINIT_WITH_PTR PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 const $pbi$::ClassDataFull $classname$_class_data_ = $classname$::InternalGenerateClassData_(); diff --git a/src/google/protobuf/compiler/cpp/parse_function_generator.cc b/src/google/protobuf/compiler/cpp/parse_function_generator.cc index 2baad4bf145d4..9391b1ae4d559 100644 --- a/src/google/protobuf/compiler/cpp/parse_function_generator.cc +++ b/src/google/protobuf/compiler/cpp/parse_function_generator.cc @@ -479,7 +479,7 @@ void ParseFunctionGenerator::GenerateTailCallTable(io::Printer* p) { // FileDescriptorProto is safe from this. IsFileDescriptorProto(descriptor_->file(), options_) ? "constexpr" - : "PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1\nconst"}, + : "PROTOBUF_CONSTINIT_WITH_PTR PROTOBUF_ATTRIBUTE_INIT_PRIORITY1\nconst"}, {"table_size_log2", tc_table_info_->table_size_log2}, {"ordered_size", ordered_fields_.size()}, {"aux_size", tc_table_info_->aux_entries.size()}, diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc index 9f70d85acc4ec..ab6084cbc98f5 100644 --- a/src/google/protobuf/extension_set.cc +++ b/src/google/protobuf/extension_set.cc @@ -164,7 +164,8 @@ void ExtensionSet::RegisterMessageExtension(const MessageLite* extendee, ExtensionInfo info(extendee, number, type, is_repeated, is_packed, verify_func, is_lazy); info.message_info = {prototype, -#if defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES) +#if defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES) && \ + !defined(PROTOBUF_DEFAULT_INSTANCES_MAY_NOT_BE_CONSTINIT) prototype->GetTcParseTable() #else nullptr diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 22c87f21587aa..a12a49e9971af 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -334,19 +334,31 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3), # define PROTOBUF_EXPORT __declspec(dllexport) # define PROTOBUF_EXPORT_TEMPLATE_DECLARE # define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllexport) +# define PROTOBUF_CONSTINIT_WITH_PTR PROTOBUF_CONSTINIT +// See PROTOBUF_CONSTINIT_WITH_PTR below. This define is used by code inside the +// protobuf library that needs to know whether it can assume a default instance, +// which may be initialized inside the same binary or in another library, is +// constinit. +# define PROTOBUF_DEFAULT_INSTANCES_MAY_NOT_BE_CONSTINIT # else # define PROTOBUF_EXPORT __declspec(dllimport) # define PROTOBUF_EXPORT_TEMPLATE_DECLARE # define PROTOBUF_EXPORT_TEMPLATE_DEFINE __declspec(dllimport) +// Pointers to dllimport extern variables on Windows require a static +// initialization and cannot be constant-initialized. This macro disables +// constinit in files where we cannot support it. +# define PROTOBUF_CONSTINIT_WITH_PTR # endif // defined(LIBPROTOBUF_EXPORTS) #elif defined(PROTOBUF_USE_DLLS) && defined(LIBPROTOBUF_EXPORTS) # define PROTOBUF_EXPORT __attribute__((visibility("default"))) # define PROTOBUF_EXPORT_TEMPLATE_DECLARE __attribute__((visibility("default"))) # define PROTOBUF_EXPORT_TEMPLATE_DEFINE +# define PROTOBUF_CONSTINIT_WITH_PTR PROTOBUF_CONSTINIT #else # define PROTOBUF_EXPORT # define PROTOBUF_EXPORT_TEMPLATE_DECLARE # define PROTOBUF_EXPORT_TEMPLATE_DEFINE +# define PROTOBUF_CONSTINIT_WITH_PTR PROTOBUF_CONSTINIT #endif #ifdef PROTOC_EXPORT diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index bbb2848c5a33d..9600b3876ba54 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -43,6 +43,7 @@ #undef PROTOBUF_ALIGNAS #undef PROTOBUF_THREAD_LOCAL #undef PROTOBUF_CONSTINIT +#undef PROTOBUF_CONSTINIT_WITH_PTR #undef PROTOBUF_CONSTEXPR #undef PROTOBUF_CONSTINIT_DEFAULT_INSTANCES #undef PROTOBUF_ATTRIBUTE_WEAK