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