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]

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


Bootstrapped and regression tested on x86_64-unknown-linux-gnu with
--enable-languages=all,obj-c++ --enable-decimal-float.

OK for trunk and 4.3?

2008-10-25  Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR c++/36921
cp/
	* typeck.c (build_x_binary_op): Do not warn for overloaded
	comparison operators that do not return boolean.
testsuite/
	* g++.dg/warn/pr36921.C: New.
Index: gcc/testsuite/g++.dg/warn/pr36921.C
===================================================================
--- gcc/testsuite/g++.dg/warn/pr36921.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/pr36921.C	(revision 0)
@@ -0,0 +1,13 @@
+/* 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(); }
+
+int main() {
+  A() < A() < A(); // should not emit warning
+  1 < 2 < 3; // { dg-warning "mathematical meaning" "parentheses" }
+  return 0;
+}
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 141350)
+++ gcc/cp/typeck.c	(working copy)
@@ -3155,11 +3155,15 @@ build_x_binary_op (enum tree_code code, 
   if (warn_parentheses
       && !processing_template_decl
       && !error_operand_p (arg1)
       && !error_operand_p (arg2)
       && (code != LSHIFT_EXPR
-	  || !CLASS_TYPE_P (TREE_TYPE (arg1))))
+	  || !CLASS_TYPE_P (TREE_TYPE (arg1)))
+      /* Do not warn about overloaded comparison operator that does
+	 not return boolean. */
+      && (TREE_CODE_CLASS (code) != tcc_comparison 
+	  || TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE))
     warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
 
   if (processing_template_decl && expr != error_mark_node)
     return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
 

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