+2004-07-17 Joseph S. Myers <jsm@polyomino.org.uk>
+
+ * 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 <stevenb@suse.de>
* cfgcleanup.c (try_simplify_condjump): Don't remove line
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);
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
@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.
+2004-07-17 Joseph S. Myers <jsm@polyomino.org.uk>
+
+ * 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 <rth@redhat.com>
* gcc.c-torture/compile/20020210-1.c: Remove XFAIL.
--- /dev/null
+/* Test operation of -Wparentheses. Warnings for X<=Y<=Z should be
+ there rather than hidden in -Wextra. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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));
+}
--- /dev/null
+/* Test operation of -Wparentheses. Warnings for assignments used as
+ truth-values. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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);
+}
--- /dev/null
+/* Test operation of -Wparentheses. Precedence warnings. + or -
+ inside shift. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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));
+}
--- /dev/null
+/* Test operation of -Wparentheses. Precedence warnings. && inside
+ ||. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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));
+}
--- /dev/null
+/* Test operation of -Wparentheses. Precedence warnings. & or ^ or +
+ or - or comparison inside |. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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));
+}
--- /dev/null
+/* Test operation of -Wparentheses. Precedence warnings. & or + or -
+ or comparison inside ^. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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));
+}
--- /dev/null
+/* Test operation of -Wparentheses. Precedence warnings. + or - or
+ comparison inside &. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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));
+}
--- /dev/null
+/* Test operation of -Wparentheses. Warnings for ambiguous else. */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+
+/* { 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);
+ }
+}