[ObjC] Use an struct to pass info to the file helpers.

No real changes, just adding the struct before the next change which is
yet another option.

PiperOrigin-RevId: 508131090
pull/11856/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 17c6597dc7
commit 99e83d07fe
  1. 36
      src/google/protobuf/compiler/objectivec/file.cc
  2. 13
      src/google/protobuf/compiler/objectivec/file.h

@ -313,15 +313,15 @@ void FileGenerator::GenerateHeader(io::Printer* p) const {
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.
std::vector<const FileDescriptor*> extra_files;
for (auto& dep : deps_with_extensions) {
if (!IsDirectDependency(dep, file_)) {
extra_files.push_back(dep);
file_options.extra_files_to_import.push_back(dep);
}
}
@ -333,22 +333,21 @@ void FileGenerator::GenerateSource(io::Printer* p) const {
generator->DetermineObjectiveCClassDefinitions(&fwd_decls);
}
std::vector<std::string> ignored_warnings;
// The generated code for oneof's uses direct ivar access, suppress the
// warning in case developer turn that on in the context they compile the
// generated code.
for (const auto& generator : message_generators_) {
if (generator->IncludesOneOfDefinition()) {
ignored_warnings.push_back("direct-ivar-access");
file_options.ignored_warnings.push_back("direct-ivar-access");
break;
}
}
if (!fwd_decls.empty()) {
ignored_warnings.push_back("dollar-in-identifier-extension");
file_options.ignored_warnings.push_back("dollar-in-identifier-extension");
}
GenerateFile(
p, GeneratedFileType::kSource, ignored_warnings, extra_files, [&] {
p, GeneratedFileType::kSource, file_options, [&] {
if (!fwd_decls.empty()) {
p->Print(
// clang-format off
@ -377,15 +376,15 @@ void FileGenerator::GenerateSource(io::Printer* p) const {
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.
std::vector<const FileDescriptor*> extra_files;
for (auto& dep : deps_with_extensions) {
if (!IsDirectDependency(dep, file_)) {
extra_files.push_back(dep);
file_options.extra_files_to_import.push_back(dep);
}
}
@ -394,13 +393,12 @@ void FileGenerator::GenerateGlobalSource(io::Printer* p) const {
generator->DetermineObjectiveCClassDefinitions(&fwd_decls);
}
std::vector<std::string> ignored_warnings;
if (!fwd_decls.empty()) {
ignored_warnings.push_back("dollar-in-identifier-extension");
file_options.ignored_warnings.push_back("dollar-in-identifier-extension");
}
GenerateFile(
p, GeneratedFileType::kSource, ignored_warnings, extra_files, [&] {
p, GeneratedFileType::kSource, file_options, [&] {
if (!fwd_decls.empty()) {
p->Print(
// clang-format off
@ -432,15 +430,15 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) const {
absl::btree_set<std::string> fwd_decls;
generator->DetermineObjectiveCClassDefinitions(&fwd_decls);
std::vector<std::string> ignored_warnings;
GeneratedFileOptions file_options;
// The generated code for oneof's uses direct ivar access, suppress the
// warning in case developer turn that on in the context they compile the
// generated code.
if (generator->IncludesOneOfDefinition()) {
ignored_warnings.push_back("direct-ivar-access");
file_options.ignored_warnings.push_back("direct-ivar-access");
}
GenerateFile(p, GeneratedFileType::kSource, ignored_warnings, {}, [&] {
GenerateFile(p, GeneratedFileType::kSource, file_options, [&] {
if (!fwd_decls.empty()) {
p->Print(
// clang-format off
@ -459,10 +457,8 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) const {
});
}
void FileGenerator::GenerateFile(
io::Printer* p, GeneratedFileType file_type,
const std::vector<std::string>& ignored_warnings,
const std::vector<const FileDescriptor*>& extra_files_to_import,
void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type,
const GeneratedFileOptions& file_options,
std::function<void()> body) const {
ImportWriter import_writer(
generation_options_.generate_for_named_framework,
@ -514,7 +510,7 @@ void FileGenerator::GenerateFile(
break;
}
for (const auto& dep : extra_files_to_import) {
for (const auto& dep : file_options.extra_files_to_import) {
import_writer.AddFile(dep, header_extension);
}
@ -564,7 +560,7 @@ void FileGenerator::GenerateFile(
"#pragma clang diagnostic push\n"
"#pragma clang diagnostic ignored \"-Wdeprecated-declarations\"\n");
// clang-format on
for (const auto& warning : ignored_warnings) {
for (const auto& warning : file_options.ignored_warnings) {
p->Print("#pragma clang diagnostic ignored \"-W$warning$\"\n", "warning",
warning);
}

@ -93,15 +93,18 @@ class FileGenerator {
private:
enum class GeneratedFileType : int { kHeader, kSource };
struct GeneratedFileOptions {
std::vector<std::string> ignored_warnings;
std::vector<const FileDescriptor*> extra_files_to_import;
};
void GenerateFile(
io::Printer* p, GeneratedFileType file_type,
const std::vector<std::string>& ignored_warnings,
const std::vector<const FileDescriptor*>& extra_files_to_import,
void GenerateFile(io::Printer* p, GeneratedFileType file_type,
const GeneratedFileOptions& file_options,
std::function<void()> body) const;
void GenerateFile(io::Printer* p, GeneratedFileType file_type,
std::function<void()> body) const {
GenerateFile(p, file_type, {}, {}, body);
GeneratedFileOptions file_options;
GenerateFile(p, file_type, file_options, body);
}
void PrintRootImplementation(

Loading…
Cancel
Save