diff options
| author | Richard Trieu <rtrieu@google.com> | 2013-06-10 18:52:07 +0000 |
|---|---|---|
| committer | Richard Trieu <rtrieu@google.com> | 2013-06-10 18:52:07 +0000 |
| commit | bb4b894e0b2d0c39d40a178872127a5cd6becd8e (patch) | |
| tree | d856d2a862f5d8d5e750454ce179086b306e896c /clang/test | |
| parent | 4c44032aa1e5c198144db0a1d0c72280009418df (diff) | |
| download | bcm5719-llvm-bb4b894e0b2d0c39d40a178872127a5cd6becd8e.tar.gz bcm5719-llvm-bb4b894e0b2d0c39d40a178872127a5cd6becd8e.zip | |
Add a new warning, -Wlogical-not-parentheses, to -Wparentheses.
This warning triggers on the logical not of a non-boolean expression on the
left hand side of comparison. Often, the user meant to negate the comparison,
not just the left hand side of the comparison. Two notes are also emitted,
the first with a fix-it to add parentheses around the comparison, and the other
to put parenthesis around the not expression to silence the warning.
bool not_equal(int x, int y) {
return !x == y; // warn here
}
return !(x == y); // first fix-it, to negate comparison.
return (!x) == y; // second fix-it, to silence warning.
llvm-svn: 183688
Diffstat (limited to 'clang/test')
| -rw-r--r-- | clang/test/SemaCXX/warn-logical-not-compare.cpp | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-logical-not-compare.cpp b/clang/test/SemaCXX/warn-logical-not-compare.cpp new file mode 100644 index 00000000000..365a0285118 --- /dev/null +++ b/clang/test/SemaCXX/warn-logical-not-compare.cpp @@ -0,0 +1,112 @@ +// RUN: %clang_cc1 -fsyntax-only -Wlogical-not-parentheses -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wlogical-not-parentheses -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s + +bool getBool(); +int getInt(); + +bool test1(int i1, int i2, bool b1, bool b2) { + bool ret; + + ret = !i1 == i2; + // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{10:10-10:10}:"(" + // CHECK: fix-it:"{{.*}}":{10:18-10:18}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{10:9-10:9}:"(" + // CHECK: fix-it:"{{.*}}":{10:12-10:12}:")" + + ret = !i1 != i2; + //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{21:10-21:10}:"(" + // CHECK: fix-it:"{{.*}}":{21:18-21:18}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{21:9-21:9}:"(" + // CHECK: fix-it:"{{.*}}":{21:12-21:12}:")" + + ret = !i1 < i2; + //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{32:10-32:10}:"(" + // CHECK: fix-it:"{{.*}}":{32:17-32:17}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{32:9-32:9}:"(" + // CHECK: fix-it:"{{.*}}":{32:12-32:12}:")" + + ret = !i1 > i2; + //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{43:10-43:10}:"(" + // CHECK: fix-it:"{{.*}}":{43:17-43:17}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{43:9-43:9}:"(" + // CHECK: fix-it:"{{.*}}":{43:12-43:12}:")" + + ret = !i1 <= i2; + //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{54:10-54:10}:"(" + // CHECK: fix-it:"{{.*}}":{54:18-54:18}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{54:9-54:9}:"(" + // CHECK: fix-it:"{{.*}}":{54:12-54:12}:")" + + ret = !i1 >= i2; + //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{65:10-65:10}:"(" + // CHECK: fix-it:"{{.*}}":{65:18-65:18}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{65:9-65:9}:"(" + // CHECK: fix-it:"{{.*}}":{65:12-65:12}:")" + + ret = i1 == i2; + ret = i1 != i2; + ret = i1 < i2; + ret = i1 > i2; + ret = i1 <= i2; + ret = i1 >= i2; + + // Warning silenced by parens. + ret = (!i1) == i2; + ret = (!i1) != i2; + ret = (!i1) < i2; + ret = (!i1) > i2; + ret = (!i1) <= i2; + ret = (!i1) >= i2; + + ret = !b1 == b2; + ret = !b1 != b2; + ret = !b1 < b2; + ret = !b1 > b2; + ret = !b1 <= b2; + ret = !b1 >= b2; + + ret = !getInt() == i1; + // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}} + // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}} + // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}} + // CHECK: to evaluate the comparison first + // CHECK: fix-it:"{{.*}}":{98:10-98:10}:"(" + // CHECK: fix-it:"{{.*}}":{98:24-98:24}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{98:9-98:9}:"(" + // CHECK: fix-it:"{{.*}}":{98:18-98:18}:")" + + ret = (!getInt()) == i1; + ret = !getBool() == b1; + return ret; +} |

