-Wnull-arithmetic was catching some false positives.  Switch the checking
code so that it will warn on specific types instead of not warning on
specific types.  Now, comparisons between non-boolean arithmetic types and
NULL will give this warning.  New types to warn on can be added later if
needed.
Index: test/SemaCXX/null_in_arithmetic_ops.cpp
===================================================================
--- test/SemaCXX/null_in_arithmetic_ops.cpp	(revision 133317)
+++ test/SemaCXX/null_in_arithmetic_ops.cpp	(working copy)
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -fblocks -Wnull-arithmetic -verify %s
 #include <stddef.h>
 
-void f() {
+void foo() {
   int a;
   bool b;
 
@@ -56,6 +56,10 @@
   b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a;
   b = &a == NULL || NULL == &a || &a != NULL || NULL != &a;
 
+  b = NULL < NULL || NULL > NULL;
+  b = NULL <= NULL || NULL >= NULL;
+  b = NULL == NULL || NULL != NULL;
+  
   b = 0 == a;
   b = 0 == &a;
 
@@ -67,4 +71,12 @@
   class X;
   void (X::*d) ();
   b = d == NULL || NULL == d || d != NULL || NULL != d;
+
+  void e();
+  b = e == NULL || NULL == e || e != NULL || NULL != e;
+
+  int f[2];
+  b = f == NULL || NULL == f || f != NULL || NULL != f;
+
+  b = "f" == NULL || NULL == "f" || "f" != NULL || NULL != "f";
 }
Index: test/SemaCXX/nullptr_in_arithmetic_ops.cpp
===================================================================
--- test/SemaCXX/nullptr_in_arithmetic_ops.cpp	(revision 133317)
+++ test/SemaCXX/nullptr_in_arithmetic_ops.cpp	(working copy)
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -fsyntax-only -fblocks -std=c++0x -verify %s
 
-void f() {
+void foo() {
   int a;
   bool b;
 
@@ -49,6 +49,10 @@
   b = &a <= nullptr || nullptr <= &a || &a >= nullptr || nullptr >= &a;
   b = &a == nullptr || nullptr == &a || &a != nullptr || nullptr != &a;
 
+  b = nullptr < nullptr || nullptr > nullptr;
+  b = nullptr <= nullptr || nullptr >= nullptr;
+  b = nullptr == nullptr || nullptr != nullptr;
+
   b = 0 == a;
   b = 0 == &a;
 
@@ -62,4 +66,12 @@
   void (X::*d) ();
   d = nullptr;
   b = d == nullptr || nullptr == d || d != nullptr || nullptr != d;
+
+  void e();
+  b = e == nullptr || nullptr == e || e != nullptr || nullptr != e;
+
+  int f[2];
+  b = f == nullptr || nullptr == f || f != nullptr || nullptr != f;
+
+  b = "f" == nullptr || nullptr == "f" || "f" != nullptr || nullptr != "f";
 }
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp	(revision 133317)
+++ lib/Sema/SemaExpr.cpp	(working copy)
@@ -8969,19 +8969,15 @@
       // if the other expression the other expression is not a pointer.
       QualType LeftType = lhs.get()->getType();
       QualType RightType = rhs.get()->getType();
-      bool LeftPointer = LeftType->isPointerType() ||
-                         LeftType->isBlockPointerType() ||
-                         LeftType->isMemberPointerType() ||
-                         LeftType->isObjCObjectPointerType();
-      bool RightPointer = RightType->isPointerType() ||
-                          RightType->isBlockPointerType() ||
-                          RightType->isMemberPointerType() ||
-                          RightType->isObjCObjectPointerType();
-      if ((LeftNull != RightNull) && !LeftPointer && !RightPointer) {
-        if (LeftNull)
+      bool LeftNumber = LeftType->isArithmeticType() &&
+                        !LeftType->isBooleanType();
+      bool RightNumber = RightType->isArithmeticType() &&
+                         !RightType->isBooleanType();
+      if ((LeftNull != RightNull)) {
+        if (LeftNull && RightNumber)
           Diag(OpLoc, diag::warn_null_in_arithmetic_operation)
                << lhs.get()->getSourceRange();
-        if (RightNull)
+        if (RightNull && LeftNumber)
           Diag(OpLoc, diag::warn_null_in_arithmetic_operation)
                << rhs.get()->getSourceRange();
       }
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to