diff options
author | Marek Polacek <polacek@redhat.com> | 2022-08-03 19:47:30 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2022-08-17 12:39:44 -0400 |
commit | 6602a2b2dee16af6e2d451c704789356042b5881 (patch) | |
tree | 413969ce72c7d74b591aa588fa5024cca4665e6c /gcc/cp | |
parent | 8d22c7cb8b1a6f9b67c54a798dd5504244614e51 (diff) | |
download | gcc-6602a2b2dee16af6e2d451c704789356042b5881.zip gcc-6602a2b2dee16af6e2d451c704789356042b5881.tar.gz gcc-6602a2b2dee16af6e2d451c704789356042b5881.tar.bz2 |
c++: Tweak for -Wpessimizing-move in templates [PR89780]
In my previous patches I've been extending our std::move warnings,
but this tweak actually dials it down a little bit. As reported in
bug 89780, it's questionable to warn about expressions in templates
that were type-dependent, but aren't anymore because we're instantiating
the template. As in,
template <typename T>
Dest withMove() {
T x;
return std::move(x);
}
template Dest withMove<Dest>(); // #1
template Dest withMove<Source>(); // #2
Saying that the std::move is pessimizing for #1 is not incorrect, but
it's not useful, because removing the std::move would then pessimize #2.
So the user can't really win. At the same time, disabling the warning
just because we're in a template would be going too far, I still want to
warn for
template <typename>
Dest withMove() {
Dest x;
return std::move(x);
}
because the std::move therein will be pessimizing for any instantiation.
So I'm using the suppress_warning machinery to that effect.
Problem: I had to add a new group to nowarn_spec_t, otherwise
suppressing the -Wpessimizing-move warning would disable a whole bunch
of other warnings, which we really don't want.
PR c++/89780
gcc/cp/ChangeLog:
* pt.cc (tsubst_copy_and_build) <case CALL_EXPR>: Maybe suppress
-Wpessimizing-move.
* typeck.cc (maybe_warn_pessimizing_move): Don't issue warnings
if they are suppressed.
(check_return_expr): Disable -Wpessimizing-move when returning
a dependent expression.
gcc/ChangeLog:
* diagnostic-spec.cc (nowarn_spec_t::nowarn_spec_t): Handle
OPT_Wpessimizing_move and OPT_Wredundant_move.
* diagnostic-spec.h (nowarn_spec_t): Add NW_REDUNDANT enumerator.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/Wpessimizing-move3.C: Remove dg-warning.
* g++.dg/cpp0x/Wredundant-move2.C: Likewise.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/pt.cc | 3 | ||||
-rw-r--r-- | gcc/cp/typeck.cc | 13 |
2 files changed, 13 insertions, 3 deletions
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 6c581fe0..fe7e809 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21215,6 +21215,9 @@ tsubst_copy_and_build (tree t, CALL_EXPR_ORDERED_ARGS (call) = ord; CALL_EXPR_REVERSE_ARGS (call) = rev; } + if (warning_suppressed_p (t, OPT_Wpessimizing_move)) + /* This also suppresses -Wredundant-move. */ + suppress_warning (ret, OPT_Wpessimizing_move); } RETURN (ret); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 19bb7f7..e95ce8f 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10447,14 +10447,16 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) else if (can_do_nrvo_p (arg, type)) { auto_diagnostic_group d; - if (warning_at (loc, OPT_Wpessimizing_move, - "moving a local object in a return statement " - "prevents copy elision")) + if (!warning_suppressed_p (expr, OPT_Wpessimizing_move) + && warning_at (loc, OPT_Wpessimizing_move, + "moving a local object in a return statement " + "prevents copy elision")) inform (loc, "remove %<std::move%> call"); } /* Warn if the move is redundant. It is redundant when we would do maybe-rvalue overload resolution even without std::move. */ else if (warn_redundant_move + && !warning_suppressed_p (expr, OPT_Wredundant_move) && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true))) { /* Make sure that overload resolution would actually succeed @@ -10699,6 +10701,11 @@ check_return_expr (tree retval, bool *no_warning) /* We don't know if this is an lvalue or rvalue use, but either way we can mark it as read. */ mark_exp_read (retval); + /* Disable our std::move warnings when we're returning + a dependent expression (c++/89780). */ + if (retval && TREE_CODE (retval) == CALL_EXPR) + /* This also suppresses -Wredundant-move. */ + suppress_warning (retval, OPT_Wpessimizing_move); return retval; } |