From 55ab8316c14569cadc578e15ffdea4f1bcbb0964 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 22 Aug 2023 08:27:05 -0700 Subject: [PATCH] [ObjC] Enable generation of .proto files using editions. PiperOrigin-RevId: 559119403 --- .../protobuf/compiler/objectivec/file.cc | 32 +++++++++++-------- .../protobuf/compiler/objectivec/generator.cc | 13 ++++++++ .../protobuf/compiler/objectivec/generator.h | 2 +- .../protobuf/compiler/objectivec/options.h | 2 ++ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/file.cc b/src/google/protobuf/compiler/objectivec/file.cc index cb146a0aa4..28998cacff 100644 --- a/src/google/protobuf/compiler/objectivec/file.cc +++ b/src/google/protobuf/compiler/objectivec/file.cc @@ -734,19 +734,25 @@ void FileGenerator::EmitFileDescription(io::Printer* p) const { const std::string objc_prefix(FileClassPrefix(file_)); std::string syntax; - switch (FileDescriptorLegacy(file_).syntax()) { - case FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN: - syntax = "GPBFileSyntaxUnknown"; - break; - case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: - syntax = "GPBFileSyntaxProto2"; - break; - case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: - syntax = "GPBFileSyntaxProto3"; - break; - case FileDescriptorLegacy::Syntax::SYNTAX_EDITIONS: - syntax = "GPBFileSyntaxProtoEditions"; - break; + if (generation_options_.experimental_strip_nonfunctional_codegen) { + // Doesn't matter for current sources, use Unknown as a marker for this + // mode. + syntax = "GPBFileSyntaxUnknown"; + } else { + switch (FileDescriptorLegacy(file_).syntax()) { + case FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN: + syntax = "GPBFileSyntaxUnknown"; + break; + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO2: + syntax = "GPBFileSyntaxProto2"; + break; + case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3: + syntax = "GPBFileSyntaxProto3"; + break; + case FileDescriptorLegacy::Syntax::SYNTAX_EDITIONS: + syntax = "GPBFileSyntaxProtoEditions"; + break; + } } p->Emit({{"file_description_name", file_description_name_}, diff --git a/src/google/protobuf/compiler/objectivec/generator.cc b/src/google/protobuf/compiler/objectivec/generator.cc index 4893d07f97..32d11f28f5 100644 --- a/src/google/protobuf/compiler/objectivec/generator.cc +++ b/src/google/protobuf/compiler/objectivec/generator.cc @@ -302,6 +302,16 @@ bool ObjectiveCGenerator::GenerateAll( options[i].second); return false; } + } else if (options[i].first == "experimental_strip_nonfunctional_codegen") { + if (!StringToBool( + options[i].second, + &generation_options.experimental_strip_nonfunctional_codegen)) { + *error = absl::StrCat( + "error: Unknown value for " + "experimental_strip_nonfunctional_codegen: ", + options[i].second); + return false; + } } else { *error = absl::StrCat("error: Unknown generator option: ", options[i].first); @@ -314,6 +324,9 @@ bool ObjectiveCGenerator::GenerateAll( if (generation_options.experimental_multi_source_generation) { generation_options.headers_use_forward_declarations = false; } + if (generation_options.experimental_strip_nonfunctional_codegen) { + generation_options.generate_minimal_imports = true; + } // ----------------------------------------------------------------- diff --git a/src/google/protobuf/compiler/objectivec/generator.h b/src/google/protobuf/compiler/objectivec/generator.h index 3219540959..0498a57cac 100644 --- a/src/google/protobuf/compiler/objectivec/generator.h +++ b/src/google/protobuf/compiler/objectivec/generator.h @@ -69,7 +69,7 @@ class PROTOC_EXPORT ObjectiveCGenerator : public CodeGenerator { std::string* error) const override; uint64_t GetSupportedFeatures() const override { - return FEATURE_PROTO3_OPTIONAL; + return (FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS); } }; diff --git a/src/google/protobuf/compiler/objectivec/options.h b/src/google/protobuf/compiler/objectivec/options.h index af9e3a5a4b..b852ca09c8 100644 --- a/src/google/protobuf/compiler/objectivec/options.h +++ b/src/google/protobuf/compiler/objectivec/options.h @@ -52,6 +52,8 @@ struct GenerationOptions { // TODO(thomasvl): Eventually flip this default to true. bool generate_minimal_imports = false; bool experimental_multi_source_generation = false; + // This is used by some of the protobuf infrastructure. + bool experimental_strip_nonfunctional_codegen = false; }; } // namespace objectivec