Skip to content

Commit

Permalink
Remove descriptor_legacy usage from compiler code
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 588916746
  • Loading branch information
mkruskal-google authored and copybara-github committed Dec 7, 2023
1 parent 17f4f91 commit 060a9be
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ 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/algorithm",
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/command_line_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "google/protobuf/compiler/allowlists/allowlists.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/descriptor_visitor.h"
#include "google/protobuf/feature_resolver.h"

Expand Down Expand Up @@ -979,7 +978,8 @@ namespace {

bool ContainsProto3Optional(const Descriptor* desc) {
for (int i = 0; i < desc->field_count(); i++) {
if (FieldDescriptorLegacy(desc->field(i)).has_optional_keyword()) {
if (desc->field(i)->real_containing_oneof() == nullptr &&
desc->field(i)->containing_oneof() != nullptr) {
return true;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/compiler/java/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ cc_library(
visibility = ["//pkg:__pkg__"],
deps = [
":java_features_bootstrap",
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:versions",
Expand Down Expand Up @@ -130,7 +129,6 @@ cc_library(
":names",
":names_internal",
"//src/google/protobuf:arena",
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",
Expand Down
4 changes: 1 addition & 3 deletions src/google/protobuf/compiler/java/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/compiler/versions.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/wire_format.h"
Expand Down Expand Up @@ -828,8 +827,7 @@ bool HasRequiredFields(const Descriptor* type) {
}

bool IsRealOneof(const FieldDescriptor* descriptor) {
return descriptor->containing_oneof() &&
!OneofDescriptorLegacy(descriptor->containing_oneof()).is_synthetic();
return descriptor->real_containing_oneof();
}

bool HasRepeatedFields(const Descriptor* descriptor) {
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/java/message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"

Expand Down Expand Up @@ -407,7 +406,8 @@ void ImmutableMessageFieldGenerator::GenerateKotlinDslMembers(
}

void ImmutableMessageFieldGenerator::GenerateKotlinOrNull(io::Printer* printer) const {
if (FieldDescriptorLegacy(descriptor_).has_optional_keyword()) {
if (descriptor_->has_presence() &&
descriptor_->real_containing_oneof() == nullptr) {
printer->Print(variables_,
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/java/message_field_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"

Expand Down Expand Up @@ -309,7 +308,8 @@ void ImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
}

void ImmutableMessageFieldLiteGenerator::GenerateKotlinOrNull(io::Printer* printer) const {
if (FieldDescriptorLegacy(descriptor_).has_optional_keyword()) {
if (descriptor_->has_presence() &&
descriptor_->real_containing_oneof() == nullptr) {
printer->Print(variables_,
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/java/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "google/protobuf/compiler/java/message_builder_lite.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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",
Expand Down
15 changes: 12 additions & 3 deletions src/google/protobuf/compiler/python/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/descriptor_visitor.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/io/printer.h"
Expand Down Expand Up @@ -167,6 +166,17 @@ std::string OptionsValue(absl::string_view serialized_options) {
}
}

std::string GetLegacySyntaxName(Edition edition) {
switch (edition) {
case Edition::EDITION_PROTO2:
return "proto2";
case Edition::EDITION_PROTO3:
return "proto3";
default:
return "editions";
}
}

} // namespace

Generator::Generator() : file_(nullptr) {}
Expand Down Expand Up @@ -534,8 +544,7 @@ void Generator::PrintFileDescriptor() const {
m["descriptor_name"] = kDescriptorKey;
m["name"] = file_->name();
m["package"] = file_->package();
m["syntax"] = std::string(
FileDescriptorLegacy::SyntaxName(FileDescriptorLegacy(file_).syntax()));
m["syntax"] = GetLegacySyntaxName(file_->edition());
m["edition"] = Edition_Name(file_->edition());
m["options"] = OptionsValue(proto_.options().SerializeAsString());
m["serialized_descriptor"] = absl::CHexEscape(file_descriptor_serialized_);
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/ruby/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ cc_library(
"//src/google/protobuf/compiler:__pkg__",
],
deps = [
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",
Expand Down
7 changes: 0 additions & 7 deletions src/google/protobuf/compiler/ruby/ruby_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#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/zero_copy_stream.h"

Expand Down Expand Up @@ -320,12 +319,6 @@ bool Generator::Generate(const FileDescriptor* file,
const std::string& parameter,
GeneratorContext* generator_context,
std::string* error) const {
if (FileDescriptorLegacy(file).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN) {
*error = "Invalid or unsupported proto syntax";
return false;
}

std::unique_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(GetOutputFilename(file->name())));
io::Printer printer(output.get(), '$');
Expand Down

0 comments on commit 060a9be

Please sign in to comment.