Skip to content

Commit

Permalink
Remove edition getter from C++ descriptor APIs
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597686725
  • Loading branch information
mkruskal-google authored and Copybara-Service committed Jan 12, 2024
1 parent e6d8669 commit 93b219f
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 80 deletions.
1 change: 1 addition & 0 deletions conformance/BUILD.bazel
Expand Up @@ -145,6 +145,7 @@ cc_library(
deps = [
":conformance_cc_proto",
"//src/google/protobuf",
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:protobuf_lite",
"//src/google/protobuf/util:differencer",
"//src/google/protobuf/util:json_util",
Expand Down
4 changes: 3 additions & 1 deletion conformance/conformance_test.cc
Expand Up @@ -24,6 +24,7 @@
#include "absl/strings/string_view.h"
#include "conformance/conformance.pb.h"
#include "conformance/conformance.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"

Expand Down Expand Up @@ -142,7 +143,8 @@ ConformanceTestSuite::ConformanceRequestSetting::NewTestMessage() const {

std::string
ConformanceTestSuite::ConformanceRequestSetting::GetSyntaxIdentifier() const {
switch (prototype_message_.GetDescriptor()->file()->edition()) {
switch (FileDescriptorLegacy(prototype_message_.GetDescriptor()->file())
.edition()) {
case Edition::EDITION_PROTO3:
return "Proto3";
case Edition::EDITION_PROTO2:
Expand Down
1 change: 1 addition & 0 deletions pkg/BUILD.bazel
Expand Up @@ -237,6 +237,7 @@ cc_dist_library(
],
tags = ["manual"],
deps = [
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:internal_visibility_for_testing",
"//src/google/protobuf:test_textproto",
"//src/google/protobuf/compiler:command_line_interface_tester",
Expand Down
13 changes: 13 additions & 0 deletions src/google/protobuf/BUILD.bazel
Expand Up @@ -649,6 +649,18 @@ cc_library(
],
)

cc_library(
name = "descriptor_legacy",
hdrs = ["descriptor_legacy.h"],
copts = COPTS,
linkopts = LINK_OPTS,
strip_include_prefix = "/src",
visibility = ["//:__subpackages__"],
deps = [
":protobuf",
],
)

filegroup(
name = "well_known_type_protos",
srcs = [
Expand Down Expand Up @@ -1110,6 +1122,7 @@ cc_test(
deps = [
":any_cc_proto",
":cc_test_protos",
":descriptor_legacy",
":port",
":protobuf",
":test_textproto",
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/compiler/code_generator.h
Expand Up @@ -156,6 +156,11 @@ class PROTOC_EXPORT CodeGenerator {
return ::google::protobuf::internal::InternalFeatureHelper::GetUnresolvedFeatures(
descriptor, extension);
}

// Retrieves the edition of a built file descriptor.
static Edition GetEdition(const FileDescriptor& file) {
return ::google::protobuf::internal::InternalFeatureHelper::GetEdition(file);
}
};

// CodeGenerators generate one or more files in a given directory. This
Expand Down
22 changes: 13 additions & 9 deletions src/google/protobuf/compiler/command_line_interface.cc
Expand Up @@ -991,8 +991,8 @@ bool ContainsProto3Optional(const Descriptor* desc) {
return false;
}

bool ContainsProto3Optional(const FileDescriptor* file) {
if (file->edition() == Edition::EDITION_PROTO3) {
bool ContainsProto3Optional(Edition edition, const FileDescriptor* file) {
if (edition == Edition::EDITION_PROTO3) {
for (int i = 0; i < file->message_type_count(); i++) {
if (ContainsProto3Optional(file->message_type(i))) {
return true;
Expand Down Expand Up @@ -1600,7 +1600,8 @@ bool CommandLineInterface::ParseInputFiles(
if (!experimental_editions_ &&
!absl::StartsWith(parsed_file->name(), "google/protobuf/") &&
!absl::StartsWith(parsed_file->name(), "upb/")) {
if (parsed_file->edition() >= Edition::EDITION_2023) {
if (::google::protobuf::internal::InternalFeatureHelper::GetEdition(*parsed_file) >=
Edition::EDITION_2023) {
std::cerr
<< parsed_file->name()
<< ": This file uses editions, but --experimental_editions has not "
Expand Down Expand Up @@ -2533,7 +2534,8 @@ bool CommandLineInterface::EnforceProto3OptionalSupport(
supported_features & CodeGenerator::FEATURE_PROTO3_OPTIONAL;
if (!supports_proto3_optional) {
for (const auto fd : parsed_files) {
if (ContainsProto3Optional(fd)) {
if (ContainsProto3Optional(
::google::protobuf::internal::InternalFeatureHelper::GetEdition(*fd), fd)) {
std::cerr << fd->name()
<< ": is a proto3 file that contains optional fields, but "
"code generator "
Expand All @@ -2558,7 +2560,9 @@ bool CommandLineInterface::EnforceEditionsSupport(
return true;
}
for (const auto* fd : parsed_files) {
if (fd->edition() < Edition::EDITION_2023) {
Edition edition =
::google::protobuf::internal::InternalFeatureHelper::GetEdition(*fd);
if (edition < Edition::EDITION_2023) {
// Legacy proto2/proto3 files don't need any checks.
continue;
}
Expand All @@ -2576,19 +2580,19 @@ bool CommandLineInterface::EnforceEditionsSupport(
fd->name(), codegen_name);
return false;
}
if (fd->edition() < minimum_edition) {
if (edition < minimum_edition) {
std::cerr << absl::Substitute(
"$0: is a file using edition $2, which isn't supported by code "
"generator $1. Please upgrade your file to at least edition $3.",
fd->name(), codegen_name, fd->edition(), minimum_edition);
fd->name(), codegen_name, edition, minimum_edition);
return false;
}
if (fd->edition() > maximum_edition) {
if (edition > maximum_edition) {
std::cerr << absl::Substitute(
"$0: is a file using edition $2, which isn't supported by code "
"generator $1. Please ask the owner of this code generator to add "
"support or switch back to a maximum of edition $3.",
fd->name(), codegen_name, fd->edition(), maximum_edition);
fd->name(), codegen_name, edition, maximum_edition);
return false;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/java/generator.h
Expand Up @@ -58,6 +58,7 @@ class PROTOC_EXPORT JavaGenerator : public CodeGenerator {
opensource_runtime_ = opensource;
}

using CodeGenerator::GetEdition;
using CodeGenerator::GetResolvedSourceFeatures;

private:
Expand Down
7 changes: 5 additions & 2 deletions src/google/protobuf/compiler/java/message_lite.cc
Expand Up @@ -25,6 +25,7 @@
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/enum_lite.h"
#include "google/protobuf/compiler/java/extension_lite.h"
#include "google/protobuf/compiler/java/generator.h"
#include "google/protobuf/compiler/java/generator_factory.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/message_builder.h"
Expand Down Expand Up @@ -476,9 +477,11 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
flags |= 0x2;
}
if (!context_->options().strip_nonfunctional_codegen) {
if (descriptor_->file()->edition() == Edition::EDITION_PROTO2) {
if (JavaGenerator::GetEdition(*descriptor_->file()) ==
Edition::EDITION_PROTO2) {
flags |= 0x1;
} else if (descriptor_->file()->edition() >= Edition::EDITION_2023) {
} else if (JavaGenerator::GetEdition(*descriptor_->file()) >=
Edition::EDITION_2023) {
flags |= 0x4;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/mock_code_generator.cc
Expand Up @@ -215,7 +215,7 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file,
maximum_edition_ = Edition::EDITION_PROTO2;
}

if (file->edition() >= Edition::EDITION_2023 &&
if (GetEdition(*file) >= Edition::EDITION_2023 &&
(suppressed_features_ & CodeGenerator::FEATURE_SUPPORTS_EDITIONS) == 0) {
internal::VisitDescriptors(*file, [&](const auto& descriptor) {
const FeatureSet& features = GetResolvedSourceFeatures(descriptor);
Expand Down
7 changes: 4 additions & 3 deletions src/google/protobuf/compiler/objectivec/file.cc
Expand Up @@ -264,10 +264,11 @@ FileGenerator::CommonState::CollectMinimalFileDepsContainingExtensions(
return result;
}

FileGenerator::FileGenerator(const FileDescriptor* file,
FileGenerator::FileGenerator(Edition edition, const FileDescriptor* file,
const GenerationOptions& generation_options,
CommonState& common_state)
: file_(file),
: edition_(edition),
file_(file),
generation_options_(generation_options),
common_state_(&common_state),
root_class_name_(FileClassName(file)),
Expand Down Expand Up @@ -777,7 +778,7 @@ void FileGenerator::EmitFileDescription(io::Printer* p) const {
// mode.
syntax = "GPBFileSyntaxUnknown";
} else {
switch (file_->edition()) {
switch (edition_) {
case Edition::EDITION_UNKNOWN:
syntax = "GPBFileSyntaxUnknown";
break;
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/compiler/objectivec/file.h
Expand Up @@ -55,7 +55,7 @@ class FileGenerator {
const bool include_custom_options;
};

FileGenerator(const FileDescriptor* file,
FileGenerator(Edition edition, const FileDescriptor* file,
const GenerationOptions& generation_options,
CommonState& common_state);
~FileGenerator() = default;
Expand Down Expand Up @@ -114,6 +114,7 @@ class FileGenerator {
generation_options_.headers_use_forward_declarations;
}

const Edition edition_;
const FileDescriptor* file_;
const GenerationOptions& generation_options_;
mutable CommonState* common_state_;
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/compiler/objectivec/generator.cc
Expand Up @@ -341,7 +341,8 @@ bool ObjectiveCGenerator::GenerateAll(

FileGenerator::CommonState state(!generation_options.strip_custom_options);
for (const auto& file : files) {
const FileGenerator file_generator(file, generation_options, state);
const FileGenerator file_generator(GetEdition(*file), file,
generation_options, state);
std::string filepath = FilePath(file);

// Generate header.
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/python/generator.cc
Expand Up @@ -544,8 +544,8 @@ void Generator::PrintFileDescriptor() const {
m["descriptor_name"] = kDescriptorKey;
m["name"] = file_->name();
m["package"] = file_->package();
m["syntax"] = GetLegacySyntaxName(file_->edition());
m["edition"] = Edition_Name(file_->edition());
m["syntax"] = GetLegacySyntaxName(GetEdition(*file_));
m["edition"] = Edition_Name(GetEdition(*file_));
m["options"] = OptionsValue(proto_.options().SerializeAsString());
m["serialized_descriptor"] = absl::CHexEscape(file_descriptor_serialized_);
if (GeneratingDescriptorProto()) {
Expand Down

0 comments on commit 93b219f

Please sign in to comment.