From 487f2f61bb1bad721f41a7f767efe04df4b0b058 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 15 Jun 2018 22:53:54 +0200 Subject: [PATCH] re PR c/86093 (volatile ignored on pointer in C) 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 | 6 ++++++ gcc/c/c-typeck.c | 7 ++++++- gcc/testsuite/ChangeLog | 3 +++ gcc/testsuite/c-c++-common/pr86093.c | 12 ++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/c-c++-common/pr86093.c diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index fa9dca077ee..89e6d1373b4 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2018-06-15 Jakub Jelinek + + PR c/86093 + * c-typeck.c (pointer_diff): Cast both pointers to unqualified types + before doing POINTER_DIFF_EXPR. + 2018-06-07 Marek Polacek PR c/85318 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index f346eaef031..5e2a2836d6d 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -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)))) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 89b05676f50..03fc2a0f309 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2018-06-15 Jakub Jelinek + 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 index 00000000000..06afbd60dba --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr86093.c @@ -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; +} -- 2.30.2