Skip to content

[Sema] -Wzero-as-null-pointer-constant: don't warn for __null #69126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clang/lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GNUNullExpr>(EStripped))
return;

if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant,
Expand All @@ -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) &&
Expand Down
8 changes: 4 additions & 4 deletions clang/test/SemaCXX/warn-zero-nullptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down