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;
         ~~ ^  ~~~~
llvm-svn: 137377
This commit is contained in:
Richard Trieu 2011-08-11 22:38:21 +00:00
parent aa07cb6a98
commit aee9e76722
3 changed files with 15 additions and 10 deletions

View File

@ -3163,6 +3163,10 @@ def warn_comparison_of_mixed_enum_types : Warning<
def warn_null_in_arithmetic_operation : Warning< def warn_null_in_arithmetic_operation : Warning<
"use of NULL in arithmetic operation">, "use of NULL in arithmetic operation">,
InGroup<DiagGroup<"null-arithmetic">>; 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< def err_invalid_this_use : Error<
"invalid use of 'this' outside of a nonstatic member function">; "invalid use of 'this' outside of a nonstatic member function">;

View File

@ -7626,9 +7626,10 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
!LeftType->canDecayToPointerType() && !LeftType->canDecayToPointerType() &&
!RightType->isAnyPointerType() && !RightType->isAnyPointerType() &&
!RightType->canDecayToPointerType()) { !RightType->canDecayToPointerType()) {
Diag(OpLoc, diag::warn_null_in_arithmetic_operation) Diag(OpLoc, diag::warn_null_in_comparison_operation)
<< (LeftNull ? lhs.get()->getSourceRange() << LeftNull /* LHS is NULL */
: rhs.get()->getSourceRange()); << (LeftNull ? rhs.get()->getType() : lhs.get()->getType())
<< lhs.get()->getSourceRange() << rhs.get()->getSourceRange();
} }
} }
} }

View File

@ -64,12 +64,12 @@ void f() {
a |= NULL; // expected-warning{{use of NULL in arithmetic operation}} a |= NULL; // expected-warning{{use of NULL in arithmetic operation}}
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 || a > NULL; // expected-warning 2{{comparison between NULL and non-pointer ('int' and NULL)}}
b = a > NULL || NULL > a; // expected-warning 2{{use of NULL in arithmetic operation}} b = NULL < a || NULL > a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
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 = a >= NULL || NULL >= a; // expected-warning 2{{use of NULL in arithmetic operation}} b = NULL <= a || NULL >= a; // expected-warning 2{{comparison between NULL and non-pointer (NULL and 'int')}}
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 = a != NULL || NULL != a; // expected-warning 2{{use of NULL in arithmetic operation}} 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;
b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a; b = &a <= NULL || NULL <= &a || &a >= NULL || NULL >= &a;
@ -82,7 +82,7 @@ void f() {
b = NULL <= NULL || NULL >= NULL; b = NULL <= NULL || NULL >= NULL;
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. // Check that even non-standard pointers don't warn.
b = c == NULL || NULL == c || c != NULL || NULL != c; b = c == NULL || NULL == c || c != NULL || NULL != c;