Skip to content

Commit 831f5fe

Browse files
jckingkyessenov
authored andcommitted
Workaround GCC friendship bugs
PiperOrigin-RevId: 533103982
1 parent 142b3b5 commit 831f5fe

File tree

9 files changed

+36
-20
lines changed

9 files changed

+36
-20
lines changed

base/internal/memory_manager.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ struct HasIsDestructorSkippable : std::false_type {};
3232

3333
template <typename T>
3434
struct HasIsDestructorSkippable<
35-
T,
36-
std::void_t<decltype(T::IsDestructorSkippable(std::declval<const T&>()))>>
35+
T, std::void_t<decltype(std::declval<const T&>().IsDestructorSkippable())>>
3736
: std::true_type {};
3837

3938
} // namespace cel::base_internal

base/memory.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class MemoryManager {
185185
T(std::forward<Args>(args)...);
186186
if constexpr (!std::is_trivially_destructible_v<T>) {
187187
if constexpr (base_internal::HasIsDestructorSkippable<T>::value) {
188-
if (!T::IsDestructorSkippable(*pointer)) {
188+
if (!pointer->IsDestructorSkippable()) {
189189
OwnDestructor(pointer,
190190
&base_internal::MemoryManagerDestructor<T>::Destruct);
191191
}
@@ -357,6 +357,18 @@ class Allocator {
357357
bool allocation_only_;
358358
};
359359

360+
// GCC before 12 has buggy friendship. Instead of calculating friendship at the
361+
// point of evaluation it does so at the point where it is written. This macro
362+
// ensures compatibility by friending both so IsDestructorSkippable works
363+
// correctly.
364+
#define CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() \
365+
private: \
366+
friend class ::cel::MemoryManager; \
367+
template <typename, typename> \
368+
friend struct ::cel::base_internal::HasIsDestructorSkippable; \
369+
\
370+
bool IsDestructorSkippable() const
371+
360372
namespace base_internal {
361373

362374
template <typename T>

base/types/list_type.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/types/span.h"
2525
#include "base/internal/data.h"
2626
#include "base/kind.h"
27+
#include "base/memory.h"
2728
#include "base/type.h"
2829

2930
namespace cel {
@@ -109,8 +110,8 @@ class ModernListType final : public ListType, public HeapData {
109110

110111
// Called by Arena-based memory managers to determine whether we actually need
111112
// our destructor called.
112-
static bool IsDestructorSkippable(const ModernListType& type) {
113-
return Metadata::IsDestructorSkippable(*type.element());
113+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
114+
return Metadata::IsDestructorSkippable(*element());
114115
}
115116

116117
explicit ModernListType(Handle<Type> element);

base/types/map_type.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/types/span.h"
2525
#include "base/internal/data.h"
2626
#include "base/kind.h"
27+
#include "base/memory.h"
2728
#include "base/type.h"
2829

2930
namespace cel {
@@ -115,9 +116,9 @@ class ModernMapType final : public MapType, public HeapData {
115116

116117
// Called by Arena-based memory managers to determine whether we actually need
117118
// our destructor called.
118-
static bool IsDestructorSkippable(const ModernMapType& type) {
119-
return Metadata::IsDestructorSkippable(*type.key()) &&
120-
Metadata::IsDestructorSkippable(*type.value());
119+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
120+
return Metadata::IsDestructorSkippable(*key()) &&
121+
Metadata::IsDestructorSkippable(*value());
121122
}
122123

123124
explicit ModernMapType(Handle<Type> key, Handle<Type> value);

base/types/optional_type.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "absl/log/absl_check.h"
2222
#include "absl/strings/string_view.h"
2323
#include "absl/types/span.h"
24+
#include "base/memory.h"
2425
#include "base/type.h"
2526
#include "base/types/opaque_type.h"
2627
#include "internal/rtti.h"
@@ -57,8 +58,8 @@ class OptionalType final : public OpaqueType {
5758

5859
// Called by Arena-based memory managers to determine whether we actually need
5960
// our destructor called.
60-
static bool IsDestructorSkippable(const OptionalType& type) {
61-
return base_internal::Metadata::IsDestructorSkippable(*type.type());
61+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
62+
return base_internal::Metadata::IsDestructorSkippable(*type());
6263
}
6364

6465
explicit OptionalType(Handle<Type> type) : type_(std::move(type)) {}

base/values/optional_value.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "absl/log/absl_check.h"
2222
#include "absl/status/statusor.h"
2323
#include "base/handle.h"
24+
#include "base/memory.h"
2425
#include "base/type.h"
2526
#include "base/types/optional_type.h"
2627
#include "base/value.h"
@@ -98,8 +99,8 @@ class EmptyOptionalValue final : public OptionalValue {
9899

99100
// Called by Arena-based memory managers to determine whether we actually need
100101
// our destructor called.
101-
static bool IsDestructorSkippable(const EmptyOptionalValue& value) {
102-
return base_internal::Metadata::IsDestructorSkippable(*value.type());
102+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
103+
return base_internal::Metadata::IsDestructorSkippable(*type());
103104
}
104105

105106
explicit EmptyOptionalValue(Handle<OptionalType> type)
@@ -117,9 +118,9 @@ class FullOptionalValue final : public OptionalValue {
117118

118119
// Called by Arena-based memory managers to determine whether we actually need
119120
// our destructor called.
120-
static bool IsDestructorSkippable(const FullOptionalValue& value) {
121-
return base_internal::Metadata::IsDestructorSkippable(*value.type()) &&
122-
base_internal::Metadata::IsDestructorSkippable(*value.value());
121+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
122+
return base_internal::Metadata::IsDestructorSkippable(*type()) &&
123+
base_internal::Metadata::IsDestructorSkippable(*value());
123124
}
124125

125126
FullOptionalValue(Handle<OptionalType> type, Handle<Value> value)

extensions/protobuf/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ cc_library(
9595
"type_provider.h",
9696
],
9797
deps = [
98+
"//base:data",
9899
"//base:handle",
99-
"//base:type",
100+
"//base:memory",
100101
"//internal:status_macros",
101102
"@com_google_absl//absl/base:core_headers",
102103
"@com_google_absl//absl/log:die_if_null",

extensions/protobuf/enum_type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "absl/base/attributes.h"
1919
#include "absl/log/die_if_null.h"
20+
#include "base/memory.h"
2021
#include "base/type.h"
2122
#include "base/type_manager.h"
2223
#include "base/types/enum_type.h"
@@ -71,8 +72,7 @@ class ProtoEnumType final : public EnumType {
7172

7273
// Called by Arena-based memory managers to determine whether we actually need
7374
// our destructor called.
74-
static bool IsDestructorSkippable(
75-
const ProtoEnumType& type ABSL_ATTRIBUTE_UNUSED) {
75+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
7676
// Our destructor is useless, we only hold pointers to protobuf-owned data.
7777
return true;
7878
}

extensions/protobuf/struct_type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "absl/status/statusor.h"
2323
#include "absl/strings/string_view.h"
2424
#include "base/handle.h"
25+
#include "base/memory.h"
2526
#include "base/type.h"
2627
#include "base/type_manager.h"
2728
#include "base/types/struct_type.h"
@@ -84,8 +85,7 @@ class ProtoStructType final : public CEL_STRUCT_TYPE_CLASS {
8485

8586
// Called by Arena-based memory managers to determine whether we actually need
8687
// our destructor called.
87-
static bool IsDestructorSkippable(
88-
const ProtoStructType& type ABSL_ATTRIBUTE_UNUSED) {
88+
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
8989
// Our destructor is useless, we only hold pointers to protobuf-owned data.
9090
return true;
9191
}

0 commit comments

Comments
 (0)