[libcxx-commits] [libcxx] [libc++] Conditionally declare `lgamma_r` as noexcept (PR #156547)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 3 09:04:23 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Yuxuan Chen (yuxuanchen1997)

<details>
<summary>Changes</summary>

An older PR https://github.com/llvm/llvm-project/pull/102036 suggested that LLVM libc declares `lgamma_r` as noexcept and is incompatible with this redeclaration. However, I recently discovered that glibc also declares the math functions to be noexcept under C++ mode. 

This line usually don't cause issues because both the glibc and this file are included as "system headers". According to [this godbolt](https://godbolt.org/z/o7Wd9PP58), both GCC and clang ignore the different exception specification between multiple declarations if they are in system headers. 

However, this seems not the case for NVCC/EDG, so a fix for this redeclaration is still desirable. This patch proposes that we should declare the function as noexcept under known libc integrations to keep the declared function consistent. 

---
Full diff: https://github.com/llvm/llvm-project/pull/156547.diff


1 Files Affected:

- (modified) libcxx/include/__random/binomial_distribution.h (+10-4) 


``````````diff
diff --git a/libcxx/include/__random/binomial_distribution.h b/libcxx/include/__random/binomial_distribution.h
index b4b4340827761..80a910b611adf 100644
--- a/libcxx/include/__random/binomial_distribution.h
+++ b/libcxx/include/__random/binomial_distribution.h
@@ -97,13 +97,19 @@ class binomial_distribution {
   }
 };
 
-// The LLVM C library provides this with conflicting `noexcept` attributes.
-#if !defined(_LIBCPP_MSVCRT_LIKE) && !defined(__LLVM_LIBC__)
-extern "C" double lgamma_r(double, int*);
+// Some libc declares the math functions to be `noexcept`.
+#if _LIBCPP_GLIBC_PREREQ(2, 8) || defined(__LLVM_LIBC__)
+#  define _LIBCPP_LGAMMA_R_NOEXCEPT _NOEXCEPT
+#else
+#  define _LIBCPP_LGAMMA_R_NOEXCEPT
+#endif
+
+#if !defined(_LIBCPP_MSVCRT_LIKE)
+    extern "C" double lgamma_r(double, int*) _LIBCPP_LGAMMA_R_NOEXCEPT;
 #endif
 
 inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
-#if defined(_LIBCPP_MSVCRT_LIKE) || defined(__LLVM_LIBC__)
+#if defined(_LIBCPP_MSVCRT_LIKE)
   return lgamma(__d);
 #else
   int __sign;

``````````

</details>


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


More information about the libcxx-commits mailing list