2019-08-30 Jakub Jelinek Backported from mainline 2019-03-26 Jakub Jelinek PR c++/89796 * semantics.c (finish_omp_atomic): Add warning_sentinel for -Wunused-value around finish_expr_stmt call. * g++.dg/gomp/pr89796.C: New test. * gcc.dg/gomp/pr89796.c: New test. --- gcc/cp/semantics.c (revision 270740) +++ gcc/cp/semantics.c (revision 270741) @@ -8462,6 +8462,11 @@ finish_omp_atomic (enum tree_code code, stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt); OMP_ATOMIC_SEQ_CST (stmt) = seq_cst; } + + /* Avoid -Wunused-value warnings here, the whole construct has side-effects + and even if it might be wrapped from fold-const.c or c-omp.c wrapped + in some tree that appears to be unused, the value is not unused. */ + warning_sentinel w (warn_unused_value); finish_expr_stmt (stmt); } --- gcc/testsuite/gcc.dg/gomp/pr89796.c (nonexistent) +++ gcc/testsuite/gcc.dg/gomp/pr89796.c (revision 270741) @@ -0,0 +1,23 @@ +/* PR c++/89796 */ +/* { dg-do compile } */ +/* { dg-additional-options "-Wunused-value" } */ + +int +f1 (int *p) +{ + int r; + #pragma omp atomic capture /* { dg-bogus "value computed is not used" } */ + { r = *p; (*p)++; } + return r; +} + +int +f2 (int *p) +{ + int s + = ({ int r; + #pragma omp atomic capture /* { dg-bogus "value computed is not used" } */ + { r = *p; (*p)++; } + r; }); + return s; +} --- gcc/testsuite/g++.dg/gomp/pr89796.C (nonexistent) +++ gcc/testsuite/g++.dg/gomp/pr89796.C (revision 270741) @@ -0,0 +1,53 @@ +// PR c++/89796 +// { dg-do compile } +// { dg-additional-options "-Wunused-value" } + +int +f1 (int &c) +{ + int r; + #pragma omp atomic capture // { dg-bogus "value computed is not used" } + { r = c; c++; } + return r; +} + +template +int +f2 (int &c) +{ + int r; + #pragma omp atomic capture // { dg-bogus "value computed is not used" } + { r = c; c++; } + return r; +} + +int +f3 (int &c) +{ + return f2 <0> (c); +} + +int +f4 (int *p) +{ + int r; + #pragma omp atomic capture // { dg-bogus "value computed is not used" } + { r = *p; (*p)++; } + return r; +} + +template +int +f5 (int *p) +{ + int r; + #pragma omp atomic capture // { dg-bogus "value computed is not used" } + { r = *p; (*p)++; } + return r; +} + +int +f6 (int *p) +{ + return f5 <0> (p); +}