[libcxx-commits] [libcxx] [libc++] Apply `[[gnu::pure]]` to `get_new_handler` (PR #220847)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 3 02:27:51 PDT 2026


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/220847

>From c2e8c92d9af2de0a47fe4e0f834d82e507a6994b Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 3 Sep 2026 16:58:17 +0800
Subject: [PATCH 1/3] [libc++] Apply `[[nodiscard]]` to `get_new_handler`

---
 libcxx/include/__new/new_handler.h                       | 2 +-
 libcxx/test/libcxx/language.support/nodiscard.verify.cpp | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
index c4473c13fab21..5c21c8419fc8a 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -20,7 +20,7 @@ _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
 typedef void (*new_handler)();
 _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
-_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
+[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
 
 _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
 _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
diff --git a/libcxx/test/libcxx/language.support/nodiscard.verify.cpp b/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
index 975eb59a7d52d..ebcf9f40cb6b3 100644
--- a/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
@@ -223,6 +223,9 @@ void test() {
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
     ::operator new[](0, std::align_val_t{1}, std::nothrow);
 #endif // _LIBCPP_HAS_ALIGNED_ALLOCATION
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::get_new_handler();
   }
 
 #if TEST_STD_VER >= 17

>From c6b9535a4bc02620b55f4f55808a020e343bcb33 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 3 Sep 2026 17:21:00 +0800
Subject: [PATCH 2/3] Apply `[[gnu::pure]]` to `get_new_handler` instead

---
 libcxx/test/libcxx/language.support/nodiscard.verify.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/libcxx/language.support/nodiscard.verify.cpp b/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
index ebcf9f40cb6b3..0e1cd1aa000ee 100644
--- a/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/language.support/nodiscard.verify.cpp
@@ -224,7 +224,7 @@ void test() {
     ::operator new[](0, std::align_val_t{1}, std::nothrow);
 #endif // _LIBCPP_HAS_ALIGNED_ALLOCATION
 
-    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    // expected-warning at +1 {{ignoring return value of function declared with pure attribute}}
     std::get_new_handler();
   }
 

>From 65269752bf8ea913bb89841fda3fdaac12fafed6 Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Thu, 3 Sep 2026 17:27:36 +0800
Subject: [PATCH 3/3] Apply `[[gnu::pure]]` to `get_new_handler` instead, fix

---
 libcxx/include/__new/new_handler.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
index 5c21c8419fc8a..a58e6924c24f6 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -20,7 +20,7 @@ _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
 typedef void (*new_handler)();
 _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
-[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
+[[__gnu__::__pure__]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
 
 _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
 _LIBCPP_END_UNVERSIONED_NAMESPACE_STD



More information about the libcxx-commits mailing list