diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index ac007958c9..5330bf04d4 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -138,6 +138,7 @@ cc_library( includes = ["."], deps = [ ":conformance_cc_proto", + "//src/google/protobuf:descriptor_legacy", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", ], diff --git a/conformance/conformance_test.cc b/conformance/conformance_test.cc index aba07a0f1d..4d7f6dcc96 100644 --- a/conformance/conformance_test.cc +++ b/conformance/conformance_test.cc @@ -46,6 +46,7 @@ #include "absl/strings/string_view.h" #include "conformance/conformance.pb.h" #include "conformance/conformance.pb.h" +#include "google/protobuf/descriptor_legacy.h" using conformance::ConformanceRequest; using conformance::ConformanceResponse; @@ -163,11 +164,12 @@ ConformanceTestSuite::ConformanceRequestSetting::NewTestMessage() const { string ConformanceTestSuite::ConformanceRequestSetting::GetTestName() const { string rname; - switch (prototype_message_.GetDescriptor()->file()->syntax()) { - case FileDescriptor::SYNTAX_PROTO3: + switch (FileDescriptorLegacy(prototype_message_.GetDescriptor()->file()) + .syntax()) { + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: rname = ".Proto3."; break; - case FileDescriptor::SYNTAX_PROTO2: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: rname = ".Proto2."; break; default: diff --git a/python/BUILD.bazel b/python/BUILD.bazel index f6fe071152..d3f8cfb200 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -114,6 +114,7 @@ cc_binary( deps = [ ":proto_api", "//:protobuf", + "//src/google/protobuf:descriptor_legacy", ] + select({ "//conditions:default": [], ":use_fast_cpp_protos": ["//external:python_headers"], diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc index de9a248073..c1fd20cdbb 100644 --- a/python/google/protobuf/pyext/descriptor.cc +++ b/python/google/protobuf/pyext/descriptor.cc @@ -33,6 +33,7 @@ #include "google/protobuf/pyext/descriptor.h" #include "absl/log/absl_check.h" +#include "google/protobuf/descriptor_legacy.h" #define PY_SSIZE_T_CLEAN #include @@ -692,8 +693,9 @@ static PyObject* EnumValueName(PyBaseDescriptor *self, PyObject *args) { } static PyObject* GetSyntax(PyBaseDescriptor *self, void *closure) { - return PyUnicode_InternFromString( - FileDescriptor::SyntaxName(_GetDescriptor(self)->file()->syntax())); + std::string syntax(FileDescriptorLegacy::SyntaxName( + FileDescriptorLegacy(_GetDescriptor(self)->file()).syntax())); + return PyUnicode_InternFromString(syntax.c_str()); } static PyGetSetDef Getters[] = { @@ -1512,8 +1514,9 @@ static int SetSerializedOptions(PyFileDescriptor *self, PyObject *value, } static PyObject* GetSyntax(PyFileDescriptor *self, void *closure) { - return PyUnicode_InternFromString( - FileDescriptor::SyntaxName(_GetDescriptor(self)->syntax())); + std::string syntax(FileDescriptorLegacy::SyntaxName( + FileDescriptorLegacy(_GetDescriptor(self)).syntax())); + return PyUnicode_InternFromString(syntax.c_str()); } static PyObject* CopyToProto(PyFileDescriptor *self, PyObject *target) { diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 5744cc9ece..d76201d8f1 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -483,6 +483,9 @@ cc_library( cc_library( name = "descriptor_legacy", hdrs = ["descriptor_legacy.h"], + copts = COPTS, + include_prefix = "google/protobuf", + linkopts = LINK_OPTS, deps = [ ":protobuf_nowkt", ":port_def", diff --git a/src/google/protobuf/compiler/BUILD.bazel b/src/google/protobuf/compiler/BUILD.bazel index c88020580e..7bbce08b4a 100644 --- a/src/google/protobuf/compiler/BUILD.bazel +++ b/src/google/protobuf/compiler/BUILD.bazel @@ -90,6 +90,7 @@ cc_library( ":code_generator", ":importer", ":retention", + "//src/google/protobuf:descriptor_legacy", "//src/google/protobuf:protobuf_nowkt", "//src/google/protobuf/compiler/allowlists", "@com_google_absl//absl/container:btree", diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 86ae4b7cad..20d6d95e46 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -37,6 +37,7 @@ #include "absl/container/btree_set.h" #include "absl/container/flat_hash_map.h" #include "google/protobuf/compiler/allowlists/allowlists.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/stubs/platform_macros.h" @@ -949,7 +950,7 @@ namespace { bool ContainsProto3Optional(const Descriptor* desc) { for (int i = 0; i < desc->field_count(); i++) { - if (desc->field(i)->has_optional_keyword()) { + if (FieldDescriptorLegacy(desc->field(i)).has_optional_keyword()) { return true; } } @@ -962,7 +963,8 @@ bool ContainsProto3Optional(const Descriptor* desc) { } bool ContainsProto3Optional(const FileDescriptor* file) { - if (file->syntax() == FileDescriptor::SYNTAX_PROTO3) { + if (FileDescriptorLegacy(file).syntax() == + FileDescriptorLegacy::Syntax::SYNTAX_PROTO3) { for (int i = 0; i < file->message_type_count(); i++) { if (ContainsProto3Optional(file->message_type(i))) { return true; diff --git a/src/google/protobuf/compiler/objectivec/BUILD.bazel b/src/google/protobuf/compiler/objectivec/BUILD.bazel index c3a3affd87..f78990394d 100644 --- a/src/google/protobuf/compiler/objectivec/BUILD.bazel +++ b/src/google/protobuf/compiler/objectivec/BUILD.bazel @@ -94,6 +94,7 @@ cc_library( deps = [ ":line_consumer", ":names", + "//src/google/protobuf:descriptor_legacy", "//src/google/protobuf:protobuf_nowkt", "//src/google/protobuf/compiler:code_generator", "@com_google_absl//absl/container:btree", diff --git a/src/google/protobuf/compiler/objectivec/file.cc b/src/google/protobuf/compiler/objectivec/file.cc index 12b5de9626..385c0ebd86 100644 --- a/src/google/protobuf/compiler/objectivec/file.cc +++ b/src/google/protobuf/compiler/objectivec/file.cc @@ -50,6 +50,7 @@ #include "google/protobuf/compiler/objectivec/message.h" #include "google/protobuf/compiler/objectivec/names.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/io/printer.h" // NOTE: src/google/protobuf/compiler/plugin.cc makes use of cerr for some @@ -701,14 +702,14 @@ void FileGenerator::PrintFileDescription(io::Printer* p) const { vars["package_value"] = file_->package().empty() ? "NULL" : absl::StrCat("\"", file_->package(), "\""); - switch (file_->syntax()) { - case FileDescriptor::SYNTAX_UNKNOWN: + switch (FileDescriptorLegacy(file_).syntax()) { + case FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN: vars["syntax"] = "GPBFileSyntaxUnknown"; break; - case FileDescriptor::SYNTAX_PROTO2: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: vars["syntax"] = "GPBFileSyntaxProto2"; break; - case FileDescriptor::SYNTAX_PROTO3: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: vars["syntax"] = "GPBFileSyntaxProto3"; break; } diff --git a/src/google/protobuf/compiler/php/BUILD.bazel b/src/google/protobuf/compiler/php/BUILD.bazel index 6d1fef5ba2..fe9e75c2c5 100644 --- a/src/google/protobuf/compiler/php/BUILD.bazel +++ b/src/google/protobuf/compiler/php/BUILD.bazel @@ -32,6 +32,7 @@ cc_library( ], deps = [ ":names", + "//src/google/protobuf:descriptor_legacy", "//src/google/protobuf:protobuf_nowkt", "//src/google/protobuf/compiler:code_generator", "@com_google_absl//absl/strings", diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index b78ceef5ab..d8d1630e90 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -47,6 +47,7 @@ #include "absl/strings/string_view.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.pb.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/io/printer.h" #include "google/protobuf/io/zero_copy_stream.h" @@ -2292,7 +2293,9 @@ bool Generator::Generate(const FileDescriptor* file, const Options& options, return false; } - if (!options.is_descriptor && file->syntax() != FileDescriptor::SYNTAX_PROTO3) { + if (!options.is_descriptor && + FileDescriptorLegacy(file).syntax() != + FileDescriptorLegacy::Syntax::SYNTAX_PROTO3) { *error = "Can only generate PHP code for proto3 .proto files.\n" "Please add 'syntax = \"proto3\";' to the top of your .proto file.\n"; diff --git a/src/google/protobuf/compiler/python/BUILD.bazel b/src/google/protobuf/compiler/python/BUILD.bazel index 90d5d21a85..5d26e0ce90 100644 --- a/src/google/protobuf/compiler/python/BUILD.bazel +++ b/src/google/protobuf/compiler/python/BUILD.bazel @@ -26,6 +26,7 @@ cc_library( "@com_github_grpc_grpc//tools/distrib/python/grpcio_tools:__subpackages__", ], deps = [ + "//src/google/protobuf:descriptor_legacy", "//src/google/protobuf:protobuf_nowkt", "//src/google/protobuf/compiler:code_generator", "//src/google/protobuf/compiler:retention", diff --git a/src/google/protobuf/compiler/python/generator.cc b/src/google/protobuf/compiler/python/generator.cc index 651aaa9910..636808a3ae 100644 --- a/src/google/protobuf/compiler/python/generator.cc +++ b/src/google/protobuf/compiler/python/generator.cc @@ -67,6 +67,7 @@ #include "google/protobuf/compiler/retention.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.pb.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/io/printer.h" #include "google/protobuf/io/strtod.h" #include "google/protobuf/io/zero_copy_stream.h" @@ -173,13 +174,13 @@ std::string StringifyDefaultValue(const FieldDescriptor& field) { return ""; } -std::string StringifySyntax(FileDescriptor::Syntax syntax) { +std::string StringifySyntax(FileDescriptorLegacy::Syntax syntax) { switch (syntax) { - case FileDescriptor::SYNTAX_PROTO2: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: return "proto2"; - case FileDescriptor::SYNTAX_PROTO3: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: return "proto3"; - case FileDescriptor::SYNTAX_UNKNOWN: + case FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN: default: ABSL_LOG(FATAL) << "Unsupported syntax; this generator only supports proto2 " @@ -440,7 +441,7 @@ void Generator::PrintFileDescriptor() const { m["descriptor_name"] = kDescriptorKey; m["name"] = file_->name(); m["package"] = file_->package(); - m["syntax"] = StringifySyntax(file_->syntax()); + m["syntax"] = StringifySyntax(FileDescriptorLegacy(file_).syntax()); m["options"] = OptionsValue( StripLocalSourceRetentionOptions(*file_).SerializeAsString()); m["serialized_descriptor"] = absl::CHexEscape(file_descriptor_serialized_); @@ -690,7 +691,9 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { "syntax='$syntax$'", "options_value", OptionsValue(options_string), "extendable", message_descriptor.extension_range_count() > 0 ? "True" : "False", - "syntax", StringifySyntax(message_descriptor.file()->syntax())); + "syntax", + StringifySyntax( + FileDescriptorLegacy(message_descriptor.file()).syntax())); printer_->Print(",\n"); // Extension ranges diff --git a/src/google/protobuf/compiler/ruby/BUILD.bazel b/src/google/protobuf/compiler/ruby/BUILD.bazel index 47948416bc..520b691943 100644 --- a/src/google/protobuf/compiler/ruby/BUILD.bazel +++ b/src/google/protobuf/compiler/ruby/BUILD.bazel @@ -17,6 +17,7 @@ cc_library( "//src/google/protobuf/compiler:__pkg__", ], deps = [ + "//src/google/protobuf:descriptor_legacy", "//src/google/protobuf:protobuf_nowkt", "//src/google/protobuf/compiler:code_generator", ], diff --git a/src/google/protobuf/compiler/ruby/ruby_generator.cc b/src/google/protobuf/compiler/ruby/ruby_generator.cc index 5013fbd584..625c5004d4 100644 --- a/src/google/protobuf/compiler/ruby/ruby_generator.cc +++ b/src/google/protobuf/compiler/ruby/ruby_generator.cc @@ -39,6 +39,7 @@ #include "google/protobuf/compiler/plugin.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.pb.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/io/printer.h" #include "google/protobuf/io/zero_copy_stream.h" @@ -79,7 +80,8 @@ std::string GetOutputFilename(absl::string_view proto_file) { } std::string LabelForField(const FieldDescriptor* field) { - if (field->has_optional_keyword() && field->containing_oneof() != nullptr) { + if (FieldDescriptorLegacy(field).has_optional_keyword() && + field->containing_oneof() != nullptr) { return "proto3_optional"; } switch (field->label()) { @@ -114,13 +116,13 @@ std::string TypeName(const FieldDescriptor* field) { } } -std::string StringifySyntax(FileDescriptor::Syntax syntax) { +std::string StringifySyntax(FileDescriptorLegacy::Syntax syntax) { switch (syntax) { - case FileDescriptor::SYNTAX_PROTO2: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: return "proto2"; - case FileDescriptor::SYNTAX_PROTO3: + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: return "proto3"; - case FileDescriptor::SYNTAX_UNKNOWN: + case FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN: default: ABSL_LOG(FATAL) << "Unsupported syntax; this generator only supports " "proto2 and proto3 syntax."; @@ -474,7 +476,7 @@ bool GenerateDslDescriptor(const FileDescriptor* file, io::Printer* printer, printer->Indent(); printer->Print("add_file(\"$filename$\", :syntax => :$syntax$) do\n", "filename", file->name(), "syntax", - StringifySyntax(file->syntax())); + StringifySyntax(FileDescriptorLegacy(file).syntax())); printer->Indent(); for (int i = 0; i < file->message_type_count(); i++) { if (!GenerateMessage(file->message_type(i), printer, error)) { @@ -557,7 +559,8 @@ bool Generator::Generate( const std::string& parameter, GeneratorContext* generator_context, std::string* error) const { - if (file->syntax() == FileDescriptor::SYNTAX_UNKNOWN) { + if (FileDescriptorLegacy(file).syntax() == + FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN) { *error = "Invalid or unsupported proto syntax"; return false; } diff --git a/src/google/protobuf/util/BUILD.bazel b/src/google/protobuf/util/BUILD.bazel index 609a86a27b..d25694c7c0 100644 --- a/src/google/protobuf/util/BUILD.bazel +++ b/src/google/protobuf/util/BUILD.bazel @@ -169,6 +169,7 @@ cc_library( visibility = ["//:__subpackages__"], deps = [ "//src/google/protobuf", + "//src/google/protobuf:descriptor_legacy", "//src/google/protobuf/io", "//src/google/protobuf/stubs", "@com_google_absl//absl/log:absl_log", diff --git a/src/google/protobuf/util/type_resolver_util.cc b/src/google/protobuf/util/type_resolver_util.cc index e408e78b89..c326aa29c7 100644 --- a/src/google/protobuf/util/type_resolver_util.cc +++ b/src/google/protobuf/util/type_resolver_util.cc @@ -43,6 +43,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/strings/strip.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/io/strtod.h" #include "google/protobuf/util/type_resolver.h" @@ -273,7 +274,7 @@ void ConvertFieldDescriptor(absl::string_view url_prefix, ConvertFieldOptions(descriptor.options(), *field->mutable_options()); } -Syntax ConvertSyntax(FileDescriptor::Syntax syntax) { +Syntax ConvertSyntax(FileDescriptorLegacy::Syntax syntax) { switch (syntax) { default: return Syntax::SYNTAX_PROTO2; @@ -282,7 +283,8 @@ Syntax ConvertSyntax(FileDescriptor::Syntax syntax) { void ConvertEnumDescriptor(const EnumDescriptor& descriptor, Enum* enum_type) { enum_type->Clear(); - enum_type->set_syntax(ConvertSyntax(descriptor.file()->syntax())); + enum_type->set_syntax( + ConvertSyntax(FileDescriptorLegacy(descriptor.file()).syntax())); enum_type->set_name(descriptor.full_name()); enum_type->mutable_source_context()->set_file_name(descriptor.file()->name()); @@ -303,7 +305,8 @@ void ConvertDescriptor(absl::string_view url_prefix, const Descriptor& descriptor, Type* type) { type->Clear(); type->set_name(descriptor.full_name()); - type->set_syntax(ConvertSyntax(descriptor.file()->syntax())); + type->set_syntax( + ConvertSyntax(FileDescriptorLegacy(descriptor.file()).syntax())); for (int i = 0; i < descriptor.field_count(); ++i) { ConvertFieldDescriptor(url_prefix, *descriptor.field(i), type->add_fields()); diff --git a/src/google/protobuf/util/type_resolver_util_test.cc b/src/google/protobuf/util/type_resolver_util_test.cc index eaf6649f00..00391693ff 100644 --- a/src/google/protobuf/util/type_resolver_util_test.cc +++ b/src/google/protobuf/util/type_resolver_util_test.cc @@ -44,6 +44,7 @@ #include #include #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/util/json_format_proto3.pb.h" #include "google/protobuf/map_unittest.pb.h" #include "google/protobuf/unittest.pb.h" @@ -466,7 +467,8 @@ class DescriptorPoolTypeResolverSyntaxTest : public testing::Test { TEST_F(DescriptorPoolTypeResolverSyntaxTest, SyntaxProto2) { const FileDescriptor* file = BuildFile("proto2"); - ASSERT_EQ(FileDescriptor::SYNTAX_PROTO2, file->syntax()); + ASSERT_EQ(FileDescriptorLegacy::Syntax::SYNTAX_PROTO2, + FileDescriptorLegacy(file).syntax()); Type type; ASSERT_TRUE( @@ -477,7 +479,8 @@ TEST_F(DescriptorPoolTypeResolverSyntaxTest, SyntaxProto2) { TEST_F(DescriptorPoolTypeResolverSyntaxTest, SyntaxProto3) { const FileDescriptor* file = BuildFile("proto3"); - ASSERT_EQ(FileDescriptor::SYNTAX_PROTO3, file->syntax()); + ASSERT_EQ(FileDescriptorLegacy::Syntax::SYNTAX_PROTO3, + FileDescriptorLegacy(file).syntax()); Type type; ASSERT_TRUE(