Skip to content

Commit 9d5caed

Browse files
zeuxian-twilightcoder
authored andcommitted
[cherry-pick stable/20230725] [Sema] -Wzero-as-null-pointer-constant: don't warn for __null (llvm#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 c4000d8 commit 9d5caed

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
@@ -500,6 +500,10 @@ Improvements to Clang's diagnostics
500500
warnings. Instead, it will suggest casting the enum object based on its
501501
underlying type.
502502

503+
- ``-Wzero-as-null-pointer-constant`` diagnostic is no longer emitted when using ``__null``
504+
(or, more commonly, ``NULL`` when the platform defines it as ``__null``) to be more consistent
505+
with GCC.
506+
503507
Bug Fixes in This Version
504508
-------------------------
505509
- 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
@@ -596,7 +596,11 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
596596

597597
if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer)
598598
return;
599-
if (E->IgnoreParenImpCasts()->getType()->isNullPtrType())
599+
600+
const Expr *EStripped = E->IgnoreParenImpCasts();
601+
if (EStripped->getType()->isNullPtrType())
602+
return;
603+
if (isa<GNUNullExpr>(EStripped))
600604
return;
601605

602606
if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant,
@@ -618,6 +622,8 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
618622

619623
// If it is a macro from system header, and if the macro name is not "NULL",
620624
// do not warn.
625+
// Note that uses of "NULL" will be ignored above on systems that define it
626+
// as __null.
621627
SourceLocation MaybeMacroLoc = E->getBeginLoc();
622628
if (Diags.getSuppressSystemWarnings() &&
623629
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)