From 99e83d07fe249ed3ce89594735a6901dfa43e904 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 8 Feb 2023 11:11:45 -0800 Subject: [PATCH] [ObjC] Use an struct to pass info to the file helpers. No real changes, just adding the struct before the next change which is yet another option. PiperOrigin-RevId: 508131090 --- .../protobuf/compiler/objectivec/file.cc | 50 +++++++++---------- .../protobuf/compiler/objectivec/file.h | 15 +++--- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/file.cc b/src/google/protobuf/compiler/objectivec/file.cc index b389701729..8cb3990ea7 100644 --- a/src/google/protobuf/compiler/objectivec/file.cc +++ b/src/google/protobuf/compiler/objectivec/file.cc @@ -313,15 +313,15 @@ void FileGenerator::GenerateHeader(io::Printer* p) const { void FileGenerator::GenerateSource(io::Printer* p) const { std::vector deps_with_extensions = common_state_->CollectMinimalFileDepsContainingExtensions(file_); + GeneratedFileOptions file_options; // If any indirect dependency provided extensions, it needs to be directly // imported so it can get merged into the root's extensions registry. // See the Note by CollectMinimalFileDepsContainingExtensions before // changing this. - std::vector extra_files; for (auto& dep : deps_with_extensions) { if (!IsDirectDependency(dep, file_)) { - extra_files.push_back(dep); + file_options.extra_files_to_import.push_back(dep); } } @@ -333,22 +333,21 @@ void FileGenerator::GenerateSource(io::Printer* p) const { generator->DetermineObjectiveCClassDefinitions(&fwd_decls); } - std::vector ignored_warnings; // The generated code for oneof's uses direct ivar access, suppress the // warning in case developer turn that on in the context they compile the // generated code. for (const auto& generator : message_generators_) { if (generator->IncludesOneOfDefinition()) { - ignored_warnings.push_back("direct-ivar-access"); + file_options.ignored_warnings.push_back("direct-ivar-access"); break; } } if (!fwd_decls.empty()) { - ignored_warnings.push_back("dollar-in-identifier-extension"); + file_options.ignored_warnings.push_back("dollar-in-identifier-extension"); } GenerateFile( - p, GeneratedFileType::kSource, ignored_warnings, extra_files, [&] { + p, GeneratedFileType::kSource, file_options, [&] { if (!fwd_decls.empty()) { p->Print( // clang-format off @@ -377,15 +376,15 @@ void FileGenerator::GenerateSource(io::Printer* p) const { void FileGenerator::GenerateGlobalSource(io::Printer* p) const { std::vector deps_with_extensions = common_state_->CollectMinimalFileDepsContainingExtensions(file_); + GeneratedFileOptions file_options; // If any indirect dependency provided extensions, it needs to be directly // imported so it can get merged into the root's extensions registry. // See the Note by CollectMinimalFileDepsContainingExtensions before // changing this. - std::vector extra_files; for (auto& dep : deps_with_extensions) { if (!IsDirectDependency(dep, file_)) { - extra_files.push_back(dep); + file_options.extra_files_to_import.push_back(dep); } } @@ -394,13 +393,12 @@ void FileGenerator::GenerateGlobalSource(io::Printer* p) const { generator->DetermineObjectiveCClassDefinitions(&fwd_decls); } - std::vector ignored_warnings; if (!fwd_decls.empty()) { - ignored_warnings.push_back("dollar-in-identifier-extension"); + file_options.ignored_warnings.push_back("dollar-in-identifier-extension"); } GenerateFile( - p, GeneratedFileType::kSource, ignored_warnings, extra_files, [&] { + p, GeneratedFileType::kSource, file_options, [&] { if (!fwd_decls.empty()) { p->Print( // clang-format off @@ -432,24 +430,24 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) const { absl::btree_set fwd_decls; generator->DetermineObjectiveCClassDefinitions(&fwd_decls); - std::vector ignored_warnings; + GeneratedFileOptions file_options; // The generated code for oneof's uses direct ivar access, suppress the // warning in case developer turn that on in the context they compile the // generated code. if (generator->IncludesOneOfDefinition()) { - ignored_warnings.push_back("direct-ivar-access"); + file_options.ignored_warnings.push_back("direct-ivar-access"); } - GenerateFile(p, GeneratedFileType::kSource, ignored_warnings, {}, [&] { + GenerateFile(p, GeneratedFileType::kSource, file_options, [&] { if (!fwd_decls.empty()) { p->Print( // clang-format off - "#pragma mark - Objective C Class declarations\n" - "// Forward declarations of Objective C classes that we can use as\n" - "// static values in struct initializers.\n" - "// We don't use [Foo class] because it is not a static value.\n" - "$fwd_decls$\n" - "\n", + "#pragma mark - Objective C Class declarations\n" + "// Forward declarations of Objective C classes that we can use as\n" + "// static values in struct initializers.\n" + "// We don't use [Foo class] because it is not a static value.\n" + "$fwd_decls$\n" + "\n", // clang-format on "fwd_decls", absl::StrJoin(fwd_decls, "\n")); } @@ -459,11 +457,9 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) const { }); } -void FileGenerator::GenerateFile( - io::Printer* p, GeneratedFileType file_type, - const std::vector& ignored_warnings, - const std::vector& extra_files_to_import, - std::function body) const { +void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type, + const GeneratedFileOptions& file_options, + std::function body) const { ImportWriter import_writer( generation_options_.generate_for_named_framework, generation_options_.named_framework_to_proto_path_mappings_path, @@ -514,7 +510,7 @@ void FileGenerator::GenerateFile( break; } - for (const auto& dep : extra_files_to_import) { + for (const auto& dep : file_options.extra_files_to_import) { import_writer.AddFile(dep, header_extension); } @@ -564,7 +560,7 @@ void FileGenerator::GenerateFile( "#pragma clang diagnostic push\n" "#pragma clang diagnostic ignored \"-Wdeprecated-declarations\"\n"); // clang-format on - for (const auto& warning : ignored_warnings) { + for (const auto& warning : file_options.ignored_warnings) { p->Print("#pragma clang diagnostic ignored \"-W$warning$\"\n", "warning", warning); } diff --git a/src/google/protobuf/compiler/objectivec/file.h b/src/google/protobuf/compiler/objectivec/file.h index 5b7c1c03b0..a69ade3fdf 100644 --- a/src/google/protobuf/compiler/objectivec/file.h +++ b/src/google/protobuf/compiler/objectivec/file.h @@ -93,15 +93,18 @@ class FileGenerator { private: enum class GeneratedFileType : int { kHeader, kSource }; + struct GeneratedFileOptions { + std::vector ignored_warnings; + std::vector extra_files_to_import; + }; - void GenerateFile( - io::Printer* p, GeneratedFileType file_type, - const std::vector& ignored_warnings, - const std::vector& extra_files_to_import, - std::function body) const; + void GenerateFile(io::Printer* p, GeneratedFileType file_type, + const GeneratedFileOptions& file_options, + std::function body) const; void GenerateFile(io::Printer* p, GeneratedFileType file_type, std::function body) const { - GenerateFile(p, file_type, {}, {}, body); + GeneratedFileOptions file_options; + GenerateFile(p, file_type, file_options, body); } void PrintRootImplementation(