From 3b00bfff7837513927c1e6f455b64c325b93a70b Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 30 Nov 2022 11:07:34 -0800 Subject: [PATCH] [ObjC] Update ImportWriter to use Printer::Emit. PiperOrigin-RevId: 491967581 --- kokoro/macos/php74/build.sh | 3 +- kokoro/macos/prepare_build_macos_rc | 1 - .../protobuf/compiler/objectivec/file.cc | 10 +- .../compiler/objectivec/import_writer.cc | 154 +++++++++++------- .../compiler/objectivec/import_writer.h | 6 +- 5 files changed, 103 insertions(+), 71 deletions(-) diff --git a/kokoro/macos/php74/build.sh b/kokoro/macos/php74/build.sh index f0444347ef..2ad4eab598 100755 --- a/kokoro/macos/php74/build.sh +++ b/kokoro/macos/php74/build.sh @@ -9,9 +9,8 @@ cd $(dirname $0)/../../.. source kokoro/macos/prepare_build_macos_rc # Install Dependencies -# PHP 7.4 is already installed on the machine brew cleanup -brew install coreutils +brew install coreutils php@7.4 # Configure path PHP_FOLDER=$(find $HOMEBREW_PREFIX -type d -regex ".*php.*/7.4.[0-9_.]*" | sort -n | tail -n 1) diff --git a/kokoro/macos/prepare_build_macos_rc b/kokoro/macos/prepare_build_macos_rc index 7156d58b32..d8b1395164 100755 --- a/kokoro/macos/prepare_build_macos_rc +++ b/kokoro/macos/prepare_build_macos_rc @@ -5,7 +5,6 @@ set -eux export HOMEBREW_PREFIX=$(brew --prefix) -export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 # Do not automatically update packages. ## # Select Xcode version diff --git a/src/google/protobuf/compiler/objectivec/file.cc b/src/google/protobuf/compiler/objectivec/file.cc index 3cad0ccd04..efe24431e9 100644 --- a/src/google/protobuf/compiler/objectivec/file.cc +++ b/src/google/protobuf/compiler/objectivec/file.cc @@ -298,7 +298,7 @@ void FileGenerator::GenerateHeader(io::Printer* printer) const { import_writer.AddFile(file_->dependency(i), header_extension); } } - import_writer.Print(printer); + import_writer.Emit(printer); } // Note: @@ -455,7 +455,7 @@ void FileGenerator::GenerateSource(io::Printer* printer) const { } } - import_writer.Print(printer); + import_writer.Emit(printer); } bool includes_oneof = false; @@ -710,9 +710,9 @@ void FileGenerator::PrintFileRuntimePreamble( import_prefix, "header", header); } } else { - ImportWriter::PrintRuntimeImports(printer, headers_to_import, - generation_options_.runtime_import_prefix, - true); + ImportWriter::EmitRuntimeImports(printer, headers_to_import, + generation_options_.runtime_import_prefix, + true); } printer->Print("\n"); diff --git a/src/google/protobuf/compiler/objectivec/import_writer.cc b/src/google/protobuf/compiler/objectivec/import_writer.cc index ff154a21ec..6ddb4f6f2c 100644 --- a/src/google/protobuf/compiler/objectivec/import_writer.cc +++ b/src/google/protobuf/compiler/objectivec/import_writer.cc @@ -162,78 +162,112 @@ void ImportWriter::AddFile(const FileDescriptor* file, other_imports_.push_back(FilePath(file) + header_extension); } -void ImportWriter::Print(io::Printer* printer) const { - bool add_blank_line = false; - - if (!protobuf_imports_.empty()) { - PrintRuntimeImports(printer, protobuf_imports_, runtime_import_prefix_); - add_blank_line = true; - } - - if (!other_framework_imports_.empty()) { - if (add_blank_line) { - printer->Print("\n"); - } - - for (std::vector::const_iterator iter = - other_framework_imports_.begin(); - iter != other_framework_imports_.end(); ++iter) { - printer->Print("#import <$header$>\n", "header", *iter); - } - - add_blank_line = true; - } - - if (!other_imports_.empty()) { - if (add_blank_line) { - printer->Print("\n"); - } - - for (std::vector::const_iterator iter = other_imports_.begin(); - iter != other_imports_.end(); ++iter) { - printer->Print("#import \"$header$\"\n", "header", *iter); - } - } +void ImportWriter::Emit(io::Printer* p) const { + p->Emit( + { + {"runtime_import", + [&] { + if (!protobuf_imports_.empty()) { + EmitRuntimeImports(p, protobuf_imports_, runtime_import_prefix_); + } + }}, + {"other_framework_imports", + [&] { + for (const auto& header : other_framework_imports_) { + p->Emit({{"header", header}}, + R"objc( + #import <$header$> + )objc"); + } + }}, + {"other_imports", + [&] { + for (const auto& header : other_imports_) { + p->Emit({{"header", header}}, + R"objc( + #import "$header$" + )objc"); + } + }}, + }, + R"objc( + $runtime_import$; + $other_framework_imports$; + $other_imports$; + )objc"); } -void ImportWriter::PrintRuntimeImports( - io::Printer* printer, const std::vector& header_to_import, +void ImportWriter::EmitRuntimeImports( + io::Printer* p, const std::vector& header_to_import, const std::string& runtime_import_prefix, bool default_cpp_symbol) { // Given an override, use that. if (!runtime_import_prefix.empty()) { - for (const auto& header : header_to_import) { - printer->Print(" #import \"$import_prefix$/$header$\"\n", "import_prefix", - runtime_import_prefix, "header", header); - } + p->Emit( + { + {"import_prefix", runtime_import_prefix}, + {"imports", + [&] { + for (const auto& header : header_to_import) { + p->Emit({{"header", header}}, + R"objc( + #import "$import_prefix$/$header$" + )objc"); + } + }}, + }, + R"objc( + $imports$; + )objc"); + return; } - const std::string framework_name(ProtobufLibraryFrameworkName); - const std::string cpp_symbol(ProtobufFrameworkImportSymbol(framework_name)); + auto v = p->WithVars({ + {"cpp_symbol", + ProtobufFrameworkImportSymbol(ProtobufLibraryFrameworkName)}, + }); if (default_cpp_symbol) { - printer->Print( - // clang-format off - "// This CPP symbol can be defined to use imports that match up to the framework\n" - "// imports needed when using CocoaPods.\n" - "#if !defined($cpp_symbol$)\n" - " #define $cpp_symbol$ 0\n" - "#endif\n" - "\n", - // clang-format on - "cpp_symbol", cpp_symbol); + p->Emit( + R"objc( + // This CPP symbol can be defined to use imports that match up to the framework + // imports needed when using CocoaPods. + #if !defined($cpp_symbol$) + #define $cpp_symbol$ 0 + #endif + + )objc"); } - printer->Print("#if $cpp_symbol$\n", "cpp_symbol", cpp_symbol); - for (const auto& header : header_to_import) { - printer->Print(" #import <$framework_name$/$header$>\n", "framework_name", - framework_name, "header", header); - } - printer->Print("#else\n"); - for (const auto& header : header_to_import) { - printer->Print(" #import \"$header$\"\n", "header", header); - } - printer->Print("#endif\n"); + p->Emit( + { + {"framework_name", ProtobufLibraryFrameworkName}, + {"framework_imports", + [&] { + for (const auto& header : header_to_import) { + p->Emit({{"header", header}}, + R"objc( + #import <$framework_name$/$header$> + )objc"); + } + }}, + {"raw_imports", + [&] { + for (const auto& header : header_to_import) { + p->Emit({{"header", header}}, + R"objc( + #import "$header$" + )objc"); + } + }}, + }, + R"objc( + #if $cpp_symbol$ + $framework_imports$ + #else + $raw_imports$ + #endif + )objc"); } void ImportWriter::ParseFrameworkMappings() { diff --git a/src/google/protobuf/compiler/objectivec/import_writer.h b/src/google/protobuf/compiler/objectivec/import_writer.h index 7ad31cacf5..f1134e1307 100644 --- a/src/google/protobuf/compiler/objectivec/import_writer.h +++ b/src/google/protobuf/compiler/objectivec/import_writer.h @@ -54,10 +54,10 @@ class ImportWriter { ~ImportWriter() = default; void AddFile(const FileDescriptor* file, const std::string& header_extension); - void Print(io::Printer* printer) const; + void Emit(io::Printer* p) const; - static void PrintRuntimeImports( - io::Printer* printer, const std::vector& header_to_import, + static void EmitRuntimeImports( + io::Printer* p, const std::vector& header_to_import, const std::string& runtime_import_prefix, bool default_cpp_symbol = false);