From: "Manuel López-Ibáñez" <lopezibanez@gmail.com>
To: "Jakub Jelinek" <jakub@redhat.com>
Cc: "Gcc Patch List" <gcc-patches@gcc.gnu.org>
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
Date: Tue, 04 Nov 2008 22:15:00 -0000 [thread overview]
Message-ID: <6c33472e0811041414l57b4b98ibb84350dcf60117f@mail.gmail.com> (raw)
In-Reply-To: <20081025162735.GM14706@tyan-ft48-01.lab.bos.redhat.com>
[-- Attachment #1: Type: text/plain, Size: 880 bytes --]
2008/10/25 Jakub Jelinek <jakub@redhat.com>:
>
> Are you sure we don't want to warn even about the:
> 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 ? "==" : "!=");
> }
> case if EQ_EXPR or NE_EXPR don't return boolean?
Thanks. I added your suggestion. Bootstrapped and regression tested on
x86_64-unknown-linux-gnu with --enable-languages=all.
OK for trunk?
2008-11-04 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.
[-- Attachment #2: fix-pr36921.diff --]
[-- Type: text/plain, Size: 2200 bytes --]
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,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;
+}
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c (revision 141531)
+++ gcc/cp/typeck.c (working copy)
@@ -3155,11 +3155,17 @@ 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
+ || code == EQ_EXPR
+ || code == NE_EXPR
+ || 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);
next prev parent reply other threads:[~2008-11-04 22:15 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-25 16:52 Manuel López-Ibáñez
2008-10-25 17:10 ` Jakub Jelinek
2008-11-04 22:15 ` Manuel López-Ibáñez [this message]
2008-12-03 17:49 ` Mark Mitchell
2008-12-19 23:32 ` Jason Merrill
2008-12-19 23:38 ` Jakub Jelinek
2008-12-19 23:50 ` Jason Merrill
2008-12-20 0:13 ` Mark Mitchell
2008-12-20 0:17 ` Jakub Jelinek
2008-12-20 12:43 ` Jakub Jelinek
2008-12-20 13:38 ` Jakub Jelinek
2008-12-20 22:33 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6c33472e0811041414l57b4b98ibb84350dcf60117f@mail.gmail.com \
--to=lopezibanez@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).