From: Richard Trieu Date: Thu, 4 Jul 2013 00:50:18 +0000 (+0000) Subject: Improve -Wlogical-not-parentheses to catch when the not is applied to an enum. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=14b7673b341c0c9edc719754b9c2faafac21fe36;p=clang Improve -Wlogical-not-parentheses to catch when the not is applied to an enum. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185602 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index c885e3f927..6f3997eb36 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -7270,7 +7270,7 @@ static void diagnoseLogicalNotOnLHSofComparison(Sema &S, ExprResult &LHS, if (!S.getLangOpts().Bool) return; // Check that left hand side is !something. - UnaryOperator *UO = dyn_cast(LHS.get()); + UnaryOperator *UO = dyn_cast(LHS.get()->IgnoreImpCasts()); if (!UO || UO->getOpcode() != UO_LNot) return; // Only check if the right hand side is non-bool arithmetic type. diff --git a/test/SemaCXX/warn-logical-not-compare.cpp b/test/SemaCXX/warn-logical-not-compare.cpp index 365a028511..7e7e3a7592 100644 --- a/test/SemaCXX/warn-logical-not-compare.cpp +++ b/test/SemaCXX/warn-logical-not-compare.cpp @@ -110,3 +110,70 @@ bool test1(int i1, int i2, bool b1, bool b2) { ret = !getBool() == b1; return ret; } + +enum E {e1, e2}; +E getE(); + +bool test2 (E e) { + bool ret; + ret = e == e1; + ret = e == getE(); + ret = getE() == e1; + ret = getE() == getE(); + + ret = !e == e1; + // 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:"{{.*}}":{124:10-124:10}:"(" + // CHECK: fix-it:"{{.*}}":{124:17-124:17}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{124:9-124:9}:"(" + // CHECK: fix-it:"{{.*}}":{124:11-124:11}:")" + + ret = !e == getE(); + // 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:"{{.*}}":{135:10-135:10}:"(" + // CHECK: fix-it:"{{.*}}":{135:21-135:21}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{135:9-135:9}:"(" + // CHECK: fix-it:"{{.*}}":{135:11-135:11}:")" + + ret = !getE() == e1; + // 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:"{{.*}}":{146:10-146:10}:"(" + // CHECK: fix-it:"{{.*}}":{146:22-146:22}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{146:9-146:9}:"(" + // CHECK: fix-it:"{{.*}}":{146:16-146:16}:")" + + ret = !getE() == getE(); + // 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:"{{.*}}":{157:10-157:10}:"(" + // CHECK: fix-it:"{{.*}}":{157:26-157:26}:")" + // CHECK: to silence this warning + // CHECK: fix-it:"{{.*}}":{157:9-157:9}:"(" + // CHECK: fix-it:"{{.*}}":{157:16-157:16}:")" + + ret = !(e == e1); + ret = !(e == getE()); + ret = !(getE() == e1); + ret = !(getE() == getE()); + + ret = (!e) == e1; + ret = (!e) == getE(); + ret = (!getE()) == e1; + ret = (!getE()) == getE(); + + return ret; +}