Skip to content

Commit

Permalink
Internal change.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696545060
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 14, 2024
1 parent 08868ff commit 5bee60d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 39 deletions.
31 changes: 11 additions & 20 deletions src/google/protobuf/compiler/cpp/field_generators/message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -783,26 +783,17 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
return _internal_mutable_$name_internal$();
}
)cc");
p->Emit(
{
{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"},
{"GetExtraArg",
[&] {
p->Emit(opts_->safe_boundary_check
? ", reinterpret_cast<const $Submsg$&>($kDefault$)"
: "");
}},
},
R"cc(
inline const $Submsg$& $Msg$::$name$(int index) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
$StrongRef$;
return _internal_$name_internal$().$Get$(index$GetExtraArg$);
}
)cc");
p->Emit({GetEmitRepeatedFieldGetterSub(*opts_, p)},
R"cc(
inline const $Submsg$& $Msg$::$name$(int index) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
$StrongRef$;
return $getter$;
}
)cc");
p->Emit(R"cc(
inline $Submsg$* $Msg$::add_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
bool bytes = field_->type() == FieldDescriptor::TYPE_BYTES;
p->Emit(
{
{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"},
{"GetExtraArg",
[&] {
p->Emit(opts_->safe_boundary_check
? ", $pbi$::GetEmptyStringAlreadyInited()"
: "");
}},
GetEmitRepeatedFieldGetterSub(*opts_, p),
{"bytes_tag",
[&] {
if (bytes) {
Expand All @@ -878,7 +872,7 @@ void RepeatedString::GenerateInlineAccessorDefinitions(io::Printer* p) const {
$WeakDescriptorSelfPin$;
$annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$().$Get$(index$GetExtraArg$);
return $getter$;
}
inline std::string* $Msg$::mutable_$name$(int index)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,20 +666,14 @@ void RepeatedStringView::GenerateAccessorDeclarations(io::Printer* p) const {

void RepeatedStringView::GenerateInlineAccessorDefinitions(
io::Printer* p) const {
p->Emit({{"Get", opts_->safe_boundary_check ? "InternalCheckedGet" : "Get"},
{"GetExtraArg",
[&] {
p->Emit(opts_->safe_boundary_check
? ", $pbi$::GetEmptyStringAlreadyInited()"
: "");
}}},
p->Emit({GetEmitRepeatedFieldGetterSub(*opts_, p)},
R"cc(
inline absl::string_view $Msg$::$name$(int index) const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$WeakDescriptorSelfPin$;
$annotate_get$;
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name_internal$().$Get$(index$GetExtraArg$);
return $getter$;
}
inline void $Msg$::set_$name$(int index, const std::string& value) {
$WeakDescriptorSelfPin$;
Expand Down
18 changes: 18 additions & 0 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,24 @@ bool HasOnDeserializeTracker(const Descriptor* descriptor,
// signature.
bool NeedsPostLoopHandler(const Descriptor* descriptor, const Options& options);

// Emit the repeated field getter for the custom options.
// If safe_boundary_check is specified, it calls the internal checked getter.
inline auto GetEmitRepeatedFieldGetterSub(const Options& options,
io::Printer* p) {
return io::Printer::Sub{
"getter",
[&options, p] {
if (options.safe_boundary_check) {
p->Emit(R"cc(
$pbi$::CheckedGetOrDefault(_internal_$name_internal$(), index)
)cc");
} else {
p->Emit(R"cc(_internal_$name_internal$().Get(index))cc");
}
}}
.WithSuffix("");
}

// Priority used for static initializers.
enum InitPriority {
kInitPriority101,
Expand Down
7 changes: 7 additions & 0 deletions src/google/protobuf/repeated_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
namespace google {
namespace protobuf {

namespace internal {

void LogIndexOutOfBounds(int index, int size) {
ABSL_DLOG(FATAL) << "Index " << index << " out of bounds " << size;
}

} // namespace internal

template <>
PROTOBUF_EXPORT_TEMPLATE_DEFINE size_t
Expand Down
26 changes: 23 additions & 3 deletions src/google/protobuf/repeated_ptr_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ namespace internal {
class MergePartialFromCodedStreamHelper;
class SwapFieldHelper;


} // namespace internal

namespace internal {
Expand Down Expand Up @@ -117,6 +116,8 @@ class GenericTypeHandler;
//
// // Only needs to be implemented if SpaceUsedExcludingSelf() is called.
// static int SpaceUsedLong(const Type&);
//
// static const Type& default_instance();
// };
class PROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
Expand Down Expand Up @@ -839,6 +840,11 @@ class GenericTypeHandler {
static inline size_t SpaceUsedLong(const Type& value) {
return value.SpaceUsedLong();
}

static const Type& default_instance() {
return *static_cast<const GenericType*>(
MessageTraits<Type>::default_instance());
}
};

template <>
Expand Down Expand Up @@ -875,6 +881,10 @@ class GenericTypeHandler<std::string> {
static size_t SpaceUsedLong(const Type& value) {
return sizeof(value) + StringSpaceUsedExcludingSelfLong(value);
}

static const Type& default_instance() {
return GetEmptyStringAlreadyInited();
}
};

} // namespace internal
Expand Down Expand Up @@ -1178,7 +1188,6 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {

using RepeatedPtrFieldBase::InternalGetArenaOffset;


private:
using InternalArenaConstructable_ = void;
using DestructorSkippable_ = void;
Expand Down Expand Up @@ -1311,7 +1320,6 @@ inline Element& RepeatedPtrField<Element>::at(int index)
return RepeatedPtrFieldBase::at<TypeHandler>(index);
}


template <typename Element>
inline Element* RepeatedPtrField<Element>::Mutable(int index)
ABSL_ATTRIBUTE_LIFETIME_BOUND {
Expand Down Expand Up @@ -1967,6 +1975,18 @@ class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator {
RepeatedPtrField<T>* field_;
};

// A utility function for logging that doesn't need any template types.
void LogIndexOutOfBounds(int index, int size);

template <typename T>
const T& CheckedGetOrDefault(const RepeatedPtrField<T>& field, int index) {
if (ABSL_PREDICT_FALSE(index < 0 || index >= field.size())) {
LogIndexOutOfBounds(index, field.size());
return GenericTypeHandler<T>::default_instance();
}
return field.Get(index);
}

} // namespace internal

// Provides a back insert iterator for RepeatedPtrField instances,
Expand Down

0 comments on commit 5bee60d

Please sign in to comment.