Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -3287,6 +3287,10 @@ def warn_non_literal_null_pointer : Warning< "expression which evaluates to zero treated as a null pointer constant of " "type %0">, InGroup; +def warn_pointer_compare : Warning< + "comparing a pointer to a null character constant; did you mean " + "to compare to %select{nullptr|NULL|0}0?">, + InGroup>; def warn_impcast_null_pointer_to_integer : Warning< "implicit conversion of %select{NULL|nullptr}0 constant to %1">, InGroup; Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -9991,6 +9991,7 @@ QualType CheckShiftOperands( // C99 6.5.7 ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, BinaryOperatorKind Opc, bool IsCompAssign = false); + void CheckNullCharToNullptr(ExprResult &E, ExprResult &NullE); // helper for CheckCompareOperand QualType CheckCompareOperands( // C99 6.5.8/9 ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, BinaryOperatorKind Opc); Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -10433,6 +10433,29 @@ return S.Context.getLogicalOperationType(); } +void Sema::CheckNullCharToNullptr(ExprResult &E, ExprResult &NullE) { + if (!NullE.get()->getType()->isAnyPointerType()) + return; + Expr::NullPointerConstantKind NullKind = + NullE.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull); + if (!E.get()->getType()->isAnyPointerType() && + E.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) == + Expr::NPCK_ZeroExpression) { + if (const auto *CL = dyn_cast(E.get())) { + if (CL->getValue() == 0) + Diag(E.get()->getExprLoc(), diag::warn_pointer_compare) + << (NullKind == Expr::NPCK_CXX11_nullptr); + } + else if (const auto *CE = dyn_cast(E.get())) { + TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); + QualType T = TI->getType(); + if (T == Context.CharTy) + Diag(E.get()->getExprLoc(), diag::warn_pointer_compare) + << (NullKind == Expr::NPCK_CXX11_nullptr); + } + } +} + // C99 6.5.8, C++ [expr.rel] QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, @@ -10466,6 +10489,8 @@ } checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); + CheckNullCharToNullptr(LHS, RHS); + CheckNullCharToNullptr(RHS, LHS); // Handle vector comparisons separately. if (LHS.get()->getType()->isVectorType() || Index: clang/test/Sema/warn-nullchar-nullptr.c =================================================================== --- /dev/null +++ clang/test/Sema/warn-nullchar-nullptr.c @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wall %s + +int test1(int *a) { + return a == '\0'; // expected-warning {{comparing a pointer to a null character constant; did you mean to compare to nullptr?}} +} + +int test2(int *a) { + return '\0' == a; // expected-warning {{comparing a pointer to a null character constant; did you mean to compare to nullptr?}} +} + +int test3(int *a) { + return a == L'\0'; // expected-warning {{comparing a pointer to a null character constant; did you mean to compare to nullptr?}} +} + +int test4(int *a) { + return a == u'\0'; // expected-warning {{comparing a pointer to a null character constant; did you mean to compare to nullptr?}} +} + +int test5(int *a) { + return a == U'\0'; // expected-warning {{comparing a pointer to a null character constant; did you mean to compare to nullptr?}} +} + +int test6(int *a) { + return a == (char)0; // expected-warning {{comparing a pointer to a null character constant; did you mean to compare to nullptr?}} +}