[libcxx-commits] [libcxx] [libc++][cmath] Adding `[[nodicard]]` (PR #171763)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 11 22:10:11 PST 2025
================
@@ -6,162 +6,477 @@
//
//===----------------------------------------------------------------------===//
-// UNSUPPORTED: c++03
-
// We don't control the implementation of the math.h functions on windows
// UNSUPPORTED: windows
+// Check that functions are marked `[[nodiscard]]`.
// Check that functions from `<cmath>` that Clang marks with the `[[gnu::const]]` attribute are declared
// `[[nodiscard]]`.
-// clang-format off
-
#include <cmath>
+
#include "test_macros.h"
void test() {
- // These tests rely on Clang's behaviour of adding `[[gnu::const]]` to the double overload of most of the functions
- // below.
- // Without that attribute being added implicitly, this test can't be checked consistently because its result depends
- // on whether we're getting libc++'s own `std::foo(double)` or the underlying C library's `foo(double)`.
-#ifdef TEST_COMPILER_CLANG
- std::ceil(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::fabs(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::floor(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::cbrt(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::copysign(0., 0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::fmax(0., 0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::fmin(0., 0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::nearbyint(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::rint(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::round(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
- std::trunc(0.); // expected-warning-re {{ignoring return value of function declared with {{.*}} attribute}}
+ // Some tests rely on Clang's behaviour of adding `[[gnu::const]]` to the double overload of most of the functions
+ // below. Without that attribute being added implicitly, this test can't be checked consistently because its result
+ // depends on whether we're getting libc++'s own `std::foo(double)` or the underlying C library's `foo(double)`, e.g.
+ // std::fabs(double).
+
+ // Functions
+
+ std::fabs(0.F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::fabs(0.); // expected-warning-re 0-1 {{ignoring return value of function declared with {{.*}} attribute}}
+ std::fabs(0.L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::fabs(0U); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ // Floating point manipulation functions
+
+ std::copysign(0.F, 0.F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::copysign(0.L, 0.L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::copysign(0U, 0U); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ // Error functions
+
+ std::erf(0.F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::erf(0.); // expected-warning-re 0-1 {{ignoring return value of function declared with {{.*}} attribute}}
+ std::erf(0.L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::erf(0U); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ std::erfc(0.F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::erfc(0.); // expected-warning-re 0-1 {{ignoring return value of function declared with {{.*}} attribute}}
+ std::erfc(0.L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::erfc(0U); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ // Exponential functions
+
+ std::exp(0.F); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::exp(0.); // expected-warning-re 0-1 {{ignoring return value of function declared with {{.*}} attribute}}
+ std::exp(0.L); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::exp(0U); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ // Floating point manipulation functions
+
+ std::frexp(0.F, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::frexp(0., 0); /// expected-warning-re 0-1 {{ignoring return value of function declared with {{.*}} attribute}}
----------------
frederick-vs-ja wrote:
> The test should be pre-C++11 compatible.
libc++ (although non-conformingly) provides `nullptr` as a macro in C++03 mode, and that macro expands to `__nullptr` which is an extension equivalent to C++11 `nullptr`. As a result, we can use `nullptr` in all modes supported by libc++.
https://github.com/llvm/llvm-project/pull/171763
More information about the libcxx-commits
mailing list