c-common.c (warn_about_parentheses): Separate warning about un-parenthized sequence...
authorDirk Mueller <dmueller@suse.de>
Wed, 31 Jan 2007 13:43:40 +0000 (13:43 +0000)
committerDirk Mueller <mueller@gcc.gnu.org>
Wed, 31 Jan 2007 13:43:40 +0000 (13:43 +0000)
2007-01-31  Dirk Mueller  <dmueller@suse.de>

        * c-common.c (warn_about_parentheses): Separate warning about
        un-parenthized sequence of comparison operators from the one
        which is supposed to warn about x <= y <= z.

        * testsuite/gcc.dg/Wparentheses-2.c: Update and add new tests.

From-SVN: r121421

gcc/ChangeLog
gcc/c-common.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wparentheses-2.c

index ba55b7c30dbdc1049c1aa949f6c491e8026eb06f..3433a8d1406b387b2a335e0681d407539024bc66 100644 (file)
@@ -1,3 +1,9 @@
+2007-01-31  Dirk Mueller  <dmueller@suse.de>
+
+       * c-common.c (warn_about_parentheses): Separate warning about
+       un-parenthized sequence of comparison operators from the one
+       which is supposed to warn about x <= y <= z.
+
 2007-01-31  Uros Bizjak  <ubizjak@gmail.com>
 
        * optabs.h (enum optab_index): Add new OTI_isinf.
index 1bd59ff5e227ae7056be37fe61a43b6086074480..cfd382991d362c1e28d9f07d4af7bd2ab00f2ed4 100644 (file)
@@ -6750,12 +6750,23 @@ warn_about_parentheses (enum tree_code code, enum tree_code code_left,
                 "suggest parentheses around comparison in operand of &");
     }
 
-  /* Similarly, check for cases like 1<=i<=10 that are probably errors.  */
-  if (TREE_CODE_CLASS (code) == tcc_comparison
-      && (TREE_CODE_CLASS (code_left) == tcc_comparison
-         || TREE_CODE_CLASS (code_right) == tcc_comparison))
-    warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
-            "have their mathematical meaning");
+  if (code == EQ_EXPR || code == NE_EXPR)
+    {
+      if (TREE_CODE_CLASS (code_left) == tcc_comparison
+          || TREE_CODE_CLASS (code_right) == tcc_comparison)
+       warning (OPT_Wparentheses,
+                "suggest parentheses around comparison in operand of %s",
+                 code == EQ_EXPR ? "==" : "!=");
+    }
+  else if (TREE_CODE_CLASS (code) == tcc_comparison)
+    {
+      if ((TREE_CODE_CLASS (code_left) == tcc_comparison
+          && code_left != NE_EXPR && code_left != EQ_EXPR)
+         || (TREE_CODE_CLASS (code_right) == tcc_comparison
+             && code_right != NE_EXPR && code_right != EQ_EXPR))
+       warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
+                "have their mathematical meaning");
+    }
 }
 
 /* If LABEL (a LABEL_DECL) has not been used, issue a warning.  */
index 355354151e9b3810e5b25129ce97f3f050154a4f..137fea50e4fbdd828a412bd260bd1146fafaddfa 100644 (file)
@@ -1,3 +1,7 @@
+2007-01-31  Dirk Mueller  <dmueller@suse.de>
+
+       gcc.dg/Wparentheses-2.c: Update and add new tests.
+
 2007-01-31  Ira Rosen  <irar@il.ibm.com> 
 
        * gcc.dg/vect/vect-37.c: Restore the original behaivior - xfail to
index e4110a8ceed97983f4283f40adea160808bacabc..51038c2d71526cdf0f409ac46eed6216e61c0140 100644 (file)
@@ -10,58 +10,112 @@ int foo (int);
 int
 bar (int a, int b, int c)
 {
-  foo (a <= b <= c); /* { dg-warning "comparison" "correct warning" } */
+  foo (a <= b <= c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((a <= b) <= c);
   foo (a <= (b <= c));
-  foo (1 <= 2 <= c); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 <= 2 <= c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 <= 2) <= c);
   foo (1 <= (2 <= c));
