Skip to content

Commit 8540483

Browse files
Use internal::MessageGlobalsBase and default_instance() API.
Using `MessageGlobalsBase::default_instance(const void*)` allows transparent handling of returning default instance at an arbitrary (but constant) offset from "message globals". PiperOrigin-RevId: 868373777
1 parent e9cf581 commit 8540483

File tree

12 files changed

+177
-116
lines changed

12 files changed

+177
-116
lines changed

src/google/protobuf/compiler/cpp/field_generators/message_field.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ std::vector<Sub> Vars(const FieldDescriptor* field, const Options& opts,
4444
bool is_foreign = IsCrossFileMessage(field);
4545
std::string field_name = FieldMemberName(field, split);
4646
std::string qualified_type = FieldMessageTypeName(field, opts);
47-
std::string default_ref =
48-
QualifiedMsgGlobalsInstanceName(field->message_type(), opts);
47+
4948
std::string base = absl::StrCat(
5049
"::", ProtobufNamespace(opts), "::",
5150
HasDescriptorMethods(field->file(), opts) ? "Message" : "MessageLite");
5251

5352
return {
5453
{"Submsg", qualified_type},
5554
{"MemberType", use_base_class ? base : qualified_type},
56-
{"kDefault", default_ref},
55+
{"kDefaultRef",
56+
absl::Substitute(
57+
"*::google::protobuf::internal::MessageGlobalsBase::default_instance<$0>(&$1)",
58+
qualified_type,
59+
QualifiedMsgGlobalsInstanceName(field->message_type(), opts))},
5760
Sub{"cast_to_field",
5861
use_base_class ? absl::Substitute("reinterpret_cast<$0*>", base) : ""}
5962
.ConditionalFunctionCall(),
@@ -193,7 +196,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
193196
$TsanDetectConcurrentRead$;
194197
$StrongRef$;
195198
const $Submsg$* p = $cast_field_$;
196-
return p != nullptr ? *p : reinterpret_cast<const $Submsg$&>($kDefault$);
199+
return p != nullptr ? *p : $kDefaultRef$;
197200
}
198201
inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
199202
$WeakDescriptorSelfPin$;
@@ -558,7 +561,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
558561
inline const $Submsg$& $Msg$::_internal_$name_internal$() const {
559562
$StrongRef$;
560563
return $has_field$ ? static_cast<const $Submsg$&>(*$cast_field_$)
561-
: reinterpret_cast<const $Submsg$&>($kDefault$);
564+
: $kDefaultRef$;
562565
}
563566
)cc");
564567
p->Emit(R"cc(

src/google/protobuf/compiler/cpp/file.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
610610
if (IsFileDescriptorProto(file_, options_)) {
611611
p->Emit(
612612
R"cc(
613-
struct $type$ {
613+
struct $type$ : ::_pbi::MessageGlobalsBase {
614614
#if defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES)
615615
constexpr $type$() : _default(::_pbi::ConstantInitialized{}) {}
616616
#else // defined(PROTOBUF_CONSTINIT_DEFAULT_INSTANCES)
@@ -635,7 +635,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
635635
generator->index_in_file_messages(), options_)},
636636
},
637637
R"cc(
638-
struct $type$ {
638+
struct $type$ : ::_pbi::MessageGlobalsBase {
639639
constexpr $type$() : _default(::_pbi::ConstantInitialized{}) {}
640640
~$type$() {}
641641
//~ _default must be the first member.
@@ -653,7 +653,7 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
653653
} else {
654654
p->Emit(
655655
R"cc(
656-
struct $type$ {
656+
struct $type$ : ::_pbi::MessageGlobalsBase {
657657
constexpr $type$() : _default(::_pbi::ConstantInitialized{}) {}
658658
~$type$() {}
659659
union {

src/google/protobuf/compiler/cpp/message.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,6 +2188,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
21882188
}},
21892189
{"decl_impl", [&] { GenerateImplDefinition(p); }},
21902190
{"classdata_type", ClassDataType(descriptor_, options_)},
2191+
{"msg_globals", MsgGlobalsInstanceName(descriptor_, options_)},
21912192
{"split_friend",
21922193
[&] {
21932194
if (!ShouldSplit(descriptor_, options_)) return;
@@ -2251,7 +2252,8 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
22512252
$descriptor_accessor$;
22522253
$get_descriptor$;
22532254
$nodiscard $static const $classname$& default_instance() {
2254-
return *reinterpret_cast<const $classname$*>(&_$classname$_globals_);
2255+
return *$pbi$::MessageGlobalsBase::default_instance<$classname$>(
2256+
&$msg_globals$);
22552257
}
22562258
$decl_oneof$;
22572259
static constexpr int kIndexInFileMessages = $index_in_file_messages$;

src/google/protobuf/compiler/java/java_features.pb.cc

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/compiler/java/java_features.pb.h

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/compiler/plugin.pb.cc

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/compiler/plugin.pb.h

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/cpp_features.pb.cc

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/cpp_features.pb.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)