Skip to content

Commit

Permalink
[ObjC] Fix minimal imports for root generation.
Browse files Browse the repository at this point in the history
Imports where the types aren't used, but they do vend extensions
also need to be imported into the generated source.

PiperOrigin-RevId: 559419899
  • Loading branch information
thomasvl authored and copybara-github committed Aug 23, 2023
1 parent 2d67dc4 commit 3bc507d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
35 changes: 14 additions & 21 deletions src/google/protobuf/compiler/objectivec/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,7 @@ void FileGenerator::GenerateSource(io::Printer* p) const {
std::vector<const FileDescriptor*> deps_with_extensions =
common_state_->CollectMinimalFileDepsContainingExtensions(file_);
GeneratedFileOptions file_options;

// If any indirect dependency provided extensions, it needs to be directly
// imported so it can get merged into the root's extensions registry.
// See the Note by CollectMinimalFileDepsContainingExtensions before
// changing this.
for (auto& dep : deps_with_extensions) {
if (!IsDirectDependency(dep, file_)) {
file_options.extra_files_to_import.push_back(dep);
}
}
file_options.forced_files_to_import = deps_with_extensions;

absl::btree_set<std::string> fwd_decls;
for (const auto& generator : message_generators_) {
Expand Down Expand Up @@ -432,16 +423,7 @@ void FileGenerator::GenerateGlobalSource(io::Printer* p) const {
std::vector<const FileDescriptor*> deps_with_extensions =
common_state_->CollectMinimalFileDepsContainingExtensions(file_);
GeneratedFileOptions file_options;

// If any indirect dependency provided extensions, it needs to be directly
// imported so it can get merged into the root's extensions registry.
// See the Note by CollectMinimalFileDepsContainingExtensions before
// changing this.
for (auto& dep : deps_with_extensions) {
if (!IsDirectDependency(dep, file_)) {
file_options.extra_files_to_import.push_back(dep);
}
}
file_options.forced_files_to_import = deps_with_extensions;

absl::btree_set<std::string> fwd_decls;
for (const auto& generator : extension_generators_) {
Expand Down Expand Up @@ -551,7 +533,18 @@ void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type,
break;
}

// If a forced file was a direct dep, move it into the file_imports.
std::vector<const FileDescriptor*> extra_files_to_import;
for (const auto& dep : file_options.forced_files_to_import) {
if (IsDirectDependency(dep, file_)) {
file_imports.insert(dep);
} else {
extra_files_to_import.push_back(dep);
}
}

if (!file_imports.empty()) {
// Output the file_imports in the order they were listed as dependencies.
for (int i = 0; i < file_->dependency_count(); i++) {
const FileDescriptor* dep = file_->dependency(i);
if (file_imports.contains(dep)) {
Expand All @@ -560,7 +553,7 @@ void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type,
}
}

for (const auto& dep : file_options.extra_files_to_import) {
for (const auto& dep : extra_files_to_import) {
import_writer.AddFile(dep, header_extension);
}

Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/objectivec/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class FileGenerator {
enum class GeneratedFileType : int { kHeader, kSource };
struct GeneratedFileOptions {
std::vector<std::string> ignored_warnings;
std::vector<const FileDescriptor*> extra_files_to_import;
std::vector<const FileDescriptor*> forced_files_to_import;
std::vector<std::string> extra_system_headers;
};

Expand Down

0 comments on commit 3bc507d

Please sign in to comment.