diff --git a/src/google/protobuf/compiler/code_generator.cc b/src/google/protobuf/compiler/code_generator.cc index c91af364a8..a9cfa2c0e6 100644 --- a/src/google/protobuf/compiler/code_generator.cc +++ b/src/google/protobuf/compiler/code_generator.cc @@ -70,7 +70,7 @@ absl::StatusOr CodeGenerator::BuildFeatureSetDefaults() // anyway. return FeatureResolver::CompileDefaults( FeatureSet::descriptor(), GetFeatureExtensions(), - PROTOBUF_MINIMUM_EDITION, PROTOBUF_MAXIMUM_EDITION); + MinimumAllowedEdition(), MaximumAllowedEdition()); } return FeatureResolver::CompileDefaults( FeatureSet::descriptor(), GetFeatureExtensions(), GetMinimumEdition(), diff --git a/src/google/protobuf/compiler/code_generator.h b/src/google/protobuf/compiler/code_generator.h index 7d0eb3904f..fc6c8dff05 100644 --- a/src/google/protobuf/compiler/code_generator.h +++ b/src/google/protobuf/compiler/code_generator.h @@ -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 diff --git a/src/google/protobuf/compiler/code_generator_unittest.cc b/src/google/protobuf/compiler/code_generator_unittest.cc index 3e5381bce5..1bbd24cb1f 100644 --- a/src/google/protobuf/compiler/code_generator_unittest.cc +++ b/src/google/protobuf/compiler/code_generator_unittest.cc @@ -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 feature_extensions_ = { GetExtensionReflection(pb::test)}; }; @@ -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(MinimumAllowedEdition()); + i <= static_cast(MaximumAllowedEdition()); ++i) { + EXPECT_TRUE(Edition_IsValid(i)); + } } #include "google/protobuf/port_undef.inc" diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 6d95e9e533..ae6b87e4ad 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -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 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_ && @@ -3037,11 +3037,11 @@ bool CommandLineInterface::WriteEditionDefaults(const DescriptorPool& pool) { std::vector 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_; } diff --git a/src/google/protobuf/compiler/mock_code_generator.h b/src/google/protobuf/compiler/mock_code_generator.h index 8091f38e4a..0e40b17df1 100644 --- a/src/google/protobuf/compiler/mock_code_generator.h +++ b/src/google/protobuf/compiler/mock_code_generator.h @@ -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 feature_extensions_ = { GetExtensionReflection(pb::test)}; diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 32eb595516..f00b17a909 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -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(PROTOBUF_MINIMUM_EDITION); - i <= static_cast(PROTOBUF_MAXIMUM_EDITION); ++i) { - EXPECT_TRUE(Edition_IsValid(i)); - } -} - TEST(IsGroupLike, GroupLikeDelimited) { using internal::cpp::IsGroupLike; const Descriptor& msg = *editions_unittest::TestDelimited::descriptor(); diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 40686db940..8e9b8ffd16 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -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 diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index 8b0eaa4102..784b102d4c 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -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