-  foo (1 <= 2 <= 3); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 <= 2 <= 3); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 <= 2) <= 3);
   foo (1 <= (2 <= 3));
-  foo (a > b > c); /* { dg-warning "comparison" "correct warning" } */
+  foo (a > b > c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((a > b) > c);
   foo (a > (b > c));
-  foo (1 > 2 > c); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 > 2 > c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 > 2) > c);
   foo (1 > (2 > c));
-  foo (1 > 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 > 2 > 3); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 > 2) > 3);
   foo (1 > (2 > 3));
-  foo (a < b <= c); /* { dg-warning "comparison" "correct warning" } */
+  foo (a < b <= c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((a < b) <= c);
   foo (a < (b <= c));
-  foo (1 < 2 <= c); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 < 2 <= c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 < 2) <= c);
   foo (1 < (2 <= c));
-  foo (1 < 2 <= 3); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 < 2 <= 3); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 < 2) <= 3);
   foo (1 < (2 <= 3));
-  foo (a <= b > c); /* { dg-warning "comparison" "correct warning" } */
+  foo (a <= b > c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((a <= b) > c);
   foo (a <= (b > c));
-  foo (1 <= 2 > c); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 <= 2 > c); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 <= 2) > c);
   foo (1 <= (2 > c));
-  foo (1 <= 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 <= 2 > 3); /* { dg-warning "mathematical meaning" "correct warning" } */
   foo ((1 <= 2) > 3);
   foo (1 <= (2 > 3));
-  foo (a <= b == c); /* { dg-warning "comparison" "correct warning" } */
+  foo (a <= b == c); /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
   foo ((a <= b) == c);
   foo (a <= (b == c));
-  foo (1 <= 2 == c); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 <= 2 == c); /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
   foo ((1 <= 2) == c);
   foo (1 <= (2 == c));
-  foo (1 <= 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 <= 2 == 3); /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
   foo ((1 <= 2) == 3);
   foo (1 <= (2 == 3));
-  foo (a != b != c); /* { dg-warning "comparison" "correct warning" } */
+  foo (a != b != c); /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
   foo ((a != b) != c);
   foo (a != (b != c));
-  foo (1 != 2 != c); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 != 2 != c); /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
   foo ((1 != 2) != c);
   foo (1 != (2 != c));
-  foo (1 != 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+  foo (1 != 2 != 3); /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
   foo ((1 != 2) != 3);
   foo (1 != (2 != 3));
+  foo (a < b == c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a < b) == c);
+  foo (a < (b == c));
+  foo (a > b == c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a > b) == c);
+  foo (a > (b == c));
+  foo (a == b < c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a == b) < c);
+  foo (a == (b < c));
+  foo (a == b > c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a == b) > c);
+  foo (a == (b > c));
+  foo (a == b == c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a == b) == c);
+  foo (a == (b == c));
+  foo (1 == 2 == 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 == 2) == 3);
+  foo (1 == (2 == 3));
+  foo (1 < 2 == 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 < 2) == 3);
+  foo (1 < (2 == 3));
+  foo (1 > 2 == 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 > 2) == 3);
+  foo (1 > (2 == 3));
+  foo (1 == 2 < 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 == 2) < 3);
+  foo (1 == (2 < 3));
+  foo (1 == 2 > 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 == 2) > 3);
+  foo (1 == (2 > 3));
+  foo (a < b != c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a < b) != c);
+  foo (a < (b != c));
+  foo (a > b != c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a > b) != c);
+  foo (a > (b != c));
+  foo (a != b < c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a != b) < c);
+  foo (a != (b < c));
+  foo (a != b > c);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((a != b) > c);
+  foo (a != (b > c));
+  foo (1 < 2 != 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 < 2) != 3);
+  foo (1 < (2 != 3));
+  foo (1 > 2 != 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 > 2) != 3);
+  foo (1 > (2 != 3));
+  foo (1 != 2 < 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 != 2) < 3);
+  foo (1 != (2 < 3));
+  foo (1 != 2 > 3);  /* { dg-warning "suggest parentheses around comparison" "correct warning" } */
+  foo ((1 != 2) > 3);
+  foo (1 != (2 > 3));
 }