Move runtime import writing into a helper.

File and ImportWriter duplicated the logic, so move it to a helper (on
ImportWriter), and share the impl instead of duplicating it since it
includes some conditional logic around Framework based import support.
pull/7604/head
Thomas Van Lenten 5 years ago
parent 57be643a36
commit c6c8bab9e2
  1. 54
      src/google/protobuf/compiler/objectivec/objectivec_file.cc
  2. 2
      src/google/protobuf/compiler/objectivec/objectivec_file.h
  3. 63
      src/google/protobuf/compiler/objectivec/objectivec_helpers.cc
  4. 4
      src/google/protobuf/compiler/objectivec/objectivec_helpers.h

@ -209,15 +209,15 @@ FileGenerator::FileGenerator(const FileDescriptor *file, const Options& options)
FileGenerator::~FileGenerator() {}
void FileGenerator::GenerateHeader(io::Printer *printer) {
std::set<string> headers;
std::vector<string> headers;
// Generated files bundled with the library get minimal imports, everything
// else gets the wrapper so everything is usable.
if (is_bundled_proto_) {
headers.insert("GPBRootObject.h");
headers.insert("GPBMessage.h");
headers.insert("GPBDescriptor.h");
headers.push_back("GPBDescriptor.h");
headers.push_back("GPBMessage.h");
headers.push_back("GPBRootObject.h");
} else {
headers.insert("GPBProtocolBuffers.h");
headers.push_back("GPBProtocolBuffers.h");
}
PrintFileRuntimePreamble(printer, headers);
@ -337,8 +337,8 @@ void FileGenerator::GenerateHeader(io::Printer *printer) {
void FileGenerator::GenerateSource(io::Printer *printer) {
// #import the runtime support.
std::set<string> headers;
headers.insert("GPBProtocolBuffers_RuntimeSupport.h");
std::vector<string> headers;
headers.push_back("GPBProtocolBuffers_RuntimeSupport.h");
PrintFileRuntimePreamble(printer, headers);
// Enums use atomic in the generated code, so add the system import as needed.
@ -590,48 +590,14 @@ void FileGenerator::GenerateSource(io::Printer *printer) {
// files. This currently only supports the runtime coming from a framework
// as defined by the official CocoaPod.
void FileGenerator::PrintFileRuntimePreamble(
io::Printer* printer, const std::set<string>& headers_to_import) const {
io::Printer* printer, const std::vector<string>& headers_to_import) const {
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n"
"\n",
"filename", file_->name());
const string framework_name(ProtobufLibraryFrameworkName);
const string cpp_symbol(ProtobufFrameworkImportSymbol(framework_name));
printer->Print(
"// 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"
"#if $cpp_symbol$\n",
"cpp_symbol", cpp_symbol);
for (std::set<string>::const_iterator iter = headers_to_import.begin();
iter != headers_to_import.end(); ++iter) {
printer->Print(
" #import <$framework_name$/$header$>\n",
"header", *iter,
"framework_name", framework_name);
}
printer->Print(
"#else\n");
for (std::set<string>::const_iterator iter = headers_to_import.begin();
iter != headers_to_import.end(); ++iter) {
printer->Print(
" #import \"$header$\"\n",
"header", *iter);
}
printer->Print(
"#endif\n"
"\n");
ImportWriter::PrintRuntimeImports(printer, headers_to_import, true);
printer->Print("\n");
}
} // namespace objectivec

@ -72,7 +72,7 @@ class FileGenerator {
const Options options_;
void PrintFileRuntimePreamble(
io::Printer* printer, const std::set<string>& headers_to_import) const;
io::Printer* printer, const std::vector<string>& headers_to_import) const;
};
} // namespace objectivec

@ -1618,30 +1618,7 @@ void ImportWriter::Print(io::Printer* printer) const {
bool add_blank_line = false;
if (!protobuf_imports_.empty()) {
const string framework_name(ProtobufLibraryFrameworkName);
const string cpp_symbol(ProtobufFrameworkImportSymbol(framework_name));
printer->Print(
"#if $cpp_symbol$\n",
"cpp_symbol", cpp_symbol);
for (std::vector<string>::const_iterator iter = protobuf_imports_.begin();
iter != protobuf_imports_.end(); ++iter) {
printer->Print(
" #import <$framework_name$/$header$>\n",
"framework_name", framework_name,
"header", *iter);
}
printer->Print(
"#else\n");
for (std::vector<string>::const_iterator iter = protobuf_imports_.begin();
iter != protobuf_imports_.end(); ++iter) {
printer->Print(
" #import \"$header$\"\n",
"header", *iter);
}
printer->Print(
"#endif\n");
PrintRuntimeImports(printer, protobuf_imports_);
add_blank_line = true;
}
@ -1674,6 +1651,44 @@ void ImportWriter::Print(io::Printer* printer) const {
}
}
void ImportWriter::PrintRuntimeImports(
io::Printer* printer,
const std::vector<string>& header_to_import,
bool default_cpp_symbol) {
const string framework_name(ProtobufLibraryFrameworkName);
const string cpp_symbol(ProtobufFrameworkImportSymbol(framework_name));
if (default_cpp_symbol) {
printer->Print(
"// 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",
"cpp_symbol", cpp_symbol);
}
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");
}
void ImportWriter::ParseFrameworkMappings() {
need_to_parse_mapping_file_ = false;
if (named_framework_to_proto_path_mappings_path_.empty()) {

@ -285,6 +285,10 @@ class PROTOC_EXPORT ImportWriter {
void AddFile(const FileDescriptor* file, const string& header_extension);
void Print(io::Printer *printer) const;
static void PrintRuntimeImports(io::Printer *printer,
const std::vector<string>& header_to_import,
bool default_cpp_symbol = false);
private:
class ProtoFrameworkCollector : public LineConsumer {
public:

Loading…
Cancel
Save