Skip to content

Commit cd29e19

Browse files
authored
[Sema] -Wzero-as-null-pointer-constant: don't warn for __null (#69126)
The implementation of -Wzero-as-null-pointer-constant was done before the following fix has been committed to GCC: https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=752e7593b0f19af233a0b7e72daab8413662b605;hp=298434c916c14e8adca2cab8a746aee29038c5b3 As a result, clang and gcc diverge on the use of `__null` and, consequently, on the use of `NULL` on systems like Linux/macOS where `NULL` is defined as `__null`. This is a problem for compatibility between gcc and clang, particularly for code bases that support C++98 or for single-source libraries that are implemented in C, but compiled as C++ via inclusion into a C++ translation unit. Code like this can not be changed to use `nullptr`, as it needs to maintain compatibility with C before C23 or C++ before C++11, but warns on the use of `NULL` in clang. The warning `Wzero-as-null-pointer-constant` is still useful with this change, as it allows to change `0` to `NULL`, which fixes gcc warnings and helps the reader distinguish between pointers and non-pointers. Users who require a full C++11 modernization pass can still use clang-tidy for that purpose.
1 parent c9c9bf0 commit cd29e19

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ Improvements to Clang's diagnostics
401401
24 | return decltype(fun_ptr)( f_ptr /*comment*/);
402402
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
403403
404+
- ``-Wzero-as-null-pointer-constant`` diagnostic is no longer emitted when using ``__null``
405+
(or, more commonly, ``NULL`` when the platform defines it as ``__null``) to be more consistent
406+
with GCC.
407+
404408
Bug Fixes in This Version
405409
-------------------------
406410
- Fixed an issue where a class template specialization whose declaration is

clang/lib/Sema/Sema.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,11 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
590590

591591
if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer)
592592
return;
593-
if (E->IgnoreParenImpCasts()->getType()->isNullPtrType())
593+
594+
const Expr *EStripped = E->IgnoreParenImpCasts();
595+
if (EStripped->getType()->isNullPtrType())
596+
return;
597+
if (isa<GNUNullExpr>(EStripped))
594598
return;
595599

596600
if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant,
@@ -612,6 +616,8 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
612616

613617
// If it is a macro from system header, and if the macro name is not "NULL",
614618
// do not warn.
619+
// Note that uses of "NULL" will be ignored above on systems that define it
620+
// as __null.
615621
SourceLocation MaybeMacroLoc = E->getBeginLoc();
616622
if (Diags.getSuppressSystemWarnings() &&
617623
SourceMgr.isInSystemMacro(MaybeMacroLoc) &&

clang/test/SemaCXX/warn-zero-nullptr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ int (S::*mp1) = 0; // expected-warning{{zero as null pointer constant}}
1616
void (*fp1)() = 0; // expected-warning{{zero as null pointer constant}}
1717
void* p1 = 0; // expected-warning{{zero as null pointer constant}}
1818

19-
// NULL is an integer constant expression, so warn on it too:
20-
void* p2 = __null; // expected-warning{{zero as null pointer constant}}
21-
void (*fp2)() = __null; // expected-warning{{zero as null pointer constant}}
22-
int (S::*mp2) = __null; // expected-warning{{zero as null pointer constant}}
19+
// __null is not treated as an integer constant expression for GCC compatibility
20+
void* p2 = __null;
21+
void (*fp2)() = __null;
22+
int (S::*mp2) = __null;
2323

2424
void f0(void* v = MACRO); // expected-warning{{zero as null pointer constant}}
2525
void f1(void* v = NULL); // expected-warning{{zero as null pointer constant}}

0 commit comments

Comments
 (0)