Only generate the immediate forward declarations when generating one file per

message.
Generating declarartions for all nested types is wasteful, and incorrect when
some nested type happens to refer to the parent type. The latter would create
two declrations for the same symbol in the .pb.cc file.

PiperOrigin-RevId: 640142695
pull/17007/head
Protobuf Team Bot 9 months ago committed by Copybara-Service
parent 85d6d513d8
commit 93d517e199
  1. 8
      src/google/protobuf/compiler/cpp/file.cc
  2. 10
      src/google/protobuf/compiler/cpp/helpers.h

@ -799,10 +799,10 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* p) {
}
CrossFileReferences refs;
ForEachField(message_generators_[idx]->descriptor(),
[this, &refs](const FieldDescriptor* field) {
GetCrossFileReferencesForField(field, &refs);
});
ForEachField<false>(message_generators_[idx]->descriptor(),
[this, &refs](const FieldDescriptor* field) {
GetCrossFileReferencesForField(field, &refs);
});
GenerateInternalForwardDeclarations(refs, p);

@ -708,10 +708,12 @@ void ListAllFields(const Descriptor* d,
void ListAllFields(const FileDescriptor* d,
std::vector<const FieldDescriptor*>* fields);
template <class T>
template <bool do_nested_types, class T>
void ForEachField(const Descriptor* d, T&& func) {
for (int i = 0; i < d->nested_type_count(); i++) {
ForEachField(d->nested_type(i), std::forward<T&&>(func));
if (do_nested_types) {
for (int i = 0; i < d->nested_type_count(); i++) {
ForEachField<true>(d->nested_type(i), std::forward<T&&>(func));
}
}
for (int i = 0; i < d->extension_count(); i++) {
func(d->extension(i));
@ -724,7 +726,7 @@ void ForEachField(const Descriptor* d, T&& func) {
template <class T>
void ForEachField(const FileDescriptor* d, T&& func) {
for (int i = 0; i < d->message_type_count(); i++) {
ForEachField(d->message_type(i), std::forward<T&&>(func));
ForEachField<true>(d->message_type(i), std::forward<T&&>(func));
}
for (int i = 0; i < d->extension_count(); i++) {
func(d->extension(i));

Loading…
Cancel
Save