[ObjC] Update ImportWriter to use Printer::Emit.

PiperOrigin-RevId: 491967581
pull/11080/head
Protobuf Team Bot 3 years ago committed by Copybara-Service
parent 51535c0a53
commit 3b00bfff78
  1. 3
      kokoro/macos/php74/build.sh
  2. 1
      kokoro/macos/prepare_build_macos_rc
  3. 10
      src/google/protobuf/compiler/objectivec/file.cc
  4. 154
      src/google/protobuf/compiler/objectivec/import_writer.cc
  5. 6
      src/google/protobuf/compiler/objectivec/import_writer.h

@ -9,9 +9,8 @@ cd $(dirname $0)/../../..
source kokoro/macos/prepare_build_macos_rc source kokoro/macos/prepare_build_macos_rc
# Install Dependencies # Install Dependencies
# PHP 7.4 is already installed on the machine
brew cleanup brew cleanup
brew install coreutils brew install coreutils php@7.4
# Configure path # Configure path
PHP_FOLDER=$(find $HOMEBREW_PREFIX -type d -regex ".*php.*/7.4.[0-9_.]*" | sort -n | tail -n 1) PHP_FOLDER=$(find $HOMEBREW_PREFIX -type d -regex ".*php.*/7.4.[0-9_.]*" | sort -n | tail -n 1)

@ -5,7 +5,6 @@
set -eux set -eux
export HOMEBREW_PREFIX=$(brew --prefix) export HOMEBREW_PREFIX=$(brew --prefix)
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 # Do not automatically update packages.
## ##
# Select Xcode version # Select Xcode version

@ -298,7 +298,7 @@ void FileGenerator::GenerateHeader(io::Printer* printer) const {
import_writer.AddFile(file_->dependency(i), header_extension); import_writer.AddFile(file_->dependency(i), header_extension);
} }
} }
import_writer.Print(printer); import_writer.Emit(printer);
} }
// Note: // Note:
@ -455,7 +455,7 @@ void FileGenerator::GenerateSource(io::Printer* printer) const {
} }
} }
import_writer.Print(printer); import_writer.Emit(printer);
} }
bool includes_oneof = false; bool includes_oneof = false;
@ -710,9 +710,9 @@ void FileGenerator::PrintFileRuntimePreamble(
import_prefix, "header", header); import_prefix, "header", header);
} }
} else { } else {
ImportWriter::PrintRuntimeImports(printer, headers_to_import, ImportWriter::EmitRuntimeImports(printer, headers_to_import,
generation_options_.runtime_import_prefix, generation_options_.runtime_import_prefix,
true); true);
} }
printer->Print("\n"); printer->Print("\n");

@ -162,78 +162,112 @@ void ImportWriter::AddFile(const FileDescriptor* file,
other_imports_.push_back(FilePath(file) + header_extension); other_imports_.push_back(FilePath(file) + header_extension);
} }
void ImportWriter::Print(io::Printer* printer) const { void ImportWriter::Emit(io::Printer* p) const {
bool add_blank_line = false; p->Emit(
{
if (!protobuf_imports_.empty()) { {"runtime_import",
PrintRuntimeImports(printer, protobuf_imports_, runtime_import_prefix_); [&] {
add_blank_line = true; if (!protobuf_imports_.empty()) {
} EmitRuntimeImports(p, protobuf_imports_, runtime_import_prefix_);
}
if (!other_framework_imports_.empty()) { }},
if (add_blank_line) { {"other_framework_imports",
printer->Print("\n"); [&] {
} for (const auto& header : other_framework_imports_) {
p->Emit({{"header", header}},
for (std::vector<std::string>::const_iterator iter = R"objc(
other_framework_imports_.begin(); #import <$header$>
iter != other_framework_imports_.end(); ++iter) { )objc");
printer->Print("#import <$header$>\n", "header", *iter); }
} }},
{"other_imports",
add_blank_line = true; [&] {
} for (const auto& header : other_imports_) {
p->Emit({{"header", header}},
if (!other_imports_.empty()) { R"objc(
if (add_blank_line) { #import "$header$"
printer->Print("\n"); )objc");
} }
}},
for (std::vector<std::string>::const_iterator iter = other_imports_.begin(); },
iter != other_imports_.end(); ++iter) { R"objc(
printer->Print("#import \"$header$\"\n", "header", *iter); $runtime_import$;
} $other_framework_imports$;
} $other_imports$;
)objc");
} }
void ImportWriter::PrintRuntimeImports( void ImportWriter::EmitRuntimeImports(
io::Printer* printer, const std::vector<std::string>& header_to_import, io::Printer* p, const std::vector<std::string>& header_to_import,
const std::string& runtime_import_prefix, bool default_cpp_symbol) { const std::string& runtime_import_prefix, bool default_cpp_symbol) {
// Given an override, use that. // Given an override, use that.
if (!runtime_import_prefix.empty()) { if (!runtime_import_prefix.empty()) {
for (const auto& header : header_to_import) { p->Emit(
printer->Print(" #import \"$import_prefix$/$header$\"\n", "import_prefix", {
runtime_import_prefix, "header", header); {"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; return;
} }
const std::string framework_name(ProtobufLibraryFrameworkName); auto v = p->WithVars({
const std::string cpp_symbol(ProtobufFrameworkImportSymbol(framework_name)); {"cpp_symbol",
ProtobufFrameworkImportSymbol(ProtobufLibraryFrameworkName)},
});
if (default_cpp_symbol) { if (default_cpp_symbol) {
printer->Print( p->Emit(
// clang-format off R"objc(
"// This CPP symbol can be defined to use imports that match up to the framework\n" // This CPP symbol can be defined to use imports that match up to the framework
"// imports needed when using CocoaPods.\n" // imports needed when using CocoaPods.
"#if !defined($cpp_symbol$)\n" #if !defined($cpp_symbol$)
" #define $cpp_symbol$ 0\n" #define $cpp_symbol$ 0
"#endif\n" #endif
"\n",
// clang-format on )objc");
"cpp_symbol", cpp_symbol);
} }
printer->Print("#if $cpp_symbol$\n", "cpp_symbol", cpp_symbol); p->Emit(
for (const auto& header : header_to_import) { {
printer->Print(" #import <$framework_name$/$header$>\n", "framework_name", {"framework_name", ProtobufLibraryFrameworkName},
framework_name, "header", header); {"framework_imports",
} [&] {
printer->Print("#else\n"); for (const auto& header : header_to_import) {
for (const auto& header : header_to_import) { p->Emit({{"header", header}},
printer->Print(" #import \"$header$\"\n", "header", header); R"objc(
} #import <$framework_name$/$header$>
printer->Print("#endif\n"); )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() { void ImportWriter::ParseFrameworkMappings() {

@ -54,10 +54,10 @@ class ImportWriter {
~ImportWriter() = default; ~ImportWriter() = default;
void AddFile(const FileDescriptor* file, const std::string& header_extension); 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( static void EmitRuntimeImports(
io::Printer* printer, const std::vector<std::string>& header_to_import, io::Printer* p, const std::vector<std::string>& header_to_import,
const std::string& runtime_import_prefix, const std::string& runtime_import_prefix,
bool default_cpp_symbol = false); bool default_cpp_symbol = false);

Loading…
Cancel
Save