From eacaa2bacc6330f3ebf7c10ad25a20304e062e42 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 5 Sep 2024 16:37:55 -0700 Subject: [PATCH] IWYU & other minor NFC changes in repeated_*_field.* PiperOrigin-RevId: 671554520 --- src/google/protobuf/repeated_field.h | 7 ++-- .../protobuf/repeated_field_unittest.cc | 2 +- src/google/protobuf/repeated_ptr_field.cc | 2 +- src/google/protobuf/repeated_ptr_field.h | 39 ++++++++++--------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 4c4ae30808..f885f49d59 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -36,7 +35,6 @@ #include "absl/base/dynamic_annotations.h" #include "absl/base/optimization.h" #include "absl/log/absl_check.h" -#include "absl/log/absl_log.h" #include "absl/meta/type_traits.h" #include "absl/strings/cord.h" #include "google/protobuf/arena.h" @@ -46,7 +44,6 @@ #include "google/protobuf/port.h" #include "google/protobuf/repeated_ptr_field.h" - // Must be included last. #include "google/protobuf/port_def.inc" @@ -79,7 +76,11 @@ constexpr int RepeatedFieldLowerClampLimit() { // overflows when multiplied by 2 (which is undefined behavior). Sizes above // this will clamp to the maximum int value instead of following exponential // growth when growing a repeated field. +#if defined(__cpp_inline_variables) +inline constexpr int kRepeatedFieldUpperClampLimit = +#else constexpr int kRepeatedFieldUpperClampLimit = +#endif (std::numeric_limits::max() / 2) + 1; template diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index 551b135811..328221b003 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -34,7 +35,6 @@ #include #include "absl/log/absl_check.h" #include "absl/numeric/bits.h" -#include "absl/random/random.h" #include "absl/strings/cord.h" #include "absl/strings/str_cat.h" #include "absl/types/span.h" diff --git a/src/google/protobuf/repeated_ptr_field.cc b/src/google/protobuf/repeated_ptr_field.cc index 19992e454e..5a899210c0 100644 --- a/src/google/protobuf/repeated_ptr_field.cc +++ b/src/google/protobuf/repeated_ptr_field.cc @@ -16,9 +16,9 @@ #include #include #include +#include #include -#include "absl/base/prefetch.h" #include "absl/log/absl_check.h" #include "google/protobuf/arena.h" #include "google/protobuf/message_lite.h" diff --git a/src/google/protobuf/repeated_ptr_field.h b/src/google/protobuf/repeated_ptr_field.h index 12bfdd77a4..b3f92c9995 100644 --- a/src/google/protobuf/repeated_ptr_field.h +++ b/src/google/protobuf/repeated_ptr_field.h @@ -26,8 +26,8 @@ #include #include #include +#include #include -#include #include #include @@ -40,7 +40,6 @@ #include "google/protobuf/message_lite.h" #include "google/protobuf/port.h" - // Must be included last. #include "google/protobuf/port_def.inc" @@ -92,7 +91,7 @@ struct IsMovable // Do not use this struct - it exists for internal use only. template struct ArenaOffsetHelper { - constexpr static size_t value = offsetof(T, arena_); + static constexpr size_t value = offsetof(T, arena_); }; // This is the common base class for RepeatedPtrFields. It deals only in void* @@ -102,11 +101,16 @@ struct ArenaOffsetHelper { // but may have a template argument called TypeHandler. Its signature is: // class TypeHandler { // public: -// typedef MyType Type; -// static Type* New(); -// static Type* NewFromPrototype(const Type* prototype, -// Arena* arena); -// static void Delete(Type*); +// using Type = MyType; +// using Movable = ...; +// +// static Type*(*)(Arena*) GetNewFunc(); +// static void GetArena(Type* value); +// +// static Type* New(Arena* arena); +// static Type* New(Arena* arena, Type&& value); +// static Type* NewFromPrototype(const Type* prototype, Arena* arena); +// static void Delete(Type*, Arena* arena); // static void Clear(Type*); // static void Merge(const Type& from, Type* to); // @@ -145,7 +149,7 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { ~RepeatedPtrFieldBase() { #ifndef NDEBUG - // Try to trigger segfault / asan failure in non-opt builds. If arena_ + // Try to trigger segfault / asan failure in non-opt builds if arena_ // lifetime has ended before the destructor. if (arena_) (void)arena_->SpaceAllocated(); #endif @@ -318,9 +322,9 @@ class PROTOBUF_EXPORT RepeatedPtrFieldBase { template void CopyFrom(const RepeatedPtrFieldBase& other) { if (&other == this) return; - RepeatedPtrFieldBase::Clear(); + Clear(); if (other.empty()) return; - RepeatedPtrFieldBase::MergeFrom(other); + MergeFrom(other); } void CloseGap(int start, int num); @@ -808,10 +812,13 @@ PROTOBUF_EXPORT void InternalOutOfLineDeleteMessageLite(MessageLite* message); template class GenericTypeHandler { public: - typedef GenericType Type; + using Type = GenericType; using Movable = IsMovable; static constexpr auto GetNewFunc() { return Arena::DefaultConstruct; } + static inline Arena* GetArena(GenericType* value) { + return Arena::InternalGetArena(value); + } static inline GenericType* New(Arena* arena) { return static_cast(Arena::DefaultConstruct(arena)); @@ -837,10 +844,6 @@ class GenericTypeHandler { delete value; #endif } - static inline Arena* GetArena(GenericType* value) { - return Arena::InternalGetArena(value); - } - static inline void Clear(GenericType* value) { value->Clear(); } static void Merge(const GenericType& from, GenericType* to); static inline size_t SpaceUsedLong(const GenericType& value) { @@ -887,10 +890,11 @@ PROTOBUF_EXPORT void* NewStringElement(Arena* arena); template <> class GenericTypeHandler { public: - typedef std::string Type; + using Type = std::string; using Movable = IsMovable; static constexpr auto GetNewFunc() { return NewStringElement; } + static inline Arena* GetArena(std::string*) { return nullptr; } static PROTOBUF_NOINLINE std::string* New(Arena* arena) { return Arena::Create(arena); @@ -902,7 +906,6 @@ class GenericTypeHandler { Arena* arena) { return New(arena); } - static inline Arena* GetArena(std::string*) { return nullptr; } static inline void Delete(std::string* value, Arena* arena) { if (arena == nullptr) { delete value;