Skip to content

Commit

Permalink
Replace PROTOBUF_{MINIMUM,MAXIMUM}_EDITION macros with constexpr func…
Browse files Browse the repository at this point in the history
…tions

for easier maintenance.
No semantic change expected.

PiperOrigin-RevId: 676596269
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 19, 2024
1 parent 7a7a087 commit 9580af3
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/code_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ absl::StatusOr<FeatureSetDefaults> CodeGenerator::BuildFeatureSetDefaults()
// anyway.
return FeatureResolver::CompileDefaults(
FeatureSet::descriptor(), GetFeatureExtensions(),
PROTOBUF_MINIMUM_EDITION, PROTOBUF_MAXIMUM_EDITION);
MinimumAllowedEdition(), MaximumAllowedEdition());
}
return FeatureResolver::CompileDefaults(
FeatureSet::descriptor(), GetFeatureExtensions(), GetMinimumEdition(),
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/compiler/code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class PROTOC_EXPORT CodeGenerator {
}
};

constexpr auto MinimumAllowedEdition() { return Edition::EDITION_PROTO2; }
constexpr auto MaximumAllowedEdition() { return Edition::EDITION_2023; }

// CodeGenerators generate one or more files in a given directory. This
// abstract interface represents the directory to which the CodeGenerator is
// to write and other information about the context in which the Generator
Expand Down
15 changes: 11 additions & 4 deletions src/google/protobuf/compiler/code_generator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class TestGenerator : public CodeGenerator {

private:
uint64_t features_ = CodeGenerator::Feature::FEATURE_SUPPORTS_EDITIONS;
Edition minimum_edition_ = PROTOBUF_MINIMUM_EDITION;
Edition maximum_edition_ = PROTOBUF_MAXIMUM_EDITION;
Edition minimum_edition_ = MinimumAllowedEdition();
Edition maximum_edition_ = MaximumAllowedEdition();
std::vector<const FieldDescriptor*> feature_extensions_ = {
GetExtensionReflection(pb::test)};
};
Expand Down Expand Up @@ -320,8 +320,15 @@ TEST_F(CodeGeneratorTest, BuildFeatureSetDefaultsUnsupported) {
auto result = generator.BuildFeatureSetDefaults();

ASSERT_TRUE(result.ok()) << result.status().message();
EXPECT_EQ(result->minimum_edition(), PROTOBUF_MINIMUM_EDITION);
EXPECT_EQ(result->maximum_edition(), PROTOBUF_MAXIMUM_EDITION);
EXPECT_EQ(result->minimum_edition(), MinimumAllowedEdition());
EXPECT_EQ(result->maximum_edition(), MaximumAllowedEdition());
}

TEST_F(CodeGeneratorTest, SupportedEditionRangeIsDense) {
for (int i = static_cast<int>(MinimumAllowedEdition());
i <= static_cast<int>(MaximumAllowedEdition()); ++i) {
EXPECT_TRUE(Edition_IsValid(i));
}
}

#include "google/protobuf/port_undef.inc"
Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/compiler/command_line_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1524,10 +1524,10 @@ bool CommandLineInterface::SetupFeatureResolution(DescriptorPool& pool) {
// Calculate the feature defaults for each built-in generator. All generators
// that support editions must agree on the supported edition range.
std::vector<const FieldDescriptor*> feature_extensions;
Edition minimum_edition = PROTOBUF_MINIMUM_EDITION;
Edition minimum_edition = MinimumAllowedEdition();
// Override maximum_edition if experimental_editions is true.
Edition maximum_edition =
!experimental_editions_ ? PROTOBUF_MAXIMUM_EDITION : Edition::EDITION_MAX;
!experimental_editions_ ? MaximumAllowedEdition() : Edition::EDITION_MAX;
for (const auto& output : output_directives_) {
if (output.generator == nullptr) continue;
if (!experimental_editions_ &&
Expand Down Expand Up @@ -3037,11 +3037,11 @@ bool CommandLineInterface::WriteEditionDefaults(const DescriptorPool& pool) {
std::vector<const FieldDescriptor*> extensions;
pool.FindAllExtensions(feature_set, &extensions);

Edition minimum = PROTOBUF_MINIMUM_EDITION;
Edition minimum = MinimumAllowedEdition();
if (edition_defaults_minimum_ != EDITION_UNKNOWN) {
minimum = edition_defaults_minimum_;
}
Edition maximum = PROTOBUF_MAXIMUM_EDITION;
Edition maximum = MaximumAllowedEdition();
if (edition_defaults_maximum_ != EDITION_UNKNOWN) {
maximum = edition_defaults_maximum_;
}
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/mock_code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class MockCodeGenerator : public CodeGenerator {
private:
std::string name_;
uint64_t suppressed_features_ = 0;
mutable Edition minimum_edition_ = PROTOBUF_MINIMUM_EDITION;
mutable Edition maximum_edition_ = PROTOBUF_MAXIMUM_EDITION;
mutable Edition minimum_edition_ = MinimumAllowedEdition();
mutable Edition maximum_edition_ = MaximumAllowedEdition();
std::vector<const FieldDescriptor*> feature_extensions_ = {
GetExtensionReflection(pb::test)};

Expand Down
7 changes: 0 additions & 7 deletions src/google/protobuf/descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7409,13 +7409,6 @@ TEST_F(ValidationErrorTest, UnusedImportWithOtherError) {
"foo.proto: Foo.foo: EXTENDEE: \"Baz\" is not defined.\n");
}

TEST(EditionsTest, DenseRange) {
for (int i = static_cast<int>(PROTOBUF_MINIMUM_EDITION);
i <= static_cast<int>(PROTOBUF_MAXIMUM_EDITION); ++i) {
EXPECT_TRUE(Edition_IsValid(i));
}
}

TEST(IsGroupLike, GroupLikeDelimited) {
using internal::cpp::IsGroupLike;
const Descriptor& msg = *editions_unittest::TestDelimited::descriptor();
Expand Down
10 changes: 0 additions & 10 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
#define PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE 1
#endif

#ifdef PROTOBUF_MINIMUM_EDITION
#error PROTOBUF_MINIMUM_EDITION was previously defined
#endif
#define PROTOBUF_MINIMUM_EDITION EDITION_PROTO2

#ifdef PROTOBUF_MAXIMUM_EDITION
#error PROTOBUF_MAXIMUM_EDITION was previously defined
#endif
#define PROTOBUF_MAXIMUM_EDITION EDITION_2023

#ifdef PROTOBUF_ALWAYS_INLINE
#error PROTOBUF_ALWAYS_INLINE was previously defined
#endif
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/port_undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
#undef PROTOBUF_IGNORE_DEPRECATION_START
#undef PROTOBUF_IGNORE_DEPRECATION_STOP
#undef PROTOBUF_RTTI
#undef PROTOBUF_MINIMUM_EDITION
#undef PROTOBUF_MAXIMUM_EDITION
#undef PROTOBUF_FIELD_OFFSET
#undef PROTOBUF_PREDICT_TRUE
#undef PROTOBUF_PREDICT_FALSE
Expand Down

0 comments on commit 9580af3

Please sign in to comment.