Change `PrintFreeFieldNumbers` to not assume that groups occupy the same field range as their containing messages.

PiperOrigin-RevId: 525876198
pull/12521/head
Luke Sandberg 2 years ago committed by Copybara-Service
parent 699093915d
commit 983dac24c3
  1. 16
      src/google/protobuf/compiler/command_line_interface.cc
  2. 5
      src/google/protobuf/compiler/command_line_interface_unittest.cc

@ -2269,10 +2269,8 @@ Parse PROTO_FILES and generate output based on the options given:
with a non-zero exit code if any warnings
are generated.
--print_free_field_numbers Print the free field numbers of the messages
defined in the given proto files. Groups share
the same field number space with the parent
message. Extension ranges are counted as
occupied fields numbers.
defined in the given proto files. Extension ranges
are counted as occupied fields numbers.
--enable_codegen_trace Enables tracing which parts of protoc are
responsible for what codegen output. Not supported
by all backends or on all platforms.)";
@ -2739,13 +2737,9 @@ typedef std::pair<int, int> FieldRange;
void GatherOccupiedFieldRanges(
const Descriptor* descriptor, absl::btree_set<FieldRange>* ranges,
std::vector<const Descriptor*>* nested_messages) {
absl::flat_hash_set<const Descriptor*> groups;
for (int i = 0; i < descriptor->field_count(); ++i) {
const FieldDescriptor* fd = descriptor->field(i);
ranges->insert(FieldRange(fd->number(), fd->number() + 1));
if (fd->type() == FieldDescriptor::TYPE_GROUP) {
groups.insert(fd->message_type());
}
}
for (int i = 0; i < descriptor->extension_range_count(); ++i) {
ranges->insert(FieldRange(descriptor->extension_range(i)->start,
@ -2759,11 +2753,7 @@ void GatherOccupiedFieldRanges(
// post-order strict.
for (int i = 0; i < descriptor->nested_type_count(); ++i) {
const Descriptor* nested_desc = descriptor->nested_type(i);
if (groups.find(nested_desc) != groups.end()) {
GatherOccupiedFieldRanges(nested_desc, ranges, nested_messages);
} else {
nested_messages->push_back(nested_desc);
}
nested_messages->push_back(nested_desc);
}
}

@ -2688,8 +2688,11 @@ TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) {
"Bar free: 1 3 6-7 9 11-INF\n"
"Baz free: 1 3 6-7 9 14\n"
"Quz.Foo free: 1-INF\n"
"Quz.C free: 1-4 6-INF\n"
"Quz.E.G.Foo free: 1-INF\n"
"Quz free: 1 3 6-7 12-14 16-INF\n");
"Quz.E.G free: 1-INF\n"
"Quz.E free: 1-8 10-14 16-INF\n"
"Quz free: 1 3 5-7 12-INF\n");
#endif
}

Loading…
Cancel
Save