[libcxx-commits] [PATCH] D158327: [libc++] Add lifetimebound attributes to clamp

Nikolas Klauser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 30 22:40:21 PDT 2023


philnik updated this revision to Diff 554900.
philnik added a comment.
Herald added a subscriber: arichardson.

Update ignore_format.txt


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158327/new/

https://reviews.llvm.org/D158327

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


Index: libcxx/utils/data/ignore_format.txt
===================================================================
--- libcxx/utils/data/ignore_format.txt
+++ 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
Index: libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
===================================================================
--- libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
+++ libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp
@@ -44,6 +44,16 @@
     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}}
Index: libcxx/include/__algorithm/clamp.h
===================================================================
--- libcxx/include/__algorithm/clamp.h
+++ 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_INLINE_VISIBILITY 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_INLINE_VISIBILITY 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
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158327.554900.patch
Type: text/x-patch
Size: 3820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230831/4a157866/attachment.bin>


More information about the libcxx-commits mailing list