Add self pinning on all message accessors for weak descriptor messages.

This guarantees that no matter how you get the instance we pin the type on any
use.

Previously, casting a `Message*` to some generated type and calling methods on it could trigger undefined behavior unless the cast is down via the ones provided by the library (eg `DownCastToGenerated`).

PiperOrigin-RevId: 596066250
pull/15272/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 0d8080ef29
commit f5b50c5022
  1. 6
      src/google/protobuf/compiler/cpp/field.cc
  2. 6
      src/google/protobuf/compiler/cpp/field_generators/cord_field.cc
  3. 8
      src/google/protobuf/compiler/cpp/field_generators/enum_field.cc
  4. 2
      src/google/protobuf/compiler/cpp/field_generators/map_field.cc
  5. 16
      src/google/protobuf/compiler/cpp/field_generators/message_field.cc
  6. 8
      src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc
  7. 20
      src/google/protobuf/compiler/cpp/field_generators/string_field.cc
  8. 3
      src/google/protobuf/compiler/cpp/helpers.h
  9. 93
      src/google/protobuf/compiler/cpp/message.cc

@ -78,6 +78,12 @@ std::vector<Sub> FieldVars(const FieldDescriptor* field, const Options& opts) {
{"ns", Namespace(field, opts)}, {"ns", Namespace(field, opts)},
{"tag_size", WireFormat::TagSize(field->number(), field->type())}, {"tag_size", WireFormat::TagSize(field->number(), field->type())},
{"deprecated_attr", DeprecatedAttribute(opts, field)}, {"deprecated_attr", DeprecatedAttribute(opts, field)},
Sub("WeakDescriptorSelfPin",
UsingImplicitWeakDescriptor(field->file(), opts)
? absl::StrCat("::", ProtobufNamespace(opts),
"::internal::StrongReference(default_instance());")
: "")
.WithSuffix(";"),
}; };
if (const auto* oneof = field->containing_oneof()) { if (const auto* oneof = field->containing_oneof()) {

@ -181,6 +181,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
printer->Emit(R"cc( printer->Emit(R"cc(
inline const ::absl::Cord& $classname$::$name$() const inline const ::absl::Cord& $classname$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$full_name$) // @@protoc_insertion_point(field_get:$full_name$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -195,6 +196,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
)cc"); )cc");
printer->Emit(R"cc( printer->Emit(R"cc(
inline void $classname$::set_$name$(const ::absl::Cord& value) { inline void $classname$::set_$name$(const ::absl::Cord& value) {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
_internal_set_$name_internal$(value); _internal_set_$name_internal$(value);
$annotate_set$; $annotate_set$;
@ -203,6 +205,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
)cc"); )cc");
printer->Emit(R"cc( printer->Emit(R"cc(
inline void $classname$::set_$name$(::absl::string_view value) { inline void $classname$::set_$name$(::absl::string_view value) {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
$set_hasbit$; $set_hasbit$;
$field$ = value; $field$ = value;
@ -345,6 +348,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
printer->Emit(R"cc( printer->Emit(R"cc(
inline const ::absl::Cord& $classname$::$name$() const inline const ::absl::Cord& $classname$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$full_name$) // @@protoc_insertion_point(field_get:$full_name$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -352,6 +356,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
)cc"); )cc");
printer->Emit(R"cc( printer->Emit(R"cc(
inline void $classname$::set_$name$(const ::absl::Cord& value) { inline void $classname$::set_$name$(const ::absl::Cord& value) {
$WeakDescriptorSelfPin$;
if ($not_has_field$) { if ($not_has_field$) {
clear_$oneof_name$(); clear_$oneof_name$();
set_has_$name_internal$(); set_has_$name_internal$();
@ -368,6 +373,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
)cc"); )cc");
printer->Emit(R"cc( printer->Emit(R"cc(
inline void $classname$::set_$name$(::absl::string_view value) { inline void $classname$::set_$name$(::absl::string_view value) {
$WeakDescriptorSelfPin$;
if ($not_has_field$) { if ($not_has_field$) {
clear_$oneof_name$(); clear_$oneof_name$();
set_has_$name_internal$(); set_has_$name_internal$();

@ -165,6 +165,7 @@ void SingularEnum::GenerateAccessorDeclarations(io::Printer* p) const {
void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $Enum$ $Msg$::$name$() const { inline $Enum$ $Msg$::$name$() const {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -174,6 +175,7 @@ void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
if (is_oneof()) { if (is_oneof()) {
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::set_$name$($Enum$ value) { inline void $Msg$::set_$name$($Enum$ value) {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
$assert_valid$; $assert_valid$;
if ($not_has_field$) { if ($not_has_field$) {
@ -194,6 +196,7 @@ void SingularEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
} else { } else {
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::set_$name$($Enum$ value) { inline void $Msg$::set_$name$($Enum$ value) {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
_internal_set_$name_internal$(value); _internal_set_$name_internal$(value);
$set_hasbit$; $set_hasbit$;
@ -398,6 +401,7 @@ void RepeatedEnum::GenerateAccessorDeclarations(io::Printer* p) const {
void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const { void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $Enum$ $Msg$::$name$(int index) const { inline $Enum$ $Msg$::$name$(int index) const {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return static_cast<$Enum$>(_internal_$name_internal$().Get(index)); return static_cast<$Enum$>(_internal_$name_internal$().Get(index));
@ -405,6 +409,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::set_$name$(int index, $Enum$ value) { inline void $Msg$::set_$name$(int index, $Enum$ value) {
$WeakDescriptorSelfPin$;
$assert_valid$; $assert_valid$;
_internal_mutable_$name_internal$()->Set(index, value); _internal_mutable_$name_internal$()->Set(index, value);
$annotate_set$ $annotate_set$
@ -413,6 +418,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::add_$name$($Enum$ value) { inline void $Msg$::add_$name$($Enum$ value) {
$WeakDescriptorSelfPin$;
$assert_valid$; $assert_valid$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add(value); _internal_mutable_$name_internal$()->Add(value);
@ -423,6 +429,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const inline const $pb$::RepeatedField<int>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$; $annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -431,6 +438,7 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $pb$::RepeatedField<int>* $Msg$::mutable_$name$() inline $pb$::RepeatedField<int>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable_list$; $annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;

@ -222,6 +222,7 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline const $Map$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { inline const $Map$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_map:$pkg.Msg.field$) // @@protoc_insertion_point(field_map:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -236,6 +237,7 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline $Map$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { inline $Map$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable$; $annotate_mutable$;
// @@protoc_insertion_point(field_mutable_map:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable_map:$pkg.Msg.field$)
return _internal_mutable_$name_internal$(); return _internal_mutable_$name_internal$();

@ -205,11 +205,13 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return p != nullptr ? *p : reinterpret_cast<const $Submsg$&>($kDefault$); return p != nullptr ? *p : reinterpret_cast<const $Submsg$&>($kDefault$);
} }
inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
} }
inline void $Msg$::unsafe_arena_set_allocated_$name$($Submsg$* value) { inline void $Msg$::unsafe_arena_set_allocated_$name$($Submsg$* value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
//~ If we're not on an arena, free whatever we were holding before. //~ If we're not on an arena, free whatever we were holding before.
@ -223,6 +225,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_unsafe_arena_set_allocated:$pkg.Msg.field$) // @@protoc_insertion_point(field_unsafe_arena_set_allocated:$pkg.Msg.field$)
} }
inline $Submsg$* $Msg$::$release_name$() { inline $Submsg$* $Msg$::$release_name$() {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$StrongRef$; $StrongRef$;
$annotate_release$; $annotate_release$;
@ -245,6 +248,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return released; return released;
} }
inline $Submsg$* $Msg$::unsafe_arena_release_$name$() { inline $Submsg$* $Msg$::unsafe_arena_release_$name$() {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$annotate_release$; $annotate_release$;
// @@protoc_insertion_point(field_release:$pkg.Msg.field$) // @@protoc_insertion_point(field_release:$pkg.Msg.field$)
@ -268,6 +272,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
inline $Submsg$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { inline $Submsg$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
//~ TODO: add tests to make sure all write accessors are //~ TODO: add tests to make sure all write accessors are
//~ able to prepare split message allocation. //~ able to prepare split message allocation.
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
$set_hasbit$; $set_hasbit$;
$Submsg$* _msg = _internal_mutable_$name_internal$(); $Submsg$* _msg = _internal_mutable_$name_internal$();
@ -278,6 +283,7 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
//~ We handle the most common case inline, and delegate less common //~ We handle the most common case inline, and delegate less common
//~ cases to the slow fallback function. //~ cases to the slow fallback function.
inline void $Msg$::set_allocated_$name$($Submsg$* value) { inline void $Msg$::set_allocated_$name$($Submsg$* value) {
$WeakDescriptorSelfPin$;
$pb$::Arena* message_arena = GetArena(); $pb$::Arena* message_arena = GetArena();
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
@ -522,6 +528,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $Submsg$* $Msg$::$release_name$() { inline $Submsg$* $Msg$::$release_name$() {
$WeakDescriptorSelfPin$;
$annotate_release$; $annotate_release$;
// @@protoc_insertion_point(field_release:$pkg.Msg.field$) // @@protoc_insertion_point(field_release:$pkg.Msg.field$)
$StrongRef$; $StrongRef$;
@ -546,6 +553,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { inline const $Submsg$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -553,6 +561,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline $Submsg$* $Msg$::unsafe_arena_release_$name$() { inline $Submsg$* $Msg$::unsafe_arena_release_$name$() {
$WeakDescriptorSelfPin$;
$annotate_release$; $annotate_release$;
// @@protoc_insertion_point(field_unsafe_arena_release:$pkg.Msg.field$) // @@protoc_insertion_point(field_unsafe_arena_release:$pkg.Msg.field$)
$StrongRef$; $StrongRef$;
@ -568,6 +577,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::unsafe_arena_set_allocated_$name$($Submsg$* value) { inline void $Msg$::unsafe_arena_set_allocated_$name$($Submsg$* value) {
$WeakDescriptorSelfPin$;
// We rely on the oneof clear method to free the earlier contents // We rely on the oneof clear method to free the earlier contents
// of this oneof. We can directly use the pointer we're given to // of this oneof. We can directly use the pointer we're given to
// set the new value. // set the new value.
@ -594,6 +604,7 @@ void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline $Submsg$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { inline $Submsg$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$Submsg$* _msg = _internal_mutable_$name_internal$(); $Submsg$* _msg = _internal_mutable_$name_internal$();
$annotate_mutable$; $annotate_mutable$;
// @@protoc_insertion_point(field_mutable:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable:$pkg.Msg.field$)
@ -742,6 +753,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $Submsg$* $Msg$::mutable_$name$(int index) inline $Submsg$* $Msg$::mutable_$name$(int index)
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable$; $annotate_mutable$;
// @@protoc_insertion_point(field_mutable:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable:$pkg.Msg.field$)
$StrongRef$; $StrongRef$;
@ -751,6 +763,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $pb$::RepeatedPtrField<$Submsg$>* $Msg$::mutable_$name$() inline $pb$::RepeatedPtrField<$Submsg$>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable_list$; $annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$StrongRef$; $StrongRef$;
@ -771,6 +784,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
R"cc( R"cc(
inline const $Submsg$& $Msg$::$name$(int index) const inline const $Submsg$& $Msg$::$name$(int index) const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
$StrongRef$; $StrongRef$;
@ -779,6 +793,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline $Submsg$* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { inline $Submsg$* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$Submsg$* _add = _internal_mutable_$name_internal$()->Add(); $Submsg$* _add = _internal_mutable_$name_internal$()->Add();
$annotate_add_mutable$; $annotate_add_mutable$;
@ -789,6 +804,7 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline const $pb$::RepeatedPtrField<$Submsg$>& $Msg$::$name$() const inline const $pb$::RepeatedPtrField<$Submsg$>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$; $annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_list:$pkg.Msg.field$)
$StrongRef$; $StrongRef$;

@ -188,6 +188,7 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions(
io::Printer* p) const { io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $Type$ $Msg$::$name$() const { inline $Type$ $Msg$::$name$() const {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -197,6 +198,7 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions(
if (is_oneof()) { if (is_oneof()) {
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::set_$name$($Type$ value) { inline void $Msg$::set_$name$($Type$ value) {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
if ($not_has_field$) { if ($not_has_field$) {
clear_$oneof_name$(); clear_$oneof_name$();
@ -216,6 +218,7 @@ void SingularPrimitive::GenerateInlineAccessorDefinitions(
} else { } else {
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::set_$name$($Type$ value) { inline void $Msg$::set_$name$($Type$ value) {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
_internal_set_$name_internal$(value); _internal_set_$name_internal$(value);
$set_hasbit$; $set_hasbit$;
@ -467,6 +470,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
io::Printer* p) const { io::Printer* p) const {
p->Emit(R"cc( p->Emit(R"cc(
inline $Type$ $Msg$::$name$(int index) const { inline $Type$ $Msg$::$name$(int index) const {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$().Get(index); return _internal_$name_internal$().Get(index);
@ -474,6 +478,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::set_$name$(int index, $Type$ value) { inline void $Msg$::set_$name$(int index, $Type$ value) {
$WeakDescriptorSelfPin$;
$annotate_set$; $annotate_set$;
_internal_mutable_$name_internal$()->Set(index, value); _internal_mutable_$name_internal$()->Set(index, value);
// @@protoc_insertion_point(field_set:$pkg.Msg.field$) // @@protoc_insertion_point(field_set:$pkg.Msg.field$)
@ -481,6 +486,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
)cc"); )cc");
p->Emit(R"cc( p->Emit(R"cc(
inline void $Msg$::add_$name$($Type$ value) { inline void $Msg$::add_$name$($Type$ value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add(value); _internal_mutable_$name_internal$()->Add(value);
$annotate_add$; $annotate_add$;
@ -490,6 +496,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
p->Emit(R"cc( p->Emit(R"cc(
inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$; $annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
@ -498,6 +505,7 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
p->Emit(R"cc( p->Emit(R"cc(
inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$() inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable_list$; $annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;

@ -448,6 +448,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
R"cc( R"cc(
inline const std::string& $Msg$::$name$() const inline const std::string& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
$if_IsDefault$; $if_IsDefault$;
@ -456,6 +457,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
template <typename Arg_, typename... Args_> template <typename Arg_, typename... Args_>
inline PROTOBUF_ALWAYS_INLINE void $Msg$::set_$name$(Arg_&& arg, inline PROTOBUF_ALWAYS_INLINE void $Msg$::set_$name$(Arg_&& arg,
Args_... args) { Args_... args) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
$update_hasbit$; $update_hasbit$;
@ -464,6 +466,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_set:$pkg.Msg.field$) // @@protoc_insertion_point(field_set:$pkg.Msg.field$)
} }
inline std::string* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { inline std::string* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
std::string* _s = _internal_mutable_$name_internal$(); std::string* _s = _internal_mutable_$name_internal$();
$annotate_mutable$; $annotate_mutable$;
@ -488,6 +491,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return $field_$.Mutable($lazy_args$, $set_args$); return $field_$.Mutable($lazy_args$, $set_args$);
} }
inline std::string* $Msg$::$release_name$() { inline std::string* $Msg$::$release_name$() {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$annotate_release$; $annotate_release$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
@ -495,6 +499,7 @@ void SingularString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
$release_impl$; $release_impl$;
} }
inline void $Msg$::set_allocated_$name$(std::string* value) { inline void $Msg$::set_allocated_$name$(std::string* value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
$PrepareSplitMessageForWrite$; $PrepareSplitMessageForWrite$;
$set_allocated_impl$; $set_allocated_impl$;
@ -870,6 +875,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
R"cc( R"cc(
inline std::string* $Msg$::add_$name$() inline std::string* $Msg$::add_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
std::string* _s = _internal_mutable_$name_internal$()->Add(); std::string* _s = _internal_mutable_$name_internal$()->Add();
$annotate_add_mutable$; $annotate_add_mutable$;
@ -878,27 +884,32 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
} }
inline const std::string& $Msg$::$name$(int index) const inline const std::string& $Msg$::$name$(int index) const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$; $annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$) // @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$().$Get$(index$GetExtraArg$); return _internal_$name_internal$().$Get$(index$GetExtraArg$);
} }
inline std::string* $Msg$::mutable_$name$(int index) inline std::string* $Msg$::mutable_$name$(int index)
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable$; $annotate_mutable$;
// @@protoc_insertion_point(field_mutable:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable:$pkg.Msg.field$)
return _internal_mutable_$name_internal$()->Mutable(index); return _internal_mutable_$name_internal$()->Mutable(index);
} }
inline void $Msg$::set_$name$(int index, const std::string& value) { inline void $Msg$::set_$name$(int index, const std::string& value) {
$WeakDescriptorSelfPin$;
_internal_mutable_$name_internal$()->Mutable(index)->assign(value); _internal_mutable_$name_internal$()->Mutable(index)->assign(value);
$annotate_set$; $annotate_set$;
// @@protoc_insertion_point(field_set:$pkg.Msg.field$) // @@protoc_insertion_point(field_set:$pkg.Msg.field$)
} }
inline void $Msg$::set_$name$(int index, std::string&& value) { inline void $Msg$::set_$name$(int index, std::string&& value) {
$WeakDescriptorSelfPin$;
_internal_mutable_$name_internal$()->Mutable(index)->assign(std::move(value)); _internal_mutable_$name_internal$()->Mutable(index)->assign(std::move(value));
$annotate_set$; $annotate_set$;
// @@protoc_insertion_point(field_set:$pkg.Msg.field$) // @@protoc_insertion_point(field_set:$pkg.Msg.field$)
} }
inline void $Msg$::set_$name$(int index, const char* value) { inline void $Msg$::set_$name$(int index, const char* value) {
$WeakDescriptorSelfPin$;
$DCHK$(value != nullptr); $DCHK$(value != nullptr);
_internal_mutable_$name_internal$()->Mutable(index)->assign(value); _internal_mutable_$name_internal$()->Mutable(index)->assign(value);
$annotate_set$; $annotate_set$;
@ -906,30 +917,35 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
} }
inline void $Msg$::set_$name$(int index, const $byte$* value, inline void $Msg$::set_$name$(int index, const $byte$* value,
std::size_t size) { std::size_t size) {
$WeakDescriptorSelfPin$;
_internal_mutable_$name_internal$()->Mutable(index)->assign( _internal_mutable_$name_internal$()->Mutable(index)->assign(
reinterpret_cast<const char*>(value), size); reinterpret_cast<const char*>(value), size);
$annotate_set$; $annotate_set$;
// @@protoc_insertion_point(field_set_pointer:$pkg.Msg.field$) // @@protoc_insertion_point(field_set_pointer:$pkg.Msg.field$)
} }
inline void $Msg$::set_$name$(int index, absl::string_view value) { inline void $Msg$::set_$name$(int index, absl::string_view value) {
$WeakDescriptorSelfPin$;
_internal_mutable_$name_internal$()->Mutable(index)->assign( _internal_mutable_$name_internal$()->Mutable(index)->assign(
value.data(), value.size()); value.data(), value.size());
$annotate_set$; $annotate_set$;
// @@protoc_insertion_point(field_set_string_piece:$pkg.Msg.field$) // @@protoc_insertion_point(field_set_string_piece:$pkg.Msg.field$)
} }
inline void $Msg$::add_$name$(const std::string& value) { inline void $Msg$::add_$name$(const std::string& value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add()->assign(value); _internal_mutable_$name_internal$()->Add()->assign(value);
$annotate_add$; $annotate_add$;
// @@protoc_insertion_point(field_add:$pkg.Msg.field$) // @@protoc_insertion_point(field_add:$pkg.Msg.field$)
} }
inline void $Msg$::add_$name$(std::string&& value) { inline void $Msg$::add_$name$(std::string&& value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add(std::move(value)); _internal_mutable_$name_internal$()->Add(std::move(value));
$annotate_add$; $annotate_add$;
// @@protoc_insertion_point(field_add:$pkg.Msg.field$) // @@protoc_insertion_point(field_add:$pkg.Msg.field$)
} }
inline void $Msg$::add_$name$(const char* value) { inline void $Msg$::add_$name$(const char* value) {
$WeakDescriptorSelfPin$;
$DCHK$(value != nullptr); $DCHK$(value != nullptr);
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add()->assign(value); _internal_mutable_$name_internal$()->Add()->assign(value);
@ -937,6 +953,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_add_char:$pkg.Msg.field$) // @@protoc_insertion_point(field_add_char:$pkg.Msg.field$)
} }
inline void $Msg$::add_$name$(const $byte$* value, std::size_t size) { inline void $Msg$::add_$name$(const $byte$* value, std::size_t size) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add()->assign( _internal_mutable_$name_internal$()->Add()->assign(
reinterpret_cast<const char*>(value), size); reinterpret_cast<const char*>(value), size);
@ -944,6 +961,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_add_pointer:$pkg.Msg.field$) // @@protoc_insertion_point(field_add_pointer:$pkg.Msg.field$)
} }
inline void $Msg$::add_$name$(absl::string_view value) { inline void $Msg$::add_$name$(absl::string_view value) {
$WeakDescriptorSelfPin$;
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;
_internal_mutable_$name_internal$()->Add()->assign(value.data(), _internal_mutable_$name_internal$()->Add()->assign(value.data(),
value.size()); value.size());
@ -952,12 +970,14 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
} }
inline const ::$proto_ns$::RepeatedPtrField<std::string>& inline const ::$proto_ns$::RepeatedPtrField<std::string>&
$Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND { $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$; $annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name_internal$(); return _internal_$name_internal$();
} }
inline ::$proto_ns$::RepeatedPtrField<std::string>* inline ::$proto_ns$::RepeatedPtrField<std::string>*
$Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND { $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_mutable_list$; $annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$) // @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$; $TsanDetectConcurrentMutation$;

@ -802,7 +802,10 @@ bool IsImplicitWeakField(const FieldDescriptor* field, const Options& options,
inline std::string SimpleBaseClass(const Descriptor* desc, inline std::string SimpleBaseClass(const Descriptor* desc,
const Options& options) { const Options& options) {
// The only base class we have derived from `Message`.
if (!HasDescriptorMethods(desc->file(), options)) return ""; if (!HasDescriptorMethods(desc->file(), options)) return "";
// We don't use the base class to be able to inject the weak descriptor pins.
if (UsingImplicitWeakDescriptor(desc->file(), options)) return "";
if (desc->extension_range_count() != 0) return ""; if (desc->extension_range_count() != 0) return "";
// Don't use a simple base class if the field tracking is enabled. This // Don't use a simple base class if the field tracking is enabled. This
// ensures generating all methods to track. // ensures generating all methods to track.

@ -472,24 +472,35 @@ bool MaybeEmitHaswordsCheck(ChunkIterator it, ChunkIterator end,
return true; return true;
} }
absl::flat_hash_map<absl::string_view, std::string> ClassVars( using Sub = ::google::protobuf::io::Printer::Sub;
const Descriptor* desc, Options opts) { std::vector<Sub> ClassVars(const Descriptor* desc, Options opts) {
absl::flat_hash_map<absl::string_view, std::string> vars = MessageVars(desc); std::vector<Sub> vars = {
{"pkg", Namespace(desc, opts)},
vars.emplace("pkg", Namespace(desc, opts)); {"Msg", ClassName(desc, false)},
vars.emplace("Msg", ClassName(desc, false)); {"pkg::Msg", QualifiedClassName(desc, opts)},
vars.emplace("pkg::Msg", QualifiedClassName(desc, opts)); {"pkg.Msg", desc->full_name()},
vars.emplace("pkg.Msg", desc->full_name());
// Old-style names, to be removed once all usages are gone in this and
// Old-style names, to be removed once all usages are gone in this and other // other files.
// files. {"classname", ClassName(desc, false)},
vars.emplace("classname", ClassName(desc, false)); {"classtype", QualifiedClassName(desc, opts)},
vars.emplace("classtype", QualifiedClassName(desc, opts)); {"full_name", desc->full_name()},
vars.emplace("full_name", desc->full_name()); {"superclass", SuperClassName(desc, opts)},
vars.emplace("superclass", SuperClassName(desc, opts));
Sub("WeakDescriptorSelfPin",
UsingImplicitWeakDescriptor(desc->file(), opts)
? absl::StrCat("::", ProtobufNamespace(opts),
"::internal::StrongReference(default_instance());")
: "")
.WithSuffix(";"),
};
for (auto& pair : MessageVars(desc)) {
vars.push_back({std::string(pair.first), pair.second});
}
for (auto& pair : UnknownFieldsVars(desc, opts)) { for (auto& pair : UnknownFieldsVars(desc, opts)) {
vars.emplace(pair); vars.push_back({std::string(pair.first), pair.second});
} }
return vars; return vars;
@ -747,6 +758,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
inline bool HasExtension( inline bool HasExtension(
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) const { _field_type, _is_packed>& id) const {
$WeakDescriptorSelfPin$;
$annotate_extension_has$; $annotate_extension_has$;
return $extensions$.Has(id.number()); return $extensions$.Has(id.number());
} }
@ -756,6 +768,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
inline void ClearExtension( inline void ClearExtension(
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) { _field_type, _is_packed>& id) {
$WeakDescriptorSelfPin$;
$extensions$.ClearExtension(id.number()); $extensions$.ClearExtension(id.number());
$annotate_extension_clear$; $annotate_extension_clear$;
} }
@ -765,6 +778,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
inline int ExtensionSize( inline int ExtensionSize(
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) const { _field_type, _is_packed>& id) const {
$WeakDescriptorSelfPin$;
$annotate_extension_repeated_size$; $annotate_extension_repeated_size$;
return $extensions$.ExtensionSize(id.number()); return $extensions$.ExtensionSize(id.number());
} }
@ -775,6 +789,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
inline typename _proto_TypeTraits::Singular::ConstType GetExtension( inline typename _proto_TypeTraits::Singular::ConstType GetExtension(
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) const { _field_type, _is_packed>& id) const {
$WeakDescriptorSelfPin$;
$annotate_extension_get$; $annotate_extension_get$;
return _proto_TypeTraits::Get(id.number(), $extensions$, id.default_value()); return _proto_TypeTraits::Get(id.number(), $extensions$, id.default_value());
} }
@ -786,6 +801,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) const _field_type, _is_packed>& id) const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_extension_get$; $annotate_extension_get$;
return _proto_TypeTraits::Get(id.number(), $extensions$, id.default_value()); return _proto_TypeTraits::Get(id.number(), $extensions$, id.default_value());
} }
@ -796,6 +812,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) _field_type, _is_packed>& id)
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_extension_mutable$; $annotate_extension_mutable$;
return _proto_TypeTraits::Mutable(id.number(), _field_type, &$extensions$); return _proto_TypeTraits::Mutable(id.number(), _field_type, &$extensions$);
} }
@ -806,6 +823,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
typename _proto_TypeTraits::Singular::ConstType value) { typename _proto_TypeTraits::Singular::ConstType value) {
$WeakDescriptorSelfPin$;
_proto_TypeTraits::Set(id.number(), _field_type, value, &$extensions$); _proto_TypeTraits::Set(id.number(), _field_type, value, &$extensions$);
$annotate_extension_set$; $annotate_extension_set$;
} }
@ -816,6 +834,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
typename _proto_TypeTraits::Singular::MutableType value) { typename _proto_TypeTraits::Singular::MutableType value) {
$WeakDescriptorSelfPin$;
_proto_TypeTraits::SetAllocated(id.number(), _field_type, value, _proto_TypeTraits::SetAllocated(id.number(), _field_type, value,
&$extensions$); &$extensions$);
$annotate_extension_set$; $annotate_extension_set$;
@ -826,6 +845,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
typename _proto_TypeTraits::Singular::MutableType value) { typename _proto_TypeTraits::Singular::MutableType value) {
$WeakDescriptorSelfPin$;
_proto_TypeTraits::UnsafeArenaSetAllocated(id.number(), _field_type, _proto_TypeTraits::UnsafeArenaSetAllocated(id.number(), _field_type,
value, &$extensions$); value, &$extensions$);
$annotate_extension_set$; $annotate_extension_set$;
@ -837,6 +857,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
ReleaseExtension( ReleaseExtension(
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) { _field_type, _is_packed>& id) {
$WeakDescriptorSelfPin$;
$annotate_extension_release$; $annotate_extension_release$;
return _proto_TypeTraits::Release(id.number(), _field_type, &$extensions$); return _proto_TypeTraits::Release(id.number(), _field_type, &$extensions$);
} }
@ -846,6 +867,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
UnsafeArenaReleaseExtension( UnsafeArenaReleaseExtension(
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) { _field_type, _is_packed>& id) {
$WeakDescriptorSelfPin$;
$annotate_extension_release$; $annotate_extension_release$;
return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type, return _proto_TypeTraits::UnsafeArenaRelease(id.number(), _field_type,
&$extensions$); &$extensions$);
@ -858,6 +880,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
int index) const { int index) const {
$WeakDescriptorSelfPin$;
$annotate_repeated_extension_get$; $annotate_repeated_extension_get$;
return _proto_TypeTraits::Get(id.number(), $extensions$, index); return _proto_TypeTraits::Get(id.number(), $extensions$, index);
} }
@ -869,6 +892,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND { int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_repeated_extension_get$; $annotate_repeated_extension_get$;
return _proto_TypeTraits::Get(id.number(), $extensions$, index); return _proto_TypeTraits::Get(id.number(), $extensions$, index);
} }
@ -879,6 +903,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
int index) ABSL_ATTRIBUTE_LIFETIME_BOUND { int index) ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_repeated_extension_mutable$; $annotate_repeated_extension_mutable$;
return _proto_TypeTraits::Mutable(id.number(), index, &$extensions$); return _proto_TypeTraits::Mutable(id.number(), index, &$extensions$);
} }
@ -889,6 +914,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
int index, typename _proto_TypeTraits::Repeated::ConstType value) { int index, typename _proto_TypeTraits::Repeated::ConstType value) {
$WeakDescriptorSelfPin$;
_proto_TypeTraits::Set(id.number(), index, value, &$extensions$); _proto_TypeTraits::Set(id.number(), index, value, &$extensions$);
$annotate_repeated_extension_set$; $annotate_repeated_extension_set$;
} }
@ -899,6 +925,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) _field_type, _is_packed>& id)
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
typename _proto_TypeTraits::Repeated::MutableType to_add = typename _proto_TypeTraits::Repeated::MutableType to_add =
_proto_TypeTraits::Add(id.number(), _field_type, &$extensions$); _proto_TypeTraits::Add(id.number(), _field_type, &$extensions$);
$annotate_repeated_extension_add_mutable$; $annotate_repeated_extension_add_mutable$;
@ -911,6 +938,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id, _field_type, _is_packed>& id,
typename _proto_TypeTraits::Repeated::ConstType value) { typename _proto_TypeTraits::Repeated::ConstType value) {
$WeakDescriptorSelfPin$;
_proto_TypeTraits::Add(id.number(), _field_type, _is_packed, value, _proto_TypeTraits::Add(id.number(), _field_type, _is_packed, value,
&$extensions$); &$extensions$);
$annotate_repeated_extension_add$; $annotate_repeated_extension_add$;
@ -923,6 +951,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) const _field_type, _is_packed>& id) const
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_repeated_extension_list$; $annotate_repeated_extension_list$;
return _proto_TypeTraits::GetRepeated(id.number(), $extensions$); return _proto_TypeTraits::GetRepeated(id.number(), $extensions$);
} }
@ -934,6 +963,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits, const $pbi$::ExtensionIdentifier<$Msg$, _proto_TypeTraits,
_field_type, _is_packed>& id) _field_type, _is_packed>& id)
ABSL_ATTRIBUTE_LIFETIME_BOUND { ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_repeated_extension_list_mutable$; $annotate_repeated_extension_list_mutable$;
return _proto_TypeTraits::MutableRepeated(id.number(), _field_type, return _proto_TypeTraits::MutableRepeated(id.number(), _field_type,
_is_packed, &$extensions$); _is_packed, &$extensions$);
@ -971,6 +1001,7 @@ void MessageGenerator::GenerateSingularFieldHasBits(
p->Emit( p->Emit(
R"cc( R"cc(
inline bool $classname$::has_$name$() const { inline bool $classname$::has_$name$() const {
$WeakDescriptorSelfPin$;
$annotate_has$; $annotate_has$;
return $weak_field_map$.Has($number$); return $weak_field_map$.Has($number$);
} }
@ -995,6 +1026,7 @@ void MessageGenerator::GenerateSingularFieldHasBits(
.WithSuffix(";")}, .WithSuffix(";")},
R"cc( R"cc(
inline bool $classname$::has_$name$() const { inline bool $classname$::has_$name$() const {
$WeakDescriptorSelfPin$;
$annotate_has$; $annotate_has$;
bool value = ($has_bits$[$has_array_index$] & $has_mask$) != 0; bool value = ($has_bits$[$has_array_index$] & $has_mask$) != 0;
$ASSUME$; $ASSUME$;
@ -1054,6 +1086,7 @@ void MessageGenerator::GenerateOneofMemberHasBits(const FieldDescriptor* field,
auto t = p->WithVars(MakeTrackerCalls(field, options_)); auto t = p->WithVars(MakeTrackerCalls(field, options_));
p->Emit(R"cc( p->Emit(R"cc(
inline bool $classname$::has_$name$() const { inline bool $classname$::has_$name$() const {
$WeakDescriptorSelfPin$;
$annotate_has$; $annotate_has$;
return $has_field$; return $has_field$;
} }
@ -1115,6 +1148,7 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field,
R"cc( R"cc(
$inline $void $classname$::clear_$name$() { $inline $void $classname$::clear_$name$() {
PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race);
$WeakDescriptorSelfPin$;
$body$; $body$;
$annotate_clear$; $annotate_clear$;
} }
@ -1127,9 +1161,10 @@ class AccessorVerifier {
public: public:
using SourceLocation = io::Printer::SourceLocation; using SourceLocation = io::Printer::SourceLocation;
AccessorVerifier(const FieldDescriptor* field) : field_(field) {} explicit AccessorVerifier(const FieldDescriptor* field) : field_(field) {}
~AccessorVerifier() { ~AccessorVerifier() {
ABSL_CHECK(!needs_annotate_) << Error(SourceLocation::current()); ABSL_CHECK(!needs_annotate_) << Error(SourceLocation::current());
ABSL_CHECK(!needs_weak_descriptor_pin_) << Error(SourceLocation::current());
} }
void operator()(absl::string_view label, io::Printer::SourceLocation loc) { void operator()(absl::string_view label, io::Printer::SourceLocation loc) {
@ -1137,14 +1172,17 @@ class AccessorVerifier {
// All accessors use $name$ or $release_name$ when constructing the // All accessors use $name$ or $release_name$ when constructing the
// function name. We hook into those to determine that an accessor is // function name. We hook into those to determine that an accessor is
// starting. // starting.
ABSL_CHECK(!needs_annotate_) << Error(loc); SetTracker(needs_annotate_, true, loc);
SetTracker(needs_weak_descriptor_pin_, true, loc);
loc_ = loc; loc_ = loc;
needs_annotate_ = true;
} else if (absl::StartsWith(label, "annotate")) { } else if (absl::StartsWith(label, "annotate")) {
// All annotation labels start with `annotate`. Eg `annotate_get`. // All annotation labels start with `annotate`. Eg `annotate_get`.
ABSL_CHECK(needs_annotate_) << Error(loc); SetTracker(needs_annotate_, false, loc);
loc_ = loc;
} else if (label == "WeakDescriptorSelfPin") {
// The self pin for weak descriptor types must be on every accessor.
SetTracker(needs_weak_descriptor_pin_, false, loc);
loc_ = loc; loc_ = loc;
needs_annotate_ = false;
} }
} }
@ -1155,7 +1193,13 @@ class AccessorVerifier {
loc_.file_name(), loc_.line()); loc_.file_name(), loc_.line());
} }
void SetTracker(bool& v, bool new_value, SourceLocation loc) {
ABSL_CHECK_NE(v, new_value) << Error(loc);
v = new_value;
}
bool needs_annotate_ = false; bool needs_annotate_ = false;
bool needs_weak_descriptor_pin_ = false;
// We keep these fields for error reporting. // We keep these fields for error reporting.
const FieldDescriptor* field_; const FieldDescriptor* field_;
// On error, we report two locations: the current one and the last one. This // On error, we report two locations: the current one and the last one. This
@ -1194,6 +1238,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
return _internal_$name_internal$().size(); return _internal_$name_internal$().size();
} }
inline int $classname$::$name$_size() const { inline int $classname$::$name$_size() const {
$WeakDescriptorSelfPin$;
$annotate_size$; $annotate_size$;
return _internal_$name_internal$_size(); return _internal_$name_internal$_size();
} }
@ -2712,6 +2757,7 @@ void MessageGenerator::GenerateSharedDestructorCode(io::Printer* p) {
R"cc( R"cc(
inline void $classname$::SharedDtor() { inline void $classname$::SharedDtor() {
$DCHK$(GetArena() == nullptr); $DCHK$(GetArena() == nullptr);
$WeakDescriptorSelfPin$;
$field_dtors$; $field_dtors$;
$split_field_dtors$; $split_field_dtors$;
$oneof_field_dtors$; $oneof_field_dtors$;
@ -3453,6 +3499,7 @@ void MessageGenerator::GenerateSwap(io::Printer* p) {
"{\n"); "{\n");
format.Indent(); format.Indent();
format("using std::swap;\n"); format("using std::swap;\n");
format("$WeakDescriptorSelfPin$");
if (HasGeneratedMethods(descriptor_->file(), options_)) { if (HasGeneratedMethods(descriptor_->file(), options_)) {
if (descriptor_->extension_range_count() > 0) { if (descriptor_->extension_range_count() > 0) {
@ -3657,6 +3704,7 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) {
format( format(
"void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const " "void $classname$::MergeImpl(::$proto_ns$::MessageLite& to_msg, const "
"::$proto_ns$::MessageLite& from_msg) {\n" "::$proto_ns$::MessageLite& from_msg) {\n"
"$WeakDescriptorSelfPin$"
" auto* const _this = static_cast<$classname$*>(&to_msg);\n" " auto* const _this = static_cast<$classname$*>(&to_msg);\n"
" auto& from = static_cast<const $classname$&>(from_msg);\n"); " auto& from = static_cast<const $classname$&>(from_msg);\n");
} }
@ -4421,6 +4469,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
p->Emit( p->Emit(
R"cc( R"cc(
PROTOBUF_NOINLINE ::size_t $classname$::ByteSizeLong() const { PROTOBUF_NOINLINE ::size_t $classname$::ByteSizeLong() const {
$WeakDescriptorSelfPin$;
$annotate_bytesize$; $annotate_bytesize$;
// @@protoc_insertion_point(message_set_byte_size_start:$full_name$) // @@protoc_insertion_point(message_set_byte_size_start:$full_name$)
::size_t total_size = $extensions$.MessageSetByteSize(); ::size_t total_size = $extensions$.MessageSetByteSize();
@ -4437,6 +4486,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
Formatter format(p); Formatter format(p);
format( format(
"::size_t $classname$::ByteSizeLong() const {\n" "::size_t $classname$::ByteSizeLong() const {\n"
"$WeakDescriptorSelfPin$"
"$annotate_bytesize$" "$annotate_bytesize$"
"// @@protoc_insertion_point(message_byte_size_start:$full_name$)\n"); "// @@protoc_insertion_point(message_byte_size_start:$full_name$)\n");
format.Indent(); format.Indent();
@ -4689,6 +4739,7 @@ void MessageGenerator::GenerateIsInitialized(io::Printer* p) {
}, },
R"cc( R"cc(
PROTOBUF_NOINLINE bool $classname$::IsInitialized() const { PROTOBUF_NOINLINE bool $classname$::IsInitialized() const {
$WeakDescriptorSelfPin$;
$test_extensions$; $test_extensions$;
$test_required_fields$; $test_required_fields$;
$test_ordinary_fields$; $test_ordinary_fields$;

Loading…
Cancel
Save