[libcxx-commits] [libcxx] b6310e6 - [libc++] Add lifetimebound attributes to clamp

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Sep 1 09:04:17 PDT 2023


Author: Nikolas Klauser
Date: 2023-09-01T09:04:11-07:00
New Revision: b6310e65f61c07e90fdbdc7f7158065fb0c94656

URL: https://github.com/llvm/llvm-project/commit/b6310e65f61c07e90fdbdc7f7158065fb0c94656
DIFF: https://github.com/llvm/llvm-project/commit/b6310e65f61c07e90fdbdc7f7158065fb0c94656.diff

LOG: [libc++] Add lifetimebound attributes to clamp

Reviewed By: #libc, ldionne

Spies: ldionne, arichardson, libcxx-commits

Differential Revision: https://reviews.llvm.org/D158327

Added: 
    

Modified: 
    libcxx/include/__algorithm/clamp.h
    libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
    libcxx/utils/data/ignore_format.txt

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__algorithm/clamp.h b/libcxx/include/__algorithm/clamp.h
index 31deb4fd94aa174..fc088279194038b 100644
--- a/libcxx/include/__algorithm/clamp.h
+++ b/libcxx/include/__algorithm/clamp.h
@@ -20,24 +20,22 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 17
-template<class _Tp, class _Compare>
-_LIBCPP_NODISCARD_EXT inline
-_LIBCPP_INLINE_VISIBILITY constexpr
-const _Tp&
-clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
-{
-    _LIBCPP_ASSERT_UNCATEGORIZED(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
-    return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
-
+template <class _Tp, class _Compare>
+_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
+clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
+      _LIBCPP_LIFETIMEBOUND const _Tp& __lo,
+      _LIBCPP_LIFETIMEBOUND const _Tp& __hi,
+      _Compare __comp) {
+  _LIBCPP_ASSERT_UNCATEGORIZED(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
+  return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
 }
 
-template<class _Tp>
-_LIBCPP_NODISCARD_EXT inline
-_LIBCPP_INLINE_VISIBILITY constexpr
-const _Tp&
-clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
-{
-    return _VSTD::clamp(__v, __lo, __hi, __less<>());
+template <class _Tp>
+_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
+clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
+      _LIBCPP_LIFETIMEBOUND const _Tp& __lo,
+      _LIBCPP_LIFETIMEBOUND const _Tp& __hi) {
+  return _VSTD::clamp(__v, __lo, __hi, __less<>());
 }
 #endif
 

diff  --git a/libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp b/libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
index b16ecfbdeb54478..6bb1a4c6bbe3dd3 100644
--- a/libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
+++ b/libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
@@ -44,6 +44,16 @@ void func() {
     auto v7 = std::minmax(0, i, Comp{});   // expected-warning {{temporary whose address is used as value of local variable 'v7' will be destroyed at the end of the full-expression}}
     auto v8 = std::minmax(i, 0, Comp{});   // expected-warning {{temporary whose address is used as value of local variable 'v8' will be destroyed at the end of the full-expression}}
   }
+#if TEST_STD_VER >= 17
+  {
+    auto&& v1 = std::clamp(1, i, i); // expected-warning {{temporary bound to local reference 'v1' will be destroyed at the end of the full-expression}}
+    auto&& v2 = std::clamp(i, 1, i); // expected-warning {{temporary bound to local reference 'v2' will be destroyed at the end of the full-expression}}
+    auto&& v3 = std::clamp(i, i, 1); // expected-warning {{temporary bound to local reference 'v3' will be destroyed at the end of the full-expression}}
+    auto&& v4 = std::clamp(1, i, i, Comp{}); // expected-warning {{temporary bound to local reference 'v4' will be destroyed at the end of the full-expression}}
+    auto&& v5 = std::clamp(i, 1, i, Comp{}); // expected-warning {{temporary bound to local reference 'v5' will be destroyed at the end of the full-expression}}
+    auto&& v6 = std::clamp(i, i, 1, Comp{}); // expected-warning {{temporary bound to local reference 'v6' will be destroyed at the end of the full-expression}}
+  }
+#endif
 #if TEST_STD_VER >= 20
   {
     auto&& v1 = std::ranges::min(0, i);         // expected-warning {{temporary bound to local reference 'v1' will be destroyed at the end of the full-expression}}

diff  --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt
index 20384d1c769e0a0..3caf9d45c783ec4 100644
--- a/libcxx/utils/data/ignore_format.txt
+++ b/libcxx/utils/data/ignore_format.txt
@@ -1,5 +1,4 @@
 libcxx/include/__algorithm/binary_search.h
-libcxx/include/__algorithm/clamp.h
 libcxx/include/__algorithm/comp_ref_type.h
 libcxx/include/__algorithm/copy_backward.h
 libcxx/include/__algorithm/copy_if.h


        


More information about the libcxx-commits mailing list