Skip to content

Commit 353d6a9

Browse files
[libc++] Conditionally declare lgamma_r as noexcept (#156547)
An older PR #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.
1 parent 132bacd commit 353d6a9

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

libcxx/include/__random/binomial_distribution.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,23 @@ class binomial_distribution {
9797
}
9898
};
9999

100-
// The LLVM C library provides this with conflicting `noexcept` attributes.
101-
#if !defined(_LIBCPP_MSVCRT_LIKE) && !defined(__LLVM_LIBC__)
102-
extern "C" double lgamma_r(double, int*);
100+
// Some libc declares the math functions to be `noexcept`.
101+
#if defined(_LIBCPP_GLIBC_PREREQ)
102+
# if _LIBCPP_GLIBC_PREREQ(2, 8)
103+
# define _LIBCPP_LGAMMA_R_NOEXCEPT _NOEXCEPT
104+
# endif
105+
#elif defined(__LLVM_LIBC__)
106+
# define _LIBCPP_LGAMMA_R_NOEXCEPT _NOEXCEPT
107+
#else
108+
# define _LIBCPP_LGAMMA_R_NOEXCEPT
109+
#endif
110+
111+
#if !defined(_LIBCPP_MSVCRT_LIKE)
112+
extern "C" double lgamma_r(double, int*) _LIBCPP_LGAMMA_R_NOEXCEPT;
103113
#endif
104114

105115
inline _LIBCPP_HIDE_FROM_ABI double __libcpp_lgamma(double __d) {
106-
#if defined(_LIBCPP_MSVCRT_LIKE) || defined(__LLVM_LIBC__)
116+
#if defined(_LIBCPP_MSVCRT_LIKE)
107117
return lgamma(__d);
108118
#else
109119
int __sign;

0 commit comments

Comments
 (0)