r185602 - Improve -Wlogical-not-parentheses to catch when the not is applied to an enum.

Richard Trieu rtrieu at google.com
Wed Jul 3 17:50:18 PDT 2013


Author: rtrieu
Date: Wed Jul  3 19:50:18 2013
New Revision: 185602

URL: http://llvm.org/viewvc/llvm-project?rev=185602&view=rev
Log:
Improve -Wlogical-not-parentheses to catch when the not is applied to an enum.

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaCXX/warn-logical-not-compare.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=185602&r1=185601&r2=185602&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Jul  3 19:50:18 2013
@@ -7270,7 +7270,7 @@ static void diagnoseLogicalNotOnLHSofCom
   if (!S.getLangOpts().Bool) return;
 
   // Check that left hand side is !something.
-  UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS.get());
+  UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS.get()->IgnoreImpCasts());
   if (!UO || UO->getOpcode() != UO_LNot) return;
 
   // Only check if the right hand side is non-bool arithmetic type.

Modified: cfe/trunk/test/SemaCXX/warn-logical-not-compare.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-logical-not-compare.cpp?rev=185602&r1=185601&r2=185602&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-logical-not-compare.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-logical-not-compare.cpp Wed Jul  3 19:50:18 2013
@@ -110,3 +110,70 @@ bool test1(int i1, int i2, bool b1, bool
   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 at -1 {{logical not is only applied to the left hand side of this comparison}}
+  // expected-note at -2 {{add parentheses after the '!' to evaluate the comparison first}}
+  // expected-note at -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 at -1 {{logical not is only applied to the left hand side of this comparison}}
+  // expected-note at -2 {{add parentheses after the '!' to evaluate the comparison first}}
+  // expected-note at -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 at -1 {{logical not is only applied to the left hand side of this comparison}}
+  // expected-note at -2 {{add parentheses after the '!' to evaluate the comparison first}}
+  // expected-note at -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 at -1 {{logical not is only applied to the left hand side of this comparison}}
+  // expected-note at -2 {{add parentheses after the '!' to evaluate the comparison first}}
+  // expected-note at -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;
+}





More information about the cfe-commits mailing list