From 3e3970a27611d6cf2c5d8cfaa73e9e17f38b530c Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Sat, 17 Jul 2004 10:20:51 +0100 Subject: [PATCH] c-typeck.c (parser_build_binary_op): Condition warnings for X<=Y<=Z on -Wparentheses instead of -Wextra. * c-typeck.c (parser_build_binary_op): Condition warnings for X<=Y<=Z on -Wparentheses instead of -Wextra. * doc/invoke.texi: Update. Document that most of -Wparentheses is supported for C only. testsuite: * gcc.dg/Wparentheses-2.c, gcc.dg/Wparentheses-3.c, gcc.dg/Wparentheses-4.c, Wparentheses-5.c, Wparentheses-6.c, Wparentheses-7.c, Wparentheses-8.c, Wparentheses-9.c: New tests. From-SVN: r84860 --- gcc/ChangeLog | 7 ++ gcc/c-typeck.c | 11 +-- gcc/doc/invoke.texi | 13 +-- gcc/testsuite/ChangeLog | 6 ++ gcc/testsuite/gcc.dg/Wparentheses-2.c | 67 ++++++++++++++ gcc/testsuite/gcc.dg/Wparentheses-3.c | 68 +++++++++++++++ gcc/testsuite/gcc.dg/Wparentheses-4.c | 85 ++++++++++++++++++ gcc/testsuite/gcc.dg/Wparentheses-5.c | 31 +++++++ gcc/testsuite/gcc.dg/Wparentheses-6.c | 121 ++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wparentheses-7.c | 121 ++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wparentheses-8.c | 103 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wparentheses-9.c | 61 +++++++++++++ 12 files changed, 683 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-2.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-3.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-4.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-5.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-6.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-7.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-8.c create mode 100644 gcc/testsuite/gcc.dg/Wparentheses-9.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 48b3e20702f..6e902afd7ad 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2004-07-17 Joseph S. Myers + + * c-typeck.c (parser_build_binary_op): Condition warnings for + X<=Y<=Z on -Wparentheses instead of -Wextra. + * doc/invoke.texi: Update. Document that most of -Wparentheses is + supported for C only. + 2004-07-17 Steven Bosscher * cfgcleanup.c (try_simplify_condjump): Don't remove line diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 2495550ed54..4befd126f08 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -2170,12 +2170,13 @@ parser_build_binary_op (enum tree_code code, tree arg1, tree arg2) if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<') warning ("suggest parentheses around comparison in operand of &"); } - } + /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ + if (TREE_CODE_CLASS (code) == '<' + && (TREE_CODE_CLASS (code1) == '<' + || TREE_CODE_CLASS (code2) == '<')) + warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); - /* Similarly, check for cases like 1<=i<=10 that are probably errors. */ - if (TREE_CODE_CLASS (code) == '<' && extra_warnings - && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')) - warning ("comparisons like X<=Y<=Z do not have their mathematical meaning"); + } unsigned_conversion_warning (result, arg1); unsigned_conversion_warning (result, arg2); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 7bf2d40259d..14f756b1c4d 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -2250,7 +2250,13 @@ Warn if a user-supplied include directory does not exist. Warn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected, or when operators are nested whose precedence people -often get confused about. +often get confused about. Only the warning for an assignment used as +a truth value is supported when compiling C++; the other warnings are +only supported when compiling C@. + +Also warn if a comparison like @samp{x<=y<=z} appears; this is +equivalent to @samp{(x<=y ? 1 : 0) <= z}, which is a different +interpretation from that of ordinary mathematical notation. Also warn about constructions where there may be confusion to which @code{if} statement an @code{else} branch belongs. Here is an example of @@ -2568,11 +2574,6 @@ but @samp{x[(void)i,j]} will not. @item An unsigned value is compared against zero with @samp{<} or @samp{>=}. -@item -A comparison like @samp{x<=y<=z} appears; this is equivalent to -@samp{(x<=y ? 1 : 0) <= z}, which is a different interpretation from -that of ordinary mathematical notation. - @item Storage-class specifiers like @code{static} are not the first things in a declaration. According to the C Standard, this usage is obsolescent. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index bdf36eaea29..d2c90e29f99 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2004-07-17 Joseph S. Myers + + * gcc.dg/Wparentheses-2.c, gcc.dg/Wparentheses-3.c, + gcc.dg/Wparentheses-4.c, Wparentheses-5.c, Wparentheses-6.c, + Wparentheses-7.c, Wparentheses-8.c, Wparentheses-9.c: New tests. + 2004-07-16 Richard Henderson * gcc.c-torture/compile/20020210-1.c: Remove XFAIL. diff --git a/gcc/testsuite/gcc.dg/Wparentheses-2.c b/gcc/testsuite/gcc.dg/Wparentheses-2.c new file mode 100644 index 00000000000..e4110a8ceed --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-2.c @@ -0,0 +1,67 @@ +/* Test operation of -Wparentheses. Warnings for X<=Y<=Z should be + there rather than hidden in -Wextra. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int +bar (int a, int b, int c) +{ + foo (a <= b <= c); /* { dg-warning "comparison" "correct warning" } */ + foo ((a <= b) <= c); + foo (a <= (b <= c)); + foo (1 <= 2 <= c); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 <= 2) <= c); + foo (1 <= (2 <= 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 (1 > 2 > c); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 > 2) > c); + foo (1 > (2 > 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 (1 < 2 <= c); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 < 2) <= c); + foo (1 < (2 <= 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 (1 <= 2 > c); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 <= 2) > c); + foo (1 <= (2 > 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 (1 <= 2 == c); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 <= 2) == c); + foo (1 <= (2 == 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 (1 != 2 != c); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 != 2) != c); + foo (1 != (2 != c)); + foo (1 != 2 != 3); /* { dg-warning "comparison" "correct warning" } */ + foo ((1 != 2) != 3); + foo (1 != (2 != 3)); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-3.c b/gcc/testsuite/gcc.dg/Wparentheses-3.c new file mode 100644 index 00000000000..64899887c38 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-3.c @@ -0,0 +1,68 @@ +/* Test operation of -Wparentheses. Warnings for assignments used as + truth-values. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses -std=gnu99" } */ + +int foo (int); + +int a, b, c; +_Bool d; + +int +bar (void) +{ + if (a = b) /* { dg-warning "assignment" "correct warning" } */ + foo (0); + if ((a = b)) + foo (1); + if (a = a) /* { dg-warning "assignment" "correct warning" } */ + foo (2); + if ((a = a)) + foo (3); + if (b = c) /* { dg-warning "assignment" "correct warning" } */ + foo (4); + else + foo (5); + if ((b = c)) + foo (6); + else + foo (7); + if (b = b) /* { dg-warning "assignment" "correct warning" } */ + foo (8); + else + foo (9); + if ((b = b)) + foo (10); + else + foo (11); + while (c = b) /* { dg-warning "assignment" "correct warning" } */ + foo (12); + while ((c = b)) + foo (13); + while (c = c) /* { dg-warning "assignment" "correct warning" } */ + foo (14); + while ((c = c)) + foo (15); + do foo (16); while (a = b); /* { dg-warning "assignment" "correct warning" } */ + do foo (17); while ((a = b)); + do foo (18); while (a = a); /* { dg-warning "assignment" "correct warning" } */ + do foo (19); while ((a = a)); + for (;c = b;) /* { dg-warning "assignment" "correct warning" } */ + foo (20); + for (;(c = b);) + foo (21); + for (;c = c;) /* { dg-warning "assignment" "correct warning" } */ + foo (22); + for (;(c = c);) + foo (23); + d = a = b; /* { dg-warning "assignment" "correct warning" } */ + foo (24); + d = (a = b); + foo (25); + d = a = a; /* { dg-warning "assignment" "correct warning" } */ + foo (26); + d = (a = a); + foo (27); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-4.c b/gcc/testsuite/gcc.dg/Wparentheses-4.c new file mode 100644 index 00000000000..dfc9d1008ef --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-4.c @@ -0,0 +1,85 @@ +/* Test operation of -Wparentheses. Precedence warnings. + or - + inside shift. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int +bar (int a, int b, int c) +{ + foo (a + b << c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a + b) << c); + foo (a + (b << c)); + foo (1 + 2 << c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) << c); + foo (1 + (2 << c)); + foo (1 + 2 << 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) << 3); + foo (1 + (2 << 3)); + foo (a << b + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a << b) + c); + foo (a << (b + c)); + foo (1 << 2 + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 << 2) + c); + foo (1 << (2 + c)); + foo (1 << 2 + 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 << 2) + 3); + foo (1 << (2 + 3)); + foo (a + b >> c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a + b) >> c); + foo (a + (b >> c)); + foo (1 + 2 >> c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) >> c); + foo (1 + (2 >> c)); + foo (1 + 2 >> 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) >> 3); + foo (1 + (2 >> 3)); + foo (a >> b + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a >> b) + c); + foo (a >> (b + c)); + foo (1 >> 2 + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 >> 2) + c); + foo (1 >> (2 + c)); + foo (1 >> 2 + 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 >> 2) + 3); + foo (1 >> (2 + 3)); + foo (a - b << c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a - b) << c); + foo (a - (b << c)); + foo (6 - 5 << c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 - 5) << c); + foo (6 - (5 << c)); + foo (6 - 5 << 4); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 - 5) << 4); + foo (6 - (5 << 4)); + foo (a << b - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a << b) - c); + foo (a << (b - c)); + foo (6 << 5 - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 << 5) - c); + foo (6 << (5 - c)); + foo (6 << 5 - 4); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 << 5) - 4); + foo (6 << (5 - 4)); + foo (a - b >> c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a - b) >> c); + foo (a - (b >> c)); + foo (6 - 5 >> c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 - 5) >> c); + foo (6 - (5 >> c)); + foo (6 - 5 >> 4); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 - 5) >> 4); + foo (6 - (5 >> 4)); + foo (a >> b - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a >> b) - c); + foo (a >> (b - c)); + foo (6 >> 5 - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 >> 5) - c); + foo (6 >> (5 - c)); + foo (6 >> 5 - 4); /* { dg-warning "parentheses" "correct warning" } */ + foo ((6 >> 5) - 4); + foo (6 >> (5 - 4)); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-5.c b/gcc/testsuite/gcc.dg/Wparentheses-5.c new file mode 100644 index 00000000000..be12f73f1c2 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-5.c @@ -0,0 +1,31 @@ +/* Test operation of -Wparentheses. Precedence warnings. && inside + ||. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int +bar (int a, int b, int c) +{ + foo (a && b || c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a && b) || c); + foo (a && (b || c)); + foo (1 && 2 || c); /* { dg-warning "parentheses" "correct warning" { xfail *-*-* } } */ + foo ((1 && 2) || c); + foo (1 && (2 || c)); + foo (1 && 2 || 3); /* { dg-warning "parentheses" "correct warning" { xfail *-*-* } } */ + foo ((1 && 2) || 3); + foo (1 && (2 || 3)); + foo (a || b && c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a || b) && c); + foo (a || (b && c)); + foo (1 || 2 && c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 || 2) && c); + foo (1 || (2 && c)); + foo (1 || 2 && 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 || 2) && 3); + foo (1 || (2 && 3)); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-6.c b/gcc/testsuite/gcc.dg/Wparentheses-6.c new file mode 100644 index 00000000000..2d2cc161394 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-6.c @@ -0,0 +1,121 @@ +/* Test operation of -Wparentheses. Precedence warnings. & or ^ or + + or - or comparison inside |. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int +bar (int a, int b, int c) +{ + foo (a & b | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) | c); + foo (a & (b | c)); + foo (1 & 2 | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) | c); + foo (1 & (2 | c)); + foo (1 & 2 | 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) | 3); + foo (1 & (2 | 3)); + foo (a | b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a | b) & c); + foo (a | (b & c)); + foo (1 | 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) & c); + foo (1 | (2 & c)); + foo (1 | 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) & 3); + foo (1 | (2 & 3)); + foo (a ^ b | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) | c); + foo (a ^ (b | c)); + foo (1 ^ 2 | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) | c); + foo (1 ^ (2 | c)); + foo (1 ^ 2 | 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) | 3); + foo (1 ^ (2 | 3)); + foo (a | b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a | b) ^ c); + foo (a | (b ^ c)); + foo (1 | 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) ^ c); + foo (1 | (2 ^ c)); + foo (1 | 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) ^ 3); + foo (1 | (2 ^ 3)); + foo (a + b | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a + b) | c); + foo (a + (b | c)); + foo (1 + 2 | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) | c); + foo (1 + (2 | c)); + foo (1 + 2 | 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) | 3); + foo (1 + (2 | 3)); + foo (a | b + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a | b) + c); + foo (a | (b + c)); + foo (1 | 2 + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) + c); + foo (1 | (2 + c)); + foo (1 | 2 + 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) + 3); + foo (1 | (2 + 3)); + foo (a - b | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a - b) | c); + foo (a - (b | c)); + foo (1 - 2 | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 - 2) | c); + foo (1 - (2 | c)); + foo (1 - 2 | 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 - 2) | 3); + foo (1 - (2 | 3)); + foo (a | b - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a | b) - c); + foo (a | (b - c)); + foo (1 | 2 - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) - c); + foo (1 | (2 - c)); + foo (1 | 2 - 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) - 3); + foo (1 | (2 - 3)); + foo (a > b | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a > b) | c); + foo (a > (b | c)); + foo (1 > 2 | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 > 2) | c); + foo (1 > (2 | c)); + foo (1 > 2 | 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 > 2) | 3); + foo (1 > (2 | 3)); + foo (a | b > c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a | b) > c); + foo (a | (b > c)); + foo (1 | 2 > c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) > c); + foo (1 | (2 > c)); + foo (1 | 2 > 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) > 3); + foo (1 | (2 > 3)); + foo (a <= b | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a <= b) | c); + foo (a <= (b | c)); + foo (1 <= 2 | c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 <= 2) | c); + foo (1 <= (2 | c)); + foo (1 <= 2 | 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 <= 2) | 3); + foo (1 <= (2 | 3)); + foo (a | b <= c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a | b) <= c); + foo (a | (b <= c)); + foo (1 | 2 <= c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) <= c); + foo (1 | (2 <= c)); + foo (1 | 2 <= 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 | 2) <= 3); + foo (1 | (2 <= 3)); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-7.c b/gcc/testsuite/gcc.dg/Wparentheses-7.c new file mode 100644 index 00000000000..f3516969d23 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-7.c @@ -0,0 +1,121 @@ +/* Test operation of -Wparentheses. Precedence warnings. & or + or - + or comparison inside ^. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int +bar (int a, int b, int c) +{ + foo (a & b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) ^ c); + foo (a & (b ^ c)); + foo (1 & 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) ^ c); + foo (1 & (2 ^ c)); + foo (1 & 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) ^ 3); + foo (1 & (2 ^ 3)); + foo (a ^ b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) & c); + foo (a ^ (b & c)); + foo (1 ^ 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) & c); + foo (1 ^ (2 & c)); + foo (1 ^ 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) & 3); + foo (1 ^ (2 & 3)); + foo (a + b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a + b) ^ c); + foo (a + (b ^ c)); + foo (1 + 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) ^ c); + foo (1 + (2 ^ c)); + foo (1 + 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) ^ 3); + foo (1 + (2 ^ 3)); + foo (a ^ b + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) + c); + foo (a ^ (b + c)); + foo (1 ^ 2 + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) + c); + foo (1 ^ (2 + c)); + foo (1 ^ 2 + 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) + 3); + foo (1 ^ (2 + 3)); + foo (a - b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a - b) ^ c); + foo (a - (b ^ c)); + foo (1 - 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 - 2) ^ c); + foo (1 - (2 ^ c)); + foo (1 - 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 - 2) ^ 3); + foo (1 - (2 ^ 3)); + foo (a ^ b - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) - c); + foo (a ^ (b - c)); + foo (1 ^ 2 - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) - c); + foo (1 ^ (2 - c)); + foo (1 ^ 2 - 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) - 3); + foo (1 ^ (2 - 3)); + foo (a >= b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a >= b) ^ c); + foo (a >= (b ^ c)); + foo (1 >= 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 >= 2) ^ c); + foo (1 >= (2 ^ c)); + foo (1 >= 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 >= 2) ^ 3); + foo (1 >= (2 ^ 3)); + foo (a ^ b >= c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) >= c); + foo (a ^ (b >= c)); + foo (1 ^ 2 >= c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) >= c); + foo (1 ^ (2 >= c)); + foo (1 ^ 2 >= 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) >= 3); + foo (1 ^ (2 >= 3)); + foo (a == b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a == b) ^ c); + foo (a == (b ^ c)); + foo (1 == 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 == 2) ^ c); + foo (1 == (2 ^ c)); + foo (1 == 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 == 2) ^ 3); + foo (1 == (2 ^ 3)); + foo (a ^ b == c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) == c); + foo (a ^ (b == c)); + foo (1 ^ 2 == c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) == c); + foo (1 ^ (2 == c)); + foo (1 ^ 2 == 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) == 3); + foo (1 ^ (2 == 3)); + foo (a < b ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a < b) ^ c); + foo (a < (b ^ c)); + foo (1 < 2 ^ c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 < 2) ^ c); + foo (1 < (2 ^ c)); + foo (1 < 2 ^ 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 < 2) ^ 3); + foo (1 < (2 ^ 3)); + foo (a ^ b < c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a ^ b) < c); + foo (a ^ (b < c)); + foo (1 ^ 2 < c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) < c); + foo (1 ^ (2 < c)); + foo (1 ^ 2 < 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 ^ 2) < 3); + foo (1 ^ (2 < 3)); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-8.c b/gcc/testsuite/gcc.dg/Wparentheses-8.c new file mode 100644 index 00000000000..ff34ee0a520 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-8.c @@ -0,0 +1,103 @@ +/* Test operation of -Wparentheses. Precedence warnings. + or - or + comparison inside &. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int +bar (int a, int b, int c) +{ + foo (a + b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a + b) & c); + foo (a + (b & c)); + foo (1 + 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) & c); + foo (1 + (2 & c)); + foo (1 + 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 + 2) & 3); + foo (1 + (2 & 3)); + foo (a & b + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) + c); + foo (a & (b + c)); + foo (1 & 2 + c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) + c); + foo (1 & (2 + c)); + foo (1 & 2 + 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) + 3); + foo (1 & (2 + 3)); + foo (a - b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a - b) & c); + foo (a - (b & c)); + foo (1 - 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 - 2) & c); + foo (1 - (2 & c)); + foo (1 - 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 - 2) & 3); + foo (1 - (2 & 3)); + foo (a & b - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) - c); + foo (a & (b - c)); + foo (1 & 2 - c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) - c); + foo (1 & (2 - c)); + foo (1 & 2 - 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) - 3); + foo (1 & (2 - 3)); + foo (a < b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a < b) & c); + foo (a < (b & c)); + foo (1 < 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 < 2) & c); + foo (1 < (2 & c)); + foo (1 < 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 < 2) & 3); + foo (1 < (2 & 3)); + foo (a & b < c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) < c); + foo (a & (b < c)); + foo (1 & 2 < c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) < c); + foo (1 & (2 < c)); + foo (1 & 2 < 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) < 3); + foo (1 & (2 < 3)); + foo (a == b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a == b) & c); + foo (a == (b & c)); + foo (1 == 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 == 2) & c); + foo (1 == (2 & c)); + foo (1 == 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 == 2) & 3); + foo (1 == (2 & 3)); + foo (a & b == c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) == c); + foo (a & (b == c)); + foo (1 & 2 == c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) == c); + foo (1 & (2 == c)); + foo (1 & 2 == 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) == 3); + foo (1 & (2 == 3)); + foo (a != b & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a != b) & c); + foo (a != (b & c)); + foo (1 != 2 & c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 != 2) & c); + foo (1 != (2 & c)); + foo (1 != 2 & 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 != 2) & 3); + foo (1 != (2 & 3)); + foo (a & b != c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((a & b) != c); + foo (a & (b != c)); + foo (1 & 2 != c); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) != c); + foo (1 & (2 != c)); + foo (1 & 2 != 3); /* { dg-warning "parentheses" "correct warning" } */ + foo ((1 & 2) != 3); + foo (1 & (2 != 3)); +} diff --git a/gcc/testsuite/gcc.dg/Wparentheses-9.c b/gcc/testsuite/gcc.dg/Wparentheses-9.c new file mode 100644 index 00000000000..0fff6ea1aa3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wparentheses-9.c @@ -0,0 +1,61 @@ +/* Test operation of -Wparentheses. Warnings for ambiguous else. */ +/* Origin: Joseph Myers */ + +/* { dg-do compile } */ +/* { dg-options "-Wparentheses" } */ + +int foo (int); + +int a, b, c; + +int +bar (void) +{ + if (a) + foo (0); + if (b) + foo (1); + else + foo (2); + if (c) /* { dg-warning "ambiguous" "correct warning" } */ + if (a) + foo (3); + else + foo (4); + if (a) + if (c) + foo (5); + if (a) + if (b) /* { dg-warning "ambiguous" "correct warning" } */ + if (c) + foo (6); + else + foo (7); + if (a) /* { dg-warning "ambiguous" "correct warning" } */ + if (b) + if (c) + foo (8); + else + foo (9); + else + foo (10); + if (a) + if (b) + if (c) + foo (11); + else + foo (12); + else + foo (13); + else + foo (14); + if (a) { + if (b) + if (c) + foo (15); + else + foo (16); + else + foo (17); + } +} -- 2.30.2