[ObjC] Put all enums into one file in multi file mode.

Since dead stripping is completely safe with C symbols and enums only generate C
functions, stick them all in one file to slightly reduce the potential cost of
multi file generation.

PiperOrigin-RevId: 503484196
pull/11614/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 5c5dcdd117
commit 13c0d0756c
  1. 9
      src/google/protobuf/compiler/objectivec/file.cc
  2. 11
      src/google/protobuf/compiler/objectivec/file.h
  3. 15
      src/google/protobuf/compiler/objectivec/generator.cc

@ -449,9 +449,12 @@ void FileGenerator::GenerateGlobalSource(io::Printer* p) const {
});
}
void FileGenerator::GenerateSourceForEnum(int idx, io::Printer* p) const {
GenerateFile(p, GeneratedFileType::kSource,
[&] { enum_generators_[idx]->GenerateSource(p); });
void FileGenerator::GenerateSourceForEnums(io::Printer* p) const {
GenerateFile(p, GeneratedFileType::kSource, [&] {
for (const auto& generator : enum_generators_) {
generator->GenerateSource(p);
}
});
}
void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) const {

@ -89,15 +89,16 @@ class FileGenerator {
void GenerateGlobalSource(io::Printer* p) const;
void GenerateSourceForMessage(int idx, io::Printer* p) const;
void GenerateSourceForEnum(int idx, io::Printer* p) const;
void GenerateSourceForEnums(io::Printer* p) const;
private:
enum class GeneratedFileType : int { kHeader, kSource };
void GenerateFile(io::Printer* p, GeneratedFileType file_type,
const std::vector<std::string>& ignored_warnings,
const std::vector<const FileDescriptor*>& extra_files,
std::function<void()> body) const;
void GenerateFile(
io::Printer* p, GeneratedFileType file_type,
const std::vector<std::string>& ignored_warnings,
const std::vector<const FileDescriptor*>& extra_files_to_import,
std::function<void()> body) const;
void GenerateFile(io::Printer* p, GeneratedFileType file_type,
std::function<void()> body) const {
GenerateFile(p, file_type, {}, {}, body);

@ -262,11 +262,10 @@ bool ObjectiveCGenerator::GenerateAll(
// This is an experimental option, and could be removed or change at any
// time; it is not documented in the README.md for that reason.
//
// Enables a mode where each type (message & enum) generates to a unique
// .m file; this is to explore impacts on code size when not
// Enables a mode where each ObjC class (messages and roots) generates to
// a unique .m file; this is to explore impacts on code size when not
// compiling/linking with `-ObjC` as then only linker visible needs should
// be pulled into the builds.
if (!StringToBool(
options[i].second,
&generation_options.experimental_multi_source_generation)) {
@ -346,15 +345,17 @@ bool ObjectiveCGenerator::GenerateAll(
}
}
for (int i = 0; i < file_generator.NumEnums(); ++i) {
// Enums only generate C functions, so they can all go in one file as
// dead stripping anything not used.
if (file_generator.NumEnums() > 0) {
std::unique_ptr<io::ZeroCopyOutputStream> output(
context->Open(NumberedObjCMFileName(filepath, file_number++)));
io::Printer printer(output.get());
file_generator.GenerateSourceForEnum(i, &printer);
file_generator.GenerateSourceForEnums(&printer);
if (printer.failed()) {
*error = absl::StrCat(
"error: internal error generating an enum implementation:",
file->name(), "::", i);
"error: internal error generating an enum implementation(s):",
file->name());
return false;
}
}

Loading…
Cancel
Save