OSDN Git Service

* c-typeck.c (parser_build_binary_op): Condition warnings for
authorjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 17 Jul 2004 09:20:51 +0000 (09:20 +0000)
committerjsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 17 Jul 2004 09:20:51 +0000 (09:20 +0000)
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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@84860 138bc75d-0d04-0410-961f-82ee72b054a4

12 files changed:
gcc/ChangeLog
gcc/c-typeck.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/Wparentheses-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-6.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-7.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-8.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wparentheses-9.c [new file with mode: 0644]

index 48b3e20..6e902af 100644 (file)
@@ -1,3 +1,10 @@
+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
index 2495550..4befd12 100644 (file)
@@ -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);
index 7bf2d40..14f756b 100644 (file)
@@ -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
@@ -2569,11 +2575,6 @@ but @samp{x[(void)i,j]} will not.
 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.
 
index bdf36ea..d2c90e2 100644 (file)
@@ -1,3 +1,9 @@
+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.
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-2.c b/gcc/testsuite/gcc.dg/Wparentheses-2.c
new file mode 100644 (file)
index 0000000..e4110a8
--- /dev/null
@@ -0,0 +1,67 @@
+/* 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));
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-3.c b/gcc/testsuite/gcc.dg/Wparentheses-3.c
new file mode 100644 (file)
index 0000000..6489988
--- /dev/null
@@ -0,0 +1,68 @@
+/* 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);
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-4.c b/gcc/testsuite/gcc.dg/Wparentheses-4.c
new file mode 100644 (file)
index 0000000..dfc9d10
--- /dev/null
@@ -0,0 +1,85 @@
+/* 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));
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-5.c b/gcc/testsuite/gcc.dg/Wparentheses-5.c
new file mode 100644 (file)
index 0000000..be12f73
--- /dev/null
@@ -0,0 +1,31 @@
+/* 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));
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-6.c b/gcc/testsuite/gcc.dg/Wparentheses-6.c
new file mode 100644 (file)
index 0000000..2d2cc16
--- /dev/null
@@ -0,0 +1,121 @@
+/* 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));
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-7.c b/gcc/testsuite/gcc.dg/Wparentheses-7.c
new file mode 100644 (file)
index 0000000..f351696
--- /dev/null
@@ -0,0 +1,121 @@
+/* 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));
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-8.c b/gcc/testsuite/gcc.dg/Wparentheses-8.c
new file mode 100644 (file)
index 0000000..ff34ee0
--- /dev/null
@@ -0,0 +1,103 @@
+/* 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));
+}
diff --git a/gcc/testsuite/gcc.dg/Wparentheses-9.c b/gcc/testsuite/gcc.dg/Wparentheses-9.c
new file mode 100644 (file)
index 0000000..0fff6ea
--- /dev/null
@@ -0,0 +1,61 @@
+/* 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);
+  }
+}