Skip to content

Commit 93b219f

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Remove edition getter from C++ descriptor APIs
PiperOrigin-RevId: 597686725
1 parent e6d8669 commit 93b219f

20 files changed

Lines changed: 143 additions & 80 deletions

conformance/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ cc_library(
145145
deps = [
146146
":conformance_cc_proto",
147147
"//src/google/protobuf",
148+
"//src/google/protobuf:descriptor_legacy",
148149
"//src/google/protobuf:protobuf_lite",
149150
"//src/google/protobuf/util:differencer",
150151
"//src/google/protobuf/util:json_util",

conformance/conformance_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/strings/string_view.h"
2525
#include "conformance/conformance.pb.h"
2626
#include "conformance/conformance.pb.h"
27+
#include "google/protobuf/descriptor_legacy.h"
2728
#include "google/protobuf/message.h"
2829
#include "google/protobuf/text_format.h"
2930

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

143144
std::string
144145
ConformanceTestSuite::ConformanceRequestSetting::GetSyntaxIdentifier() const {
145-
switch (prototype_message_.GetDescriptor()->file()->edition()) {
146+
switch (FileDescriptorLegacy(prototype_message_.GetDescriptor()->file())
147+
.edition()) {
146148
case Edition::EDITION_PROTO3:
147149
return "Proto3";
148150
case Edition::EDITION_PROTO2:

pkg/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ cc_dist_library(
237237
],
238238
tags = ["manual"],
239239
deps = [
240+
"//src/google/protobuf:descriptor_legacy",
240241
"//src/google/protobuf:internal_visibility_for_testing",
241242
"//src/google/protobuf:test_textproto",
242243
"//src/google/protobuf/compiler:command_line_interface_tester",

src/google/protobuf/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,18 @@ cc_library(
649649
],
650650
)
651651

652+
cc_library(
653+
name = "descriptor_legacy",
654+
hdrs = ["descriptor_legacy.h"],
655+
copts = COPTS,
656+
linkopts = LINK_OPTS,
657+
strip_include_prefix = "/src",
658+
visibility = ["//:__subpackages__"],
659+
deps = [
660+
":protobuf",
661+
],
662+
)
663+
652664
filegroup(
653665
name = "well_known_type_protos",
654666
srcs = [
@@ -1110,6 +1122,7 @@ cc_test(
11101122
deps = [
11111123
":any_cc_proto",
11121124
":cc_test_protos",
1125+
":descriptor_legacy",
11131126
":port",
11141127
":protobuf",
11151128
":test_textproto",

src/google/protobuf/compiler/code_generator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ class PROTOC_EXPORT CodeGenerator {
156156
return ::google::protobuf::internal::InternalFeatureHelper::GetUnresolvedFeatures(
157157
descriptor, extension);
158158
}
159+
160+
// Retrieves the edition of a built file descriptor.
161+
static Edition GetEdition(const FileDescriptor& file) {
162+
return ::google::protobuf::internal::InternalFeatureHelper::GetEdition(file);
163+
}
159164
};
160165

161166
// CodeGenerators generate one or more files in a given directory. This

src/google/protobuf/compiler/command_line_interface.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ bool ContainsProto3Optional(const Descriptor* desc) {
991991
return false;
992992
}
993993

994-
bool ContainsProto3Optional(const FileDescriptor* file) {
995-
if (file->edition() == Edition::EDITION_PROTO3) {
994+
bool ContainsProto3Optional(Edition edition, const FileDescriptor* file) {
995+
if (edition == Edition::EDITION_PROTO3) {
996996
for (int i = 0; i < file->message_type_count(); i++) {
997997
if (ContainsProto3Optional(file->message_type(i))) {
998998
return true;
@@ -1600,7 +1600,8 @@ bool CommandLineInterface::ParseInputFiles(
16001600
if (!experimental_editions_ &&
16011601
!absl::StartsWith(parsed_file->name(), "google/protobuf/") &&
16021602
!absl::StartsWith(parsed_file->name(), "upb/")) {
1603-
if (parsed_file->edition() >= Edition::EDITION_2023) {
1603+
if (::google::protobuf::internal::InternalFeatureHelper::GetEdition(*parsed_file) >=
1604+
Edition::EDITION_2023) {
16041605
std::cerr
16051606
<< parsed_file->name()
16061607
<< ": This file uses editions, but --experimental_editions has not "
@@ -2533,7 +2534,8 @@ bool CommandLineInterface::EnforceProto3OptionalSupport(
25332534
supported_features & CodeGenerator::FEATURE_PROTO3_OPTIONAL;
25342535
if (!supports_proto3_optional) {
25352536
for (const auto fd : parsed_files) {
2536-
if (ContainsProto3Optional(fd)) {
2537+
if (ContainsProto3Optional(
2538+
::google::protobuf::internal::InternalFeatureHelper::GetEdition(*fd), fd)) {
25372539
std::cerr << fd->name()
25382540
<< ": is a proto3 file that contains optional fields, but "
25392541
"code generator "
@@ -2558,7 +2560,9 @@ bool CommandLineInterface::EnforceEditionsSupport(
25582560
return true;
25592561
}
25602562
for (const auto* fd : parsed_files) {
2561-
if (fd->edition() < Edition::EDITION_2023) {
2563+
Edition edition =
2564+
::google::protobuf::internal::InternalFeatureHelper::GetEdition(*fd);
2565+
if (edition < Edition::EDITION_2023) {
25622566
// Legacy proto2/proto3 files don't need any checks.
25632567
continue;
25642568
}
@@ -2576,19 +2580,19 @@ bool CommandLineInterface::EnforceEditionsSupport(
25762580
fd->name(), codegen_name);
25772581
return false;
25782582
}
2579-
if (fd->edition() < minimum_edition) {
2583+
if (edition < minimum_edition) {
25802584
std::cerr << absl::Substitute(
25812585
"$0: is a file using edition $2, which isn't supported by code "
25822586
"generator $1. Please upgrade your file to at least edition $3.",
2583-
fd->name(), codegen_name, fd->edition(), minimum_edition);
2587+
fd->name(), codegen_name, edition, minimum_edition);
25842588
return false;
25852589
}
2586-
if (fd->edition() > maximum_edition) {
2590+
if (edition > maximum_edition) {
25872591
std::cerr << absl::Substitute(
25882592
"$0: is a file using edition $2, which isn't supported by code "
25892593
"generator $1. Please ask the owner of this code generator to add "
25902594
"support or switch back to a maximum of edition $3.",
2591-
fd->name(), codegen_name, fd->edition(), maximum_edition);
2595+
fd->name(), codegen_name, edition, maximum_edition);
25922596
return false;
25932597
}
25942598
}

src/google/protobuf/compiler/java/generator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class PROTOC_EXPORT JavaGenerator : public CodeGenerator {
5858
opensource_runtime_ = opensource;
5959
}
6060

61+
using CodeGenerator::GetEdition;
6162
using CodeGenerator::GetResolvedSourceFeatures;
6263

6364
private:

src/google/protobuf/compiler/java/message_lite.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "google/protobuf/compiler/java/doc_comment.h"
2626
#include "google/protobuf/compiler/java/enum_lite.h"
2727
#include "google/protobuf/compiler/java/extension_lite.h"
28+
#include "google/protobuf/compiler/java/generator.h"
2829
#include "google/protobuf/compiler/java/generator_factory.h"
2930
#include "google/protobuf/compiler/java/helpers.h"
3031
#include "google/protobuf/compiler/java/message_builder.h"
@@ -476,9 +477,11 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
476477
flags |= 0x2;
477478
}
478479
if (!context_->options().strip_nonfunctional_codegen) {
479-
if (descriptor_->file()->edition() == Edition::EDITION_PROTO2) {
480+
if (JavaGenerator::GetEdition(*descriptor_->file()) ==
481+
Edition::EDITION_PROTO2) {
480482
flags |= 0x1;
481-
} else if (descriptor_->file()->edition() >= Edition::EDITION_2023) {
483+
} else if (JavaGenerator::GetEdition(*descriptor_->file()) >=
484+
Edition::EDITION_2023) {
482485
flags |= 0x4;
483486
}
484487
}

src/google/protobuf/compiler/mock_code_generator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file,
215215
maximum_edition_ = Edition::EDITION_PROTO2;
216216
}
217217

218-
if (file->edition() >= Edition::EDITION_2023 &&
218+
if (GetEdition(*file) >= Edition::EDITION_2023 &&
219219
(suppressed_features_ & CodeGenerator::FEATURE_SUPPORTS_EDITIONS) == 0) {
220220
internal::VisitDescriptors(*file, [&](const auto& descriptor) {
221221
const FeatureSet& features = GetResolvedSourceFeatures(descriptor);

src/google/protobuf/compiler/objectivec/file.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,11 @@ FileGenerator::CommonState::CollectMinimalFileDepsContainingExtensions(
264264
return result;
265265
}
266266

267-
FileGenerator::FileGenerator(const FileDescriptor* file,
267+
FileGenerator::FileGenerator(Edition edition, const FileDescriptor* file,
268268
const GenerationOptions& generation_options,
269269
CommonState& common_state)
270-
: file_(file),
270+
: edition_(edition),
271+
file_(file),
271272
generation_options_(generation_options),
272273
common_state_(&common_state),
273274
root_class_name_(FileClassName(file)),
@@ -777,7 +778,7 @@ void FileGenerator::EmitFileDescription(io::Printer* p) const {
777778
// mode.
778779
syntax = "GPBFileSyntaxUnknown";
779780
} else {
780-
switch (file_->edition()) {
781+
switch (edition_) {
781782
case Edition::EDITION_UNKNOWN:
782783
syntax = "GPBFileSyntaxUnknown";
783784
break;

0 commit comments

Comments
 (0)