This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PING^3: [PATCH] correct "comparisons do not have their mathematical meaning" warning


On Monday, 29. January 2007 14:50, Joseph S. Myers wrote:

> In the testcase, dg-warning "comparison" would match both diagnostics.
> You should use something more specific like dg-warning "suggest
> parentheses around comparison" so that in every case you are testing which
> of the two diagnostics is given.

Sure, updated accordingly. Regtested this particular testcase again. 


2007-01-22  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.

--- c-common.c	(revision 121080)
+++ c-common.c	(working copy)
@@ -6750,12 +6803,23 @@ warn_about_parentheses (enum tree_code c
 		 "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");
+    }
 }
 
 
--- testsuite/gcc.dg/Wparentheses-2.c	(revision 121080)
+++ testsuite/gcc.dg/Wparentheses-2.c	(working copy)
@@ -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));
 }



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]