Skip to content

Commit 69cca9b

Browse files
authored
Remove fast-path check for non-clang compilers in MessageCreator. (#21612)
* Remove fast-path check for non-clang compilers in MessageCreator. This check can lead to issues if the translation units are compiled with different compilers. PiperOrigin-RevId: 755012244 * Fix incorrect merge
1 parent 21fdb7a commit 69cca9b

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/google/protobuf/message_lite.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ class MessageCreator {
119119
func_(func) {}
120120

121121
// Template for testing.
122-
template <bool test_call = false, typename MessageLite>
122+
template <typename MessageLite>
123123
MessageLite* New(const MessageLite* prototype_for_func,
124124
const MessageLite* prototype_for_copy, Arena* arena) const;
125125

126-
template <bool test_call = false, typename MessageLite>
126+
template <typename MessageLite>
127127
MessageLite* PlacementNew(const MessageLite* prototype_for_func,
128128
const MessageLite* prototype_for_copy, void* mem,
129129
Arena* arena) const;
@@ -1187,19 +1187,15 @@ inline void AssertDownCast(const MessageLite& from, const MessageLite& to) {
11871187
<< "Cannot downcast " << from.GetTypeName() << " to " << to.GetTypeName();
11881188
}
11891189

1190-
template <bool test_call, typename MessageLite>
1190+
template <typename MessageLite>
11911191
PROTOBUF_ALWAYS_INLINE inline MessageLite* MessageCreator::PlacementNew(
11921192
const MessageLite* prototype_for_func,
11931193
const MessageLite* prototype_for_copy, void* mem, Arena* arena) const {
11941194
ABSL_DCHECK_EQ(reinterpret_cast<uintptr_t>(mem) % alignment_, 0u);
11951195
const Tag as_tag = tag();
1196-
// When the feature is not enabled we skip the `as_tag` check since it is
1197-
// unnecessary. Except for testing, where we want to test the copy logic even
1198-
// when we can't use it for real messages.
1199-
constexpr bool kMustBeFunc = !test_call && !internal::EnableCustomNew();
12001196
static_assert(kFunc < 0 && !(kZeroInit < 0) && !(kMemcpy < 0),
12011197
"Only kFunc must be the only negative value");
1202-
if (ABSL_PREDICT_FALSE(kMustBeFunc || as_tag < 0)) {
1198+
if (ABSL_PREDICT_FALSE(static_cast<int8_t>(as_tag) < 0)) {
12031199
PROTOBUF_DEBUG_COUNTER("MessageCreator.Func").Inc();
12041200
return static_cast<MessageLite*>(func_(prototype_for_func, mem, arena));
12051201
}
@@ -1293,15 +1289,15 @@ PROTOBUF_ALWAYS_INLINE inline MessageLite* MessageCreator::PlacementNew(
12931289
return Launder(reinterpret_cast<MessageLite*>(mem));
12941290
}
12951291

1296-
template <bool test_call, typename MessageLite>
1292+
template <typename MessageLite>
12971293
PROTOBUF_ALWAYS_INLINE inline MessageLite* MessageCreator::New(
12981294
const MessageLite* prototype_for_func,
12991295
const MessageLite* prototype_for_copy, Arena* arena) const {
1300-
return PlacementNew<test_call>(prototype_for_func, prototype_for_copy,
1301-
arena != nullptr
1302-
? arena->AllocateAligned(allocation_size_)
1303-
: ::operator new(allocation_size_),
1304-
arena);
1296+
return PlacementNew(prototype_for_func, prototype_for_copy,
1297+
arena != nullptr
1298+
? arena->AllocateAligned(allocation_size_)
1299+
: ::operator new(allocation_size_),
1300+
arena);
13051301
}
13061302

13071303
} // namespace internal

src/google/protobuf/port.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,19 +384,16 @@ constexpr T* Launder(T* p) {
384384
}
385385

386386
#if defined(PROTOBUF_CUSTOM_VTABLE)
387-
constexpr bool EnableCustomNew() { return true; }
388387
template <typename T>
389388
constexpr bool EnableCustomNewFor() {
390389
return true;
391390
}
392391
#elif ABSL_HAVE_BUILTIN(__is_bitwise_cloneable)
393-
constexpr bool EnableCustomNew() { return true; }
394392
template <typename T>
395393
constexpr bool EnableCustomNewFor() {
396394
return __is_bitwise_cloneable(T);
397395
}
398396
#else
399-
constexpr bool EnableCustomNew() { return false; }
400397
template <typename T>
401398
constexpr bool EnableCustomNewFor() {
402399
return false;

0 commit comments

Comments
 (0)