Internal Code Change

PiperOrigin-RevId: 516665926
pull/12204/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 44bd03c609
commit 309c50e8f3
  1. 3
      src/google/protobuf/BUILD.bazel
  2. 2
      src/google/protobuf/compiler/java/BUILD.bazel
  3. 6
      src/google/protobuf/compiler/java/helpers.cc
  4. 9
      src/google/protobuf/compiler/java/helpers.h
  5. 3
      src/google/protobuf/compiler/java/message_field.cc
  6. 3
      src/google/protobuf/compiler/java/message_field_lite.cc
  7. 4
      src/google/protobuf/compiler/java/message_lite.cc
  8. 14
      src/google/protobuf/descriptor.cc
  9. 1
      src/google/protobuf/descriptor.h
  10. 6
      src/google/protobuf/descriptor_unittest.cc
  11. 18
      src/google/protobuf/dynamic_message.cc
  12. 23
      src/google/protobuf/generated_message_reflection.cc
  13. 3
      src/google/protobuf/generated_message_reflection.h
  14. 11
      src/google/protobuf/message.h
  15. 13
      src/google/protobuf/proto3_arena_unittest.cc

@ -403,6 +403,7 @@ cc_library(
"descriptor.h",
"descriptor.pb.h",
"descriptor_database.h",
"descriptor_legacy.h",
"dynamic_message.h",
"field_access_listener.h",
"generated_enum_reflection.h",
@ -898,6 +899,7 @@ cc_test(
}),
deps = [
":cc_test_protos",
":descriptor_legacy",
":protobuf",
"//src/google/protobuf/compiler:importer",
"//src/google/protobuf/testing",
@ -1147,6 +1149,7 @@ cc_test(
}),
deps = [
":cc_test_protos",
":descriptor_legacy",
":protobuf",
":test_util",
"//src/google/protobuf/stubs",

@ -35,6 +35,7 @@ cc_library(
include_prefix = "google/protobuf/compiler/java",
visibility = ["//pkg:__pkg__"],
deps = [
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"@com_google_absl//absl/container:flat_hash_set",
@ -112,6 +113,7 @@ cc_library(
deps = [
":names",
":names_internal",
"//src/google/protobuf:descriptor_legacy",
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",

@ -52,6 +52,7 @@
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/strtod.h"
#include "google/protobuf/wire_format.h"
@ -832,6 +833,11 @@ bool HasRequiredFields(const Descriptor* type) {
return HasRequiredFields(type, &already_seen);
}
bool IsRealOneof(const FieldDescriptor* descriptor) {
return descriptor->containing_oneof() &&
!OneofDescriptorLegacy(descriptor->containing_oneof()).is_synthetic();
}
bool HasRepeatedFields(const Descriptor* descriptor) {
for (int i = 0; i < descriptor->field_count(); ++i) {
const FieldDescriptor* field = descriptor->field(i);

@ -43,6 +43,7 @@
#include "google/protobuf/compiler/java/options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/printer.h"
// Must be last.
@ -354,10 +355,7 @@ inline bool HasPackedFields(const Descriptor* descriptor) {
// them has a required field. Return true if a required field is found.
bool HasRequiredFields(const Descriptor* descriptor);
inline bool IsRealOneof(const FieldDescriptor* descriptor) {
return descriptor->containing_oneof() &&
!descriptor->containing_oneof()->is_synthetic();
}
bool IsRealOneof(const FieldDescriptor* descriptor);
inline bool HasHasbit(const FieldDescriptor* descriptor) {
return internal::cpp::HasHasbit(descriptor);
@ -366,7 +364,8 @@ inline bool HasHasbit(const FieldDescriptor* descriptor) {
// Whether generate classes expose public PARSER instances.
inline bool ExposePublicParser(const FileDescriptor* descriptor) {
// TODO(liujisi): Mark the PARSER private in 3.1.x releases.
return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2;
return FileDescriptorLegacy(descriptor).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_PROTO2;
}
// Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet

@ -42,6 +42,7 @@
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"
@ -414,7 +415,7 @@ void ImmutableMessageFieldGenerator::GenerateKotlinDslMembers(
}
void ImmutableMessageFieldGenerator::GenerateKotlinOrNull(io::Printer* printer) const {
if (descriptor_->has_optional_keyword()) {
if (FieldDescriptorLegacy(descriptor_).has_optional_keyword()) {
printer->Print(variables_,
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");

@ -42,6 +42,7 @@
#include "google/protobuf/compiler/java/doc_comment.h"
#include "google/protobuf/compiler/java/helpers.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"
@ -325,7 +326,7 @@ void ImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
}
void ImmutableMessageFieldLiteGenerator::GenerateKotlinOrNull(io::Printer* printer) const {
if (descriptor_->has_optional_keyword()) {
if (FieldDescriptorLegacy(descriptor_).has_optional_keyword()) {
printer->Print(variables_,
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");

@ -54,6 +54,7 @@
#include "google/protobuf/compiler/java/message_builder_lite.h"
#include "google/protobuf/compiler/java/name_resolver.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/printer.h"
#include "google/protobuf/wire_format.h"
@ -507,7 +508,8 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
std::vector<uint16_t> chars;
int flags = 0;
if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO2) {
if (FileDescriptorLegacy(descriptor_->file()).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_PROTO2) {
flags |= 0x1;
}
if (descriptor_->options().message_set_wire_format()) {

@ -71,6 +71,7 @@
#include "google/protobuf/any.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_database.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/generated_message_util.h"
#include "google/protobuf/io/strtod.h"
@ -4871,10 +4872,11 @@ PROTOBUF_NOINLINE static bool ExistingFileMatchesProto(
existing_file->CopyTo(&existing_proto);
// TODO(liujisi): Remove it when CopyTo supports copying syntax params when
// syntax="proto2".
if (existing_file->syntax() == FileDescriptor::SYNTAX_PROTO2 &&
if (FileDescriptorLegacy(existing_file).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_PROTO2 &&
proto.has_syntax()) {
existing_proto.set_syntax(
existing_file->SyntaxName(existing_file->syntax()));
existing_proto.set_syntax(FileDescriptorLegacy::SyntaxName(
FileDescriptorLegacy(existing_file).syntax()));
}
return existing_proto.SerializeAsString() == proto.SerializeAsString();
@ -8435,7 +8437,8 @@ bool HasHasbit(const FieldDescriptor* field) {
// message fields a hasbit only if "optional" is present. If the user is
// explicitly writing "optional", it is likely they are writing it on
// primitive fields also.
return (field->has_optional_keyword() || field->is_required()) &&
return (FieldDescriptorLegacy(field).has_optional_keyword() ||
field->is_required()) &&
!field->options().weak();
}
@ -8449,7 +8452,8 @@ static bool FileUtf8Verification(const FileDescriptor* file) {
// Which level of UTF-8 enforcemant is placed on this file.
Utf8CheckMode GetUtf8CheckMode(const FieldDescriptor* field, bool is_lite) {
if (field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 &&
if (FileDescriptorLegacy(field->file()).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
FieldEnforceUtf8(field)) {
return Utf8CheckMode::kStrict;
} else if (!is_lite && FileUtf8Verification(field->file())) {

@ -1105,6 +1105,7 @@ class PROTOBUF_EXPORT OneofDescriptor : private internal::SymbolBase {
OneofDescriptor() {}
friend class DescriptorBuilder;
friend class Descriptor;
friend class FieldDescriptor;
};
PROTOBUF_INTERNAL_CHECK_CLASS_SIZE(OneofDescriptor, 40);

@ -48,6 +48,7 @@
#include "absl/log/scoped_mock_log.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_custom_options.pb.h"
#include "google/protobuf/stubs/common.h"
@ -540,7 +541,7 @@ TEST_F(FileDescriptorTest, Syntax) {
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
EXPECT_TRUE(file != nullptr);
EXPECT_EQ(FileDescriptor::SYNTAX_PROTO2, file->syntax());
EXPECT_EQ(FileDescriptorLegacy::Syntax::SYNTAX_PROTO2, FileDescriptorLegacy(file).syntax());
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("proto2", other.syntax());
@ -551,7 +552,8 @@ TEST_F(FileDescriptorTest, Syntax) {
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
EXPECT_TRUE(file != nullptr);
EXPECT_EQ(FileDescriptor::SYNTAX_PROTO3, file->syntax());
EXPECT_EQ(FileDescriptorLegacy::Syntax::SYNTAX_PROTO3,
FileDescriptorLegacy(file).syntax());
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("proto3", other.syntax());

@ -69,18 +69,19 @@
#include <memory>
#include <new>
#include "google/protobuf/arenastring.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/extension_set.h"
#include "google/protobuf/generated_message_reflection.h"
#include "google/protobuf/generated_message_util.h"
#include "google/protobuf/unknown_field_set.h"
#include "google/protobuf/arenastring.h"
#include "google/protobuf/extension_set.h"
#include "google/protobuf/map_field.h"
#include "google/protobuf/map_field_inl.h"
#include "google/protobuf/map_type_handler.h"
#include "google/protobuf/reflection_ops.h"
#include "google/protobuf/repeated_field.h"
#include "google/protobuf/unknown_field_set.h"
#include "google/protobuf/wire_format.h"
@ -107,7 +108,7 @@ bool IsMapFieldInApi(const FieldDescriptor* field) { return field->is_map(); }
inline bool InRealOneof(const FieldDescriptor* field) {
return field->containing_oneof() &&
!field->containing_oneof()->is_synthetic();
!OneofDescriptorLegacy(field->containing_oneof()).is_synthetic();
}
// Compute the byte size of the in-memory representation of the field.
@ -369,7 +370,8 @@ void DynamicMessage::SharedCtor(bool lock_factory) {
// Initialize oneof cases.
int oneof_count = 0;
for (int i = 0; i < descriptor->oneof_decl_count(); ++i) {
if (descriptor->oneof_decl(i)->is_synthetic()) continue;
if (OneofDescriptorLegacy(descriptor->oneof_decl(i)).is_synthetic())
continue;
new (MutableOneofCaseRaw(oneof_count++)) uint32_t{0};
}
@ -684,7 +686,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock(
// or not that field is set.
int real_oneof_count = 0;
for (int i = 0; i < type->oneof_decl_count(); i++) {
if (!type->oneof_decl(i)->is_synthetic()) {
if (!OneofDescriptorLegacy(type->oneof_decl(i)).is_synthetic()) {
real_oneof_count++;
}
}
@ -758,7 +760,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock(
// The oneofs.
for (int i = 0; i < type->oneof_decl_count(); i++) {
if (!type->oneof_decl(i)->is_synthetic()) {
if (!OneofDescriptorLegacy(type->oneof_decl(i)).is_synthetic()) {
size = AlignTo(size, kSafeAlignment);
offsets[type->field_count() + i] = size;
size += kMaxOneofUnionSize;
@ -776,7 +778,7 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock(
// Compute the size of default oneof instance and offsets of default
// oneof fields.
for (int i = 0; i < type->oneof_decl_count(); i++) {
if (type->oneof_decl(i)->is_synthetic()) continue;
if (OneofDescriptorLegacy(type->oneof_decl(i)).is_synthetic()) continue;
for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) {
const FieldDescriptor* field = type->oneof_decl(i)->field(j);
// oneof fields are not accessed through offsets, but we still have the

@ -50,6 +50,7 @@
#include "absl/synchronization/mutex.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/extension_set.h"
#include "google/protobuf/generated_message_tctable_gen.h"
#include "google/protobuf/generated_message_tctable_impl.h"
@ -957,7 +958,7 @@ void Reflection::SwapOneofField(Message* lhs, Message* rhs,
const FieldDescriptor* field;
};
ABSL_DCHECK(!oneof_descriptor->is_synthetic());
ABSL_DCHECK(!OneofDescriptorLegacy(oneof_descriptor).is_synthetic());
uint32_t oneof_case_lhs = GetOneofCase(*lhs, oneof_descriptor);
uint32_t oneof_case_rhs = GetOneofCase(*rhs, oneof_descriptor);
@ -1181,7 +1182,7 @@ void Reflection::InternalSwap(Message* lhs, Message* rhs) const {
const int oneof_decl_count = descriptor_->oneof_decl_count();
for (int i = 0; i < oneof_decl_count; i++) {
const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
if (!oneof->is_synthetic()) {
if (!OneofDescriptorLegacy(oneof).is_synthetic()) {
SwapOneofField<true>(lhs, rhs, oneof);
}
}
@ -2399,7 +2400,7 @@ const void* Reflection::GetRawRepeatedField(const Message& message,
const FieldDescriptor* Reflection::GetOneofFieldDescriptor(
const Message& message, const OneofDescriptor* oneof_descriptor) const {
if (oneof_descriptor->is_synthetic()) {
if (OneofDescriptorLegacy(oneof_descriptor).is_synthetic()) {
const FieldDescriptor* field = oneof_descriptor->field(0);
return HasField(message, field) ? field : nullptr;
}
@ -2482,7 +2483,8 @@ const FieldDescriptor* Reflection::FindKnownExtensionByNumber(
}
bool Reflection::SupportsUnknownEnumValues() const {
return descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
return FileDescriptorLegacy(descriptor_->file()).syntax() ==
FileDescriptorLegacy::Syntax::SYNTAX_PROTO3;
}
// ===================================================================
@ -2548,9 +2550,16 @@ uint32_t* Reflection::MutableHasBits(Message* message) const {
return GetPointerAtOffset<uint32_t>(message, schema_.HasBitsOffset());
}
uint32_t Reflection::GetOneofCase(
const Message& message, const OneofDescriptor* oneof_descriptor) const {
ABSL_DCHECK(!OneofDescriptorLegacy(oneof_descriptor).is_synthetic());
return internal::GetConstRefAtOffset<uint32_t>(
message, schema_.GetOneofCaseOffset(oneof_descriptor));
}
uint32_t* Reflection::MutableOneofCase(
Message* message, const OneofDescriptor* oneof_descriptor) const {
ABSL_DCHECK(!oneof_descriptor->is_synthetic());
ABSL_DCHECK(!OneofDescriptorLegacy(oneof_descriptor).is_synthetic());
return GetPointerAtOffset<uint32_t>(
message, schema_.GetOneofCaseOffset(oneof_descriptor));
}
@ -2741,7 +2750,7 @@ void Reflection::SwapBit(Message* message1, Message* message2,
bool Reflection::HasOneof(const Message& message,
const OneofDescriptor* oneof_descriptor) const {
if (oneof_descriptor->is_synthetic()) {
if (OneofDescriptorLegacy(oneof_descriptor).is_synthetic()) {
return HasField(message, oneof_descriptor->field(0));
}
return (GetOneofCase(message, oneof_descriptor) > 0);
@ -2761,7 +2770,7 @@ void Reflection::ClearOneofField(Message* message,
void Reflection::ClearOneof(Message* message,
const OneofDescriptor* oneof_descriptor) const {
if (oneof_descriptor->is_synthetic()) {
if (OneofDescriptorLegacy(oneof_descriptor).is_synthetic()) {
ClearField(message, oneof_descriptor->field(0));
return;
}

@ -133,8 +133,7 @@ struct ReflectionSchema {
uint32_t GetObjectSize() const { return static_cast<uint32_t>(object_size_); }
bool InRealOneof(const FieldDescriptor* field) const {
return field->containing_oneof() &&
!field->containing_oneof()->is_synthetic();
return field->real_containing_oneof();
}
// Offset of a non-oneof field. Getting a field offset is slightly more

@ -1191,8 +1191,8 @@ class PROTOBUF_EXPORT Reflection final {
inline const uint32_t* GetHasBits(const Message& message) const;
inline uint32_t* MutableHasBits(Message* message) const;
inline uint32_t GetOneofCase(const Message& message,
const OneofDescriptor* oneof_descriptor) const;
uint32_t GetOneofCase(const Message& message,
const OneofDescriptor* oneof_descriptor) const;
inline uint32_t* MutableOneofCase(
Message* message, const OneofDescriptor* oneof_descriptor) const;
inline bool HasExtensionSet(const Message& /* message */) const {
@ -1527,13 +1527,6 @@ const Type& Reflection::DefaultRaw(const FieldDescriptor* field) const {
return *reinterpret_cast<const Type*>(schema_.GetFieldDefault(field));
}
uint32_t Reflection::GetOneofCase(
const Message& message, const OneofDescriptor* oneof_descriptor) const {
ABSL_DCHECK(!oneof_descriptor->is_synthetic());
return internal::GetConstRefAtOffset<uint32_t>(
message, schema_.GetOneofCaseOffset(oneof_descriptor));
}
bool Reflection::HasOneofField(const Message& message,
const FieldDescriptor* field) const {
return (GetOneofCase(message, field->containing_oneof()) ==

@ -36,6 +36,7 @@
#include "google/protobuf/text_format.h"
#include <gtest/gtest.h>
#include "absl/strings/match.h"
#include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/test_util.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_proto3_arena.pb.h"
@ -297,11 +298,13 @@ TEST(Proto3OptionalTest, OptionalFieldDescriptor) {
for (int i = 0; i < d->field_count(); i++) {
const FieldDescriptor* f = d->field(i);
if (absl::StartsWith(f->name(), "singular")) {
EXPECT_FALSE(f->has_optional_keyword()) << f->full_name();
EXPECT_FALSE(FieldDescriptorLegacy(f).has_optional_keyword())
<< f->full_name();
EXPECT_FALSE(f->has_presence()) << f->full_name();
EXPECT_FALSE(f->containing_oneof()) << f->full_name();
} else {
EXPECT_TRUE(f->has_optional_keyword()) << f->full_name();
EXPECT_TRUE(FieldDescriptorLegacy(f).has_optional_keyword())
<< f->full_name();
EXPECT_TRUE(f->has_presence()) << f->full_name();
EXPECT_TRUE(f->containing_oneof()) << f->full_name();
}
@ -316,8 +319,8 @@ TEST(Proto3OptionalTest, Extensions) {
"protobuf_unittest.Proto3OptionalExtensions.ext_with_optional");
ABSL_CHECK(no_optional);
ABSL_CHECK(with_optional);
EXPECT_FALSE(no_optional->has_optional_keyword());
EXPECT_TRUE(with_optional->has_optional_keyword());
EXPECT_FALSE(FieldDescriptorLegacy(no_optional).has_optional_keyword());
EXPECT_TRUE(FieldDescriptorLegacy(with_optional).has_optional_keyword());
const Descriptor* d = protobuf_unittest::Proto3OptionalExtensions::descriptor();
EXPECT_TRUE(d->options().HasExtension(
@ -365,7 +368,7 @@ TEST(Proto3OptionalTest, OptionalFieldReflection) {
const google::protobuf::OneofDescriptor* o = d->FindOneofByName("_optional_int32");
ABSL_CHECK(f);
ABSL_CHECK(o);
EXPECT_TRUE(o->is_synthetic());
EXPECT_TRUE(OneofDescriptorLegacy(o).is_synthetic());
EXPECT_FALSE(r->HasField(msg, f));
EXPECT_FALSE(r->HasOneof(msg, o));

Loading…
Cancel
Save