diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index c5975a854c..1ce1040137 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(); } }; diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index acd65a44bf..574420ad58 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -2378,8 +2378,8 @@ const FieldDescriptor* FileDescriptor::FindExtensionByCamelcaseName( void Descriptor::ExtensionRange::CopyTo( DescriptorProto_ExtensionRange* proto) const { - proto->set_start(this->start); - proto->set_end(this->end); + proto->set_start(start_); + proto->set_end(end_); if (options_ != &ExtensionRangeOptions::default_instance()) { *proto->mutable_options() = *options_; } @@ -2390,8 +2390,8 @@ Descriptor::FindExtensionRangeContainingNumber(int number) const { // Linear search should be fine because we don't expect a message to have // more than a couple extension ranges. for (int i = 0; i < extension_range_count(); i++) { - if (number >= extension_range(i)->start && - number < extension_range(i)->end) { + if (number >= extension_range(i)->start_number() && + number < extension_range(i)->end_number()) { return extension_range(i); } } @@ -3172,25 +3172,24 @@ void Descriptor::DebugString(int depth, std::string* contents, for (int i = 0; i < extension_range_count(); i++) { absl::SubstituteAndAppend(contents, "$0 extensions $1", prefix, - extension_range(i)->start); - if (extension_range(i)->end > extension_range(i)->start + 1) { + extension_range(i)->start_number()); + if (extension_range(i)->end_number() > + extension_range(i)->start_number() + 1) { absl::SubstituteAndAppend(contents, " to $0", - extension_range(i)->end - 1); - } - if (extension_range(i)->options_ != nullptr) { - if (extension_range(i)->options_->declaration_size() > 0) { - absl::StrAppend(contents, " ["); - for (int j = 0; j < extension_range(i)->options_->declaration_size(); - ++j) { - if (j > 0) { - absl::StrAppend(contents, ","); - } - absl::SubstituteAndAppend( - contents, " declaration = { $0 }", - extension_range(i)->options_->declaration(j).ShortDebugString()); + extension_range(i)->end_number() - 1); + } + if (extension_range(i)->options().declaration_size() > 0) { + absl::StrAppend(contents, " ["); + for (int j = 0; j < extension_range(i)->options().declaration_size(); + ++j) { + if (j > 0) { + absl::StrAppend(contents, ","); } - absl::StrAppend(contents, " ] "); + absl::SubstituteAndAppend( + contents, " declaration = { $0 }", + extension_range(i)->options().declaration(j).ShortDebugString()); } + absl::StrAppend(contents, " ] "); } absl::StrAppend(contents, ";\n"); } @@ -4699,9 +4698,9 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( placeholder_message->extension_range_count_ = 1; placeholder_message->extension_ranges_ = alloc.AllocateArray(1); - placeholder_message->extension_ranges_[0].start = 1; + placeholder_message->extension_ranges_[0].start_ = 1; // kMaxNumber + 1 because ExtensionRange::end is exclusive. - placeholder_message->extension_ranges_[0].end = + placeholder_message->extension_ranges_[0].end_ = FieldDescriptor::kMaxNumber + 1; placeholder_message->extension_ranges_[0].options_ = nullptr; } @@ -5642,15 +5641,16 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, const FieldDescriptor* field = result->field(i); for (int j = 0; j < result->extension_range_count(); j++) { const Descriptor::ExtensionRange* range = result->extension_range(j); - if (range->start <= field->number() && field->number() < range->end) { + if (range->start_number() <= field->number() && + field->number() < range->end_number()) { message_hints_[result].RequestHintOnFieldNumbers( proto.extension_range(j), DescriptorPool::ErrorCollector::NUMBER); AddError(field->full_name(), proto.extension_range(j), DescriptorPool::ErrorCollector::NUMBER, [&] { return absl::Substitute( "Extension range $0 to $1 includes field \"$2\" ($3).", - range->start, range->end - 1, field->name(), - field->number()); + range->start_number(), range->end_number() - 1, + field->name(), field->number()); }); } } @@ -5682,27 +5682,29 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, const Descriptor::ExtensionRange* range1 = result->extension_range(i); for (int j = 0; j < result->reserved_range_count(); j++) { const Descriptor::ReservedRange* range2 = result->reserved_range(j); - if (range1->end > range2->start && range2->end > range1->start) { + if (range1->end_number() > range2->start && + range2->end > range1->start_number()) { AddError(result->full_name(), proto.extension_range(i), DescriptorPool::ErrorCollector::NUMBER, [&] { return absl::Substitute( "Extension range $0 to $1 overlaps with " "reserved range $2 to $3.", - range1->start, range1->end - 1, range2->start, - range2->end - 1); + range1->start_number(), range1->end_number() - 1, + range2->start, range2->end - 1); }); } } for (int j = i + 1; j < result->extension_range_count(); j++) { const Descriptor::ExtensionRange* range2 = result->extension_range(j); - if (range1->end > range2->start && range2->end > range1->start) { + if (range1->end_number() > range2->start_number() && + range2->end_number() > range1->start_number()) { AddError(result->full_name(), proto.extension_range(i), DescriptorPool::ErrorCollector::NUMBER, [&] { return absl::Substitute( "Extension range $0 to $1 overlaps with " "already-defined range $2 to $3.", - range2->start, range2->end - 1, range1->start, - range1->end - 1); + range2->start_number(), range2->end_number() - 1, + range1->start_number(), range1->end_number() - 1); }); } } @@ -6105,11 +6107,11 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, void DescriptorBuilder::BuildExtensionRange( const DescriptorProto::ExtensionRange& proto, const Descriptor* parent, Descriptor::ExtensionRange* result, internal::FlatAllocator& alloc) { - result->start = proto.start(); - result->end = proto.end(); + result->start_ = proto.start(); + result->end_ = proto.end(); result->containing_type_ = parent; - if (result->start <= 0) { + if (result->start_number() <= 0) { message_hints_[parent].RequestHintOnFieldNumbers( proto, DescriptorPool::ErrorCollector::NUMBER, result->start_number(), result->end_number()); @@ -7064,7 +7066,7 @@ void DescriptorBuilder::SuggestFieldNumbers(FileDescriptor* file, } for (int i = 0; i < message->extension_range_count(); i++) { auto range = message->extension_range(i); - add_range(range->start, range->end); + add_range(range->start_number(), range->end_number()); } used_ordinals.push_back( {FieldDescriptor::kMaxNumber, FieldDescriptor::kMaxNumber + 1}); @@ -7498,7 +7500,7 @@ void DescriptorBuilder::ValidateExtensionRangeOptions( for (int i = 0; i < message.extension_range_count(); i++) { const auto& range = *message.extension_range(i); - if (range.end > max_extension_range + 1) { + if (range.end_number() > max_extension_range + 1) { AddError(message.full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, [&] { return absl::Substitute( diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 13f661b18b..3ae042f482 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -434,10 +434,10 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { void CopyTo(DescriptorProto_ExtensionRange* proto) const; // Returns the start field number of this range (inclusive). - int start_number() const { return start; } + int start_number() const { return start_; } // Returns the end field number of this range (exclusive). - int end_number() const { return end; } + int end_number() const { return end_; } // Returns the index of this extension range within the message's extension // range array. @@ -462,9 +462,12 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // Never nullptr. const Descriptor* containing_type() const { return containing_type_; } - // TODO(b/282012847) Make these private. - int start; // NOLINT(google3-readability-class-member-naming) - int end; // NOLINT(google3-readability-class-member-naming) +#ifdef PROTOBUF_FUTURE_EXTENSION_RANGE_CLASS + + private: +#endif + int start_; + int end_; const ExtensionRangeOptions* options_; private: diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index d5db1d2cc0..f0a9dc69c1 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -2064,11 +2064,11 @@ TEST_F(ExtensionDescriptorTest, ExtensionRanges) { EXPECT_EQ(0, bar_->extension_range_count()); ASSERT_EQ(2, foo_->extension_range_count()); - EXPECT_EQ(10, foo_->extension_range(0)->start); - EXPECT_EQ(30, foo_->extension_range(1)->start); + EXPECT_EQ(10, foo_->extension_range(0)->start_number()); + EXPECT_EQ(30, foo_->extension_range(1)->start_number()); - EXPECT_EQ(20, foo_->extension_range(0)->end); - EXPECT_EQ(40, foo_->extension_range(1)->end); + EXPECT_EQ(20, foo_->extension_range(0)->end_number()); + EXPECT_EQ(40, foo_->extension_range(1)->end_number()); } TEST_F(ExtensionDescriptorTest, Extensions) { @@ -3107,8 +3107,9 @@ TEST_P(AllowUnknownDependenciesTest, UnknownExtendee) { EXPECT_EQ("UnknownType", extendee->name()); EXPECT_TRUE(extendee->is_placeholder()); ASSERT_EQ(1, extendee->extension_range_count()); - EXPECT_EQ(1, extendee->extension_range(0)->start); - EXPECT_EQ(FieldDescriptor::kMaxNumber + 1, extendee->extension_range(0)->end); + EXPECT_EQ(1, extendee->extension_range(0)->start_number()); + EXPECT_EQ(FieldDescriptor::kMaxNumber + 1, + extendee->extension_range(0)->end_number()); } TEST_P(AllowUnknownDependenciesTest, CustomOption) { diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 01cff18e77..a054643295 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -246,6 +246,10 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3), // Owner: mkruskal@ #define PROTOBUF_FUTURE_EDITIONS 1 +// Used to make ExtensionRange into a fully-fledged descriptor class. +// Owner: mkruskal@ +#define PROTOBUF_FUTURE_EXTENSION_RANGE_CLASS 1 + #endif #ifdef PROTOBUF_VERSION