Use Emit instead of format for extension code gen.

PiperOrigin-RevId: 578983008
pull/14591/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent da7245ca0b
commit 1c549200ff
  1. 204
      src/google/protobuf/compiler/cpp/extension.cc
  2. 4
      src/google/protobuf/compiler/cpp/extension.h
  3. 8
      src/google/protobuf/compiler/java/java_features.pb.cc
  4. 7
      src/google/protobuf/compiler/java/java_features.pb.h
  5. 10
      src/google/protobuf/cpp_features.pb.cc
  6. 7
      src/google/protobuf/cpp_features.pb.h

@ -11,9 +11,12 @@
#include "google/protobuf/compiler/cpp/extension.h" #include "google/protobuf/compiler/cpp/extension.h"
#include <string>
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_replace.h" #include "absl/strings/str_replace.h"
#include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/cpp/options.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/printer.h" #include "google/protobuf/io/printer.h"
@ -77,97 +80,134 @@ ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor,
variables_["scope"] = scope; variables_["scope"] = scope;
variables_["scoped_name"] = ExtensionName(descriptor_); variables_["scoped_name"] = ExtensionName(descriptor_);
variables_["number"] = absl::StrCat(descriptor_->number()); variables_["number"] = absl::StrCat(descriptor_->number());
bool add_verify_fn =
// Only verify msgs.
descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
// Options say to verify.
ShouldVerify(descriptor_->message_type(), options_, scc_analyzer_) &&
ShouldVerify(descriptor_->containing_type(), options_, scc_analyzer_);
variables_["verify_fn"] =
add_verify_fn
? absl::StrCat("&", FieldMessageTypeName(descriptor_, options_),
"::InternalVerify")
: "nullptr";
} }
ExtensionGenerator::~ExtensionGenerator() {} ExtensionGenerator::~ExtensionGenerator() = default;
bool ExtensionGenerator::IsScoped() const { bool ExtensionGenerator::IsScoped() const {
return descriptor_->extension_scope() != nullptr; return descriptor_->extension_scope() != nullptr;
} }
void ExtensionGenerator::GenerateDeclaration(io::Printer* printer) const { void ExtensionGenerator::GenerateDeclaration(io::Printer* p) const {
Formatter format(printer, variables_); auto var = p->WithVars(variables_);
auto annotate = p->WithAnnotations({{"name", descriptor_}});
// If this is a class member, it needs to be declared "static". Otherwise,
// it needs to be "extern". In the latter case, it also needs the DLL p->Emit({{"qualifier",
// export/import specifier. // If this is a class member, it needs to be declared "static".
std::string qualifier; // Otherwise, it needs to be "extern". In the latter case, it
if (!IsScoped()) { // also needs the DLL export/import specifier.
qualifier = "extern"; IsScoped() ? "static"
if (!options_.dllexport_decl.empty()) { : options_.dllexport_decl.empty()
qualifier = absl::StrCat(options_.dllexport_decl, " ", qualifier); ? "extern"
} : absl::StrCat(options_.dllexport_decl, " extern")}},
} else { R"cc(
qualifier = "static"; static const int $constant_name$ = $number$;
} $qualifier$ ::$proto_ns$::internal::ExtensionIdentifier<
$extendee$, ::$proto_ns$::internal::$type_traits$, $field_type$,
format( $packed$>
"static const int $constant_name$ = $number$;\n" $name$;
"$1$ ::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n" )cc");
" ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$ >\n"
" ${2$$name$$}$;\n",
qualifier, descriptor_);
} }
void ExtensionGenerator::GenerateDefinition(io::Printer* printer) { void ExtensionGenerator::GenerateDefinition(io::Printer* p) {
Formatter format(printer, variables_); auto vars = p->WithVars(variables_);
std::string default_str; auto generate_default_string = [&] {
// If this is a class member, it needs to be declared in its class scope. if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
if (descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { // We need to declare a global string which will contain the default
// We need to declare a global string which will contain the default value. // value. We cannot declare it at class scope because that would require
// We cannot declare it at class scope because that would require exposing // exposing it in the header which would be annoying for other reasons. So
// it in the header which would be annoying for other reasons. So we // we replace :: with _ in the name and declare it as a global.
// replace :: with _ in the name and declare it as a global. return absl::StrReplaceAll(variables_["scoped_name"], {{"::", "_"}}) +
default_str = "_default";
absl::StrReplaceAll(variables_["scoped_name"], {{"::", "_"}}) + } else if (descriptor_->message_type()) {
"_default"; // We have to initialize the default instance for extensions at
format("const std::string $1$($2$);\n", default_str, // registration time.
DefaultValue(options_, descriptor_)); return absl::StrCat(FieldMessageTypeName(descriptor_, options_),
} else if (descriptor_->message_type()) { "::default_instance()");
// We have to initialize the default instance for extensions at registration } else {
// time. return DefaultValue(options_, descriptor_);
default_str = absl::StrCat(FieldMessageTypeName(descriptor_, options_), }
"::default_instance()"); };
} else {
default_str = DefaultValue(options_, descriptor_); auto local_var = p->WithVars({
} {"default_str", generate_default_string()},
{"default_val", DefaultValue(options_, descriptor_)},
// Likewise, class members need to declare the field constant variable. {"message_type", descriptor_->message_type() != nullptr
if (IsScoped()) { ? FieldMessageTypeName(descriptor_, options_)
format( : ""},
"#if !defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912)\n" });
"const int $scope$$constant_name$;\n" p->Emit(
"#endif\n"); {
} {"declare_default_str",
[&] {
if (IsLazilyInitializedFile(descriptor_->file()->name())) { if (descriptor_->cpp_type() != FieldDescriptor::CPPTYPE_STRING)
format( return;
"PROTOBUF_CONSTINIT$ dllexport_decl$ "
"PROTOBUF_ATTRIBUTE_INIT_PRIORITY2\n" // If this is a class member, it needs to be declared in its class
"::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n" // scope.
" ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$>\n" p->Emit(R"cc(
" $scoped_name$($constant_name$);\n"); const std::string $default_str$($default_val$);
} else { )cc");
format( }},
"$dllexport_decl $PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 " {"declare_const_var",
"::$proto_ns$::internal::ExtensionIdentifier< $extendee$,\n" [&] {
" ::$proto_ns$::internal::$type_traits$, $field_type$, $packed$>\n" if (!IsScoped()) return;
" $scoped_name$($constant_name$, $1$, $verify_fn$);\n", // Likewise, class members need to declare the field constant
default_str); // variable.
} p->Emit(R"cc(
#if !defined(_MSC_VER) || (_MSC_VER >= 1900 && _MSC_VER < 1912)
const int $scope$$constant_name$;
#endif
)cc");
}},
{"define_extension_id",
[&] {
if (IsLazilyInitializedFile(descriptor_->file()->name())) {
p->Emit(R"cc(
PROTOBUF_CONSTINIT$ dllexport_decl$
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::$proto_ns$::internal::
ExtensionIdentifier<
$extendee$, ::$proto_ns$::internal::$type_traits$,
$field_type$, $packed$>
$scoped_name$($constant_name$);
)cc");
return;
}
bool should_verify =
// Only verify msgs.
descriptor_->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
// Options say to verify.
ShouldVerify(descriptor_->message_type(), options_,
scc_analyzer_) &&
ShouldVerify(descriptor_->containing_type(), options_,
scc_analyzer_);
if (should_verify) {
p->Emit(R"cc(
$dllexport_decl $PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::
$proto_ns$::internal::ExtensionIdentifier<
$extendee$, ::$proto_ns$::internal::$type_traits$,
$field_type$, $packed$>
$scoped_name$($constant_name$, $default_str$,
&$message_type$::InternalVerify);
)cc");
} else {
p->Emit(R"cc(
$dllexport_decl $PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::
$proto_ns$::internal::ExtensionIdentifier<
$extendee$, ::$proto_ns$::internal::$type_traits$,
$field_type$, $packed$>
$scoped_name$($constant_name$, $default_str$, nullptr);
)cc");
}
}},
},
R"cc(
$declare_default_str$;
$declare_const_var$;
$define_extension_id$;
)cc");
} }
} // namespace cpp } // namespace cpp

