* [PATCH] correct "comparisons do not have their mathematical meaning" warning
@ 2006-12-20 15:55 Dirk Mueller
2007-01-06 13:42 ` PING: " Dirk Mueller
0 siblings, 1 reply; 9+ messages in thread
From: Dirk Mueller @ 2006-12-20 15:55 UTC (permalink / raw)
To: gcc-patches
Hi,
I'd like to separate the "comparisons like X<= Y <= Z do not have their
mathematical meaning" warning a bit more. Right now, it warns about
constructs like:
if (a < b < c)
..
if (a >= b >= c)
as well as about constructs like
if ( a > b == true)
Triggering the "do not have their mathematical meaning" warning for the 3rd is
surprising and not very helpful (given that the evaluation order is
well-defined and the programmer likely knew what he wanted to do, much unlike
in the first two cases).
The trouble is that the existing testsuite already tested for a warning for
the 3rd case. In order to not "regress", I've added a separate warning, much
in the spirit for the other -Wparentheses warnings ("suggest parentheses
around comparison in operand of =="), and limited the "mathematical meaning"
warning to sequences of <,<=,>=,> operators.
Below is bootstrapped and regtested against trunk on i686-suse-linux. Ok?
2006-12-20 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: Add new tests.
--- c-common.c (revision 119972)
+++ c-common.c (working copy)
@@ -6680,12 +6680,24 @@ 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 ? "==" : "!=");
+ }
+
+ if (TREE_CODE_CLASS (code) == tcc_comparison)
+ {
+ if (TREE_CODE_CLASS (code) == tcc_comparison
+ && TREE_CODE_CLASS (code_left) == tcc_comparison
+ && code != NE_EXPR && code != EQ_EXPR
+ && code_left != NE_EXPR && code_left != EQ_EXPR)
+ warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
+ "have their mathematical meaning");
+ }
}
Index: testsuite/gcc.dg/Wparentheses-2.c
===================================================================
--- testsuite/gcc.dg/Wparentheses-2.c (revision 119972)
+++ testsuite/gcc.dg/Wparentheses-2.c (working copy)
@@ -64,4 +64,52 @@ bar (int a, int b, int c)
foo (1 != 2 != 3); /* { dg-warning "comparison" "correct warning" } */
foo ((1 != 2) != 3);
foo (1 != (2 != 3));
+ foo (a < b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a < b) == c);
+ foo (a < (b == c));
+ foo (a > b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a > b) == c);
+ foo (a > (b == c));
+ foo (a == b < c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) < c);
+ foo (a == (b < c));
+ foo (a == b > c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) > c);
+ foo (a == (b > c));
+ foo (1 < 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 < 2) == 3);
+ foo (1 < (2 == 3));
+ foo (1 > 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 > 2) == 3);
+ foo (1 > (2 == 3));
+ foo (1 == 2 < 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) < 3);
+ foo (1 == (2 < 3));
+ foo (1 == 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) > 3);
+ foo (1 == (2 > 3));
+ foo (a < b != c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a < b) != c);
+ foo (a < (b != c));
+ foo (a > b != c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a > b) != c);
+ foo (a > (b != c));
+ foo (a != b < c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a != b) < c);
+ foo (a != (b < c));
+ foo (a != b > c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a != b) > c);
+ foo (a != (b > c));
+ foo (1 < 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 < 2) != 3);
+ foo (1 < (2 != 3));
+ foo (1 > 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 > 2) != 3);
+ foo (1 > (2 != 3));
+ foo (1 != 2 < 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 != 2) < 3);
+ foo (1 != (2 < 3));
+ foo (1 != 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 != 2) > 3);
+ foo (1 != (2 > 3));
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* PING: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2006-12-20 15:55 [PATCH] correct "comparisons do not have their mathematical meaning" warning Dirk Mueller
@ 2007-01-06 13:42 ` Dirk Mueller
2007-01-08 5:28 ` Ian Lance Taylor
0 siblings, 1 reply; 9+ messages in thread
From: Dirk Mueller @ 2007-01-06 13:42 UTC (permalink / raw)
To: gcc-patches
On Wednesday, 20. December 2006 16:55, Dirk Mueller wrote:
> I'd like to separate the "comparisons like X<= Y <= Z do not have their
> mathematical meaning" warning a bit more.
PING: http://gcc.gnu.org/ml/gcc-patches/2006-12/msg01453.html
Thanks,
Dirk
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: PING: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-06 13:42 ` PING: " Dirk Mueller
@ 2007-01-08 5:28 ` Ian Lance Taylor
2007-01-12 12:55 ` Dirk Mueller
0 siblings, 1 reply; 9+ messages in thread
From: Ian Lance Taylor @ 2007-01-08 5:28 UTC (permalink / raw)
To: Dirk Mueller; +Cc: gcc-patches
Dirk Mueller <dmuell@gmx.net> writes:
> On Wednesday, 20. December 2006 16:55, Dirk Mueller wrote:
>
> > I'd like to separate the "comparisons like X<= Y <= Z do not have their
> > mathematical meaning" warning a bit more.
>
> PING: http://gcc.gnu.org/ml/gcc-patches/2006-12/msg01453.html
This patch as it stands does not look right. In the second block it
checks TREE_CODE_CLASS (code) twice and does not check TREE_CODE_CLASS
(code_right) at all. I also don't understand why you require that
code_left != NE_EXPR && code_left != EQ_EXPR. I don't even see how
that case could arise.
I think it would be cleaner to use an else:
if (code == EQ_EXPR || code == NE_EXPR)
...
else if (TREE_CODE_CLASS (code) == tcc_comparison)
...
However, I can not approve this patch to the C/C++ frontends.
Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: PING: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-08 5:28 ` Ian Lance Taylor
@ 2007-01-12 12:55 ` Dirk Mueller
2007-01-12 15:03 ` Ian Lance Taylor
0 siblings, 1 reply; 9+ messages in thread
From: Dirk Mueller @ 2007-01-12 12:55 UTC (permalink / raw)
To: gcc-patches; +Cc: Ian Lance Taylor
[-- Attachment #1: Type: text/plain, Size: 1599 bytes --]
On Monday, 8. January 2007 06:28, Ian Lance Taylor wrote:
> This patch as it stands does not look right. In the second block it
> checks TREE_CODE_CLASS (code) twice
Whoops :) Fixed.
> and does not check TREE_CODE_CLASS (code_right) at all.
it could do that, but it seems to be never necessary. I've tried to construct
a testcase, but the tree seems to be always built on the left (because
comparison operators are left associative ?!). I've added the code_right
variant now to the patch as well, no practical difference as far as I can
see.
> I also don't understand why you require that
> code_left != NE_EXPR && code_left != EQ_EXPR. I don't even see how
> that case could arise.
it can happen on a == b == c chains that it matches there as well (even though
there was already a warning emitted before).
> I think it would be cleaner to use an else:
> if (code == EQ_EXPR || code == NE_EXPR)
> ...
> else if (TREE_CODE_CLASS (code) == tcc_comparison)
Ok, done now.
> However, I can not approve this patch to the C/C++ frontends.
Many thanks for reviewing anyway and providing feedback! I'm not sure who is
able to approve it anyway.
Here is the updated patch. bootstrapped, regression tested with all languages
with no additional failures on i686-suse-linux.
2007-01-12 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: Add new tests.
[-- Attachment #2: parentheses-mathematical-2.diff --]
[-- Type: text/x-diff, Size: 3598 bytes --]
--- gcc/c-common.c (revision 120684)
+++ gcc/c-common.c (working copy)
@@ -6752,12 +6751,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");
+ }
}
Index: gcc/testsuite/gcc.dg/Wparentheses-2.c
===================================================================
--- gcc/testsuite/gcc.dg/Wparentheses-2.c (revision 120684)
+++ gcc/testsuite/gcc.dg/Wparentheses-2.c (working copy)
@@ -64,4 +64,52 @@ bar (int a, int b, int c)
foo (1 != 2 != 3); /* { dg-warning "comparison" "correct warning" } */
foo ((1 != 2) != 3);
foo (1 != (2 != 3));
+ foo (a < b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a < b) == c);
+ foo (a < (b == c));
+ foo (a > b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a > b) == c);
+ foo (a > (b == c));
+ foo (a == b < c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) < c);
+ foo (a == (b < c));
+ foo (a == b > c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) > c);
+ foo (a == (b > c));
+ foo (1 < 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 < 2) == 3);
+ foo (1 < (2 == 3));
+ foo (1 > 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 > 2) == 3);
+ foo (1 > (2 == 3));
+ foo (1 == 2 < 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) < 3);
+ foo (1 == (2 < 3));
+ foo (1 == 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) > 3);
+ foo (1 == (2 > 3));
+ foo (a < b != c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a < b) != c);
+ foo (a < (b != c));
+ foo (a > b != c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a > b) != c);
+ foo (a > (b != c));
+ foo (a != b < c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a != b) < c);
+ foo (a != (b < c));
+ foo (a != b > c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a != b) > c);
+ foo (a != (b > c));
+ foo (1 < 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 < 2) != 3);
+ foo (1 < (2 != 3));
+ foo (1 > 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 > 2) != 3);
+ foo (1 > (2 != 3));
+ foo (1 != 2 < 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 != 2) < 3);
+ foo (1 != (2 < 3));
+ foo (1 != 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 != 2) > 3);
+ foo (1 != (2 > 3));
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: PING: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-12 12:55 ` Dirk Mueller
@ 2007-01-12 15:03 ` Ian Lance Taylor
2007-01-29 10:59 ` PING^3: " Dirk Mueller
0 siblings, 1 reply; 9+ messages in thread
From: Ian Lance Taylor @ 2007-01-12 15:03 UTC (permalink / raw)
To: Dirk Mueller; +Cc: gcc-patches
Dirk Mueller <dmueller@suse.de> writes:
> 2007-01-12 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: Add new tests.
Thanks for making the changes. This looks good to me now, although a
C/C++ frontend maintainer will have to actually approve it.
Perhaps you should add a few a == b == c type tests to the test case?
Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* PING^3: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-12 15:03 ` Ian Lance Taylor
@ 2007-01-29 10:59 ` Dirk Mueller
2007-01-29 13:50 ` Joseph S. Myers
0 siblings, 1 reply; 9+ messages in thread
From: Dirk Mueller @ 2007-01-29 10:59 UTC (permalink / raw)
To: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 542 bytes --]
On Friday, 12. January 2007 16:02, Ian Lance Taylor wrote:
> Thanks for making the changes. This looks good to me now, although a
> C/C++ frontend maintainer will have to actually approve it.
YAPA (yet another ping attempt):
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.
[-- Attachment #2: parentheses-mathematical-4.diff --]
[-- Type: text/x-diff, Size: 6595 bytes --]
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,40 +10,40 @@ 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" } */
@@ -64,4 +64,58 @@ bar (int a, int b, int c)
foo (1 != 2 != 3); /* { dg-warning "comparison" "correct warning" } */
foo ((1 != 2) != 3);
foo (1 != (2 != 3));
+ foo (a < b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a < b) == c);
+ foo (a < (b == c));
+ foo (a > b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a > b) == c);
+ foo (a > (b == c));
+ foo (a == b < c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) < c);
+ foo (a == (b < c));
+ foo (a == b > c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) > c);
+ foo (a == (b > c));
+ foo (a == b == c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a == b) == c);
+ foo (a == (b == c));
+ foo (1 == 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) == 3);
+ foo (1 == (2 == 3));
+ foo (1 < 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 < 2) == 3);
+ foo (1 < (2 == 3));
+ foo (1 > 2 == 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 > 2) == 3);
+ foo (1 > (2 == 3));
+ foo (1 == 2 < 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) < 3);
+ foo (1 == (2 < 3));
+ foo (1 == 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 == 2) > 3);
+ foo (1 == (2 > 3));
+ foo (a < b != c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a < b) != c);
+ foo (a < (b != c));
+ foo (a > b != c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a > b) != c);
+ foo (a > (b != c));
+ foo (a != b < c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a != b) < c);
+ foo (a != (b < c));
+ foo (a != b > c); /* { dg-warning "comparison" "correct warning" } */
+ foo ((a != b) > c);
+ foo (a != (b > c));
+ foo (1 < 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 < 2) != 3);
+ foo (1 < (2 != 3));
+ foo (1 > 2 != 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 > 2) != 3);
+ foo (1 > (2 != 3));
+ foo (1 != 2 < 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 != 2) < 3);
+ foo (1 != (2 < 3));
+ foo (1 != 2 > 3); /* { dg-warning "comparison" "correct warning" } */
+ foo ((1 != 2) > 3);
+ foo (1 != (2 > 3));
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: PING^3: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-29 10:59 ` PING^3: " Dirk Mueller
@ 2007-01-29 13:50 ` Joseph S. Myers
2007-01-29 17:00 ` Dirk Mueller
0 siblings, 1 reply; 9+ messages in thread
From: Joseph S. Myers @ 2007-01-29 13:50 UTC (permalink / raw)
To: Dirk Mueller; +Cc: gcc-patches
On Mon, 29 Jan 2007, Dirk Mueller wrote:
> * testsuite/gcc.dg/Wparentheses-2.c: Update and add new tests.
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.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: PING^3: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-29 13:50 ` Joseph S. Myers
@ 2007-01-29 17:00 ` Dirk Mueller
2007-01-29 17:45 ` Joseph S. Myers
0 siblings, 1 reply; 9+ messages in thread
From: Dirk Mueller @ 2007-01-29 17:00 UTC (permalink / raw)
To: gcc-patches; +Cc: Joseph S. Myers
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));
}
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: PING^3: [PATCH] correct "comparisons do not have their mathematical meaning" warning
2007-01-29 17:00 ` Dirk Mueller
@ 2007-01-29 17:45 ` Joseph S. Myers
0 siblings, 0 replies; 9+ messages in thread
From: Joseph S. Myers @ 2007-01-29 17:45 UTC (permalink / raw)
To: Dirk Mueller; +Cc: gcc-patches
On Mon, 29 Jan 2007, Dirk Mueller wrote:
> 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.
This version is OK.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2007-01-29 17:43 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-20 15:55 [PATCH] correct "comparisons do not have their mathematical meaning" warning Dirk Mueller
2007-01-06 13:42 ` PING: " Dirk Mueller
2007-01-08 5:28 ` Ian Lance Taylor
2007-01-12 12:55 ` Dirk Mueller
2007-01-12 15:03 ` Ian Lance Taylor
2007-01-29 10:59 ` PING^3: " Dirk Mueller
2007-01-29 13:50 ` Joseph S. Myers
2007-01-29 17:00 ` Dirk Mueller
2007-01-29 17:45 ` Joseph S. Myers
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).