From 972609b6790ef182c908a657bef7ca4a81135ccd Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Wed, 8 Mar 2023 14:41:31 -0800 Subject: [PATCH] Rollback: Migrate syntax reflection to new feature-based system. PiperOrigin-RevId: 515146548 --- .../compiler/cpp/parse_function_generator.cc | 2 +- src/google/protobuf/descriptor.cc | 7 +------ src/google/protobuf/descriptor.h | 5 +++++ .../protobuf/generated_message_reflection.cc | 2 +- src/google/protobuf/wire_format.cc | 17 ++++++++++++----- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/parse_function_generator.cc b/src/google/protobuf/compiler/cpp/parse_function_generator.cc index c9ba0ed1fd..864ea45b5b 100644 --- a/src/google/protobuf/compiler/cpp/parse_function_generator.cc +++ b/src/google/protobuf/compiler/cpp/parse_function_generator.cc @@ -1034,7 +1034,7 @@ void ParseFunctionGenerator::GenerateLengthDelim(Formatter& format, const FieldDescriptor* val = field->message_type()->map_value(); ABSL_CHECK(val); if (val->type() == FieldDescriptor::TYPE_ENUM && - !internal::cpp::HasPreservingUnknownEnumSemantics(val)) { + !internal::cpp::HasPreservingUnknownEnumSemantics(field)) { format( "auto object = " "::$proto_ns$::internal::InitEnumParseWrapper<" diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 329d779280..9a38da5286 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -3521,11 +3521,6 @@ bool FieldDescriptor::is_packed() const { } } -bool FieldDescriptor::requires_utf8_validation() const { - return type() == TYPE_STRING && - file()->syntax() == FileDescriptor::SYNTAX_PROTO3; -} - bool Descriptor::GetSourceLocation(SourceLocation* out_location) const { std::vector path; GetLocationPath(&path); @@ -8420,7 +8415,7 @@ void LazyDescriptor::Once(const ServiceDescriptor* service) { namespace cpp { bool HasPreservingUnknownEnumSemantics(const FieldDescriptor* field) { - return !field->legacy_enum_field_treated_as_closed(); + return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3; } bool HasHasbit(const FieldDescriptor* field) { diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 4a4462aa04..f1d2032be0 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -2457,6 +2457,11 @@ inline bool FieldDescriptor::has_presence() const { file()->syntax() == FileDescriptor::SYNTAX_PROTO2; } +inline bool FieldDescriptor::requires_utf8_validation() const { + return type() == TYPE_STRING && + file()->syntax() == FileDescriptor::SYNTAX_PROTO3; +} + inline bool FieldDescriptor::legacy_enum_field_treated_as_closed() const { return type() == TYPE_ENUM && file()->syntax() == FileDescriptor::SYNTAX_PROTO2; diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index 317eaf7194..6fab21493d 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -1551,7 +1551,7 @@ void CheckInOrder(const FieldDescriptor* field, uint32_t* last) { namespace internal { bool CreateUnknownEnumValues(const FieldDescriptor* field) { bool open_enum = false; - return !field->legacy_enum_field_treated_as_closed() || open_enum; + return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || open_enum; } } // namespace internal using internal::CreateUnknownEnumValues; diff --git a/src/google/protobuf/wire_format.cc b/src/google/protobuf/wire_format.cc index f70f224730..fe62d1c622 100644 --- a/src/google/protobuf/wire_format.cc +++ b/src/google/protobuf/wire_format.cc @@ -411,6 +411,10 @@ bool WireFormat::ParseAndMergeMessageSetField(uint32_t field_number, } } +static bool StrictUtf8Check(const FieldDescriptor* field) { + return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3; +} + bool WireFormat::ParseAndMergeField( uint32_t tag, const FieldDescriptor* field, // May be nullptr for unknown @@ -480,7 +484,8 @@ bool WireFormat::ParseAndMergeField( if (!WireFormatLite::ReadPrimitive( input, &value)) return false; - if (!field->legacy_enum_field_treated_as_closed()) { + if (message->GetDescriptor()->file()->syntax() == + FileDescriptor::SYNTAX_PROTO3) { message_reflection->AddEnumValue(message, field, value); } else { const EnumValueDescriptor* enum_value = @@ -561,7 +566,7 @@ bool WireFormat::ParseAndMergeField( // Handle strings separately so that we can optimize the ctype=CORD case. case FieldDescriptor::TYPE_STRING: { - bool strict_utf8_check = field->requires_utf8_validation(); + bool strict_utf8_check = StrictUtf8Check(field); std::string value; if (!WireFormatLite::ReadString(input, &value)) return false; if (strict_utf8_check) { @@ -863,7 +868,9 @@ const char* WireFormat::_InternalParseAndMergeField( case FieldDescriptor::TYPE_ENUM: { auto rep_enum = reflection->MutableRepeatedFieldInternal(msg, field); - if (!field->legacy_enum_field_treated_as_closed()) { + bool open_enum = false; + if (field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 || + open_enum) { ptr = internal::PackedEnumParser(rep_enum, ptr, ctx); } else { return ctx->ReadPackedVarint( @@ -975,7 +982,7 @@ const char* WireFormat::_InternalParseAndMergeField( // Handle strings separately so that we can optimize the ctype=CORD case. case FieldDescriptor::TYPE_STRING: utf8_check = true; - strict_utf8_check = field->requires_utf8_validation(); + strict_utf8_check = StrictUtf8Check(field); PROTOBUF_FALLTHROUGH_INTENDED; case FieldDescriptor::TYPE_BYTES: { int size = ReadSize(&ptr); @@ -1411,7 +1418,7 @@ uint8_t* WireFormat::InternalSerializeField(const FieldDescriptor* field, // Handle strings separately so that we can get string references // instead of copying. case FieldDescriptor::TYPE_STRING: { - bool strict_utf8_check = field->requires_utf8_validation(); + bool strict_utf8_check = StrictUtf8Check(field); std::string scratch; const std::string& value = field->is_repeated()