diff --git a/src/google/protobuf/compiler/code_generator.h b/src/google/protobuf/compiler/code_generator.h index e48ea30fca..35f105b261 100644 --- a/src/google/protobuf/compiler/code_generator.h +++ b/src/google/protobuf/compiler/code_generator.h @@ -119,11 +119,11 @@ class PROTOC_EXPORT CodeGenerator { // Returns the minimum edition (inclusive) supported by this generator. Any // proto files with an edition before this will result in an error. - virtual Edition GetMinimumEdition() const { return PROTOBUF_MINIMUM_EDITION; } + virtual Edition GetMinimumEdition() const { return Edition::EDITION_UNKNOWN; } // Returns the maximum edition (inclusive) supported by this generator. Any // proto files with an edition after this will result in an error. - virtual Edition GetMaximumEdition() const { return PROTOBUF_MAXIMUM_EDITION; } + virtual Edition GetMaximumEdition() const { return Edition::EDITION_UNKNOWN; } // Builds a default feature set mapping for this generator. // diff --git a/src/google/protobuf/compiler/cpp/generator.h b/src/google/protobuf/compiler/cpp/generator.h index d962a7e0e3..64f334d5b3 100644 --- a/src/google/protobuf/compiler/cpp/generator.h +++ b/src/google/protobuf/compiler/cpp/generator.h @@ -22,6 +22,7 @@ #include "google/protobuf/compiler/code_generator.h" #include "absl/status/status.h" #include "google/protobuf/cpp_features.pb.h" +#include "google/protobuf/descriptor.pb.h" // Must be included last. #include "google/protobuf/port_def.inc" @@ -72,6 +73,9 @@ class PROTOC_EXPORT CppGenerator : public CodeGenerator { return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS; } + Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } + Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } + std::vector GetFeatureExtensions() const override { return {GetExtensionReflection(pb::cpp)}; } diff --git a/src/google/protobuf/compiler/mock_code_generator.cc b/src/google/protobuf/compiler/mock_code_generator.cc index 18748a61bc..d7057a1e67 100644 --- a/src/google/protobuf/compiler/mock_code_generator.cc +++ b/src/google/protobuf/compiler/mock_code_generator.cc @@ -204,7 +204,8 @@ bool MockCodeGenerator::Generate(const FileDescriptor* file, GeneratorContext* context, std::string* error) const { if (FileDescriptorLegacy(file).syntax() == - FileDescriptorLegacy::SYNTAX_EDITIONS) { + FileDescriptorLegacy::SYNTAX_EDITIONS && + (suppressed_features_ & CodeGenerator::FEATURE_SUPPORTS_EDITIONS) == 0) { internal::VisitDescriptors(*file, [&](const auto& descriptor) { const FeatureSet& features = GetResolvedSourceFeatures(descriptor); ABSL_CHECK(features.HasExtension(pb::test)) diff --git a/src/google/protobuf/compiler/objectivec/generator.h b/src/google/protobuf/compiler/objectivec/generator.h index 69109a8dd1..be5a6a4488 100644 --- a/src/google/protobuf/compiler/objectivec/generator.h +++ b/src/google/protobuf/compiler/objectivec/generator.h @@ -16,6 +16,7 @@ #include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" // Must be included last #include "google/protobuf/port_def.inc" @@ -48,6 +49,8 @@ class PROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator { uint64_t GetSupportedFeatures() const override { return (FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS); } + Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } + Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/plugin.cc b/src/google/protobuf/compiler/plugin.cc index 166e1c0851..471caa7914 100644 --- a/src/google/protobuf/compiler/plugin.cc +++ b/src/google/protobuf/compiler/plugin.cc @@ -94,15 +94,18 @@ bool GenerateCode(const CodeGeneratorRequest& request, CodeGeneratorResponse* response, std::string* error_msg) { DescriptorPool pool; - // Initialize feature set default mapping. - absl::StatusOr defaults = - generator.BuildFeatureSetDefaults(); - if (!defaults.ok()) { - *error_msg = absl::StrCat("error generating feature defaults: ", - defaults.status().message()); - return false; + if (generator.GetSupportedFeatures() & + CodeGenerator::FEATURE_SUPPORTS_EDITIONS) { + // Initialize feature set default mapping. + absl::StatusOr defaults = + generator.BuildFeatureSetDefaults(); + if (!defaults.ok()) { + *error_msg = absl::StrCat("error generating feature defaults: ", + defaults.status().message()); + return false; + } + pool.SetFeatureSetDefaults(*defaults); } - pool.SetFeatureSetDefaults(*defaults); for (int i = 0; i < request.proto_file_size(); i++) { const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); diff --git a/src/google/protobuf/compiler/rust/generator.h b/src/google/protobuf/compiler/rust/generator.h index ac1f3a450b..d4b05a5fcf 100644 --- a/src/google/protobuf/compiler/rust/generator.h +++ b/src/google/protobuf/compiler/rust/generator.h @@ -36,6 +36,8 @@ class PROTOC_EXPORT RustGenerator final uint64_t GetSupportedFeatures() const override { return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS; } + Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } + Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } }; } // namespace rust