re PR c/68412 (ICE with -Wall -Wextra in fold_binary_loc())
authorMarek Polacek <polacek@redhat.com>
Thu, 19 Nov 2015 22:04:00 +0000 (22:04 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 19 Nov 2015 22:04:00 +0000 (22:04 +0000)
PR c/68412
* c-typeck.c (parser_build_binary_op): Properly handle
C_MAYBE_CONST_EXPR before calling warn_tautological_cmp.

* gcc.dg/pr68412-2.c: New test.
* gcc.dg/pr68412.c: New test.

From-SVN: r230627

gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr68412-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr68412.c [new file with mode: 0644]

index b561fa07cb0787c9e7254f6d8c10fc92c86608bd..a0cab878d6f4e7611f0ce1c82ccb5edeed17a4c8 100644 (file)
@@ -1,3 +1,9 @@
+2015-11-19  Marek Polacek  <polacek@redhat.com>
+
+       PR c/68412
+       * c-typeck.c (parser_build_binary_op): Properly handle
+       C_MAYBE_CONST_EXPR before calling warn_tautological_cmp.
+
 2015-11-17  David Malcolm  <dmalcolm@redhat.com>
 
        * c-parser.c (set_c_expr_source_range): Bulletproof both
index c18c3076752145d12841782b38c5ec7378faa478..5cb0f7e622edeea020f91eac7415b68925dc472c 100644 (file)
@@ -3512,7 +3512,28 @@ parser_build_binary_op (location_t location, enum tree_code code,
                           code1, arg1.value, code2, arg2.value);
 
   if (warn_tautological_compare)
-    warn_tautological_cmp (location, code, arg1.value, arg2.value);
+    {
+      tree lhs = arg1.value;
+      tree rhs = arg2.value;
+      if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
+       {
+         if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
+             && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
+           lhs = NULL_TREE;
+         else
+           lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
+       }
+      if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
+       {
+         if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
+             && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
+           rhs = NULL_TREE;
+         else
+           rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
+       }
+      if (lhs != NULL_TREE && rhs != NULL_TREE)
+       warn_tautological_cmp (location, code, lhs, rhs);
+    }
 
   if (warn_logical_not_paren
       && TREE_CODE_CLASS (code) == tcc_comparison
index 4a4c4ce7c07dabb7c703bf0721d97b3d507e7534..16a6167945135f1a7824e422eeae86386b41acfe 100644 (file)
@@ -1,3 +1,9 @@
+2015-11-19  Marek Polacek  <polacek@redhat.com>
+
+       PR c/68412
+       * gcc.dg/pr68412-2.c: New test.
+       * gcc.dg/pr68412.c: New test.
+
 2015-11-19  Senthil Kumar Selvaraj  <senthil_kumar.selvaraj@atmel.com>
 
        * gcc.dg/tree-ssa/vrp98.c: New testcase.
diff --git a/gcc/testsuite/gcc.dg/pr68412-2.c b/gcc/testsuite/gcc.dg/pr68412-2.c
new file mode 100644 (file)
index 0000000..be1dcfa
--- /dev/null
@@ -0,0 +1,15 @@
+/* PR c/68412 */
+/* { dg-do compile } */
+/* { dg-options "-Wall -Wextra" } */
+
+int
+fn1 (int i)
+{
+  return ({ i; }) == ({ i; }); /* { dg-warning "self-comparison always evaluates to true" } */
+}
+
+int
+fn2 (int i)
+{
+  return ({ i + 1; }) != ({ i + 1; }); /* { dg-warning "self-comparison always evaluates to false" } */
+}
diff --git a/gcc/testsuite/gcc.dg/pr68412.c b/gcc/testsuite/gcc.dg/pr68412.c
new file mode 100644 (file)
index 0000000..825eb63
--- /dev/null
@@ -0,0 +1,41 @@
+/* PR c/68412 */
+/* { dg-do compile } */
+/* { dg-options "-Wall -Wextra" } */
+
+#define M (sizeof (int) * __CHAR_BIT__)
+
+int
+fn1 (int i)
+{
+  return i == (-1 << 8); /* { dg-warning "left shift of negative value" } */
+}
+
+int
+fn2 (int i)
+{
+  return i == (1 << M); /* { dg-warning "left shift count" } */
+}
+
+int
+fn3 (int i)
+{
+  return i == 10 << (M - 1); /* { dg-warning "requires" } */
+}
+
+int
+fn4 (int i)
+{
+  return i == 1 << -1; /* { dg-warning "left shift count" } */
+}
+
+int
+fn5 (int i)
+{
+  return i == 1 >> M; /* { dg-warning "right shift count" } */
+}
+
+int
+fn6 (int i)
+{
+  return i == 1 >> -1; /* { dg-warning "right shift count" } */
+}