PR c++/36921 [4.3/4.4 Regression] warning "comparison does not have mathematical meaning" is not correct for overloaded operators that do not return boolean

Jakub Jelinek jakub@redhat.com
Sat Dec 20 13:38:00 GMT 2008


On Sat, Dec 20, 2008 at 12:47:20AM +0100, Jakub Jelinek wrote:
> You're right, that works too.  I've so far just checked this testcase,
> will do full bootstrap/regtest soon.

This passed bootstrap/regtest on x86_64-linux and works for both C and C++.

2008-12-20  Jakub Jelinek  <jakub@redhat.com>
	    Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR c++/36921
	* c-common.c (warn_about_parentheses): Remove ARG_UNUSED from
	arg_left.  Don't warn about X<=Y<=Z if comparison's type isn't
	integral.

	* g++.dg/warn/pr36921.C: New.

--- gcc/c-common.c.jj	2008-11-15 11:13:56.000000000 +0100
+++ gcc/c-common.c	2008-12-20 01:34:20.000000000 +0100
@@ -8059,7 +8059,7 @@ warn_array_subscript_with_type_char (tre
 
 void
 warn_about_parentheses (enum tree_code code,
-			enum tree_code code_left, tree ARG_UNUSED (arg_left),
+			enum tree_code code_left, tree arg_left,
 			enum tree_code code_right, tree arg_right)
 {
   if (!warn_parentheses)
@@ -8169,9 +8169,11 @@ warn_about_parentheses (enum tree_code c
     default:
       if (TREE_CODE_CLASS (code) == tcc_comparison
 	   && ((TREE_CODE_CLASS (code_left) == tcc_comparison
-		&& code_left != NE_EXPR && code_left != EQ_EXPR)
+		&& code_left != NE_EXPR && code_left != EQ_EXPR
+		&& INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
 	       || (TREE_CODE_CLASS (code_right) == tcc_comparison
-		   && code_right != NE_EXPR && code_right != EQ_EXPR)))
+		   && code_right != NE_EXPR && code_right != EQ_EXPR
+		   && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))))
 	warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not "
 		 "have their mathematical meaning");
       return;
--- gcc/testsuite/g++.dg/warn/pr36921.C.jj	2008-12-20 00:37:34.000000000 +0100
+++ gcc/testsuite/g++.dg/warn/pr36921.C	2008-12-20 00:37:34.000000000 +0100
@@ -0,0 +1,27 @@
+/* PR 36921: comparison operator can be overloaded. Do not emit
+   warnings in such case.
+ { dg-do compile }
+ { dg-options "-Wparentheses" }
+*/
+struct A {};
+A operator<(A, A) { return A(); }
+A operator>(A, A) { return A(); }
+A operator<=(A, A) { return A(); }
+A operator>=(A, A) { return A(); }
+A operator==(A, A) { return A(); }
+A operator!=(A, A) { return A(); }
+
+int main() {
+  A() < A() < A(); // should not emit warning
+  1 < 2 < 3; // { dg-warning "mathematical meaning" "parentheses" }
+  A() > A() > A(); // should not emit warning
+  1 > 2 > 3; // { dg-warning "mathematical meaning" "parentheses" }
+  A() <= A() <= A(); // should not emit warning
+  1 <= 2 <= 3; // { dg-warning "mathematical meaning" "parentheses" }
+  A() >= A() >= A(); // should not emit warning
+  1 >= 2 >= 3; // { dg-warning "mathematical meaning" "parentheses" }
+
+  A() == A() < A (); // { dg-warning "suggest parentheses" "parentheses" }
+  A() < A() != A (); // { dg-warning "suggest parentheses" "parentheses" }
+  return 0;
+}

	Jakub



More information about the Gcc-patches mailing list