diff --git a/src/google/protobuf/compiler/objectivec/import_writer.cc b/src/google/protobuf/compiler/objectivec/import_writer.cc index d7ef3b9a3b..ca0718bed6 100644 --- a/src/google/protobuf/compiler/objectivec/import_writer.cc +++ b/src/google/protobuf/compiler/objectivec/import_writer.cc @@ -140,15 +140,11 @@ void ImportWriter::AddFile(const FileDescriptor* file, return; } - // Lazy parse any mappings. - if (need_to_parse_mapping_file_) { - ParseFrameworkMappings(); - } + auto module_name = ModuleForFile(file); - auto proto_lookup = proto_file_to_framework_name_.find(file->name()); - if (proto_lookup != proto_file_to_framework_name_.end()) { + if (!module_name.empty()) { other_framework_imports_.emplace_back(absl::StrCat( - proto_lookup->second, "/", FilePathBasename(file), header_extension)); + module_name, "/", FilePathBasename(file), header_extension)); return; } @@ -166,6 +162,23 @@ void ImportWriter::AddRuntimeImport(const std::string& header_name) { protobuf_imports_.push_back(header_name); } +std::string ImportWriter::ModuleForFile(const FileDescriptor* file) { + ABSL_DCHECK(!IsProtobufLibraryBundledProtoFile(file)); + + // Lazy parse any mappings. + if (need_to_parse_mapping_file_) { + ParseFrameworkMappings(); + } + + auto proto_lookup = proto_file_to_framework_name_.find(file->name()); + + if (proto_lookup != proto_file_to_framework_name_.end()) { + return proto_lookup->second; + } + + return ""; +} + void ImportWriter::PrintFileImports(io::Printer* p) const { if (!other_framework_imports_.empty()) { for (const auto& header : other_framework_imports_) { diff --git a/src/google/protobuf/compiler/objectivec/import_writer.h b/src/google/protobuf/compiler/objectivec/import_writer.h index 9f5e0a0206..1c168b1c60 100644 --- a/src/google/protobuf/compiler/objectivec/import_writer.h +++ b/src/google/protobuf/compiler/objectivec/import_writer.h @@ -55,6 +55,9 @@ class ImportWriter { void AddFile(const FileDescriptor* file, const std::string& header_extension); void AddRuntimeImport(const std::string& header_name); + // This can return an empty string if there is no module for the file. It also + // does not handle bundled proto files. + std::string ModuleForFile(const FileDescriptor* file); void PrintFileImports(io::Printer* p) const; void PrintRuntimeImports(io::Printer* p, bool default_cpp_symbol) const;