Skip to content

Commit 849b650

Browse files
committed
[clang] Skip defaulted functions in zero-as-null-pointer-constant.
The zero-as-null-pointer-constant check should not fire if it is inside a defaulted function, e.g. defaulted spaceship operators. Add C++20 tests with spaceship operators. Fixes #50221 Differential Revision: https://reviews.llvm.org/D138727
1 parent e939378 commit 849b650

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

clang/lib/Sema/Sema.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
597597
CodeSynthesisContext::RewritingOperatorAsSpaceship)
598598
return;
599599

600+
// Ignore null pointers in defaulted comparison operators.
601+
FunctionDecl *FD = getCurFunctionDecl();
602+
if (FD && FD->isDefaulted()) {
603+
return;
604+
}
605+
600606
// If it is a macro from system header, and if the macro name is not "NULL",
601607
// do not warn.
602608
SourceLocation MaybeMacroLoc = E->getBeginLoc();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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
9+
_CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
10+
};
11+
12+
struct strong_ordering {
13+
signed char value;
14+
15+
friend constexpr bool operator==(strong_ordering v,
16+
_CmpUnspecifiedParam) noexcept {
17+
return v.value == 0;
18+
}
19+
friend constexpr bool operator<(strong_ordering v,
20+
_CmpUnspecifiedParam) noexcept {
21+
return v.value < 0;
22+
}
23+
friend constexpr bool operator>(strong_ordering v,
24+
_CmpUnspecifiedParam) noexcept {
25+
return v.value > 0;
26+
}
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+
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+
}

0 commit comments

Comments
 (0)