Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions editions/generated_files_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ TEST(Generated, EditionDefaults2023InternalFeatures) {
legacy_closed_enum: false
string_type: STRING
enum_name_uses_string_view: false
repeated_type: LEGACY
}
)pb"));
}
Expand Down
36 changes: 36 additions & 0 deletions src/google/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ cc_library(
"parse_context.h",
"raw_ptr.h",
"repeated_field.h",
"repeated_field_proxy.h",
"repeated_ptr_field.h",
"runtime_version.h",
"serial_arena.h",
Expand Down Expand Up @@ -954,6 +955,41 @@ cc_test(
],
)

proto_library(
name = "repeated_field_proxy_test_proto",
srcs = ["repeated_field_proxy_test.proto"],
strip_import_prefix = "/src",
deps = [
":cpp_features_proto",
":unittest_proto",
],
)

cc_proto_library(
name = "repeated_field_proxy_test_cc_proto",
deps = [":repeated_field_proxy_test_proto"],
)

cc_test(
name = "repeated_field_proxy_test",
size = "small",
srcs = [
"repeated_field_proxy_test.cc",
],
deps = [
":arena",
":protobuf",
":protobuf_lite",
":repeated_field_proxy_test_cc_proto",
":test_textproto",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/strings:cord",
"@abseil-cpp//absl/strings:string_view",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)

# This provides just the header files for use in projects that need to build
# shared libraries for dynamic loading. This target is available until Bazel
# adds native support for such use cases.
Expand Down
12 changes: 12 additions & 0 deletions src/google/protobuf/compiler/cpp/field_generators/cord_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ void SetCordVariables(
: absl::StrCat(
QualifiedClassName(descriptor->containing_type(), options),
"::", MakeDefaultFieldName(descriptor));
if (descriptor->is_repeated()) {
switch (descriptor->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
(*variables)["repeated_type"] = absl::StrCat(
"::", ProtobufNamespace(options), "::RepeatedField<::absl::Cord>");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
(*variables)["repeated_type"] =
absl::StrCat("::", ProtobufNamespace(options),
"::RepeatedFieldProxy<::absl::Cord>");
}
}
}

class CordFieldGenerator : public FieldGeneratorBase {
Expand Down
91 changes: 68 additions & 23 deletions src/google/protobuf/compiler/cpp/field_generators/enum_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,28 @@ void RepeatedEnum::GenerateAccessorDeclarations(io::Printer* p) const {
[[nodiscard]] $DEPRECATED$ $Enum$ $name$(int index) const;
$DEPRECATED$ void $set_name$(int index, $Enum$ value);
$DEPRECATED$ void $add_name$($Enum$ value);
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedField<int>& $name$() const;
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedField<int>* $nonnull$ $mutable_name$();
)cc");
switch (field_->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
p->Emit(R"cc(
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedField<int>& $name$()
const;
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedField<int>* $nonnull$ $mutable_name$();

private:
private:
)cc");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
p->Emit(R"cc(
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedFieldProxy<const int> $name$()
const;
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedFieldProxy<int> $mutable_name$();

private:
)cc");
break;
}
p->Emit(R"cc(
const $pb$::RepeatedField<int>& $_internal_name$() const;
$pb$::RepeatedField<int>* $nonnull$ $_internal_mutable_name$();

Expand Down Expand Up @@ -437,26 +455,53 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_add:$pkg.Msg.field$)
}
)cc");
p->Emit(R"cc(
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name_internal$();
}
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedField<int>* $nonnull$ $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$set_hasbit$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name_internal$();
}
)cc");
switch (field_->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
p->Emit(R"cc(
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name_internal$();
}
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedField<int>* $nonnull$ $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$set_hasbit$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name_internal$();
}
)cc");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
p->Emit(R"cc(
inline $pb$::RepeatedFieldProxy<const int> $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return BuildRepeatedProxy<int>(_internal_$name_internal$());
}
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedFieldProxy<int> $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$set_hasbit$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return BuildRepeatedProxy<int>(*_internal_mutable_$name_internal$());
}
)cc");
break;
}

if (should_split()) {
p->Emit(R"cc(
inline const $pb$::RepeatedField<int>& $Msg$::_internal_$name_internal$()
Expand Down
116 changes: 89 additions & 27 deletions src/google/protobuf/compiler/cpp/field_generators/message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,27 @@ void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const {

p->Emit(R"cc(
[[nodiscard]] $DEPRECATED$ $Submsg$* $nonnull$ $mutable_name$(int index);
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedPtrField<$Submsg$>* $nonnull$
$mutable_name$();
)cc");
switch (field_->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
p->Emit(R"cc(
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedPtrField<$Submsg$>* $nonnull$
$mutable_name$();

private:
private:
)cc");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
p->Emit(R"cc(
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedFieldProxy<$Submsg$>
$mutable_name$();

private:
)cc");
break;
}

p->Emit(R"cc(
const $pb$::RepeatedPtrField<$Submsg$>& $_internal_name$() const;
$pb$::RepeatedPtrField<$Submsg$>* $nonnull$ $_internal_mutable_name$();
)cc");
Expand All @@ -778,9 +795,20 @@ void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const {
public:
[[nodiscard]] $DEPRECATED$ const $Submsg$& $name$(int index) const;
$DEPRECATED$ $Submsg$* $nonnull$ $add_name$();
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedPtrField<$Submsg$>& $name$()
const;
)cc");
switch (field_->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
p->Emit(R"cc(
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedPtrField<$Submsg$>&
$name$() const;
)cc");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
p->Emit(R"cc(
$DEPRECATED$ $pb$::RepeatedFieldProxy<const $Submsg$> $name$() const;
)cc");
break;
}
}

void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
Expand All @@ -799,19 +827,37 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return _internal_mutable_$name_internal$()->Mutable(index);
}
)cc");
switch (field_->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
p->Emit(R"cc(
inline $pb$::RepeatedPtrField<$Submsg$>* $nonnull$
$Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$set_hasbit$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$StrongRef$;
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name_internal$();
}
)cc");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
p->Emit(R"cc(
inline $pb$::RepeatedFieldProxy<$Submsg$> $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$set_hasbit$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$StrongRef$;
$TsanDetectConcurrentMutation$;
return BuildRepeatedProxy<$Submsg$>(*_internal_mutable_$name_internal$());
}
)cc");
break;
}

p->Emit(R"cc(
inline $pb$::RepeatedPtrField<$Submsg$>* $nonnull$ $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$set_hasbit$;
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$StrongRef$;
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name_internal$();
}
)cc");
p->Emit(R"cc(
inline const $Submsg$& $Msg$::$name$(int index) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
Expand All @@ -836,16 +882,32 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return _add;
}
)cc");
p->Emit(R"cc(
inline const $pb$::RepeatedPtrField<$Submsg$>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
$StrongRef$;
return _internal_$name_internal$();
}
)cc");
switch (field_->cpp_repeated_type()) {
case FieldDescriptor::CppRepeatedType::kRepeated:
p->Emit(R"cc(
inline const $pb$::RepeatedPtrField<$Submsg$>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
$StrongRef$;
return _internal_$name_internal$();
}
)cc");
break;
case FieldDescriptor::CppRepeatedType::kProxy:
p->Emit(R"cc(
inline $pb$::RepeatedFieldProxy<const $Submsg$> $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
$StrongRef$;
return BuildRepeatedProxy<$Submsg$>(_internal_$name_internal$());
}
)cc");
break;
}

if (should_split()) {
p->Emit(R"cc(
Expand Down
Loading
Loading