@ -51,10 +51,10 @@ class PROTOC_EXPORT ExtensionGenerator {
~ExtensionGenerator(); ~ExtensionGenerator();
// Header stuff. // Header stuff.
void GenerateDeclaration(io::Printer* printer) const; void GenerateDeclaration(io::Printer* p) const;
// Source file stuff. // Source file stuff.
void GenerateDefinition(io::Printer* printer); void GenerateDefinition(io::Printer* p);
bool IsScoped() const; bool IsScoped() const;

@ -375,9 +375,11 @@ void JavaFeatures::InternalSwap(JavaFeatures* PROTOBUF_RESTRICT other) {
&descriptor_table_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto_getter, &descriptor_table_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto_once, &descriptor_table_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto_getter, &descriptor_table_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto_once,
file_level_metadata_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto[0]); file_level_metadata_google_2fprotobuf_2fcompiler_2fjava_2fjava_5ffeatures_2eproto[0]);
} }
PROTOC_EXPORT PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::FeatureSet, PROTOC_EXPORT PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::
::google::protobuf::internal::MessageTypeTraits< ::pb::JavaFeatures >, 11, false> google::protobuf::internal::ExtensionIdentifier<
java(kJavaFieldNumber, ::pb::JavaFeatures::default_instance(), nullptr); ::google::protobuf::FeatureSet, ::google::protobuf::internal::MessageTypeTraits< ::pb::JavaFeatures >,
11, false>
java(kJavaFieldNumber, ::pb::JavaFeatures::default_instance(), nullptr);
// @@protoc_insertion_point(namespace_scope) // @@protoc_insertion_point(namespace_scope)
} // namespace pb } // namespace pb
namespace google { namespace google {

@ -318,9 +318,10 @@ class PROTOC_EXPORT JavaFeatures final :
static const int kJavaFieldNumber = 1001; static const int kJavaFieldNumber = 1001;
PROTOC_EXPORT extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::FeatureSet, PROTOC_EXPORT extern ::google::protobuf::internal::ExtensionIdentifier<
::google::protobuf::internal::MessageTypeTraits< ::pb::JavaFeatures >, 11, false > ::google::protobuf::FeatureSet, ::google::protobuf::internal::MessageTypeTraits< ::pb::JavaFeatures >, 11,
java; false>
java;
// =================================================================== // ===================================================================

@ -298,10 +298,12 @@ void CppFeatures::InternalSwap(CppFeatures* PROTOBUF_RESTRICT other) {
&descriptor_table_google_2fprotobuf_2fcpp_5ffeatures_2eproto_getter, &descriptor_table_google_2fprotobuf_2fcpp_5ffeatures_2eproto_once, &descriptor_table_google_2fprotobuf_2fcpp_5ffeatures_2eproto_getter, &descriptor_table_google_2fprotobuf_2fcpp_5ffeatures_2eproto_once,
file_level_metadata_google_2fprotobuf_2fcpp_5ffeatures_2eproto[0]); file_level_metadata_google_2fprotobuf_2fcpp_5ffeatures_2eproto[0]);
} }
PROTOBUF_CONSTINIT PROTOBUF_EXPORT PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 PROTOBUF_CONSTINIT PROTOBUF_EXPORT
::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::FeatureSet, PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 ::google::protobuf::internal::
::google::protobuf::internal::MessageTypeTraits< ::pb::CppFeatures >, 11, false> ExtensionIdentifier<
cpp(kCppFieldNumber); ::google::protobuf::FeatureSet, ::google::protobuf::internal::MessageTypeTraits< ::pb::CppFeatures >,
11, false>
cpp(kCppFieldNumber);
// @@protoc_insertion_point(namespace_scope) // @@protoc_insertion_point(namespace_scope)
} // namespace pb } // namespace pb
namespace google { namespace google {

@ -253,9 +253,10 @@ class PROTOBUF_EXPORT CppFeatures final :
static const int kCppFieldNumber = 1000; static const int kCppFieldNumber = 1000;
PROTOBUF_EXPORT extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::FeatureSet, PROTOBUF_EXPORT extern ::google::protobuf::internal::ExtensionIdentifier<
::google::protobuf::internal::MessageTypeTraits< ::pb::CppFeatures >, 11, false > ::google::protobuf::FeatureSet, ::google::protobuf::internal::MessageTypeTraits< ::pb::CppFeatures >, 11,
cpp; false>
cpp;
// =================================================================== // ===================================================================

Loading…
Cancel
Save