This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ PATCH for c++/86981, implement -Wpessimizing-move


On Mon, 2018-08-20 at 17:08 -0400, Marek Polacek wrote:
> This patch implements -Wpessimizing-move, a C++-specific warning that
> warns
> when using std::move in a return statement precludes the
> NRVO.  Consider:
> 
> struct T { };
> 
> T f()
> {
>   T t;
>   return std::move(t);
> }
> 
> where we could elide the copy were it not for the move call; the
> standard
> requires that the expression be the name of a non-volatile automatic
> object, so
> no function call would work there.
> Had 't' been a parameter, the move would have been merely redundant,
> but that's
> for another warning, -Wredundant-move, which should be a fairly easy
> extension
> of this one.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?

[...snip...]

> +	  /* Warn if we could do copy elision were it not for the
> move.  */
> +	  if (can_do_nrvo_p (arg, functype)
> +	      && warning (OPT_Wpessimizing_move, "moving a local object "
> +			  "in a return statement prevents copy elision"))
> +	    inform (input_location, "remove %<std::move%> call");
> +	}
> +    }

As of r263675 it's now possible to tell the diagnostics subsystem that
the warning and note are related by using an auto_diagnostic_group
instance [1], so please can this read:

          if (can_do_nrvo_p (arg, functype))
            {
              auto_diagnostic_group d;
              if (warning (OPT_Wpessimizing_move, "moving a local object "
                           "in a return statement prevents copy elision"))
                inform (input_location, "remove %<std::move%> call");
            }

Dave

[1] see https://gcc.gnu.org/ml/gcc-patches/2018-08/msg01190.html

Not that this does much yet, but I'm hoping to make it do so,
especially for cases like this where both diagnostics share the same
location


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]