c++: Fix bogus -Wvolatile warning in C++20 [PR98947]
commit7a18bc4ae62081021f4fd90d591a588cac931f77
authorMarek Polacek <polacek@redhat.com>
Wed, 3 Feb 2021 22:57:22 +0000 (3 17:57 -0500)
committerMarek Polacek <polacek@redhat.com>
Fri, 5 Feb 2021 16:11:04 +0000 (5 11:11 -0500)
tree96f3a425cf90a4941e732ea85fd19c5f5afb1f69
parent1cbc10d894494c34987d1f42f955e7843457ee38
c++: Fix bogus -Wvolatile warning in C++20 [PR98947]

Since most of volatile is deprecated in C++20, we are required to warn
for compound assignments to volatile variables and so on.  But here we
have

  volatile int x, y, z;
  (b ? x : y) = 1;

and we shouldn't warn, because simple assignments like x = 24; should
not provoke the warning when they are a discarded-value expression.

We warn here because when ?: is used as an lvalue, we transform it in
cp_build_modify_expr/COND_EXPR from (a ? b : c) = rhs to

  (a ? (b = rhs) : (c = rhs))

and build_conditional_expr then calls mark_lvalue_use for the new
artificial assignments, which then evokes the warning.  The calls
to mark_lvalue_use were added in r160289 to suppress warnings in
Wunused-var-10.c, but looks like they're no longer needed.

To warn on

    (b ? (x = 2) : y) = 1;
    (b ? x : (y = 5)) = 1;

I've tweaked a check in mark_use/MODIFY_EXPR.

I'd argue this is a regression because GCC 9 doesn't warn.

gcc/cp/ChangeLog:

PR c++/98947
* call.c (build_conditional_expr_1): Don't call mark_lvalue_use
on arg2/arg3.
* expr.c (mark_use) <case MODIFY_EXPR>: Don't check read_p when
issuing the -Wvolatile warning.  Only set TREE_THIS_VOLATILE if
a warning was emitted.

gcc/testsuite/ChangeLog:

PR c++/98947
* g++.dg/cpp2a/volatile5.C: New test.
gcc/cp/call.c
gcc/cp/expr.c
gcc/testsuite/g++.dg/cpp2a/volatile5.C [new file with mode: 0644]