In a discarded-value expression, volatile lvalues of certain forms are
converted to rvalues.  We were failing to do this because of the
force_paren_obfuscation, so we need to strip that.

Tested x86_64-pc-linux-gnu, applied to trunk.
commit fdc1c62f623cf9b440b3cdafd0da874371cb651d
Author: Jason Merrill <ja...@redhat.com>
Date:   Sat Mar 3 10:20:12 2018 -0500

            PR c++/84686 - missing volatile loads.
    
            * cvt.c (convert_to_void): Call maybe_undo_parenthesized_ref.

diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c
index f5da08bbee2..40e7576f23c 100644
--- a/gcc/cp/cvt.c
+++ b/gcc/cp/cvt.c
@@ -1063,6 +1063,8 @@ convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
       || TREE_TYPE (expr) == error_mark_node)
     return error_mark_node;
 
+  expr = maybe_undo_parenthesized_ref (expr);
+
   expr = mark_discarded_use (expr);
   if (implicit == ICV_CAST)
     /* An explicit cast to void avoids all -Wunused-but-set* warnings.  */
diff --git a/gcc/testsuite/g++.dg/tree-ssa/volatile2.C b/gcc/testsuite/g++.dg/tree-ssa/volatile2.C
new file mode 100644
index 00000000000..bec60442477
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/volatile2.C
@@ -0,0 +1,20 @@
+// PR c++/84686
+// { dg-additional-options -fdump-tree-gimple }
+// { dg-final { scan-tree-dump-times "= i" 10 "gimple" } }
+
+volatile int i;
+
+int main()
+{
+  i; //evaluated (a load is performed)
+  (i); //unevaluated => the load shall be performed
+
+  (void)i; //evaluated (a load is performed)
+  (void)(i); //unevaluated => the load shall be performed
+
+  (void)i; //evaluated (a load is performed)
+  (void)(i); //unevaluated => the load shall be performed
+
+  (i,i); // the two subexpression are evaluated
+  ((i),(i)); // no evaluation, => two loads shall happen
+}

Reply via email to