1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
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
|