Skip to content

Commit b91b1f6

Browse files
committed
[libc++] Avoid -Wzero-as-null-pointer-constant in operator<=>
Issue #43670 describes a situation where the following comparison will issue a warning when -Wzero-as-null-pointer-constant is enabled: #include <compare> auto b = (1 <=> 2) < 0; This code uses operator<(strong_ordering, Unspecified), which is specified by the Standard to only work with a literal 0. In the library, this is achieved by constructing Unspecified from a pointer, which works but has the downside of triggering the warning. This patch uses an alternative implementation where we require that the operator is used exactly with an int of value 0 (known at compile-time), however that value can technically be an expression like `1 - 1`, which makes us a bit less strict than what's specified in the Standard. Fixes #43670
1 parent 6bb5a0b commit b91b1f6

File tree

3 files changed

+82
-65
lines changed

3 files changed

+82
-65
lines changed

libcxx/include/__compare/ordering.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ class partial_ordering;
3030
class weak_ordering;
3131
class strong_ordering;
3232

33-
template <class _Tp, class... _Args>
34-
inline constexpr bool __one_of_v = (is_same_v<_Tp, _Args> || ...);
35-
3633
struct _CmpUnspecifiedParam {
37-
_LIBCPP_HIDE_FROM_ABI constexpr _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
38-
39-
template <class _Tp, class = enable_if_t<!__one_of_v<_Tp, int, partial_ordering, weak_ordering, strong_ordering>>>
40-
_CmpUnspecifiedParam(_Tp) = delete;
34+
template <class _Tp, class = __enable_if_t<is_same_v<_Tp, int>>>
35+
_LIBCPP_HIDE_FROM_ABI consteval _CmpUnspecifiedParam(_Tp __zero) noexcept {
36+
// If anything other than 0 is provided, the behavior is undefined.
37+
// We catch this case by making this function consteval and making the program ill-formed if
38+
// a value other than 0 is provided. This technically also accepts things that are not
39+
// literal 0s like `1 - 1`. The alternative is to use the fact that a pointer can be
40+
// constructed from literal 0, but this conflicts with `-Wzero-as-null-pointer-constant`.
41+
if (__zero != 0)
42+
__builtin_trap();
43+
}
4144
};
4245

4346
class partial_ordering {
@@ -269,7 +272,8 @@ inline constexpr strong_ordering strong_ordering::greater(_OrdResult::__greater)
269272
/// The types partial_ordering, weak_ordering, and strong_ordering are
270273
/// collectively termed the comparison category types.
271274
template <class _Tp>
272-
concept __comparison_category = __one_of_v<_Tp, partial_ordering, weak_ordering, strong_ordering>;
275+
concept __comparison_category =
276+
is_same_v<_Tp, partial_ordering> || is_same_v<_Tp, weak_ordering> || is_same_v<_Tp, strong_ordering>;
273277

274278
#endif // _LIBCPP_STD_VER >= 20
275279

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// In MSVC mode, there's a slightly different number of errors printed for
12+
// each of these, so it doesn't add up to the exact expected count of 18.
13+
// XFAIL: msvc
14+
15+
// <compare>
16+
17+
// Ensure we reject all cases where an argument other than a literal 0 is used
18+
// for a comparison against a comparison category type.
19+
20+
// Also ensure that we don't warn about providing a null pointer constant when
21+
// comparing an ordering type against literal 0, since one of the common
22+
// implementation strategies is to use a pointer as the "unspecified type".
23+
// ADDITIONAL_COMPILE_FLAGS: -Wzero-as-null-pointer-constant
24+
25+
#include <compare>
26+
27+
#include "test_macros.h"
28+
29+
#define TEST_FAIL(v, op) \
30+
do { \
31+
void(v op 0L); \
32+
void(0L op v); \
33+
void(v op 1); \
34+
void(1 op v); \
35+
void(v op nullptr); \
36+
void(nullptr op v); \
37+
} while (false)
38+
39+
#define TEST_PASS(v, op) \
40+
do { \
41+
void(v op 0); \
42+
void(0 op v); \
43+
LIBCPP_ONLY(void(v op(1 - 1))); \
44+
LIBCPP_ONLY(void((1 - 1) op v)); \
45+
} while (false)
46+
47+
template <typename T>
48+
void test_category(T v) {
49+
TEST_FAIL(v, ==); // expected-error 18 {{}}
50+
TEST_FAIL(v, !=); // expected-error 18 {{}}
51+
TEST_FAIL(v, <); // expected-error 18 {{}}
52+
TEST_FAIL(v, <=); // expected-error 18 {{}}
53+
TEST_FAIL(v, >); // expected-error 18 {{}}
54+
TEST_FAIL(v, >=); // expected-error 18 {{}}
55+
TEST_FAIL(v, <=>); // expected-error 18 {{}}
56+
57+
TEST_PASS(v, ==);
58+
TEST_PASS(v, !=);
59+
TEST_PASS(v, <);
60+
TEST_PASS(v, >);
61+
TEST_PASS(v, <=);
62+
TEST_PASS(v, >=);
63+
TEST_PASS(v, <=>);
64+
}
65+
66+
void f() {
67+
test_category(std::strong_ordering::equivalent);
68+
test_category(std::weak_ordering::equivalent);
69+
test_category(std::partial_ordering::equivalent);
70+
}

libcxx/test/std/language.support/cmp/cmp.categories.pre/zero_type.verify.cpp

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)