From 404ec743ed64e0810ad6cc059b24eba9f7bdc858 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Mon, 15 May 2023 11:16:25 -0700 Subject: [PATCH] Migrate call-sites of ExtensionRangeOption fields. These are being migrated to accessor methods of the new descriptor-like class. PiperOrigin-RevId: 532169110 --- .../compiler/command_line_interface.cc | 4 +-- src/google/protobuf/compiler/cpp/message.cc | 29 ++++++++++--------- src/google/protobuf/compiler/cpp/message.h | 3 +- .../compiler/cpp/parse_function_generator.cc | 6 ++-- src/google/protobuf/compiler/java/helpers.h | 2 +- .../compiler/java/message_serialization.cc | 2 +- .../compiler/java/message_serialization.h | 2 +- .../protobuf/compiler/objectivec/message.cc | 8 ++--- .../protobuf/compiler/python/generator.cc | 5 ++-- 9 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 1beca44ff2..f983646dbd 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -2937,8 +2937,8 @@ void GatherOccupiedFieldRanges( ranges->insert(FieldRange(fd->number(), fd->number() + 1)); } for (int i = 0; i < descriptor->extension_range_count(); ++i) { - ranges->insert(FieldRange(descriptor->extension_range(i)->start, - descriptor->extension_range(i)->end)); + ranges->insert(FieldRange(descriptor->extension_range(i)->start_number(), + descriptor->extension_range(i)->end_number())); } for (int i = 0; i < descriptor->reserved_range_count(); ++i) { ranges->insert(FieldRange(descriptor->reserved_range(i)->start, diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 6eb8106c22..d2d9cfcd79 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -156,7 +156,7 @@ std::vector SortFieldsByNumber( struct ExtensionRangeSorter { bool operator()(const Descriptor::ExtensionRange* left, const Descriptor::ExtensionRange* right) const { - return left->start < right->start; + return left->start_number() < right->start_number(); } }; @@ -3672,11 +3672,11 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* p, format("\n"); } -void MessageGenerator::GenerateSerializeOneExtensionRange( - io::Printer* p, const Descriptor::ExtensionRange* range) { +void MessageGenerator::GenerateSerializeOneExtensionRange(io::Printer* p, + int start, int end) { absl::flat_hash_map vars = variables_; - vars["start"] = absl::StrCat(range->start); - vars["end"] = absl::StrCat(range->end); + vars["start"] = absl::StrCat(start); + vars["end"] = absl::StrCat(end); Formatter format(p, vars); format("// Extension range [$start$, $end$)\n"); format( @@ -3832,19 +3832,18 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) { void AddToRange(const Descriptor::ExtensionRange* range) { if (!has_current_range_) { - current_combined_range_ = *range; + min_start_ = range->start_number(); + max_end_ = range->end_number(); has_current_range_ = true; } else { - current_combined_range_.start = - std::min(current_combined_range_.start, range->start); - current_combined_range_.end = - std::max(current_combined_range_.end, range->end); + min_start_ = std::min(min_start_, range->start_number()); + max_end_ = std::max(max_end_, range->end_number()); } } void Flush() { if (has_current_range_) { - mg_->GenerateSerializeOneExtensionRange(p_, ¤t_combined_range_); + mg_->GenerateSerializeOneExtensionRange(p_, min_start_, max_end_); } has_current_range_ = false; } @@ -3853,7 +3852,8 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) { MessageGenerator* mg_; io::Printer* p_; bool has_current_range_ = false; - Descriptor::ExtensionRange current_combined_range_; + int min_start_ = 0; + int max_end_ = 0; }; // We need to track the largest weak field, because weak fields are serialized @@ -3909,7 +3909,8 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) { i < ordered_fields.size() || j < sorted_extensions.size();) { if ((j == sorted_extensions.size()) || (i < descriptor_->field_count() && - ordered_fields[i]->number() < sorted_extensions[j]->start)) { + ordered_fields[i]->number() < + sorted_extensions[j]->start_number())) { const FieldDescriptor* field = ordered_fields[i++]; re.Flush(); if (field->options().weak()) { @@ -4001,7 +4002,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBodyShuffled( format("case $1$: {\n", index++); format.Indent(); - GenerateSerializeOneExtensionRange(p, r); + GenerateSerializeOneExtensionRange(p, r->start_number(), r->end_number()); format("break;\n"); format.Outdent(); diff --git a/src/google/protobuf/compiler/cpp/message.h b/src/google/protobuf/compiler/cpp/message.h index 8e6703c9cc..7aead7e980 100644 --- a/src/google/protobuf/compiler/cpp/message.h +++ b/src/google/protobuf/compiler/cpp/message.h @@ -147,8 +147,7 @@ class MessageGenerator { // Or, if fields.size() == 1, just call GenerateSerializeOneField(). void GenerateSerializeOneofFields( io::Printer* p, const std::vector& fields); - void GenerateSerializeOneExtensionRange( - io::Printer* p, const Descriptor::ExtensionRange* range); + void GenerateSerializeOneExtensionRange(io::Printer* p, int start, int end); // Generates has_foo() functions and variables for singular field has-bits. void GenerateSingularFieldHasBits(const FieldDescriptor* field, diff --git a/src/google/protobuf/compiler/cpp/parse_function_generator.cc b/src/google/protobuf/compiler/cpp/parse_function_generator.cc index b28e3b6b82..eda08fce8e 100644 --- a/src/google/protobuf/compiler/cpp/parse_function_generator.cc +++ b/src/google/protobuf/compiler/cpp/parse_function_generator.cc @@ -1281,11 +1281,11 @@ void ParseFunctionGenerator::GenerateParseIterationBody( if (i > 0) format(" ||\n "); uint32_t start_tag = WireFormatLite::MakeTag( - range->start, static_cast(0)); + range->start_number(), static_cast(0)); uint32_t end_tag = WireFormatLite::MakeTag( - range->end, static_cast(0)); + range->end_number(), static_cast(0)); - if (range->end > FieldDescriptor::kMaxNumber) { + if (range->end_number() > FieldDescriptor::kMaxNumber) { format("($1$u <= tag)", start_tag); } else { format("($1$u <= tag && tag < $2$u)", start_tag, end_tag); diff --git a/src/google/protobuf/compiler/java/helpers.h b/src/google/protobuf/compiler/java/helpers.h index c0e503c035..5b2f8fd210 100644 --- a/src/google/protobuf/compiler/java/helpers.h +++ b/src/google/protobuf/compiler/java/helpers.h @@ -333,7 +333,7 @@ struct FieldOrderingByNumber { struct ExtensionRangeOrdering { bool operator()(const Descriptor::ExtensionRange* a, const Descriptor::ExtensionRange* b) const { - return a->start < b->start; + return a->start_number() < b->start_number(); } }; diff --git a/src/google/protobuf/compiler/java/message_serialization.cc b/src/google/protobuf/compiler/java/message_serialization.cc index fb258b8dc5..a344d7d65b 100644 --- a/src/google/protobuf/compiler/java/message_serialization.cc +++ b/src/google/protobuf/compiler/java/message_serialization.cc @@ -42,7 +42,7 @@ namespace java { void GenerateSerializeExtensionRange(io::Printer* printer, const Descriptor::ExtensionRange* range) { printer->Print("extensionWriter.writeUntil($end$, output);\n", "end", - absl::StrCat(range->end)); + absl::StrCat(range->end_number())); } } // namespace java diff --git a/src/google/protobuf/compiler/java/message_serialization.h b/src/google/protobuf/compiler/java/message_serialization.h index 3b7c8461cb..738ecd6adc 100644 --- a/src/google/protobuf/compiler/java/message_serialization.h +++ b/src/google/protobuf/compiler/java/message_serialization.h @@ -78,7 +78,7 @@ void GenerateSerializeFieldsAndExtensions( // number of extension ranges without fields in between them. const Descriptor::ExtensionRange* range = nullptr; while (range_idx < sorted_extensions.size() && - sorted_extensions[range_idx]->end <= field->number()) { + sorted_extensions[range_idx]->end_number() <= field->number()) { range = sorted_extensions[range_idx++]; } diff --git a/src/google/protobuf/compiler/objectivec/message.cc b/src/google/protobuf/compiler/objectivec/message.cc index 5e25b8e02f..4b0ef3e821 100644 --- a/src/google/protobuf/compiler/objectivec/message.cc +++ b/src/google/protobuf/compiler/objectivec/message.cc @@ -150,7 +150,7 @@ struct FieldOrderingByStorageSize { struct ExtensionRangeOrdering { bool operator()(const Descriptor::ExtensionRange* a, const Descriptor::ExtensionRange* b) const { - return a->start < b->start; + return a->start_number() < b->start_number(); } }; @@ -177,10 +177,10 @@ struct SimpleExtensionRange { std::vector result; result.reserve(sorted_extensions.size()); for (const auto ext : sorted_extensions) { - if (!result.empty() && result.back().end == ext->start) { - result.back().end = ext->end; + if (!result.empty() && result.back().end == ext->start_number()) { + result.back().end = ext->end_number(); } else { - result.emplace_back(ext->start, ext->end); + result.emplace_back(ext->start_number(), ext->end_number()); } } return result; diff --git a/src/google/protobuf/compiler/python/generator.cc b/src/google/protobuf/compiler/python/generator.cc index fc63c34d6b..eb8c381ab6 100644 --- a/src/google/protobuf/compiler/python/generator.cc +++ b/src/google/protobuf/compiler/python/generator.cc @@ -702,8 +702,9 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { for (int i = 0; i < message_descriptor.extension_range_count(); ++i) { const Descriptor::ExtensionRange* range = message_descriptor.extension_range(i); - printer_->Print("($start$, $end$), ", "start", absl::StrCat(range->start), - "end", absl::StrCat(range->end)); + printer_->Print("($start$, $end$), ", "start", + absl::StrCat(range->start_number()), "end", + absl::StrCat(range->end_number())); } printer_->Print("],\n"); printer_->Print("oneofs=[\n");