Replace PROTOBUF_{MINIMUM,MAXIMUM}_EDITION macros with constexpr functions

for easier maintenance.
No semantic change expected.

PiperOrigin-RevId: 676596269
pull/18381/head
Protobuf Team Bot 2 months ago committed by Copybara-Service
parent 7a7a087f56
commit 9580af32d1
  1. 2
      src/google/protobuf/compiler/code_generator.cc
  2. 3
      src/google/protobuf/compiler/code_generator.h
  3. 15
      src/google/protobuf/compiler/code_generator_unittest.cc
  4. 8
      src/google/protobuf/compiler/command_line_interface.cc
  5. 4
      src/google/protobuf/compiler/mock_code_generator.h
  6. 7
      src/google/protobuf/descriptor_unittest.cc
  7. 10
      src/google/protobuf/port_def.inc
  8. 2
      src/google/protobuf/port_undef.inc

@ -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(),

@ -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

@ -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)};
};
@ -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"

@ -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_ &&
@ -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_;
}

@ -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)};

@ -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();

@ -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

@ -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

Loading…
Cancel
Save