diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1bb842f9b8888..e7db9512faf26 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -401,6 +401,10 @@ Improvements to Clang's diagnostics 24 | return decltype(fun_ptr)( f_ptr /*comment*/); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- ``-Wzero-as-null-pointer-constant`` diagnostic is no longer emitted when using ``__null`` + (or, more commonly, ``NULL`` when the platform defines it as ``__null``) to be more consistent + with GCC. + Bug Fixes in This Version ------------------------- - Fixed an issue where a class template specialization whose declaration is diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 67533ccbdf347..acb765559e6a8 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -590,7 +590,11 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) { if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer) return; - if (E->IgnoreParenImpCasts()->getType()->isNullPtrType()) + + const Expr *EStripped = E->IgnoreParenImpCasts(); + if (EStripped->getType()->isNullPtrType()) + return; + if (isa(EStripped)) return; if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant, @@ -612,6 +616,8 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) { // If it is a macro from system header, and if the macro name is not "NULL", // do not warn. + // Note that uses of "NULL" will be ignored above on systems that define it + // as __null. SourceLocation MaybeMacroLoc = E->getBeginLoc(); if (Diags.getSuppressSystemWarnings() && SourceMgr.isInSystemMacro(MaybeMacroLoc) && diff --git a/clang/test/SemaCXX/warn-zero-nullptr.cpp b/clang/test/SemaCXX/warn-zero-nullptr.cpp index 45f05fa5703b1..684572f8ef67d 100644 --- a/clang/test/SemaCXX/warn-zero-nullptr.cpp +++ b/clang/test/SemaCXX/warn-zero-nullptr.cpp @@ -16,10 +16,10 @@ int (S::*mp1) = 0; // expected-warning{{zero as null pointer constant}} void (*fp1)() = 0; // expected-warning{{zero as null pointer constant}} void* p1 = 0; // expected-warning{{zero as null pointer constant}} -// NULL is an integer constant expression, so warn on it too: -void* p2 = __null; // expected-warning{{zero as null pointer constant}} -void (*fp2)() = __null; // expected-warning{{zero as null pointer constant}} -int (S::*mp2) = __null; // expected-warning{{zero as null pointer constant}} +// __null is not treated as an integer constant expression for GCC compatibility +void* p2 = __null; +void (*fp2)() = __null; +int (S::*mp2) = __null; void f0(void* v = MACRO); // expected-warning{{zero as null pointer constant}} void f1(void* v = NULL); // expected-warning{{zero as null pointer constant}}