-
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
Open
yuxuanchen1997
wants to merge
14
commits into
main
Choose a base branch
from
users/yuxuanchen1997/libcxx-is-clock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
74f494a
[libcxx] Implement C++20 std::chrono::is_clock, std::chrono::is_clock_v
yuxuanchen1997 5fcfd58
Apply suggestions in libcxx/include/__chrono/is_clock.h
yuxuanchen1997 5c09a5d
address comments
yuxuanchen1997 7f1ad78
only test steady_clock if a macro is set
yuxuanchen1997 11329c1
format in test
yuxuanchen1997 bd7ca12
more strict checks
yuxuanchen1997 12a38fb
apply suggestions to test file
yuxuanchen1997 3af9f2d
simplify time_point checks
yuxuanchen1997 1871808
Merge branch 'main' into users/yuxuanchen1997/libcxx-is-clock
yuxuanchen1997 5340360
include <ratio>
yuxuanchen1997 4afb48a
fix AIX CI
yuxuanchen1997 4a01d77
fix monotonic clock check
yuxuanchen1997 89223a8
still check for specialization
yuxuanchen1997 6f7a8a6
fix if _LIBCPP_HAS_MONOTONIC_CLOCK
yuxuanchen1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// -*- 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> | ||
#include <__type_traits/integral_constant.h> | ||
#include <__type_traits/is_arithmetic.h> | ||
#include <__type_traits/is_same.h> | ||
#include <ratio> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
#if _LIBCPP_STD_VER >= 20 | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
namespace chrono { | ||
|
||
template <class _Rep, class _Period> | ||
class duration; | ||
|
||
template <class _Clock, class _Duration> | ||
class time_point; | ||
|
||
// Helper to check that _Tp::time_point has the form time_point<_, typename _Tp::duration>. | ||
template <class _TimePoint, class _ClockType> | ||
constexpr bool __is_valid_clock_time_point_v = false; | ||
|
||
template <class _TimePointClock, class _ClockType> | ||
constexpr bool __is_valid_clock_time_point_v<time_point<_TimePointClock, typename _ClockType::duration>, _ClockType> = | ||
true; | ||
|
||
// Check if a clock satisfies the Cpp17Clock requirements as defined in [time.clock.req] | ||
template <class _Tp> | ||
_LIBCPP_NO_SPECIALIZATIONS inline constexpr bool is_clock_v = requires { | ||
typename _Tp::rep; | ||
requires is_arithmetic_v<typename _Tp::rep>; | ||
|
||
typename _Tp::period; | ||
requires __is_ratio_v<typename _Tp::period>; | ||
|
||
typename _Tp::duration; | ||
requires _IsSame<typename _Tp::duration, duration<typename _Tp::rep, typename _Tp::period>>::value; | ||
|
||
typename _Tp::time_point; | ||
requires __is_valid_clock_time_point_v<typename _Tp::time_point, _Tp>; | ||
|
||
_Tp::is_steady; | ||
requires _IsSame<decltype(_Tp::is_steady), const bool>::value; | ||
|
||
_Tp::now(); | ||
requires _IsSame<decltype(_Tp::now()), typename _Tp::time_point>::value; | ||
}; | ||
|
||
template <class _Tp> | ||
struct _LIBCPP_NO_SPECIALIZATIONS is_clock : bool_constant<is_clock_v<_Tp>> {}; | ||
|
||
} // namespace chrono | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP_STD_VER | ||
#endif // _LIBCPP___CHRONO_IS_CLOCK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
//===----------------------------------------------------------------------===// | ||
yuxuanchen1997 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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> | ||
#include <ratio> | ||
|
||
#include "test_macros.h" | ||
|
||
struct EmptyStruct {}; | ||
|
||
// Test structs missing required members | ||
struct MissingRep { | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<MissingRep>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
struct MissingPeriod { | ||
using rep = long; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<MissingPeriod>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
struct MissingDuration { | ||
using rep = long; | ||
using time_point = long; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
struct MissingTimePoint { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
static constexpr bool is_steady = false; | ||
static std::chrono::time_point<MissingTimePoint> now(); | ||
}; | ||
|
||
struct MissingIsSteady { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<MissingIsSteady>; | ||
static time_point now(); | ||
}; | ||
|
||
struct MissingNow { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<MissingNow>; | ||
static constexpr bool is_steady = false; | ||
}; | ||
|
||
// Valid clock types | ||
struct ValidSteadyClock { | ||
using rep = long long; | ||
using period = std::nano; | ||
using duration = std::chrono::nanoseconds; | ||
using time_point = std::chrono::time_point<ValidSteadyClock>; | ||
static constexpr bool is_steady = true; | ||
static time_point now(); | ||
}; | ||
|
||
struct ValidSystemClock { | ||
using rep = long long; | ||
using period = std::micro; | ||
using duration = std::chrono::microseconds; | ||
using time_point = std::chrono::time_point<ValidSystemClock>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
// Test clocks with invalid is_steady type | ||
struct WrongIsSteadyType { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<WrongIsSteadyType>; | ||
static bool is_steady; // Not const bool | ||
static time_point now(); | ||
}; | ||
|
||
struct WrongIsSteadyNonBool { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<WrongIsSteadyNonBool>; | ||
static constexpr int is_steady = 1; // Not bool | ||
static time_point now(); | ||
}; | ||
|
||
// Test clocks with invalid now() return type | ||
struct WrongNowReturnType { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<WrongNowReturnType>; | ||
static constexpr bool is_steady = false; | ||
static int now(); // Wrong return type | ||
}; | ||
|
||
// Test clocks with invalid period type | ||
struct WrongPeriodType { | ||
using rep = long; | ||
using period = int; // Not a ratio | ||
using duration = std::chrono::seconds; | ||
using time_point = std::chrono::time_point<WrongPeriodType>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
// Test clocks with invalid rep type (neither arithmetic nor numeric_limits specialized) | ||
struct InvalidRepType { | ||
using rep = EmptyStruct; // Not arithmetic, no numeric_limits specialization | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::duration<EmptyStruct>; | ||
using time_point = std::chrono::time_point<InvalidRepType, duration>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
// Test clocks with wrong duration type | ||
struct WrongDurationType { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::milliseconds; // Should be duration<long, ratio<1>> | ||
using time_point = std::chrono::time_point<WrongDurationType>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
// Test clocks with wrong time_point type | ||
struct WrongTimePointType { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::duration<long, std::ratio<1>>; | ||
using time_point = int; // Not a time_point | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
struct WrongTimePointClock { | ||
using rep = long; | ||
using period = std::ratio<1>; | ||
using duration = std::chrono::duration<long, std::ratio<1>>; | ||
using time_point = std::chrono::time_point<ValidSystemClock>; // Wrong clock type | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
// Valid clock with time_point that has matching duration instead of matching clock | ||
struct ValidClockWithDurationMatch { | ||
using rep = int; | ||
using period = std::milli; | ||
using duration = std::chrono::duration<int, std::milli>; | ||
using time_point = std::chrono::time_point<ValidSystemClock, duration>; // Valid: matches duration | ||
static constexpr bool is_steady = false; | ||
static time_point now(); | ||
}; | ||
|
||
// Test both is_clock and is_clock_v | ||
static_assert(std::chrono::is_clock<std::chrono::system_clock>::value); | ||
static_assert(std::chrono::is_clock_v<std::chrono::system_clock>); | ||
|
||
// Test standard clock types | ||
static_assert(std::chrono::is_clock_v<std::chrono::system_clock>); | ||
#if _LIBCPP_HAS_MONOTONIC_CLOCK | ||
static_assert(std::chrono::is_clock_v<std::chrono::steady_clock>); | ||
#endif | ||
static_assert(std::chrono::is_clock_v<std::chrono::high_resolution_clock>); | ||
|
||
// Test non-clock types | ||
static_assert(!std::chrono::is_clock_v<EmptyStruct>); | ||
static_assert(!std::chrono::is_clock_v<int>); | ||
static_assert(!std::chrono::is_clock_v<void>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock::time_point>); | ||
#if _LIBCPP_HAS_MONOTONIC_CLOCK | ||
static_assert(!std::chrono::is_clock_v<std::chrono::steady_clock::time_point>); | ||
#endif | ||
static_assert(!std::chrono::is_clock_v<std::chrono::seconds>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::milliseconds>); | ||
|
||
// Test structs missing required members | ||
static_assert(!std::chrono::is_clock_v<MissingRep>); | ||
static_assert(!std::chrono::is_clock_v<MissingPeriod>); | ||
static_assert(!std::chrono::is_clock_v<MissingDuration>); | ||
static_assert(!std::chrono::is_clock_v<MissingTimePoint>); | ||
static_assert(!std::chrono::is_clock_v<MissingIsSteady>); | ||
static_assert(!std::chrono::is_clock_v<MissingNow>); | ||
|
||
// Test valid custom clocks | ||
static_assert(std::chrono::is_clock_v<ValidSteadyClock>); | ||
static_assert(std::chrono::is_clock_v<ValidSystemClock>); | ||
static_assert(std::chrono::is_clock_v<ValidClockWithDurationMatch>); | ||
|
||
// cv-qualified and reference types | ||
static_assert(std::chrono::is_clock_v<const std::chrono::system_clock>); | ||
static_assert(std::chrono::is_clock_v<volatile std::chrono::system_clock>); | ||
static_assert(std::chrono::is_clock_v<const volatile std::chrono::system_clock>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock&>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock&&>); | ||
static_assert(!std::chrono::is_clock_v<const std::chrono::system_clock&>); | ||
|
||
// array and pointer types | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock[]>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock[10]>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock*>); | ||
static_assert(!std::chrono::is_clock_v<std::chrono::system_clock* const>); | ||
|
||
// The Standard defined a minimum set of checks and allowed implementation to perform stricter checks. The following | ||
// static asserts are implementation specific and a conforming standard library implementation doesn't have to produce | ||
// the same outcome. | ||
|
||
// Test clocks with invalid is_steady type | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongIsSteadyType>); // is_steady not const bool | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongIsSteadyNonBool>); // is_steady not bool type | ||
|
||
// Test clocks with invalid now() return type | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongNowReturnType>); // now() doesn't return time_point | ||
|
||
// Test clocks with invalid period type | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongPeriodType>); // period is not a ratio | ||
|
||
// Test clocks with invalid rep type | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<InvalidRepType>); // rep is not arithmetic and no numeric_limits | ||
|
||
// Test clocks with wrong duration type | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongDurationType>); // duration doesn't match duration<rep, period> | ||
|
||
// Test clocks with wrong time_point type | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongTimePointType>); // time_point is not a time_point | ||
LIBCPP_STATIC_ASSERT(!std::chrono::is_clock_v<WrongTimePointClock>); // time_point has wrong clock and wrong duration |
28 changes: 28 additions & 0 deletions
28
libcxx/test/std/time/time.traits.is.clock/trait.is.clock.compile.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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> | ||
#include <ratio> | ||
|
||
#if !__has_warning("-Winvalid-specializations") | ||
// expected-no-diagnostics | ||
#else | ||
|
||
namespace std::chrono { | ||
// try adding specializations to is_clock | ||
template <> | ||
struct is_clock<int> : std::false_type {}; // expected-error@*:* {{'is_clock' cannot be specialized}} | ||
|
||
template <> | ||
constexpr bool is_clock_v<float> = false; // expected-error@*:* {{'is_clock_v' cannot be specialized}} | ||
|
||
} // namespace std::chrono | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.