-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libcxx] Implement C++20 std::chrono::is_clock, std::chrono::is_clock_v #160607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-libcxx Author: Yuxuan Chen (yuxuanchen1997) ChangesImplemented [time.traits.is.clock] from P0355. Full diff: https://github.com/llvm/llvm-project/pull/160607.diff 5 Files Affected:
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index e050362abb658..210ea8be4a69d 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -261,6 +261,7 @@ set(files
__chrono/formatter.h
__chrono/gps_clock.h
__chrono/hh_mm_ss.h
+ __chrono/is_clock.h
__chrono/high_resolution_clock.h
__chrono/leap_second.h
__chrono/literals.h
diff --git a/libcxx/include/__chrono/is_clock.h b/libcxx/include/__chrono/is_clock.h
new file mode 100644
index 0000000000000..dc64c9eacd80b
--- /dev/null
+++ b/libcxx/include/__chrono/is_clock.h
@@ -0,0 +1,45 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___CHRONO_IS_CLOCK_H
+#define _LIBCPP___CHRONO_IS_CLOCK_H
+
+#include <__config>
+
+#if _LIBCPP_STD_VER >= 20
+
+# include <__type_traits/integral_constant.h>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace chrono {
+
+template <class>
+struct is_clock : std::false_type {};
+
+template <class _Tp>
+ requires requires {
+ typename _Tp::rep;
+ typename _Tp::period;
+ typename _Tp::duration;
+ typename _Tp::time_point;
+ _Tp::is_steady;
+ _Tp::now();
+ }
+struct is_clock<_Tp> : std::true_type {};
+
+template <class _Tp>
+_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_clock_v = is_clock<_Tp>::value;
+
+} // namespace chrono
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER
+#endif // _LIBCPP___CHRONO_IS_CLOCK_H
diff --git a/libcxx/include/chrono b/libcxx/include/chrono
index 82e99a31bcc9f..f3bb08ef386c9 100644
--- a/libcxx/include/chrono
+++ b/libcxx/include/chrono
@@ -1057,6 +1057,7 @@ constexpr chrono::year operator ""y(unsigned lo
# include <__chrono/day.h>
# include <__chrono/exception.h>
# include <__chrono/hh_mm_ss.h>
+# include <__chrono/is_clock.h>
# include <__chrono/literals.h>
# include <__chrono/local_info.h>
# include <__chrono/month.h>
diff --git a/libcxx/modules/std/chrono.inc b/libcxx/modules/std/chrono.inc
index 66eccd8d290ad..db405d482bf9e 100644
--- a/libcxx/modules/std/chrono.inc
+++ b/libcxx/modules/std/chrono.inc
@@ -25,8 +25,8 @@ export namespace std {
using std::chrono::duration_values;
- // using std::chrono::is_clock;
- // using std::chrono::is_clock_v;
+ using std::chrono::is_clock;
+ using std::chrono::is_clock_v;
// [time.duration.nonmember], duration arithmetic
using std::chrono::operator+;
diff --git a/libcxx/test/std/time/time.traits.is.clock/trait.is.clock.pass.cpp b/libcxx/test/std/time/time.traits.is.clock/trait.is.clock.pass.cpp
new file mode 100644
index 0000000000000..b15e91c48b0be
--- /dev/null
+++ b/libcxx/test/std/time/time.traits.is.clock/trait.is.clock.pass.cpp
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++20
+
+#include <chrono>
+
+struct NotAClock {};
+
+int main(int, char**) {
+ static_assert(!std::chrono::is_clock_v<NotAClock>, "should not be treated as a clock");
+
+ static_assert(std::chrono::is_clock_v<std::chrono::system_clock>);
+ static_assert(std::chrono::is_clock_v<std::chrono::steady_clock>);
+ static_assert(std::chrono::is_clock_v<std::chrono::high_resolution_clock>);
+ return 0;
+}
|
d25ec00 to
ec64186
Compare
977db81 to
74f494a
Compare
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.pass.cpp
Show resolved
Hide resolved
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.pass.cpp
Outdated
Show resolved
Hide resolved
Would it make sense to create a sub-task to track this item in #99982? |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
|
After some feedback, we decided to implement more checks based on the
The set of requirements for |
8a6adc0 to
bd7ca12
Compare
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.pass.cpp
Outdated
Show resolved
Hide resolved
This comment was marked as resolved.
This comment was marked as resolved.
frederick-vs-ja
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. CC @philnik777
@H-G-Hristov I've just created the corresponding subtask. @yuxuanchen1997 I modified the PR description to associate this PR with the corresponding subtask. Please double-check. |
libcxx/include/__chrono/is_clock.h
Outdated
| template <class _Tp> | ||
| _LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_clock_v = requires { | ||
| typename _Tp::rep; | ||
| requires is_arithmetic_v<typename _Tp::rep>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems wrong. Cpp17Clock requires the type to be "an arithmetic type or a class emulating an arithmetic type". I don't think "a class emulating an arithmetic type" is defined anywhere, but IMO we should rather be permissive here than strict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify what a permissive outcome might be? Do we want to drop this requires? Or do we want a different check based on https://eel.is/c++draft/time.clock.req#:~:text=a%20class%20emulating%20an%20arithmetic%20type ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess at this moment we can use the following for simplicity. After all, we should submit an LWG issue for clarifying "class emulating an arithmetic type".
| requires is_arithmetic_v<typename _Tp::rep>; | |
| requires is_arithmetic_v<typename _Tp::rep> || is_class_v<typename _Tp::rep> || is_union_v<typename _Tp::rep>; |
(Note that union types are also class types in the C++ core language.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was informed by LWG that the issue has been long standing. I was advised, I quote, to "just write the code as though it's an arithmetic type, and if some program-defined class doesn't compile, then that class doesn't emulate an arithmetic type sufficiently closely."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by "informed by LWG"? Is there some issue about this?
libcxx/include/__chrono/is_clock.h
Outdated
| template <class _Rep, class _Period> | ||
| class duration; | ||
|
|
||
| template <class _Clock, class _Duration> | ||
| class time_point; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't forward declare classes in random places. Either include the appropriate header or make a forward declaring header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should introduce a forward declaring header here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want them to be __fwd/duration.h or __fwd/__chrono/duration.h?
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/time/time.traits.is.clock/trait.is.clock.compile.verify.cpp
Show resolved
Hide resolved
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.verify.cpp
Show resolved
Hide resolved
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.verify.cpp
Outdated
Show resolved
Hide resolved
cf3cd07 to
8349c64
Compare
900f3de to
9f1068c
Compare
|
@philnik777 @frederick-vs-ja I believe I have addressed all your comments. I am going to land this now. If anything doesn't work feel free to comment back. |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/186/builds/13770 Here is the relevant piece of the build log for the reference |
libcxx/test/libcxx/time/time.traits.is.clock/trait.is.clock.compile.verify.cpp
Show resolved
Hide resolved
|
@yuxuanchen1997 Please wait for approval next time, especially when changes have explicitly been requested. There is no need to rush patches in. |
Sorry. There was an earlier acceptance and it was my oversight by treating your request to change as an earlier request. |
…_v (llvm#160607) Implemented [[*time.traits.is.clock*]](https://eel.is/c++draft/time.traits.is.clock) from [P0355R7](https://wg21.link/p0355r7). This patch implements the C++20 feature `is_clock` and `is_clock_v` based on the documentation [on cppreference](https://en.cppreference.com/w/cpp/chrono/is_clock.html) Fixes llvm#166049.
Implemented [time.traits.is.clock] from P0355R7.
This patch implements the C++20 feature
is_clockandis_clock_vbased on the documentation on cppreferenceFixes #166049.