@@ -101,67 +101,59 @@ bool ExtensionGenerator::IsScoped() const {
101101 return descriptor_->extension_scope () != nullptr ;
102102}
103103
104- namespace {
105- bool ShouldGenerateFeatureSetDefaultData (absl::string_view extension) {
106- return extension == " pb.java" || extension == " pb.java_mutable" ||
107- extension == " pb.test" || extension == " pb.proto1" ;
104+ void ExtensionGenerator::GenerateFeatureDefaults (io::Printer* p) const {
105+ auto var = p->WithVars (variables_);
106+ auto annotate = p->WithAnnotations ({{" name" , descriptor_}});
107+ if (descriptor_->message_type () == nullptr ) return ;
108+ absl::string_view extendee = descriptor_->containing_type ()->full_name ();
109+ if (extendee != " google.protobuf.FeatureSet" ) return ;
110+
111+ absl::StatusOr<FeatureSetDefaults> defaults =
112+ FeatureResolver::CompileDefaults (descriptor_->containing_type (),
113+ {descriptor_}, ProtocMinimumEdition (),
114+ MaximumKnownEdition ());
115+ if (!defaults.ok ()) {
116+ return ;
117+ }
118+ p->Emit ({{" defaults" , absl::Base64Escape (defaults->SerializeAsString ())},
119+ {" extension_type" , ClassName (descriptor_->message_type (), true )},
120+ {" function_name" , " GetFeatureSetDefaultsData" }},
121+ R"cc(
122+ namespace internal {
123+ template <>
124+ inline ::absl::string_view $function_name$<$extension_type$>() {
125+ static constexpr char kDefaults[] = "$defaults$";
126+ return kDefaults;
127+ }
128+ } // namespace internal
129+ )cc" );
108130}
109- } // namespace
110131
111132void ExtensionGenerator::GenerateDeclaration (io::Printer* p) const {
112133 auto var = p->WithVars (variables_);
113134 auto annotate = p->WithAnnotations ({{" name" , descriptor_}});
114135 p->Emit (
115- {{" constant_qualifier" ,
116- // If this is a class member, it needs to be declared
117- // `static constexpr`.
118- // Otherwise, it will be
119- // `inline constexpr`.
120- IsScoped () ? " static" : " " },
121- {" id_qualifier" ,
122- // If this is a class member, it needs to be declared "static".
123- // Otherwise, it needs to be "extern". In the latter case, it
124- // also needs the DLL export/import specifier.
125- IsScoped () ? " static"
126- : options_.dllexport_decl .empty ()
127- ? " extern"
128- : absl::StrCat (options_.dllexport_decl , " extern" )},
129- {" feature_set_defaults" ,
130- [&] {
131- if (!ShouldGenerateFeatureSetDefaultData (descriptor_->full_name ())) {
132- return ;
133- }
134- if (descriptor_->message_type () == nullptr ) return ;
135- absl::string_view extendee =
136- descriptor_->containing_type ()->full_name ();
137- if (extendee != " google.protobuf.FeatureSet" ) return ;
138-
139- std::vector<const FieldDescriptor*> extensions = {descriptor_};
140- absl::StatusOr<FeatureSetDefaults> defaults =
141- FeatureResolver::CompileDefaults (
142- descriptor_->containing_type (), extensions,
143- ProtocMinimumEdition (), MaximumKnownEdition ());
144- ABSL_CHECK_OK (defaults);
145- p->Emit (
146- {{" defaults" , absl::Base64Escape (defaults->SerializeAsString ())},
147- {" extension_type" , ClassName (descriptor_->message_type (), true )},
148- {" function_name" , " GetFeatureSetDefaultsData" }},
149- R"cc(
150- namespace internal {
151- template <>
152- inline ::absl::string_view $function_name$<$extension_type$>() {
153- static constexpr char kDefaults[] = "$defaults$";
154- return kDefaults;
155- }
156- } // namespace internal
157- )cc" );
158- }}},
136+ {
137+ {" constant_qualifier" ,
138+ // If this is a class member, it needs to be declared
139+ // `static constexpr`.
140+ // Otherwise, it will be
141+ // `inline constexpr`.
142+ IsScoped () ? " static" : " " },
143+ {" id_qualifier" ,
144+ // If this is a class member, it needs to be declared "static".
145+ // Otherwise, it needs to be "extern". In the latter case, it
146+ // also needs the DLL export/import specifier.
147+ IsScoped () ? " static"
148+ : options_.dllexport_decl .empty ()
149+ ? " extern"
150+ : absl::StrCat (options_.dllexport_decl , " extern" )},
151+ },
159152 R"cc(
160153 inline $constant_qualifier $constexpr int $constant_name$ = $number$;
161154 $id_qualifier$ $pbi$::ExtensionIdentifier<
162155 $extendee$, $pbi$::$type_traits$, $field_type$, $packed$>
163156 $name$;
164- $feature_set_defaults$;
165157 )cc" );
166158}
167159
0 commit comments