[libcxx-commits] [libcxx] [libcxx] Use alias for detecting overriden function (PR #114961)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 6 05:46:04 PST 2024


================
@@ -54,7 +54,7 @@ _LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) noexcept {
 #  ifdef _LIBCPP_HAS_NO_EXCEPTIONS
 #    if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
   _LIBCPP_ASSERT_SHIM(
-      !std::__is_function_overridden(static_cast<void* (*)(std::size_t)>(&operator new)),
+      !std::__is_function_overridden<std::__overload_of<void* (std::size_t)>(&operator new)>(),
----------------
ldionne wrote:

I was able to remove `__overload_of` this way:

1. You need to use `static_cast<Return (*)(Args...)>(name)` instead of `static_cast<Return(Args...)>(name)`. You're casting to a function-pointer type, not to a function type.
2. You need to make `__is_function_overridden` non-`constexpr`, otherwise Clang will like this, which makes a lot of sense since this comparison should always be done at runtime:
```
[...]/libcxxabi/src/stdlib_new_delete.cpp:66:1: error: constexpr function never produces a constant expression [-Winvalid-constexpr]
   66 | _LIBCPP_OVERRIDABLE_FUNCTION(_Znwm, void*, operator new, (std::size_t size)) _THROW_BAD_ALLOC {
      | ^
[...]/libcxxabi/../libcxx/src/include/overridable_function.h:76:20: note: expanded from macro '_LIBCPP_OVERRIDABLE_FUNCTION'
   76 |     constexpr bool __is_function_overridden<static_cast<type(*) arglist>(name)>() {                                              \
      |                    ^
[...]/libcxxabi/src/stdlib_new_delete.cpp:66:1: note: comparison against address of weak declaration '&operator new' can only be performed at runtime
[...]/libcxxabi/../libcxx/src/include/overridable_function.h:77:49: note: expanded from macro '_LIBCPP_OVERRIDABLE_FUNCTION'
   77 |       return static_cast<type(*) arglist>(name) != symbol##_impl__;                                                    \
      |                                                 ^
```


Overall, the changes for Mach-O look like this:

```diff
diff --git a/libcxx/src/include/overridable_function.h b/libcxx/src/include/overridable_function.h
index 0ce9f9c1cee5..b392a099ab36 100644
--- a/libcxx/src/include/overridable_function.h
+++ b/libcxx/src/include/overridable_function.h
@@ -60,13 +60,8 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _Func>
-_LIBCPP_HIDE_FROM_ABI constexpr _Func* __overload_of(_Func* f) {
-  return f;
-}
-
 template <auto _Func>
-_LIBCPP_HIDE_FROM_ABI constexpr bool __is_function_overridden();
+_LIBCPP_HIDE_FROM_ABI bool __is_function_overridden();
 
 _LIBCPP_END_NAMESPACE_STD
 
@@ -78,8 +73,8 @@ _LIBCPP_END_NAMESPACE_STD
     extern __typeof(symbol##_impl__) name __attribute__((weak_import));                                                \
     _LIBCPP_BEGIN_NAMESPACE_STD                                                                                        \
     template <>                                                                                                        \
-    constexpr bool __is_function_overridden<__overload_of<type arglist>(name)>() {                                     \
-      return __overload_of<type arglist>(name) != symbol##_impl__;                                                     \
+    bool __is_function_overridden<static_cast<type(*) arglist>(name)>() {                                              \
+      return static_cast<type(*) arglist>(name) != symbol##_impl__;                                                    \
     }                                                                                                                  \
     _LIBCPP_END_NAMESPACE_STD                                                                                          \
     type symbol##_impl__ arglist
```

And you can also replace mentions of `__overload_of` in the `.cpp` files by appropriate `static_cast`s.

https://github.com/llvm/llvm-project/pull/114961


More information about the libcxx-commits mailing list