Skip to content

Commit 9f1e7c4

Browse files
Merge pull request #8223 from ian-twilightcoder/zero-as-null
[cherry-pick stable/20230725] [Sema] -Wzero-as-null-pointer-constant: don't warn for __null (llvm#69126)
2 parents ad45284 + 9d5caed commit 9f1e7c4

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)