r316605 - Ignore implicity casts for zero-as-null-pointer-constant warning

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 25 13:23:13 PDT 2017


Author: erichkeane
Date: Wed Oct 25 13:23:13 2017
New Revision: 316605

URL: http://llvm.org/viewvc/llvm-project?rev=316605&view=rev
Log:
Ignore implicity casts for zero-as-null-pointer-constant warning

The repro in https://bugs.llvm.org/show_bug.cgi?id=34362
caused the left nullptr to be cast to a int* implicitly, which
resulted diagnosing this falsely.

Differential Revision: https://reviews.llvm.org/D39301

Modified:
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/test/SemaCXX/warn-zero-nullptr.cpp

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=316605&r1=316604&r2=316605&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Wed Oct 25 13:23:13 2017
@@ -438,7 +438,7 @@ void Sema::diagnoseNullableToNonnullConv
 void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) {
   if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer)
     return;
-  if (E->getType()->isNullPtrType())
+  if (E->IgnoreParenImpCasts()->getType()->isNullPtrType())
     return;
   // nullptr only exists from C++11 on, so don't warn on its absence earlier.
   if (!getLangOpts().CPlusPlus11)

Modified: cfe/trunk/test/SemaCXX/warn-zero-nullptr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-zero-nullptr.cpp?rev=316605&r1=316604&r2=316605&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-zero-nullptr.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-zero-nullptr.cpp Wed Oct 25 13:23:13 2017
@@ -25,3 +25,10 @@ void g() {
 // Warn on these too. Matches gcc and arguably makes sense.
 void* pp = (decltype(nullptr))0; // expected-warning{{zero as null pointer constant}}
 void* pp2 = static_cast<decltype(nullptr)>(0); // expected-warning{{zero as null pointer constant}}
+
+// Shouldn't warn.
+namespace pr34362 {
+struct A { operator int*() { return nullptr; } };
+void func() { if (nullptr == A()) {} }
+void func2() { if ((nullptr) == A()) {} }
+}




More information about the cfe-commits mailing list