[ObjC] Add ModuleForFile helper method in ImportWriter (#12012)

Custom `protoc` plugins may use this method to look up the framework name for a given file if they want to generate code not related to `import`s.

Closes #12012

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12012 from dnkoutso:import_writer_module_for_file 57844831d5
PiperOrigin-RevId: 512629671
pull/11951/head
Dimitris Koutsogiorgas 2 years ago committed by Copybara-Service
parent 343cef1bd9
commit 10d185f7bd
  1. 27
      src/google/protobuf/compiler/objectivec/import_writer.cc
  2. 3
      src/google/protobuf/compiler/objectivec/import_writer.h

@ -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_) {

@ -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;

Loading…
Cancel
Save