[cfe-commits] r137377 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/SemaCXX/null_in_arithmetic_ops.cpp

Richard Trieu rtrieu at google.com
Thu Aug 11 15:38:21 PDT 2011


Author: rtrieu
Date: Thu Aug 11 17:38:21 2011
New Revision: 137377

URL: http://llvm.org/viewvc/llvm-project?rev=137377&view=rev
Log:
The current warning in -Wnull-arithmetic for comparisons between NULL and non-pointers is not very helpful.  This patch will update the wording to be more helpful to users.

Old warning:

warning: use of NULL in arithmetic operation [-Wnull-arithmetic]
  return 10 <= NULL;
            ^  ~~~~

New warning:

warning: comparison between NULL and non-pointer ('int' and NULL) [-Wnull-arithmetic]
  return 10 <= NULL;
         ~~ ^  ~~~~

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaCXX/null_in_arithmetic_ops.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=137377&r1=137376&r2=137377&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Aug 11 17:38:21 2011
@@ -3163,6 +3163,10 @@
 def warn_null_in_arithmetic_operation : Warning<
   "use of NULL in arithmetic operation">,
   InGroup<DiagGroup<"null-arithmetic">>;
+def warn_null_in_comparison_operation : Warning<
+  "comparison between NULL and non-pointer "
+  "%select{(%1 and NULL)|(NULL and %1)}0">,
+  InGroup<DiagGroup<"null-arithmetic">>;
 
 def err_invalid_this_use : Error<
   "invalid use of 'this' outside of a nonstatic member function">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=137377&r1=137376&r2=137377&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Aug 11 17:38:21 2011
@@ -7626,9 +7626,10 @@
             !LeftType->canDecayToPointerType() &&
             !RightType->isAnyPointerType() &&
             !RightType->canDecayToPointerType()) {
-          Diag(OpLoc, diag::warn_null_in_arithmetic_operation)
-            << (LeftNull ? lhs.get()->getSourceRange()
-                         : rhs.get()->getSourceRange());
+          Diag(OpLoc, diag::warn_null_in_comparison_operation)
+            << LeftNull /* LHS is NULL */
+            << (LeftNull ? rhs.get()->getType() : lhs.get()->getType())
+            << lhs.get()->getSourceRange() << rhs.get()->getSourceRange();
         }
       }
     }

Modified: cfe/trunk/test/SemaCXX/null_in_arithmetic_ops.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/null_in_arithmetic_ops.cpp?rev=137377&r1=137376&r2=137377&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/null_in_arithmetic_ops.cpp (original)
+++ cfe/trunk/test/SemaCXX/null_in_arithmetic_ops.cpp Thu Aug 11 17:38:21 2011
@@ -64,12 +64,12 @@
   a |= NULL; // expected-warning{{use of NULL in arithmetic operation}}
   a ^= NULL; // expected-warning{{use of NULL in arithmetic operation}}
 
-  b = a < NULL || NULL < a; // expected-warning 2{{use of NULL in arithmetic operation}}
-  b = a > NULL || NULL > a; // expected-warning 2{{use of NULL in arithmetic operation}}
-  b = a <= NULL || NULL <= a; // expected-warning 2{{use of NULL in arithmetic operation}}
-  b = a >= NULL || NULL >= a; // expected-warning 2{{use of NULL in arithmetic operation}}
-  b = a == NULL || NULL == a; // expected-warning 2{{use of NULL in arithmetic operation}}
-  b = a != NULL || NULL != a; // expected-warning 2{{use of NULL in arithmetic operation}}
+  b = a < NULL || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
+  b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
+  b = a <= NULL || a >= NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
+  b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
+  b = a == NULL || a != NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
+  b = NULL == a || NULL != a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
 
   b = &a < NULL || NULL < &a || &a > NULL || NULL > &a;
   b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a;
@@ -82,7 +82,7 @@
   b = NULL <= NULL || NULL >= NULL;
   b = NULL == NULL || NULL != NULL;
 
-  b = ((NULL)) != a;  // expected-warning{{use of NULL in arithmetic operation}}
+  b = ((NULL)) != a;  // expected-warning{{comparison between NULL and non-pointer (NULL and 'int')}}
 
   // Check that even non-standard pointers don't warn.
   b = c == NULL || NULL == c || c != NULL || NULL != c;





More information about the cfe-commits mailing list