re PR c/86093 (volatile ignored on pointer in C)
authorJakub Jelinek <jakub@redhat.com>
Fri, 15 Jun 2018 20:53:54 +0000 (22:53 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 15 Jun 2018 20:53:54 +0000 (22:53 +0200)
PR c/86093
* c-typeck.c (pointer_diff): Cast both pointers to unqualified types
before doing POINTER_DIFF_EXPR.

* c-c++-common/pr86093.c: New test.

From-SVN: r261663

gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/pr86093.c [new file with mode: 0644]

index fa9dca077ee98864a923dc0d5da8bd90638c4e4e..89e6d1373b4176897564a05aa6bdc65823352aaf 100644 (file)
@@ -1,3 +1,9 @@
+2018-06-15  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/86093
+       * c-typeck.c (pointer_diff): Cast both pointers to unqualified types
+       before doing POINTER_DIFF_EXPR.
+
 2018-06-07  Marek Polacek  <polacek@redhat.com>
 
        PR c/85318
index f346eaef031181c9078c359469968dc3b820f9eb..5e2a2836d6dae9132942403e624c6c02ade9a770 100644 (file)
@@ -3840,7 +3840,12 @@ pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
     op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
                           convert (inttype, op1), false);
   else
-    op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
+    {
+      /* Cast away qualifiers.  */
+      op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
+      op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
+      op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
+    }
 
   /* This generates an error if op1 is pointer to incomplete type.  */
   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
index 89b05676f50c0035806b4d2edd24cae430bc00f1..03fc2a0f309e0b8f4e48164d57d9d22d3181d7cf 100644 (file)
@@ -1,5 +1,8 @@
 2018-06-15  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c/86093
+       * c-c++-common/pr86093.c: New test.
+
        PR middle-end/85878
        * gfortran.fortran-torture/compile/pr85878.f90: New test.
 
diff --git a/gcc/testsuite/c-c++-common/pr86093.c b/gcc/testsuite/c-c++-common/pr86093.c
new file mode 100644 (file)
index 0000000..06afbd6
--- /dev/null
@@ -0,0 +1,12 @@
+/* PR c/86093 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-not "return 0;" "optimized" } } */
+
+char *volatile p;
+
+__PTRDIFF_TYPE__
+foo (void)
+{
+  return p - p;
+}