Skip to content

Commit

Permalink
Omit the _raw_default_instance_ declaration when using DLL linkage.
Browse files Browse the repository at this point in the history
In that mode we might not be able to make a `constexpr` pointer to the default
instance. Omitting the declaration will only turn off an optimization, but the
code will still be correct.

Fixes #17303

PiperOrigin-RevId: 649160060
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 3, 2024
1 parent 6e5a867 commit 79c55cb
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 80 deletions.
15 changes: 13 additions & 2 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,18 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
$pbi$::ParseContext* ctx);
)cc");
}},
{"raw_default_instance",
[&] {
// We can't make a constexpr pointer to the global if we have DLL
// linkage so skip this. The fallback in
// `MessageLite::GetStrongPointerForType` will do the right thing in
// those platforms.
if (!options_.dllexport_decl.empty()) return;
p->Emit(R"cc(
static constexpr const void* _raw_default_instance_ =
&_$classname$_default_instance_;
)cc");
}},
{"decl_impl", [&] { GenerateImplDefinition(p); }},
{"classdata_type",
HasDescriptorMethods(descriptor_->file(), options_)
Expand Down Expand Up @@ -2062,8 +2074,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
$decl_data$;
$post_loop_handler$;
static constexpr const void* _raw_default_instance_ =
&_$classname$_default_instance_;
$raw_default_instance$;
friend class ::$proto_ns$::MessageLite;
friend class ::$proto_ns$::Arena;
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/compiler/java/java_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/google/protobuf/cpp_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 0 additions & 66 deletions src/google/protobuf/descriptor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 79c55cb

Please sign in to comment.