[libcxx-commits] [libcxx] [libc++][cmath] Adding `[[nodicard]]` (PR #171763)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Dec 15 21:33:19 PST 2025
================
@@ -6,162 +6,483 @@
//
//===----------------------------------------------------------------------===//
-// 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}}
----------------
H-G-Hristov wrote:
@frederick-vs-ja @philnik777 I need an advice about the `double` overloads. Some functions as `std::fabs` seem to fall into the category as explained in the comment above and produce the respective warning but some don't seem to do that on Clang. I'm not sure how to handle the tests for these overloads (I've added `0-1`). Thanks for any advice!
https://github.com/llvm/llvm-project/pull/171763
More information about the libcxx-commits
mailing list