Internal Code Change

PiperOrigin-RevId: 516655054
pull/12229/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 5d97d43863
commit bdeacc7fb8
  1. 1
      conformance/BUILD.bazel
  2. 8
      conformance/conformance_test.cc
  3. 1
      python/BUILD.bazel
  4. 11
      python/google/protobuf/pyext/descriptor.cc
  5. 3
      src/google/protobuf/BUILD.bazel
  6. 1
      src/google/protobuf/compiler/BUILD.bazel
  7. 6
      src/google/protobuf/compiler/command_line_interface.cc
  8. 1
      src/google/protobuf/compiler/objectivec/BUILD.bazel
  9. 9
      src/google/protobuf/compiler/objectivec/file.cc
  10. 1
      src/google/protobuf/compiler/php/BUILD.bazel
  11. 5
      src/google/protobuf/compiler/php/php_generator.cc
  12. 1
      src/google/protobuf/compiler/python/BUILD.bazel
  13. 15
      src/google/protobuf/compiler/python/generator.cc
  14. 1
      src/google/protobuf/compiler/ruby/BUILD.bazel
  15. 17
      src/google/protobuf/compiler/ruby/ruby_generator.cc
  16. 1
      src/google/protobuf/util/BUILD.bazel
  17. 9
      src/google/protobuf/util/type_resolver_util.cc
  18. 7
      src/google/protobuf/util/type_resolver_util_test.cc

@ -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",
],

@ -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:

@ -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"],

@ -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 <Python.h>
@ -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) {

@ -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",

@ -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",

@ -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;

@ -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",

@ -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;
}

@ -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",

@ -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";

@ -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",

@ -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

@ -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",
],

@ -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;
}

@ -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",

@ -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());

@ -44,6 +44,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#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(

Loading…
Cancel
Save