This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: 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
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>
- Cc: Manuel López-Ibáñez <lopezibanez at gmail dot com>, Gcc Patch List <gcc-patches at gcc dot gnu dot org>, Mark Mitchell <mark at codesourcery dot com>
- Date: Sat, 20 Dec 2008 10:41:44 +0100
- Subject: Re: 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
- References: <6c33472e0810250750y33c10265g3195e8613de1b1fc@mail.gmail.com> <20081025162735.GM14706@tyan-ft48-01.lab.bos.redhat.com> <6c33472e0811041414l57b4b98ibb84350dcf60117f@mail.gmail.com> <494C2488.7000002@redhat.com> <20081219231529.GK17496@tyan-ft48-01.lab.bos.redhat.com> <494C2E6C.8090006@redhat.com> <20081219234720.GL17496@tyan-ft48-01.lab.bos.redhat.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
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