Skip to content

Commit 6d778ff

Browse files
committed
Use __enable_if__ attribute
1 parent 4ca3e7d commit 6d778ff

File tree

3 files changed

+22
-46
lines changed

3 files changed

+22
-46
lines changed

libcxx/include/__compare/ordering.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ class strong_ordering;
3333
struct _CmpUnspecifiedParam {
3434
// If anything other than a literal 0 is provided, the behavior is undefined by the Standard.
3535
//
36-
// We handle this by making this function consteval and making the program ill-formed if
37-
// a value other than 0 is provided. This technically also accepts things that are not
38-
// literal 0s like `1 - 1`. The alternative is to use the fact that a pointer can be
39-
// constructed from literal 0, but this conflicts with `-Wzero-as-null-pointer-constant`.
36+
// The alternative to the `__enable_if__` attribute would be to use the fact that a pointer
37+
// can be constructed from literal 0, but this conflicts with `-Wzero-as-null-pointer-constant`.
4038
template <class _Tp, class = __enable_if_t<is_same_v<_Tp, int> > >
41-
_LIBCPP_HIDE_FROM_ABI consteval _CmpUnspecifiedParam(_Tp __zero) noexcept _LIBCPP_DIAGNOSE_ERROR(
42-
__zero != 0, "Only literal 0 is allowed as the operand of a comparison with one of the ordering types") {}
39+
_LIBCPP_HIDE_FROM_ABI consteval _CmpUnspecifiedParam(_Tp __zero) noexcept
40+
# if __has_attribute(__enable_if__)
41+
__attribute__((__enable_if__(
42+
__zero == 0, "Only literal 0 is allowed as the operand of a comparison with one of the ordering types")))
43+
# endif
44+
{
45+
}
4346
};
4447

4548
class partial_ordering {

libcxx/include/__config

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,12 +1453,6 @@ __sanitizer_verify_double_ended_contiguous_container(const void*, const void*, c
14531453
# define _LIBCPP_DIAGNOSE_WARNING(...)
14541454
# endif
14551455

1456-
# if __has_attribute(__diagnose_if__)
1457-
# define _LIBCPP_DIAGNOSE_ERROR(...) __attribute__((__diagnose_if__(__VA_ARGS__, "error")))
1458-
# else
1459-
# define _LIBCPP_DIAGNOSE_ERROR(...)
1460-
# endif
1461-
14621456
// Use a function like macro to imply that it must be followed by a semicolon
14631457
# if __has_cpp_attribute(fallthrough)
14641458
# define _LIBCPP_FALLTHROUGH() [[fallthrough]]

libcxx/test/std/language.support/cmp/cmp.categories.pre/reject-other-than-literal-zero.verify.cpp

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,19 @@
2626

2727
#include "test_macros.h"
2828

29-
#define TEST_FAIL_INVALID_VALUE(v, op) \
30-
do { \
31-
void(v op 1); \
32-
void(1 op v); \
33-
} while (false)
34-
35-
#define TEST_FAIL_INVALID_TYPE(v, op) \
29+
#define TEST_FAIL(v, op) \
3630
do { \
31+
/* invalid types */ \
3732
void(v op 0L); \
3833
void(0L op v); \
3934
void(v op 0.0); \
4035
void(0.0 op v); \
4136
void(v op nullptr); \
4237
void(nullptr op v); \
43-
} while (false)
44-
45-
#define TEST_FAIL_NOT_COMPILE_TIME(v, op) \
46-
do { \
38+
/* invalid value */ \
39+
void(v op 1); \
40+
void(1 op v); \
41+
/* value not known at compile-time */ \
4742
int i = 0; \
4843
void(v op i); \
4944
void(i op v); \
@@ -59,29 +54,13 @@
5954

6055
template <typename T>
6156
void test_category(T v) {
62-
TEST_FAIL_INVALID_TYPE(v, ==); // expected-error 18 {{invalid operands to binary expression}}
63-
TEST_FAIL_INVALID_TYPE(v, !=); // expected-error 18 {{invalid operands to binary expression}}
64-
TEST_FAIL_INVALID_TYPE(v, <); // expected-error 18 {{invalid operands to binary expression}}
65-
TEST_FAIL_INVALID_TYPE(v, <=); // expected-error 18 {{invalid operands to binary expression}}
66-
TEST_FAIL_INVALID_TYPE(v, >); // expected-error 18 {{invalid operands to binary expression}}
67-
TEST_FAIL_INVALID_TYPE(v, >=); // expected-error 18 {{invalid operands to binary expression}}
68-
TEST_FAIL_INVALID_TYPE(v, <=>); // expected-error 18 {{invalid operands to binary expression}}
69-
70-
TEST_FAIL_INVALID_VALUE(v, ==); // expected-error 6 {{Only literal 0 is allowed as the operand}}
71-
TEST_FAIL_INVALID_VALUE(v, !=); // expected-error 6 {{Only literal 0 is allowed as the operand}}
72-
TEST_FAIL_INVALID_VALUE(v, <); // expected-error 6 {{Only literal 0 is allowed as the operand}}
73-
TEST_FAIL_INVALID_VALUE(v, <=); // expected-error 6 {{Only literal 0 is allowed as the operand}}
74-
TEST_FAIL_INVALID_VALUE(v, >); // expected-error 6 {{Only literal 0 is allowed as the operand}}
75-
TEST_FAIL_INVALID_VALUE(v, >=); // expected-error 6 {{Only literal 0 is allowed as the operand}}
76-
TEST_FAIL_INVALID_VALUE(v, <=>); // expected-error 6 {{Only literal 0 is allowed as the operand}}
77-
78-
TEST_FAIL_NOT_COMPILE_TIME(v, ==); // expected-error 6 {{call to consteval function}}
79-
TEST_FAIL_NOT_COMPILE_TIME(v, !=); // expected-error 6 {{call to consteval function}}
80-
TEST_FAIL_NOT_COMPILE_TIME(v, <); // expected-error 6 {{call to consteval function}}
81-
TEST_FAIL_NOT_COMPILE_TIME(v, <=); // expected-error 6 {{call to consteval function}}
82-
TEST_FAIL_NOT_COMPILE_TIME(v, >); // expected-error 6 {{call to consteval function}}
83-
TEST_FAIL_NOT_COMPILE_TIME(v, >=); // expected-error 6 {{call to consteval function}}
84-
TEST_FAIL_NOT_COMPILE_TIME(v, <=>); // expected-error 6 {{call to consteval function}}
57+
TEST_FAIL(v, ==); // expected-error 30 {{invalid operands to binary expression}}
58+
TEST_FAIL(v, !=); // expected-error 30 {{invalid operands to binary expression}}
59+
TEST_FAIL(v, <); // expected-error 30 {{invalid operands to binary expression}}
60+
TEST_FAIL(v, <=); // expected-error 30 {{invalid operands to binary expression}}
61+
TEST_FAIL(v, >); // expected-error 30 {{invalid operands to binary expression}}
62+
TEST_FAIL(v, >=); // expected-error 30 {{invalid operands to binary expression}}
63+
TEST_FAIL(v, <=>); // expected-error 30 {{invalid operands to binary expression}}
8564

8665
TEST_PASS(v, ==);
8766
TEST_PASS(v, !=);

0 commit comments

Comments
 (0)