xref: /llvm-project/clang/test/SemaCXX/warn-zero-nullptr-cxx20.cpp (revision 849b650cf3b60e60f5e3a6457fe52e9c63e76e4c)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -Wzero-as-null-pointer-constant -std=c++20
2 
3 namespace std {
4 class strong_ordering;
5 
6 // Mock how STD defined unspecified parameters for the operators below.
7 struct _CmpUnspecifiedParam {
8   consteval
_CmpUnspecifiedParamstd::_CmpUnspecifiedParam9   _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
10 };
11 
12 struct strong_ordering {
13   signed char value;
14 
operator ==(strong_ordering v,_CmpUnspecifiedParam)15   friend constexpr bool operator==(strong_ordering v,
16                                    _CmpUnspecifiedParam) noexcept {
17     return v.value == 0;
18   }
operator <(strong_ordering v,_CmpUnspecifiedParam)19   friend constexpr bool operator<(strong_ordering v,
20                                   _CmpUnspecifiedParam) noexcept {
21     return v.value < 0;
22   }
operator >(strong_ordering v,_CmpUnspecifiedParam)23   friend constexpr bool operator>(strong_ordering v,
24                                   _CmpUnspecifiedParam) noexcept {
25     return v.value > 0;
26   }
operator >=(strong_ordering v,_CmpUnspecifiedParam)27   friend constexpr bool operator>=(strong_ordering v,
28                                    _CmpUnspecifiedParam) noexcept {
29     return v.value >= 0;
30   }
31   static const strong_ordering equal, greater, less;
32 };
33 constexpr strong_ordering strong_ordering::equal = {0};
34 constexpr strong_ordering strong_ordering::greater = {1};
35 constexpr strong_ordering strong_ordering::less = {-1};
36 } // namespace std
37 
38 struct A {
39   int a;
40   constexpr auto operator<=>(const A &other) const = default;
41 };
42 
test_cxx_rewritten_binary_ops()43 void test_cxx_rewritten_binary_ops() {
44   A a1, a2;
45   bool result;
46   result = (a1 < a2);
47   result = (a1 >= a2);
48   int *ptr = 0; // expected-warning{{zero as null pointer constant}}
49   result = (a1 > (ptr == 0 ? a1 : a2)); // expected-warning{{zero as null pointer constant}}
50   result = (a1 > ((a1 > (ptr == 0 ? a1 : a2)) ? a1 : a2)); // expected-warning{{zero as null pointer constant}}
51 }
52